Projekt

Obecné

Profil

Stáhnout (2.24 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var path = require('path');
4
var fs = require('fs');
5
var acorn = require('./acorn.js');
6

    
7
var infile, forceFile, silent = false, compact = false, tokenize = false;
8
var options = {};
9

    
10
function help(status) {
11
  var print = (status === 0) ? console.log : console.error;
12
  print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|...|--ecma2015|--ecma2016|--ecma2017|--ecma2018|...]");
13
  print("        [--tokenize] [--locations] [---allow-hash-bang] [--compact] [--silent] [--module] [--help] [--] [infile]");
14
  process.exit(status);
15
}
16

    
17
for (var i = 2; i < process.argv.length; ++i) {
18
  var arg = process.argv[i];
19
  if ((arg === "-" || arg[0] !== "-") && !infile) { infile = arg; }
20
  else if (arg === "--" && !infile && i + 2 === process.argv.length) { forceFile = infile = process.argv[++i]; }
21
  else if (arg === "--locations") { options.locations = true; }
22
  else if (arg === "--allow-hash-bang") { options.allowHashBang = true; }
23
  else if (arg === "--silent") { silent = true; }
24
  else if (arg === "--compact") { compact = true; }
25
  else if (arg === "--help") { help(0); }
26
  else if (arg === "--tokenize") { tokenize = true; }
27
  else if (arg === "--module") { options.sourceType = "module"; }
28
  else {
29
    var match = arg.match(/^--ecma(\d+)$/);
30
    if (match)
31
      { options.ecmaVersion = +match[1]; }
32
    else
33
      { help(1); }
34
  }
35
}
36

    
37
function run(code) {
38
  var result;
39
  try {
40
    if (!tokenize) {
41
      result = acorn.parse(code, options);
42
    } else {
43
      result = [];
44
      var tokenizer = acorn.tokenizer(code, options), token;
45
      do {
46
        token = tokenizer.getToken();
47
        result.push(token);
48
      } while (token.type !== acorn.tokTypes.eof)
49
    }
50
  } catch (e) {
51
    console.error(infile && infile !== "-" ? e.message.replace(/\(\d+:\d+\)$/, function (m) { return m.slice(0, 1) + infile + " " + m.slice(1); }) : e.message);
52
    process.exit(1);
53
  }
54
  if (!silent) { console.log(JSON.stringify(result, null, compact ? null : 2)); }
55
}
56

    
57
if (forceFile || infile && infile !== "-") {
58
  run(fs.readFileSync(infile, "utf8"));
59
} else {
60
  var code = "";
61
  process.stdin.resume();
62
  process.stdin.on("data", function (chunk) { return code += chunk; });
63
  process.stdin.on("end", function () { return run(code); });
64
}
(6-6/6)