解析 JavaScript 来检测代码
我需要将 JavaScript 文件拆分为单个指令。 例如
a = 2;
foo()
function bar() {
b = 5;
print("spam");
}
必须分成三个指令。 (赋值、函数调用和函数定义)。
基本上我需要检测代码,在这些指令之间注入代码来执行检查。 用“;”分割 显然不起作用,因为您也可以用换行符结束指令,也许我不想在函数和类定义中插入代码(我还不知道)。 我使用 flex/Bison 但在这种情况下,该规则的语义操作将是“打印解析树中的所有后代并将我的代码放在最后”我认为基本的 Bison 无法完成。 我该怎么做呢? 我还需要拆分代码,因为我需要使用 python-spidermonkey 与 Python 交互。 或者...是否已经有一个图书馆可以让我免于重新发明轮子? 它不一定是Python 语言。
I need to split a JavaScript file into single instructions. For example
a = 2;
foo()
function bar() {
b = 5;
print("spam");
}
has to be separated into three instructions. (assignment, function call and function definition).
Basically I need to instrument the code, injecting code between these instructions to perform checks. Splitting by ";" wouldn't obviously work because you can also end instructions with newlines and maybe I don't want to instrument code inside function and class definitions (I don't know yet). I took a course about grammars with flex/Bison but in this case the semantic action for this rule would be "print all the descendants in the parse tree and put my code at the end" which can't be done with basic Bison I think. How do I do this? I also need to split the code because I need to interface with Python with python-spidermonkey.
Or... is there a library out there already which saves me from reinventing the wheel? It doesn't have to be in Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
为什么不使用现有的 JavaScript 解释器,例如 Rhino (Java) 或 python-spidermonkey (不确定这个是否还活着)? 它将解析 JS,然后您可以检查生成的解析树。 我不确定重新创建原始代码有多容易,但这主要取决于检测代码的可读性。 如果没有人看过它,只需生成一个非常紧凑的表格即可。
睡衣 可能也很有趣; 这是一个 Python 到 JavaScript 的转译器。
[编辑] 虽然乍一看这并不能解决您的问题,但您可以将其用于不同的方法:不用检测 JavaScript,而是用 Python 编写代码(可以轻松检测;所有工具都已存在)并且然后将结果转换为 JavaScript。
最后,如果您想在 Python 中解决问题但找不到解析器:使用 Java 引擎向代码添加注释,然后您可以在 Python 中搜索注释来检测代码。
忘记我的解析器。 https://bitbucket.org/mvantellingen/pyjsparser 是一个很棒且完整的解析器。 我在这里修复了一些错误:https://bitbucket.org/nullie/pyjsparser
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么不使用 JavaScript 解析器? 有很多,包括 ANTLR 的 Python API 和 SpiderMonkey 的 Python 包装器。
Why not use a JavaScript parser? There are lots, including a Python API for ANTLR and a Python wrapper around SpiderMonkey.