JavaScript parser in JavaScript
JavascriptParsingJavascript Problem Overview
I need to add some lightweight syntactic sugar to JavaScript source code, and process it using a JavaScript-based build system. Are there any open source JavaScript parsers written in JavaScript? And are they reasonably fast when run on top of V8 or a similar high-performance JavaScript implementation?
Thank you for any pointers you can provide!
Javascript Solutions
Solution 1 - Javascript
UglifyJS (JS compressor/beautifier in JavaScript) contains a complete JavaScript parser that exposes a simple API. It's heavily tested and used in some big projects (WebKit).
Solution 2 - Javascript
The fastest Javascript parser in Javascript was esprima.
It also gives you
> Sensible format for the abstract syntax tree (AST), compatible with Mozilla Parser API
Solution 3 - Javascript
Crescent Fresh answered this question in the comments:
> JSLint contains a JavaScript parser written in JavaScript. See JSlint by Douglas Crockford Around line 2712 begins the parser. JSLint is written to also handle html so you'd have to gloss over those parts
Solution 4 - Javascript
acorn is a really fast JavaScript parser written in JavaScript. It's even faster than esprima now. The results I got in Chrome form esprima's speed comparison page:
Source Esprima UglifyJS2 Traceur Acorn
Underscore 1.4.1 15.1 23.8 14.2 7.6
Backbone 1.0.0 17.1 30.2 16.7 7.9
jQuery 1.9.1 241.1 247.2 125.4 81.4
Total 273.3 ms 301.2 ms 156.3 ms 96.9 ms
It's compatible with Mozilla's Parser API, so you can use escodegen to generate JavaScript from the parse trees.
Solution 5 - Javascript
It's not a JavaScript parser itself, but there's a project called Jison (like Bison) for generating parsers that's written in JS.
Solution 6 - Javascript
The only metacircular interpreter that I have seen implemented in JavaScript is the Narcissus Engine.
It was developed also by Brendan Eich, they used a lot of non-standard extensions that are specific to SpiderMonkey, I think it will not work on V8.
Solution 7 - Javascript
Microsoft has developed the TypeScript compiler in TypeScript. Since TypeScript is a strict superset of JavaScript, and TypeScript compiles to JavaScript, the resulting compiler is technically a JavaScript compiler written in JavaScript.
That of course depends upon your definition of "compiler". But if a compiler accepting a superset of language A is not a language A compiler, that excludes GCC, Clang and pretty much every other compiler.
Solution 8 - Javascript
https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API:
> Recent builds of the standalone SpiderMonkey shell include a > reflection of the SpiderMonkey parser, made available as a JavaScript > API.
Note that this is only an API in JavaScript, the parser is C++.
Solution 9 - Javascript
JS/CC - The LALR(1) parser and lexical analyzer generator for JavaScript, written in JavaScript - http://jscc.phorward-software.com/