1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var fs, esprima, fname, content, options, syntax;
|
30 |
|
31 | if (typeof require === 'function') {
|
32 | fs = require('fs');
|
33 | esprima = require('esprima');
|
34 | } else if (typeof load === 'function') {
|
35 | try {
|
36 | load('esprima.js');
|
37 | } catch (e) {
|
38 | load('../esprima.js');
|
39 | }
|
40 | }
|
41 |
|
42 |
|
43 | if (typeof console === 'undefined' && typeof process === 'undefined') {
|
44 | console = { log: print };
|
45 | fs = { readFileSync: readFile };
|
46 | process = { argv: arguments, exit: quit };
|
47 | process.argv.unshift('esparse.js');
|
48 | process.argv.unshift('rhino');
|
49 | }
|
50 |
|
51 | function showUsage() {
|
52 | console.log('Usage:');
|
53 | console.log(' esparse [options] file.js');
|
54 | console.log();
|
55 | console.log('Available options:');
|
56 | console.log();
|
57 | console.log(' --comment Gather all line and block comments in an array');
|
58 | console.log(' --loc Include line-column location info for each syntax node');
|
59 | console.log(' --range Include index-based range for each syntax node');
|
60 | console.log(' --raw Display the raw value of literals');
|
61 | console.log(' --tokens List all tokens in an array');
|
62 | console.log(' --tolerant Tolerate errors on a best-effort basis (experimental)');
|
63 | console.log(' -v, --version Shows program version');
|
64 | console.log();
|
65 | process.exit(1);
|
66 | }
|
67 |
|
68 | if (process.argv.length <= 2) {
|
69 | showUsage();
|
70 | }
|
71 |
|
72 | options = {};
|
73 |
|
74 | process.argv.splice(2).forEach(function (entry) {
|
75 |
|
76 | if (entry === '-h' || entry === '--help') {
|
77 | showUsage();
|
78 | } else if (entry === '-v' || entry === '--version') {
|
79 | console.log('ECMAScript Parser (using Esprima version', esprima.version, ')');
|
80 | console.log();
|
81 | process.exit(0);
|
82 | } else if (entry === '--comment') {
|
83 | options.comment = true;
|
84 | } else if (entry === '--loc') {
|
85 | options.loc = true;
|
86 | } else if (entry === '--range') {
|
87 | options.range = true;
|
88 | } else if (entry === '--raw') {
|
89 | options.raw = true;
|
90 | } else if (entry === '--tokens') {
|
91 | options.tokens = true;
|
92 | } else if (entry === '--tolerant') {
|
93 | options.tolerant = true;
|
94 | } else if (entry.slice(0, 2) === '--') {
|
95 | console.log('Error: unknown option ' + entry + '.');
|
96 | process.exit(1);
|
97 | } else if (typeof fname === 'string') {
|
98 | console.log('Error: more than one input file.');
|
99 | process.exit(1);
|
100 | } else {
|
101 | fname = entry;
|
102 | }
|
103 | });
|
104 |
|
105 | if (typeof fname !== 'string') {
|
106 | console.log('Error: no input file.');
|
107 | process.exit(1);
|
108 | }
|
109 |
|
110 | try {
|
111 | content = fs.readFileSync(fname, 'utf-8');
|
112 | syntax = esprima.parse(content, options);
|
113 | console.log(JSON.stringify(syntax, null, 4));
|
114 | } catch (e) {
|
115 | console.log('Error: ' + e.message);
|
116 | process.exit(1);
|
117 | }
|