我的语法如下:
node: '{' type ',' '"expression"' ':' rightSide '}' ;
rightSide:
call # callAlternative
| identifier # identifierAlternative
;
现在,在我的访问者中,我实现了方法visitNode(Parser.NodeContext ctx)
,并希望访问rightSide规则的正确方法,无论哪个替代方法匹配。#
标签用于为每个备选项生成专用方法,righSide
规则不再具有访问方法。visitNode
中的ctx
也只有ctx.rightSide()
,没有ctx.callAlternative()
和/或ctx.identifierAlternative()
。
如何做到这一点?
@Override
public SomeObj visitNode(Parser.NodeContext ctx) {
// how to detect which of the two alternatives was matched? ctx only has ctx.rightSide()
// What is the something in ctx.something ?
if(....){ // how to decide here??
visitIdentifierAlternative(ctx.something);
} else visitCallAlternative(ctx.something);
return new SomeObj();
}
@Override
public SomeObj visitIdentifierAlternative(Parser.IdentifierAlternativeContext ctx) {
// Do things only to be done for IdentifierAlternative
return new SomeObj();
}
@Override
public SomeObj visitCallAlternative(Parser.CallAlternativeContext ctx) {
// Do things only to be done for CallAlternative
return new SomeObj();
}
不直接调用visitIdentifierAlternative
或visitCallAlternative
。您只需调用visit
,它就会自行选择合适的方法。