1 #!/usr/bin/env node
3 var path = require("path");
4 var fs = require("fs");
5 var acorn = require("../dist/acorn.js");
7 var infile, parsed, tokens, options = {}, silent = false, compact = false, tokenize = false;
9 function help(status) {
10 var print = (status == 0) ? console.log : console.error;
11 print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6]");
12 print(" [--tokenize] [--locations] [---allow-hash-bang] [--compact] [--silent] [--help] [--] infile");
13 process.exit(status);
14 }
16 for (var i = 2; i < process.argv.length; ++i) {
17 var arg = process.argv[i];
18 if (arg[0] != "-" && !infile) infile = arg;
19 else if (arg == "--" && !infile && i + 2 == process.argv.length) infile = process.argv[++i];
20 else if (arg == "--ecma3") options.ecmaVersion = 3;
21 else if (arg == "--ecma5") options.ecmaVersion = 5;
22 else if (arg == "--ecma6") options.ecmaVersion = 6;
23 else if (arg == "--ecma7") options.ecmaVersion = 7;
24 else if (arg == "--locations") options.locations = true;
25 else if (arg == "--allow-hash-bang") options.allowHashBang = true;
26 else if (arg == "--silent") silent = true;
27 else if (arg == "--compact") compact = true;
28 else if (arg == "--help") help(0);
29 else if (arg == "--tokenize") tokenize = true;
30 else help(1);
31 }
33 try {
34 var code = fs.readFileSync(infile, "utf8");
36 if (!tokenize)
37 parsed = acorn.parse(code, options);
38 else {
39 var get = acorn.tokenize(code, options);
40 tokens = [];
41 while (true) {
42 var token = get();
43 tokens.push(token);
44 if (token.type.type == "eof")
45 break;
46 }
47 }
48 } catch(e) {
49 console.log(e.message);
50 process.exit(1);
51 }
53 if (!silent)
54 console.log(JSON.stringify(tokenize ? tokens : parsed, null, compact ? null : 2));