3 Copyright (c) jQuery Foundation, Inc. and Contributors, All Rights Reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /*jslint sloppy:true node:true rhino:true */
28 var fs, esprima, fname, content, options, syntax;
30 if (typeof require === 'function') {
32 esprima = require('esprima');
33 } else if (typeof load === 'function') {
37 load('../esprima.js');
41 // Shims to Node.js objects when running under Rhino.
42 if (typeof console === 'undefined' && typeof process === 'undefined') {
43 console = { log: print };
44 fs = { readFileSync: readFile };
45 process = { argv: arguments, exit: quit };
46 process.argv.unshift('esparse.js');
47 process.argv.unshift('rhino');
50 function showUsage() {
51 console.log('Usage:');
52 console.log(' esparse [options] file.js');
54 console.log('Available options:');
56 console.log(' --comment Gather all line and block comments in an array');
57 console.log(' --loc Include line-column location info for each syntax node');
58 console.log(' --range Include index-based range for each syntax node');
59 console.log(' --raw Display the raw value of literals');
60 console.log(' --tokens List all tokens in an array');
61 console.log(' --tolerant Tolerate errors on a best-effort basis (experimental)');
62 console.log(' -v, --version Shows program version');
67 if (process.argv.length <= 2) {
73 process.argv.splice(2).forEach(function (entry) {
75 if (entry === '-h' || entry === '--help') {
77 } else if (entry === '-v' || entry === '--version') {
78 console.log('ECMAScript Parser (using Esprima version', esprima.version, ')');
81 } else if (entry === '--comment') {
82 options.comment = true;
83 } else if (entry === '--loc') {
85 } else if (entry === '--range') {
87 } else if (entry === '--raw') {
89 } else if (entry === '--tokens') {
90 options.tokens = true;
91 } else if (entry === '--tolerant') {
92 options.tolerant = true;
93 } else if (entry.slice(0, 2) === '--') {
94 console.log('Error: unknown option ' + entry + '.');
96 } else if (typeof fname === 'string') {
97 console.log('Error: more than one input file.');
104 if (typeof fname !== 'string') {
105 console.log('Error: no input file.');
109 // Special handling for regular expression literal since we need to
110 // convert it to a string literal, otherwise it will be decoded
111 // as object "{}" and the regular expression would be lost.
112 function adjustRegexLiteral(key, value) {
113 if (key === 'value' && value instanceof RegExp) {
114 value = value.toString();
120 content = fs.readFileSync(fname, 'utf-8');
121 syntax = esprima.parse(content, options);
122 console.log(JSON.stringify(syntax, adjustRegexLiteral, 4));
124 console.log('Error: ' + e.message);