我如何知道父上下文中匹配的替代方案?
How can I know the matched alternative in the parent context?
我如何知道父上下文中匹配的备选方案,例如在我的语法中
simpleAssign: name = ID '=' value = (STRING | BOOLEAN | INTEGER | DOUBLE );
simpleReference: name = ID '=' value = ID;
listAssign: name = ID '=' value = listString #listStringAssign;
assign: simpleAssign #simpleVariableAssign
| listAssign #listOfVariableAssign
| simpleReference #referenceToVariable
;
assignVariableBlock: assign + #assignVariabels;
我想知道在我的函数 enterAssignVariableBlock 中匹配的备选方案。
@Override public void enterAssignVariableBlock(StudyParser.AssignVariableBlockContext ctx) {
// switch matched alternative (simpleVariableAssign | listOfVariableAssign | referenceToVariable ) do
}
enter...
方法不会被称为 enterAssignVariableBlock (...)
,而是 enterAssignVariabels(...)
,因为您通过 #assignVariabels
.
将其标记为这样
虽然理想情况下,父级不应该关心其子级的具体实现,但您可以通过以下方式从父级规则中找出类型:
@Override
public void enterAssignVariabels(StudyParser.AssignVariabelsContext ctx) {
for (StudyParser.AssignContext childCtx : ctx.assign()) {
if (childCtx instanceof StudyParser.SimpleVariableAssignContext) {
// #simpleVariableAssign
}
else if (childCtx instanceof StudyParser.ListOfVariableAssignContext) {
// #listOfVariableAssign
}
else {
// #referenceToVariable
}
}
}
我如何知道父上下文中匹配的备选方案,例如在我的语法中
simpleAssign: name = ID '=' value = (STRING | BOOLEAN | INTEGER | DOUBLE );
simpleReference: name = ID '=' value = ID;
listAssign: name = ID '=' value = listString #listStringAssign;
assign: simpleAssign #simpleVariableAssign
| listAssign #listOfVariableAssign
| simpleReference #referenceToVariable
;
assignVariableBlock: assign + #assignVariabels;
我想知道在我的函数 enterAssignVariableBlock 中匹配的备选方案。
@Override public void enterAssignVariableBlock(StudyParser.AssignVariableBlockContext ctx) {
// switch matched alternative (simpleVariableAssign | listOfVariableAssign | referenceToVariable ) do
}
enter...
方法不会被称为 enterAssignVariableBlock (...)
,而是 enterAssignVariabels(...)
,因为您通过 #assignVariabels
.
虽然理想情况下,父级不应该关心其子级的具体实现,但您可以通过以下方式从父级规则中找出类型:
@Override
public void enterAssignVariabels(StudyParser.AssignVariabelsContext ctx) {
for (StudyParser.AssignContext childCtx : ctx.assign()) {
if (childCtx instanceof StudyParser.SimpleVariableAssignContext) {
// #simpleVariableAssign
}
else if (childCtx instanceof StudyParser.ListOfVariableAssignContext) {
// #listOfVariableAssign
}
else {
// #referenceToVariable
}
}
}