Nashorn 抽象语法树遍历 [英] Nashorn Abstract Syntax Tree Traversal

查看:54
本文介绍了Nashorn 抽象语法树遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Nashorn 解析这个 Javascript:

I am attempting to parse this Javascript via Nashorn:

function someFunction() { return b + 1 };

并导航到所有语句.这包括函数内部的语句.

and navigate to all of the statements. This including statements inside the function.

下面的代码只是打印:"function {U%}someFunction = [] function {U%}someFunction()"

The code below just prints: "function {U%}someFunction = [] function {U%}someFunction()"

我如何进入"函数节点到它的主体return b + 1"?我认为我需要与访问者一起遍历树并获取子节点?

How do I "get inside" the function node to it's body "return b + 1"? I presume I need to traverse the tree with a visitor and get the child node?

我一直在关注以下问题的第二个答案:Java Javascript 解析器

I have been following the second answer to the following question: Javascript parser for Java

import jdk.nashorn.internal.ir.Block;
import jdk.nashorn.internal.ir.FunctionNode;
import jdk.nashorn.internal.ir.Statement;
import jdk.nashorn.internal.parser.Parser;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.ErrorManager;
import jdk.nashorn.internal.runtime.Source;
import jdk.nashorn.internal.runtime.options.Options;

import java.util.List;

public class Main {

    public static void main(String[] args){
        Options options = new Options("nashorn");
        options.set("anon.functions", true);
        options.set("parse.only", true);
        options.set("scripting", true);

        ErrorManager errors = new ErrorManager();
        Context context = new Context(options, errors, Thread.currentThread().getContextClassLoader());
        Source source   = Source.sourceFor("test", "function someFunction() { return b + 1; }  ");
        Parser parser = new Parser(context.getEnv(), source, errors);
        FunctionNode functionNode = parser.parse();
        Block block = functionNode.getBody();
        List<Statement> statements = block.getStatements();

        for(Statement statement: statements){
            System.out.println(statement);
        }
    }
}

推荐答案

使用 nashorn 引擎的私有/内部实现类不是一个好主意.启用安全管理器后,您将获得访问异常.使用 jdk9 及更高版本,您将在没有安全管理器的情况下遇到模块访问错误(如 jdk.nashorn.internal.* 包未从 nashorn 模块导出).

Using private/internal implementation classes of nashorn engine is not a good idea. With security manager on, you'll get access exception. With jdk9 and beyond, you'll get module access error w/without security manager (as jdk.nashorn.internal.* packages not exported from nashorn module).

您有两种使用 nashorn 解析 javascript 的选项:

You've two options to parse javascript using nashorn:

要使用Parser API,您需要使用jdk9+.

To use Parser API, you need to use jdk9+.

  • 对于 jdk8,可以使用 parser.js

  • For jdk8, you can use parser.js

load("nashorn:parser.js");

load("nashorn:parser.js");

并从脚本中调用解析"函数.此函数返回一个 JSON 对象,该对象表示所解析脚本的 AST.

and call "parse" function from script. This function returns a JSON object that represents AST of the script parsed.

查看此示例:http://hg.openjdk.java.net/jdk8u/jdk8u-dev/nashorn/file/a6d0aec77286/samples/astviewer.js

这篇关于Nashorn 抽象语法树遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆