i planning to implement a javascript parser in java. i know that there are several ways to do it. there are a view frameworks/engines/parser which could help to do it right, like:
- ANTLR 3/4: it seems like there is only a js grammer for v3
- Mozilla Rhino: atm i can parse variable names on initital (top-) namespace. but i am not able to parse nested scopes e.g. object members.. hm..
- Nashorn: maybe i should give it a try..?
maybe
- closure-compiler: IMHO this is very nice. but not for "non-google" js-code :) e.g. you have to apply several coding conventions to your javascript sources to get it working properly..
- maybe it is possible to adapt Packer to do it? Is there a Java implementation of Packer???
there is EcmaScript 5.1 related to this article. it seems to be very comfortable. but this is not exactly what i´am looking for.. and still no java :)
my question is:
what could/would be the best way to parse javascript for
- (object-)function names
- (object-)member names e.g. variables
is it even possible to do it?
what would be your approach? for me it is not essential to parse ALL special markups of javascript.. the important factor would be to parse function/variables in a consistent context for the typical markups like this:
// Avoid `console` errors in browsers that lack a console.
function Object() {
var method;
var noop = function() {
};
var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
};
var obj = new Object();
var test = "Hello World";
the parse should be able to deliver this information:
Node: Object
Node: Object.method
Node: Object.noop
Node: Object.length
Node: Object.console
Node: Object
Node: obj
Node: test
there is no direct need of any determination if the node is a function/variable.