antlr4访客不起作用

时间:2016-04-01 20:38:49

标签: groovy antlr4

我正在尝试处理.pas文件。我生成词法分析器,解析器,访问者,语法听众

这是我的自定义访问者实现

class GroovyPascalVisitor extends PascalBaseVisitor<ScriptSkeleton> {

    private static final String PASCAL_SCRIPT_NAME = "PascalScript"

    @Lazy ScriptSkeleton scriptSkeleton = new ScriptSkeleton()

    @Override
    public ScriptSkeleton visitProgramHeading(
            PascalParser.ProgramHeadingContext ctx) {
        log.info "Visiting program heading"
        def scriptName = ctx.identifier().getText()?.trim()
        if (!scriptName) {
            scriptName = PASCAL_SCRIPT_NAME +
                    "${new Random().nextInt(1000)}"
        }
        scriptSkeleton.scriptName = scriptName
        return scriptSkeleton
    }
}

以下是我脚本中的代码

def file = new ANTLRFileStream(pas)
def lexer = new PascalLexer(file)
def parser = new PascalParser(new CommonTokenStream(lexer))

tree = parser.program()
stringTree = tree.toStringTree(parser)

visitor = new GroovyPascalVisitor()
skeleton = visitor.visit(tree)

.pas文件内容如下所示

program HelloWorld;

begin
    writeln('Hello, World!');
end.

但是当我运行脚本时 - 它不会从日志中打印消息,而对象scriptSkeleton为空,这意味着它根本不会调用visitProgramHeading

这是什么问题?

0 个答案:

没有答案