74

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!

emk
  • 60,150
  • 6
  • 45
  • 50
  • 3
    "I need to add some lightweight syntactic sugar to JavaScript source code" You do? Like what? – Peter Bailey Mar 31 '10 at 16:15
  • you need it for code exposition or for editor? – Luca Filosofi Mar 31 '10 at 16:29
  • 1
    Objective-J does that. you might want to checkout its open sourced code. But it uses a totally different syntax similar to Objective-C and interprets to Javascript at runtime afaik. – Anurag Mar 31 '10 at 16:31
  • 3
    JSLint contains a JavaScript parser written in JavaScript. See http://www.jslint.com/fulljslint.js Around line 2049 begins the parser. JSLint is written to also handle html so you'd have to gloss over those parts. – Crescent Fresh Mar 31 '10 at 17:03
  • Crescent French: Please repost your comment as an answer, and I'll vote you up. Thank you! – emk Mar 31 '10 at 21:15
  • The source code for jslint is available on GitHub: https://github.com/douglascrockford/JSLint => https://github.com/douglascrockford/JSLint/blob/master/jslint.js – dmitris Dec 06 '13 at 13:47

9 Answers9

33

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).

mishoo
  • 2,415
  • 1
  • 18
  • 12
  • 3
    UglifyJS is great and thank you for it. Thanks also for HTMLArea, which was a great reference for me when developing a WYSIWYG editor around 7 or 8 years ago. I would suggest mentioning your interest when linking to UglifyJS though. – Tim Down Jan 26 '12 at 23:48
  • 3
    I'd just like to point out that this parser is ready to go, and seems to work perfectly. It's contained in a separate file, so no need to wade through jslint.js to take out parts you don't need. +1 – tex Feb 22 '12 at 10:46
25

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

Johannes Gerer
  • 25,508
  • 5
  • 29
  • 35
24

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

Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42
emk
  • 60,150
  • 6
  • 45
  • 50
13

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.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
9

It's not a JavaScript parser itself, but there's a project called Jison (like Bison) for generating parsers that's written in JS.

alunny
  • 908
  • 4
  • 10
7

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.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • Interesting! I suppose there's a parser hiding in there somewhere. :-) Unfortunately, I'm looking for something that runs under V8. But thanks for the very interesting link! – emk Mar 31 '10 at 21:17
5

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.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
3

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++.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
0

JS/CC - The LALR(1) parser and lexical analyzer generator for JavaScript, written in JavaScript - http://jscc.phorward-software.com/

Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54