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 for a really simple self-evaluating anonymous function and it choked. Am I doing it wrong?
(function (scope) {
....
}(this));
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 for a really simple self-evaluating anonymous function and it choked. Am I doing it wrong?
(function (scope) {
....
}(this));
Share
Improve this question
edited Apr 24, 2012 at 4:10
Cheeso
asked Apr 24, 2012 at 3:58
CheesoCheeso
193k106 gold badges485 silver badges734 bronze badges
1
-
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
orundefined
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 Commented Apr 24, 2012 at 5:15
4 Answers
Reset to default 4Check out Esprima: http://esprima/
A separate project that generates a similar abstract syntax tree is here: http://boshi.inimino/3box/PanPG/build/js_ast.html
Try Esprima (esprima), a project I have started few months ago. Its AST output is patible with Mozilla Reflect.parse, it runs almost everywhere from IE 6 to Node.js, the parser is extremely fast (the fastest among its petitor), heavily unit tested (500+ and growing) with 100% code coverage.
Esprima is ES5 pliant (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.
JS.js (a Javascript interpreter written in Javascript) probably has a Javascript parser as a ponent, but I don't know how easy it is to get access to or use from the outside.
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.