4

Mozilla have delivered an API for parsing a Javascript module to generate an abstract syntax tree. They call it Reflect.parse.

Is there a Reflect.parse, or something similar, written as a standalone module in Javascript? something I could run on any ES5 engine to produce a syntax tree? Failing that is there a standalone tool in C++ that does this for me? Or a service?


I tried doctorjs.org for a really simple self-evaluating anonymous function and it choked. Am I doing it wrong?

(function (scope) {
  ....
}(this));
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • BTW, *this* has nothing whatever to do with scope. In the code snippet above, the variable *scope* will reference the *this* object of the execution context calling the (anonymous) function, which could be any object at all, or `null` or `undefined` in ES5 strict mode. It seems to be global code, so *scope* will reference the global object and therefore would be much better named *global* or *GLOBAL* or similar (or perhaps *window*, but that supposes a browser-like environment, which may not be appropriate). – RobG Apr 24 '12 at 05:15

4 Answers4

4

Check out Esprima: http://esprima.org/

A separate project that generates a similar abstract syntax tree is here: http://boshi.inimino.org/3box/PanPG/build/js_ast.html

Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
4

Try Esprima (esprima.org), a project I have started few months ago. Its AST output is compatible with Mozilla Reflect.parse, it runs almost everywhere from IE 6 to Node.js, the parser is extremely fast (the fastest among its competitor), heavily unit tested (500+ and growing) with 100% code coverage.

Esprima is ES5 compliant (including strict mode), there is even WIP for ES6 (and Harmony) features support. It is known to parse tons of JavaScript out there, from standard library such jQuery to million-lines web apps code, without any problem.

Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39
2

JS.js (a Javascript interpreter written in Javascript) probably has a Javascript parser as a component, but I don't know how easy it is to get access to or use from the outside.

ricochet1k
  • 1,232
  • 9
  • 11
0

something I could run on any ES5 engine to produce a syntax tree? Failing that is there a standalone tool in C++ that does this for me?

You can get the source and build the SpiderMonkey JavaScript standalone shell (the JS engine in Mozilla's Firefox), which will have Reflect.parse built-in, so you can make a little script wrapping SpiderMonkey's functionality that would be usable as a tool.

cdleary
  • 69,512
  • 53
  • 163
  • 191