1 #!/usr/bin/env node
2 /*
3 Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
4 Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
27 /*jslint sloppy:true node:true rhino:true */
29 var fs, esprima, fname, content, options, syntax;
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 }
42 // Shims to Node.js objects when running under Rhino.
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 }
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 }
68 if (process.argv.length <= 2) {
69 showUsage();
70 }
72 options = {};
74 process.argv.splice(2).forEach(function (entry) {
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 });
105 if (typeof fname !== 'string') {
106 console.log('Error: no input file.');
107 process.exit(1);
108 }
110 // Special handling for regular expression literal since we need to
111 // convert it to a string literal, otherwise it will be decoded
112 // as object "{}" and the regular expression would be lost.
113 function adjustRegexLiteral(key, value) {
114 if (key === 'value' && value instanceof RegExp) {
115 value = value.toString();
116 }
117 return value;
118 }
120 try {
121 content = fs.readFileSync(fname, 'utf-8');
122 syntax = esprima.parse(content, options);
123 console.log(JSON.stringify(syntax, adjustRegexLiteral, 4));
124 } catch (e) {
125 console.log('Error: ' + e.message);
126 process.exit(1);
127 }