Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JavaScript to instrument code

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.

like image 520
BruceBerry Avatar asked May 09 '09 16:05

BruceBerry


2 Answers

Why not use a JavaScript parser? There are lots, including a Python API for ANTLR and a Python wrapper around SpiderMonkey.

like image 138
Hank Gay Avatar answered Oct 04 '22 07:10

Hank Gay


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

like image 44
Ira Baxter Avatar answered Oct 04 '22 07:10

Ira Baxter