我正在尝试处理.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
。
这是什么问题?