As far as I know, you cannot access AST from JavaScript in Rhino. I would look at Esprima . This is a complete JavaScript parser written in JavaScript, and it has a simple API for parsing code.
Here is a simple example that calculates the ratio of a keyword to an identifier:
var tokens = esprima.parse(script, { tokens: true }).tokens;
var identifierCount = 0;
var keywordCount = 0;
tokens.forEach(function (token) {
if (token.type === 'Keyword') {
keywordCount++;
}
else if (token.type === 'Identifier') {
identifierCount++;
}
});
var ratio = keywordCount / identifierCount;
source
share