什么是 JavaScript AST,如何使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16127985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:22:33  来源:igfitidea点击:

What is JavaScript AST, how to play with it?

javascriptabstract-syntax-tree

提问by jiyinyiyong

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?

抽象语法树.. 我一直听说在 Github 上编译为 SpiderMonkey AST。
那么,这是 JS 语法树的实际标准吗?还有 V8,V8 是否使用相同类型的 AST?

How can I play with that?

我怎么能玩呢?

采纳答案by slezica

SpiderMonkey offers the parser api. This is probably the easiest way to get your hands on the syntax objects.

SpiderMonkey 提供了解析器 api。这可能是掌握语法对象的最简单方法。

There's also open js-jsparsers like Esprima(which is ECMAScript, really, but it's right up the alley)

还有像Esprima这样的开放式js-js解析器(这是 ECMAScript,真的,但它就在胡同里)

回答by cuixiping

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.

1.你可以看看AST explorer。一个在线工具,用于探索由 10 多个解析器生成的 AST。是学习一门语言的AST树的好工具。
Github.com 上的 AST 资源管理器源

enter image description here

在此处输入图片说明



2.Also you can paste your js code into JavaScript AST visualizerand click "show ast" button, you will see the AST visully.

2.您也可以将您的 js 代码粘贴到JavaScript AST 可视化工具中,然后单击“显示 AST”按钮,您将可以直观地看到 AST。

demo js code:

演示js代码:

function foo(d) {
  d += 3;
    return d+999
}
function bar(d) {
    return d*100
}

js ast demo

js ast 演示

回答by prosti

If you would like to try out the acron parser from professor Marijnh https://github.com/marijnhtry out this link: https://astexplorer.net/

如果你想尝试 Marijnh 教授的 acro 解析器https://github.com/marijnh试试这个链接:https: //astexplorer.net/

This is a tiny, fast JavaScript parser, written completely in JavaScript.

这是一个小巧、快速的 JavaScript 解析器,完全用 JavaScript 编写。

The above-mentioned JavaScript AST visualizeruses Esprima engine and has been also written in JavaScrpt.

上面提到的JavaScript AST 可视化器使用 Esprima 引擎,并且也是用 JavaScrpt 编写的。

JavaScript dominates in parsing AST because JavaScript engines are super optimized today. https://en.wikipedia.org/wiki/JavaScript_engine

JavaScript 在解析 AST 方面占主导地位,因为今天 JavaScript 引擎已经过超级优化。https://en.wikipedia.org/wiki/JavaScript_engine

SpiderMonkey AST standard of JS syntax tree? Is V8 using the same kind of AST?

JS语法树的SpiderMonkey AST标准?V8 是否使用相同类型的 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.

这两个 Web 浏览器引擎都在内部使用 C++ 编写了 AST 处理。这就是为什么除了eval.

回答by Rvach.Flyver

I know only of one specification of Javascript AST: https://github.com/estree/estree

我只知道 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.

它起源于 Mozilla 的 Dave Herman 的出版物,此后演变为社区标准。所以它应该在某种程度上匹配 SpiderMonkey,但我不确定 V8 和 JSC。

Would appreciate if someone could provide more information on the matter.

如果有人可以提供有关此事的更多信息,我们将不胜感激。