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.
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.
Share Improve this question edited Nov 20, 2010 at 16:05 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked May 9, 2009 at 16:05 BruceBerryBruceBerry 1,1861 gold badge9 silver badges21 bronze badges 2- Well, I would try jQuery AOP – mozillanerd Commented Oct 7, 2009 at 1:15
- I don't see this suggestion followed up. I have instrumented large javascript libraries. I hope you give it a try; it is easy to use. – mozillanerd Commented Nov 16, 2010 at 20:51
5 Answers
Reset to default 4Why not use a JavaScript parser? There are lots, including a Python API for ANTLR and a Python wrapper around SpiderMonkey.
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 plete, DMS can regenerate valid JavaScript code (plete with the orignal ments if unaffected).
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 pact 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 ments to the code which you can then search for in Python to instrument the code.
Why not try a javascript beautifier?
For example http://jsbeautifier/
Or see Command line JavaScript code beautifier that works on Windows and Linux
Forget my parser. https://bitbucket/mvantellingen/pyjsparser is great and plete parser. I've fixed a couple of it's bugs here: https://bitbucket/nullie/pyjsparser