解析 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 解析器? 有很多,包括 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.
JavaScript 很难解析; 你需要一个完整的 JavaScript 解析器。
DMS 软件重组工具包可以解析完整的 JavaScript 并构建相应的AST。
然后可以使用 AST 运算符遍历树以“拆分它”。 然而,更简单的是应用源到源的转换,查找一种表面语法 (JavaScript) 模式,并将其替换为另一种模式。 您可以使用此类转换将检测插入到代码中,而不是拆分代码以保留进行插入的位置。 转换完成后,DMS 可以重新生成有效的 JavaScript 代码(如果不受影响,则包含原始注释)。
JavaScript is tricky to parse; you need a full JavaScript parser.
The DMS Software Reengineering Toolkit can parse full JavaScript and build a corresponding AST.
AST operators can then be used to walk over the tree to "split it". Even easier, however, is to apply source-to-source transformations that look for one surface syntax (JavaScript) pattern, and replace it by another. You can use such transformations to insert the instrumentation into the code, rather than splitting the code to make holds in which to do the insertions. After the transformations are complete, DMS can regenerate valid JavaScript code (complete with the orignal comments if unaffected).
为什么不使用现有的 JavaScript 解释器,例如 Rhino (Java) 或 python-spidermonkey (不确定这个是否还活着)? 它将解析 JS,然后您可以检查生成的解析树。 我不确定重新创建原始代码有多容易,但这主要取决于检测代码的可读性。 如果没有人看过它,只需生成一个非常紧凑的表格即可。
睡衣 可能也很有趣; 这是一个 Python 到 JavaScript 的转译器。
[编辑] 虽然乍一看这并不能解决您的问题,但您可以将其用于不同的方法:不用检测 JavaScript,而是用 Python 编写代码(可以轻松检测;所有工具都已存在)并且然后将结果转换为 JavaScript。
最后,如果您想在 Python 中解决问题但找不到解析器:使用 Java 引擎向代码添加注释,然后您可以在 Python 中搜索注释来检测代码。
Why not use an existing JavaScript interpreter like Rhino (Java) or python-spidermonkey (not sure whether this one is still alive)? It will parse the JS and then you can examine the resulting parse tree. I'm not sure how easy it will be to recreate the original code but that mostly depends on how readable the instrumented code must be. If no one ever looks at it, just generate a really compact form.
pyjamas might also be of interest; this is a Python to JavaScript transpiler.
[EDIT] While this doesn't solve your problem at first glance, you might use it for a different approach: Instead of instrumenting JavaScript, write your code in Python instead (which can be easily instrumented; all the tools are already there) and then convert the result to JavaScript.
Lastly, if you want to solve your problem in Python but can't find a parser: Use a Java engine to add comments to the code which you can then search for in Python to instrument the code.
为什么不尝试一下 javascript 美化器呢?
例如 http://jsbeautifier.org/
或参见 适用于 Windows 和 Linux 的命令行 JavaScript 代码美化器
Why not try a javascript beautifier?
For example http://jsbeautifier.org/
Or see Command line JavaScript code beautifier that works on Windows and Linux
忘记我的解析器。 https://bitbucket.org/mvantellingen/pyjsparser 是一个很棒且完整的解析器。 我在这里修复了一些错误:https://bitbucket.org/nullie/pyjsparser
Forget my parser. https://bitbucket.org/mvantellingen/pyjsparser is great and complete parser. I've fixed a couple of it's bugs here: https://bitbucket.org/nullie/pyjsparser