I need to take a load of Javascript and instrument it automatically: specifically I want to log every call to a function, and provide a list of arguments that the function was invoked with.
I have a half-baked way of doing this with Python : using 're' to match 'function' keywords - but it's really quite primitive (doesn't deal with comments and so on).
I'm no expert (very far from it) with ANTLR: but how easy could I leverage a already built Javascript Parser to perform this ? (Can we 'hook' out to standard java to dynamically create the code I need at the right point?)
BTW: the actually logging will (probably) be done with log4javascript; but I might also just use 'alerts' - the hard bit is getting the code-injection working....
So for example, I need to turn something like:
function foo(bar) {
...
}
into:
function foo(bar) {
alert("<scriptname.js>: foo was called with arguments: [bar="+bar+"]");
...
}