How to use esprima to parse a javascript file, from Java?

1.1k Views Asked by At

My requirement is to parse a javascript file and obtain the AST(Abstract Syntax Tree) in json format. The esprima.parseScript() function accepts the js code and generates the AST correctly. But, how to use esprima to read the js code from a different js file and parse it ? I need to be able to call the js function from a Java class.

I tried the reading the js which I wanted to parse, in Java and passed it as an input to the function. But, the following code gives an exception.

parser.js:

    load('esprima.js');
    function parsejs(jscode){
       var op = esprima.parseScript(jscode);
       return JSON.stringify(op);
     }`

js that needs to be parsed, example.js: function temp(){ return 5;}

Java code to read example.js and pass it as an argument to parsejs function:

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
        BufferedReader reader = new BufferedReader(newFileReader("example.js"));
        StringBuilder stringBuilder = new StringBuilder();
        char[] buffer = new char[10];
        while (reader.read(buffer) != -1) {
            stringBuilder.append(new String(buffer));
            buffer = new char[10];
        }
        reader.close();

        String content = stringBuilder.toString();
        engine.eval(new FileReader("parser.js"));
        Invocable invocable = (Invocable) engine;
        Object result = invocable.invokeFunction("parsejs",content);

        System.out.println(result);

But, I am getting the following exception:

javax.script.ScriptException: Error: Line 1: Unexpected token ILLEGAL in file:/C:/esprima.js at line number 5035 at column number 9
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
    at jsconvert.JSConvert.main(JSConvert.java:49)
Caused by: file:/C:/esprima.js:5035:9 Error: Line 1: Unexpected token ILLEGAL
    at jdk.nashorn.internal.runtime.ECMAException.create(ECMAException.java:113)
    at jdk.nashorn.internal.scripts.Script$Recompilation$174$226935AADA$esprima.L:12#L:4990#ErrorHandler#throwError(file:/C:/esprima.js:5035)
    at jdk.nashorn.internal.scripts.Script$Recompilation$173$233070A$esprima.L:12#L:5122#Scanner#throwUnexpectedToken(file:/C:/esprima.js:5164)
    at jdk.nashorn.internal.scripts.Script$Recompilation$142$250130$esprima.L:12#L:5122#Scanner#scanPunctuator(file:/C:/esprima.js:5667)
    at jdk.nashorn.internal.scripts.Script$Recompilation$127$274970$esprima.L:12#L:5122#Scanner#lex(file:/C:/esprima.js:6264)
    at jdk.nashorn.internal.scripts.Script$Recompilation$121$92260$esprima.L:12#L:1822#Parser#nextToken(file:/C:/esprima.js:2079)
    at jdk.nashorn.internal.scripts.Script$Recompilation$148$95915A$esprima.L:12#L:1822#Parser#expect(file:/C:/esprima.js:2166)
    at jdk.nashorn.internal.scripts.Script$Recompilation$150$189401$esprima.L:12#L:1822#Parser#parseFunctionSourceElements(file:/C:/esprima.js:4197)
    at jdk.nashorn.internal.scripts.Script$Recompilation$137$194562A$esprima.L:12#L:1822#Parser#parseFunctionDeclaration(file:/C:/esprima.js:4347)
    at jdk.nashorn.internal.scripts.Script$Recompilation$136$152687$esprima.L:12#L:1822#Parser#parseStatementListItem(file:/C:/esprima.js:3379)
    at jdk.nashorn.internal.scripts.Script$Recompilation$133$213757$esprima.L:12#L:1822#Parser#parseScript(file:/C:/esprima.js:4723)
    at jdk.nashorn.internal.scripts.Script$Recompilation$117$3582AAA$esprima.L:12#L:58#parse(file:/C:/esprima.js:122)
    at jdk.nashorn.internal.scripts.Script$Recompilation$116$5584AAA$esprima.L:12#L:58#parseScript(file:/C:/esprima.js:145)
    at jdk.nashorn.internal.scripts.Script$Recompilation$115$71A$\^eval\_.parsejs(<eval>:7)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:639)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386)
1

There are 1 best solutions below

0
On

I found the issue in my code. The issue was in the way I was reading the javascript. When I changed the code to use readLine() instead of reading each character, it started working fine.