首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析用于导入的犀牛脚本

解析用于导入的犀牛脚本
EN

Stack Overflow用户
提问于 2018-02-07 11:55:11
回答 1查看 286关注 0票数 0

我需要找到犀牛脚本中使用的所有进口。例如,导入如下:- importPackage(Packages.org.json) importPackage(Packages.java.lang)

var jsonResponse = (Packages.java.lang.String) (xyz代码)。

如何使用犀牛解析器/ using有效地实现这一目标?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-13 16:57:01

我以以下方式实现了所需的功能(如果其他人遇到了这个问题):我使用了mozilla的javascript解析器,这给了我一个AstNode。有了这个AstNode之后,我创建了另一个实现mozilla NodeVisitor的类,在该类中,我根据需要重写了访问方法。

以下是代码片段:-

代码语言:javascript
复制
public class ASTtry {
static class Visitor implements NodeVisitor {
    List<String> ls;

    Visitor(List<String> loc) {
        ls = loc;
    }

    @Override
    public boolean visit(AstNode node) {
        if (node instanceof Name) {
            ls.add(node.getString());
        }
        if (node instanceof Loop) {
            int type = node.getType();
            ls.add(Token.typeToName(type));
        }
        return true;
    }
}

public static void main(String[] args) throws IOException {
    ASTtry ast = new ASTtry();
    CompilerEnvirons env = new CompilerEnvirons();
    setEnvironmentVariables(env);
    File scriptFile = new File("whatever path my file was");
    FileReader fr = new FileReader(scriptFile);
    AstNode root = new Parser(env).parse(fr, null, 1);
    List<String> allTokens = new ArrayList<>();
    root.visit(new Visitor(allTokens));
    Properties properties = new Properties();
    InputStream propFile;
    propFile = new FileInputStream("I had imports seperated by commas in this file");
    properties.load(propFile);
    ast.check(properties, allTokens);
}

private static void setEnvironmentVariables(CompilerEnvirons env) {
    env.setRecordingLocalJsDocComments(true);
    env.setAllowSharpComments(true);
    env.setRecordingComments(true);
    env.setIdeMode(true);
    env.setErrorReporter(new ErrReporter());
    env.setRecoverFromErrors(true);
    env.setGenerateObserverCount(true);
}

public boolean check(Properties properties, List<String> loc) {
    once I had all the lines of code, imports, i can easily loop around them for all the validations I need.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48663191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档