5

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.

1
  • FYI: An AST API was added in Rhino 1.7R3 Commented Jan 18, 2013 at 13:31

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

This parser was really good the above code worked for me Thank you. Can you provide a tutorial for this parser so that i can extract the AST of javascript code of a webpage in javscript
I haven't done much with it yet, so I don't have any experience with stuff like that. The parse function returns the AST though, so it shouldn't be too difficult. The documentation is at esprima.org/doc/index.html and the author's blog (ariya.ofilabs.com) has some examples of how to use it. Hopefully those help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.