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, I must start it. This is for analyzing AST code to calculate the relationship 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 decryption and execution of boot exploits.

+5
source share
1 answer

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;
+3
source

All Articles