react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / node_modules / esprima-fb / bin / esparse.js
80555 views#!/usr/bin/env node1/*2Copyright (C) 2012 Ariya Hidayat <[email protected]>3Copyright (C) 2011 Ariya Hidayat <[email protected]>45Redistribution and use in source and binary forms, with or without6modification, are permitted provided that the following conditions are met:78* Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.10* Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in the12documentation and/or other materials provided with the distribution.1314THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"15AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY18DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526/*jslint sloppy:true node:true rhino:true */2728var fs, esprima, fname, content, options, syntax;2930if (typeof require === 'function') {31fs = require('fs');32esprima = require('esprima');33} else if (typeof load === 'function') {34try {35load('esprima.js');36} catch (e) {37load('../esprima.js');38}39}4041// Shims to Node.js objects when running under Rhino.42if (typeof console === 'undefined' && typeof process === 'undefined') {43console = { log: print };44fs = { readFileSync: readFile };45process = { argv: arguments, exit: quit };46process.argv.unshift('esparse.js');47process.argv.unshift('rhino');48}4950function showUsage() {51console.log('Usage:');52console.log(' esparse [options] file.js');53console.log();54console.log('Available options:');55console.log();56console.log(' --comment Gather all line and block comments in an array');57console.log(' --loc Include line-column location info for each syntax node');58console.log(' --range Include index-based range for each syntax node');59console.log(' --raw Display the raw value of literals');60console.log(' --tokens List all tokens in an array');61console.log(' --tolerant Tolerate errors on a best-effort basis (experimental)');62console.log(' -v, --version Shows program version');63console.log();64process.exit(1);65}6667if (process.argv.length <= 2) {68showUsage();69}7071options = {};7273process.argv.splice(2).forEach(function (entry) {7475if (entry === '-h' || entry === '--help') {76showUsage();77} else if (entry === '-v' || entry === '--version') {78console.log('ECMAScript Parser (using Esprima version', esprima.version, ')');79console.log();80process.exit(0);81} else if (entry === '--comment') {82options.comment = true;83} else if (entry === '--loc') {84options.loc = true;85} else if (entry === '--range') {86options.range = true;87} else if (entry === '--raw') {88options.raw = true;89} else if (entry === '--tokens') {90options.tokens = true;91} else if (entry === '--tolerant') {92options.tolerant = true;93} else if (entry.slice(0, 2) === '--') {94console.log('Error: unknown option ' + entry + '.');95process.exit(1);96} else if (typeof fname === 'string') {97console.log('Error: more than one input file.');98process.exit(1);99} else {100fname = entry;101}102});103104if (typeof fname !== 'string') {105console.log('Error: no input file.');106process.exit(1);107}108109try {110content = fs.readFileSync(fname, 'utf-8');111syntax = esprima.parse(content, options);112console.log(JSON.stringify(syntax, null, 4));113} catch (e) {114console.log('Error: ' + e.message);115process.exit(1);116}117118119