将JavaScript解析为仪器代码 [英] Parse JavaScript to instrument code

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

问题描述

我需要将JavaScript文件拆分为单个指令。例如

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).

基本上我需要检测代码,在这些指令之间注入代码以执行检查。分裂为;显然不会起作用,因为你也可以用换行结束指令,也许我不想在函数和类定义中设置代码(我还不知道)。我用 flex / Bison 但在这种情况下,此规则的语义操作将是打印解析树中的所有后代并将我的代码放在最后我认为不能用基本的野牛做。我该怎么做呢?我还需要拆分代码,因为我需要使用python-spidermonkey与Python接口。
或者......那里有一个图书馆,这可以让我免于重新发明轮子吗?它不一定是在Python中。

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.

推荐答案

为什么不使用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解析为仪器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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