1 | 'use strict';
|
---|
2 |
|
---|
3 | var path = require('path');
|
---|
4 | var fs = require('fs');
|
---|
5 | var acorn = require('./acorn.js');
|
---|
6 |
|
---|
7 | var inputFilePaths = [], forceFileName = false, fileMode = false, 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] [--allow-await-outside-function] [--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[0] !== "-" || arg === "-") { inputFilePaths.push(arg); }
|
---|
20 | else if (arg === "--") {
|
---|
21 | inputFilePaths.push.apply(inputFilePaths, process.argv.slice(i + 1));
|
---|
22 | forceFileName = true;
|
---|
23 | break
|
---|
24 | } else if (arg === "--locations") { options.locations = true; }
|
---|
25 | else if (arg === "--allow-hash-bang") { options.allowHashBang = true; }
|
---|
26 | else if (arg === "--allow-await-outside-function") { options.allowAwaitOutsideFunction = true; }
|
---|
27 | else if (arg === "--silent") { silent = true; }
|
---|
28 | else if (arg === "--compact") { compact = true; }
|
---|
29 | else if (arg === "--help") { help(0); }
|
---|
30 | else if (arg === "--tokenize") { tokenize = true; }
|
---|
31 | else if (arg === "--module") { options.sourceType = "module"; }
|
---|
32 | else {
|
---|
33 | var match = arg.match(/^--ecma(\d+)$/);
|
---|
34 | if (match)
|
---|
35 | { options.ecmaVersion = +match[1]; }
|
---|
36 | else
|
---|
37 | { help(1); }
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | function run(codeList) {
|
---|
42 | var result = [], fileIdx = 0;
|
---|
43 | try {
|
---|
44 | codeList.forEach(function (code, idx) {
|
---|
45 | fileIdx = idx;
|
---|
46 | if (!tokenize) {
|
---|
47 | result = acorn.parse(code, options);
|
---|
48 | options.program = result;
|
---|
49 | } else {
|
---|
50 | var tokenizer = acorn.tokenizer(code, options), token;
|
---|
51 | do {
|
---|
52 | token = tokenizer.getToken();
|
---|
53 | result.push(token);
|
---|
54 | } while (token.type !== acorn.tokTypes.eof)
|
---|
55 | }
|
---|
56 | });
|
---|
57 | } catch (e) {
|
---|
58 | console.error(fileMode ? e.message.replace(/\(\d+:\d+\)$/, function (m) { return m.slice(0, 1) + inputFilePaths[fileIdx] + " " + m.slice(1); }) : e.message);
|
---|
59 | process.exit(1);
|
---|
60 | }
|
---|
61 | if (!silent) { console.log(JSON.stringify(result, null, compact ? null : 2)); }
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (fileMode = inputFilePaths.length && (forceFileName || !inputFilePaths.includes("-") || inputFilePaths.length !== 1)) {
|
---|
65 | run(inputFilePaths.map(function (path) { return fs.readFileSync(path, "utf8"); }));
|
---|
66 | } else {
|
---|
67 | var code = "";
|
---|
68 | process.stdin.resume();
|
---|
69 | process.stdin.on("data", function (chunk) { return code += chunk; });
|
---|
70 | process.stdin.on("end", function () { return run([code]); });
|
---|
71 | }
|
---|