parsing javascript

315 views
Skip to first unread message

thushara wijeratna

unread,
Mar 9, 2012, 3:12:31 PM3/9/12
to mozilla-rhino
Is it possible to parse javascript and extract string literals from
the code using Rhino?
I can use a separate HTML parser to identify the <script> blocks, so
the script blocks can be passed to Rhino.
I was using JSParser.parseJavascript() to get a Node object, but
having some trouble navigating the Node object.

thx,
thushara

thushara wijeratna

unread,
Mar 9, 2012, 6:08:20 PM3/9/12
to mozilla-rhino
I wanted to follow up with what I have, may be someone could offer a
better way to traverse the graph.
This shows how to get string literals from an expression node. With
this method, I'd have to specifically address each node type that may
have string literals in a number of forms (ex: ArrayLiteral)

public class JSParser {
private AstNode parseJavascript(String scriptText) throws
IOException {
Reader reader = new StringReader(scriptText);
CompilerEnvirons compilerEnv = new CompilerEnvirons();
ErrorReporter errorReporter = compilerEnv.getErrorReporter();
Parser parser = new Parser(compilerEnv, errorReporter);
return parser.parse(reader,null, 1);
}

static public void main(String[] args) {
JSParser jsp = new JSParser(); String scriptText = "keywords
= ['Rainier','Shasta','Mount Baker'];"
AstNode node = jsp.parseJavascript(scriptText);
for (Node v=node.getFirstChild(); v != null; v = v.getNext())
{
Token t;
int tp = v.getType();
if (tp == Token.EXPR_RESULT) {
ExpressionStatement expr = (ExpressionStatement)v;
AstNode exprNode = expr.getExpression();
if (exprNode.getType()==Token.ASSIGN) {
Assignment asg = (Assignment)exprNode;
AstNode valNode = asg.getRight();
if (valNode.getType()==Token.ARRAYLIT) {
ArrayLiteral arrNode =
(ArrayLiteral)valNode;
List<AstNode> elts =
arrNode.getElements();
for (AstNode eltNode : elts) {
if (eltNode.getType() == Token.STRING)
{

System.out.println(((StringLiteral)eltNode).getValue());

Travis Ennis

unread,
Mar 9, 2012, 11:13:07 PM3/9/12
to mozill...@googlegroups.com
The easiest way to traverse the AST is to use the org.mozilla.javascript.ast.NodeVisitor. To do something with all of the string nodes you could do:

NodeVisitor nodeVisitor = new NodeVisitor() {
            public boolean visit(AstNode node) {
                int type = node.getType();
                if (type == Token.STRING)
                    //do something;
                return true;
            }
        };

then pass this to the visit method of the AstRoot class

rootNode.visit(nodeVisitor);

Thanks,
Travis
--
Travis Ennis



thushara wijeratna

unread,
Mar 10, 2012, 11:20:48 PM3/10/12
to mozilla-rhino
Thanks. Makes it very simple.
Message has been deleted

richa

unread,
Dec 12, 2013, 11:54:15 PM12/12/13
to mozill...@googlegroups.com
         
can you please tell me which packages I has to import for this code to run& what is dependance for the same(which jar file is required for the same)



===============================================================================
Reply all
Reply to author
Forward
0 new messages