JavaScript parser in JavaScript [closed]
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!
Solution 1:
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:
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:
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:
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:
It's not a JavaScript parser itself, but there's a project called Jison (like Bison) for generating parsers that's written in JS.