用于 Java 的 Javascript 解析器 [英] Javascript parser for Java

查看:31
本文介绍了用于 Java 的 Javascript 解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能推荐一个像样的 Java Javascript 解析器?我相信可以使用 Rhino,但是仅进行解析似乎有点矫枉过正,还是唯一合适的解决方案?任何建议将不胜感激.谢谢.

Anyone can recommend a decent Javascript parser for Java? I believe Rhino can be used, however it seems an overkill for just doing parsing, or is it the only decent solution? Any suggestion would be greatly appreciated. Thanks.

推荐答案

来自 https://github.com/google/caja/blob/master/src/com/google/caja/parser/js/Parser.java

下面的语法是语法 this 的上下文无关表示解析器解析.它与 EcmaScript 262 Edition 3 (ES3) 不一致,其中实现与 ES3 不一致.分号插入规则正确处理所需的表达式中可能的回溯回溯在代码中被彻底注释,因为分号插入需要来自词法分析器和解析器的信息并且无法确定具有有限的前瞻.

The grammar below is a context-free representation of the grammar this parser parses. It disagrees with EcmaScript 262 Edition 3 (ES3) where implementations disagree with ES3. The rules for semicolon insertion and the possible backtracking in expressions needed to properly handle backtracking are commented thoroughly in code, since semicolon insertion requires information from both the lexer and parser and is not determinable with finite lookahead.

值得注意的功能

  1. 在错误不会阻止任何进一步错误的队列上报告警告,以便我们可以在一次编译过程中报告多个错误,而不是强迫开发人员玩打地鼠游戏.
  2. 不解析 Firefox 风格的 catch ( if ),因为它们不适用于 IE 和许多其他解释器.
  3. 识别 const,因为许多解释器都可以识别(不是 IE)但会发出警告.
  4. 允许,但警告,ArrayObject 构造函数中的尾随逗号.
  5. 允许关键字作为标识符名称,但由于不同的解释器具有不同的关键字集而发出警告.这使我们能够使用广泛的关键字集.
  1. Reports warnings on a queue where an error doesn't prevent any further errors, so that we can report multiple errors in a single compile pass instead of forcing developers to play whack-a-mole.
  2. Does not parse Firefox style catch (<Identifier> if <Expression>) since those don't work on IE and many other interpreters.
  3. Recognizes const since many interpreters do (not IE) but warns.
  4. Allows, but warns, on trailing commas in Array and Object constructors.
  5. Allows keywords as identifier names but warns since different interpreters have different keyword sets. This allows us to use an expansive keyword set.

要解析严格的代码,请传入一个 PedanticWarningMessageQueueMessageLevel#WARNING 及以上转换为 MessageLevel#FATAL_ERROR.

To parse strict code, pass in a PedanticWarningMessageQueue that converts MessageLevel#WARNING and above to MessageLevel#FATAL_ERROR.

<小时>

CajaTestCase.js 展示了如何设置解析器,同一个类中的 [fromResource] 和 [fromString] 展示了如何获取输入正确的类型.


CajaTestCase.js shows how to set up a parser, and [fromResource] and [fromString] in the same class show how to get an input of the right kind.

这篇关于用于 Java 的 Javascript 解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆