Parse JavaScript to instrument code

前端 未结 5 1416
醉酒成梦
醉酒成梦 2021-02-09 05:56

I need to split a JavaScript file into single instructions. For example

a = 2;
foo()
function bar() {
    b = 5;
    print(\"spam\");
}

has to

5条回答
  •  渐次进展
    2021-02-09 06:26

    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.

提交回复
热议问题