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

查看:211
本文介绍了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


下面的语法是这个
解析器解析的语法的无上下文表示。它不同意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. 报告队列上的警告,其中错误不会阻止任何进一步的错误,这样我们就可以在单个编译过程中报告多个错误,而不是强迫开发人员玩whack-a-mole。

  2. 不解析Firefox样式 catch(< Identifier> if< Expression>),因为那些不适用于IE和许多其他解释器。

  3. 识别 const ,因为许多口译员都做(不是IE)但警告。

  4. 允许,但警告,在数组对象构造函数中的尾随逗号。

  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.

要解析严格的代码,请传入 PedanticWarningMessageQueue
MessageLevel#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天全站免登陆