Skip to content

Instantly share code, notes, and snippets.

@lindenb
Last active April 16, 2017 07:45
Show Gist options
  • Save lindenb/f398a6e034032559af45cedc74e93a20 to your computer and use it in GitHub Desktop.
Save lindenb/f398a6e034032559af45cedc74e93a20 to your computer and use it in GitHub Desktop.
Javascript+java nashorn contains a javascript AST parser. See // http://stackoverflow.com/questions/29154309/nashorn-parser-api-on-jdk-8

Javascript+java8 nashorn contains a javascript AST parser. See // http://stackoverflow.com/questions/29154309/nashorn-parser-api-on-jdk-8

example

function x(n) {var a=[];for(var i=0;i< n;i++) { a.push(i); }} x(1000);

#run

$ jjs test.js  | python -m json.tool

#output:

{
    "body": [
        {
            "body": {
                "body": [
                    {
                        "declarations": [
                            {
                                "id": {
                                    "name": "a", 
                                    "type": "Identifier"
                                }, 
                                "init": {
                                    "elements": [], 
                                    "type": "ArrayExpression"
                                }, 
                                "type": "VariableDeclarator"
                            }
                        ], 
                        "type": "VariableDeclaration"
                    }, 
                    {
                        "declarations": [
                            {
                                "id": {
                                    "name": "i", 
                                    "type": "Identifier"
                                }, 
                                "init": {
                                    "type": "Literal", 
                                    "value": 0
                                }, 
                                "type": "VariableDeclarator"
                            }
                        ], 
                        "type": "VariableDeclaration"
                    }, 
                    {
                        "body": {
                            "body": [
                                {
                                    "expression": {
                                        "arguments": [
                                            {
                                                "name": "i", 
                                                "type": "Identifier"
                                            }
                                        ], 
                                        "callee": {
                                            "computed": false, 
                                            "object": {
                                                "name": "a", 
                                                "type": "Identifier"
                                            }, 
                                            "property": "push", 
                                            "type": "MemberExpression"
                                        }, 
                                        "type": "CallExpression"
                                    }, 
                                    "type": "ExpressionStatement"
                                }
                            ], 
                            "type": "BlockStatement"
                        }, 
                        "init": null, 
                        "test": {
                            "left": {
                                "name": "i", 
                                "type": "Identifier"
                            }, 
                            "operator": "<", 
                            "right": {
                                "name": "n", 
                                "type": "Identifier"
                            }, 
                            "type": "BinaryExpression"
                        }, 
                        "type": "ForStatement", 
                        "update": {
                            "argument": {
                                "name": "i", 
                                "type": "Identifier"
                            }, 
                            "operator": "++", 
                            "prefix": false, 
                            "type": "UpdateExpression"
                        }
                    }
                ], 
                "type": "BlockStatement"
            }, 
            "defaults": [], 
            "expression": false, 
            "generator": false, 
            "id": {
                "name": "x", 
                "type": "Identifier"
            }, 
            "params": [
                {
                    "name": "n", 
                    "type": "Identifier"
                }
            ], 
            "rest": null, 
            "type": "FunctionDeclaration"
        }, 
        {
            "expression": {
                "arguments": [
                    {
                        "type": "Literal", 
                        "value": 1000
                    }
                ], 
                "callee": {
                    "name": "x", 
                    "type": "Identifier"
                }, 
                "type": "CallExpression"
            }, 
            "type": "ExpressionStatement"
        }
    ], 
    "type": "Program"
}
// http://stackoverflow.com/questions/29154309/nashorn-parser-api-on-jdk-8
load("nashorn:parser.js");
var ast = parse("function x(n) {var a=[];for(var i=0;i< n;i++) { a.push(i); }} x(1000);");
print(JSON.stringify(ast));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment