JavaScript 中的 JavaScript 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2554519/
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
JavaScript parser in JavaScript
提问by emk
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?
我需要向 JavaScript 源代码添加一些轻量级的语法糖,并使用基于 JavaScript 的构建系统对其进行处理。有没有用 JavaScript 编写的开源 JavaScript 解析器?在 V8 或类似的高性能 JavaScript 实现之上运行时,它们是否相当快?
Thank you for any pointers you can provide!
感谢您提供的任何指示!
采纳答案by emk
Crescent Fresh answered this question in the comments:
Crescent Fresh在评论中回答了这个问题:
JSLint contains a JavaScript parser written in JavaScript. See JSlint by Douglas CrockfordAround line 2712 begins the parser. JSLint is written to also handle html so you'd have to gloss over those parts
JSLint 包含一个用 JavaScript 编写的 JavaScript 解析器。参见Douglas Crockford 的 JSlint大约第 2712 行开始解析器。JSLint 被编写为也处理 html,所以你必须掩盖这些部分
回答by mishoo
回答by Johannes Gerer
回答by Claudiu
acornis a really fast JavaScript parser written in JavaScript. It's even faster than esprimanow. The results I got in Chrome form esprima's speed comparison page:
acorn是一个用 JavaScript 编写的非常快速的 JavaScript 解析器。它现在甚至比esprima还要快。我在 Chrome 表单esprima 的速度比较页面中得到的结果:
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 escodegento generate JavaScript from the parse trees.
它与 Mozilla 的 Parser API 兼容,因此您可以使用escodegen从解析树生成 JavaScript。
回答by alunny
回答by CMS
The only metacircular interpreterthat I have seen implemented in JavaScript is the NarcissusEngine.
我见过的唯一用 JavaScript 实现的元循环解释器是Narcissus引擎。
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.
它也是由 Brendan Eich 开发的,他们使用了许多特定于SpiderMonkey的非标准扩展,我认为它不适用于 V8。
回答by Janus Troelsen
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.
Microsoft 在 TypeScript 中开发了 TypeScript 编译器。由于 TypeScript 是 JavaScript 的严格超集,并且 TypeScript 编译为 JavaScript,因此生成的编译器在技术上是用 JavaScript 编写的 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.
这当然取决于您对“编译器”的定义。但是,如果接受语言 A 超集的编译器不是语言 A 编译器,则不包括 GCC、Clang 和几乎所有其他编译器。
回答by Janus Troelsen
https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API:
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.
独立 SpiderMonkey shell 的最新版本包括 SpiderMonkey 解析器的反射,可作为 JavaScript API 使用。
Note that this is only an API in JavaScript, the parser is C++.
请注意,这只是 JavaScript 中的 API,解析器是 C++。
回答by Pavel Vlasov
JS/CC - The LALR(1) parser and lexical analyzer generator for JavaScript, written in JavaScript - http://jscc.phorward-software.com/
JS/CC - 用于 JavaScript 的 LALR(1) 解析器和词法分析器生成器,用 JavaScript 编写 - http://jscc.phorward-software.com/

