Parse JavaScript to instrument code

前端 未结 5 1415
醉酒成梦
醉酒成梦 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:50

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

提交回复
热议问题