user1335906
user1335906

Reputation: 145

Using Rhino parser in javascript code to parse strings in javascript

I am new to Rhino parser. Can i use this rhino parser in javascript code to extract the Abstract Syntax Tree of javascript code in any html file. If so ho should i start this.This is for Analyzing AST of the code for computing the ratio between keywords and words used in javascript, to identify common decryption schemes, and to calculate the occurrences of certain classes of function calls such as fromCharCode(), eval(),and some string functions that are commonly used for the decryption and execution of drive-by-download exploits.

Upvotes: 5

Views: 1786

Answers (1)

Matthew Crumley
Matthew Crumley

Reputation: 102735

As far as I know, you can't access the AST from JavaScript in Rhino. I would look at the Esprima parser though. It's a complete JavaScript parser written in JavaScript and it has a simple API for doing code analysis.

Here's a simple example that calculates the keyword to identifier ratio:

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;

Upvotes: 3

Related Questions