Reputation: 4713
Abstract Syntax Tree.. I always heard that compile to SpiderMonkey AST on Github.
So, is that a actual standard of JS syntax tree? And there's V8, is V8 using the same kind of AST?
How can I play with that?
Upvotes: 80
Views: 61027
Reputation: 827
Abstract Syntax Tree (AST), is a tree representation of program source code.
There is a couple JavaScript AST
standards:
Here is a list of JavaScript parsers:
acorn
, used in eslint;acorn
, supports all new language featuresShift AST
You can find more parsers on astexplorer.net, most of them estree
compatible.
While most parsers that supports estree
can be easily replaced by each other, babel
has very rich infrastructure needed to comfortable work with AST
.
It has:
AST
-node from string.AST
-nodes.One of the simplest way to play with AST
, is using putout, which is based on babel
and supports simplified way of transforming JavaScript
code with help of plugins API.
Here is example of removing DebuggerStatement
node:
module.exports.replace = () => ({
'debugger': '',
});
If you want switch places of variables, changing the way of declaration:
module.exports.replace = () => ({
'let __a = __b': 'const __b = __a'
});
If you want transform this code into return x[0]
:
for (const x of y) {
return x;
}
You can use:
module.exports.replace = () => ({
'for (const __a of __b) {return __a}': 'return __a[0]',
});
With help of putout
you can make simplest transformation of JavaScript
code without handling with AST
directly.
Upvotes: 16
Reputation: 304
I know only of one specification of Javascript AST: https://github.com/estree/estree
It originated from publication of Dave Herman from Mozilla and since then evolved as community standard. So it should match SpiderMonkey at some degree but I'm not sure about V8 and JSC.
Would appreciate if someone could provide more information on the matter.
Upvotes: 1
Reputation: 46331
If you would like to try out the acron parser from professor Marijnh https://github.com/marijnh try out this link: https://astexplorer.net/
This is a tiny, fast JavaScript parser, written completely in JavaScript.
The above-mentioned JavaScript AST visualizer uses Esprima engine and has been also written in JavaScrpt.
JavaScript dominates in parsing AST because JavaScript engines are super optimized today. https://en.wikipedia.org/wiki/JavaScript_engine
SpiderMonkey AST standard of JS syntax tree? Is V8 using the same kind of AST?
Both of these web browser engines do have AST processing inside written in C++. This is why JavaScrpt code will run fast in most cases except for eval
.
Upvotes: 4
Reputation: 25371
1.You can take a look at AST explorer. An online tool to explore the ASTs generated by more than 10 parsers. It is a good tool to learn AST tree of a language.
AST explorer source at Github.com.
2.Also you can paste your js code into JavaScript AST visualizer and click "show ast" button, you will see the AST visully.
demo js code:
function foo(d) {
d += 3;
return d+999
}
function bar(d) {
return d*100
}
Upvotes: 73
Reputation: 76899
SpiderMonkey offers the parser api. This is probably the easiest way to get your hands on the syntax objects.
There's also open js-js parsers like Esprima (which is ECMAScript, really, but it's right up the alley)
Upvotes: 33