从AST获取方法时的错误声明

问题描述 投票:1回答:2

我正在尝试使用AST获取方法的声明,因此可以遍历它,查找某些语句。它以前曾经工作过,但是我不能再继续工作了,我得到的宣言对我来说就像是某种解析错误。我找不到有关如何解决此问题的信息:

{<|java+method:///MetricsTest/testMethod()|,compilationUnit([],[],src=|java+method:///MetricsTest/testMethod()|(0,30,<1,0>,<2,2>),decl=|java+compilationUnit:///MetricsTest/testMethod()|,messages=[error("Syntax error on token \"void\", @ expected",|java+method:///MetricsTest/testMethod()|(7,4,<1,0>,<1,0>)),error("Syntax error, insert \"enum Identifier\" to complete EnumHeader",|java+method:///MetricsTest/testMethod()|(23,1,<1,0>,<1,0>))])>}

我的(部分)代码如下:

public rel[loc, loc] getMethods(M3 model) {
    return { <x,y> | <x,y> <- model.containment
               , x.scheme=="java+class"
               , y.scheme=="java+method" || y.scheme=="java+constructor" 
               };
}

public rel[loc, Declaration] getMethodLocWithDeclaration(M3 model) {
    rel[loc, loc] methods = getMethods(model);
    rel[loc, Declaration] methodsWithAST = {};
    for(<loc c, loc m> <- methods) {
        methodsWithAST += <m, createAstFromFile(m, false)>;
    }

    return methodsWithAST;
}

有人知道我该如何解决吗?

rascal
2个回答
0
投票

createAstFromFile函数将返回AST的“一半”,即使输入文件中存在解析错误。因此,您获得了一个compilationUnit节点,但是在它的正下方有一个error节点,其中包含有关解析错误的信息。


0
投票

我最终要做的是为每个文件创建一个AST,访问该AST,每当我发现构造函数或方法时,都将impl添加到列表中。对于此列表,我发现我可以使用src来找到实际的loc

© www.soinside.com 2019 - 2024. All rights reserved.