简体   繁体   English

如何使用 esprima 从 Java 解析 javascript 文件?

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

My requirement is to parse a javascript file and obtain the AST(Abstract Syntax Tree) in json format.我的要求是解析 javascript 文件并获得 json 格式的 AST(抽象语法树)。 The esprima.parseScript() function accepts the js code and generates the AST correctly. esprima.parseScript() function 接受 js 代码并正确生成 AST。 But, how to use esprima to read the js code from a different js file and parse it?但是,如何使用 esprima 从不同的 js 文件中读取 js 代码并进行解析呢? I need to be able to call the js function from a Java class.我需要能够从 Java class 调用 js function。

I tried the reading the js which I wanted to parse, in Java and passed it as an input to the function.我尝试在 Java 中读取我想要解析的 js,并将其作为输入传递给 function。 But, the following code gives an exception.但是,下面的代码给出了一个例外。

parser.js:解析器.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;}需要解析的js,example.js: function temp(){ return 5;}

Java code to read example.js and pass it as an argument to parsejs function: Java 代码读取 example.js 并将其作为参数传递给 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)

I found the issue in my code.我在我的代码中发现了这个问题。 The issue was in the way I was reading the javascript.问题在于我阅读 javascript 的方式。 When I changed the code to use readLine() instead of reading each character, it started working fine.当我将代码更改为使用 readLine() 而不是读取每个字符时,它开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript解析器本身是用ESPRIMA这样的javascript编写的,它是如何工作的? 然后谁来解析ESPRIMA的javascript - How it works that Javascript parser is itself written in javascript like ESPRIMA ? Then who parses the javascript of ESPRIMA 在使用r.js优化js文件时使用esprima解析文件错误 - parse error using esprima for file while optimizing js files with r.js 如何在 Javascript 中使用 XLSX 库来解析来自特定行的 Excel 文件 - How to use XLSX library in Javascript to parse an Excel file from a particular row 无法使用Esprima / Acorn解析函数:意外令牌&#39;(&#39; - Unable to parse function with Esprima/Acorn: unexpected token '(' 如何使用Java和JavaScript访问和解析XML文件 - How to access and parse XML file using Java and JavaScript 如何使用JavaScript加载和解析服务器中的静态JSON文件 - How to use a JavaScript to load and parse a static JSON file in the server 如何获取javascript文件内容并解析它以使用它的变量 - how to fetch a javascript file content and parse it to use it's variables 如何使用Java解析JavaScript的链接? - How to parse javascript for links with java? 如何在Java中解析javascript变量 - How to parse javascript variable in Java 如何用Java中的Jsoup解析javascript变量中的html? - How to parse html from javascript variables with Jsoup in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM