Пытаюсь следовать примеру в окончательной справке по antlr 4 и застрял на странице 147, где я не могу найти ссылку на parser.VarContext для моей java грамматики. ** - Мне нужно добраться до идентификатора, и я не могу найти VarContext , только VarLocalContext, у которого нет идентификатора. Я что-то упустил в определениях? Я проверил, что класс создается, так что это еще что-то странное.
Я выделил жирным шрифтом строки, которые вызывают у меня проблемы, но VariableDeclaratorContext существует, но не соответствует тому, что мне нужно.
Мой источник ниже.
enter code here
import org.antlr.v4.runtime.tree.ParseTreeProperty;
public class RefPhase<CatchesContext> extends JavaBaseListener {
ParseTreeProperty<Scope> scopes;
GlobalScope globals;
Scope currentScope; // resolve symbols starting in this scope
public RefPhase(GlobalScope globals, ParseTreeProperty<Scope> scopes) {
this.scopes = scopes;
this.globals = globals;
}
@SuppressWarnings("unchecked")
public void enterFile(CatchesContext ctx) {
currentScope = (Scope) globals;
}
public void enterCompilationUnit(JavaParser.CompilationUnitContext ctx) {
currentScope = scopes.get(ctx);
}
public void exitCompilationUnit(JavaParser.CompilationUnitContext ctx) {
currentScope = currentScope.getEnclosingScope();
}
public void enterBlock(JavaParser.BlockContext ctx) {
currentScope = scopes.get(ctx);
}
public void exitBlock(JavaParser.BlockContext ctx) {
currentScope = currentScope.getEnclosingScope();
}
**public void exitVar(JavaParser.VariableDeclaratorContext ctx) {
String name = ctx.ID().getSymbol().getText();**
Symbol var = currentScope.resolve(name);
if ( var==null ) {
CheckSymbols.error(ctx.ID().getSymbol(), "no such variable: "+name);
}
if ( var instanceof FunctionSymbol ) {
CheckSymbols.error(ctx.ID().getSymbol(), name+" is not a variable");
}
}
public void exitCallContext(JavaParser.CatchesContext ctx) {
// can only handle f(...) not expr(...)
String funcName = ctx.ID().getText();
Symbol meth = ((Object) currentScope).resolve(funcName);
if ( meth==null ) {
CheckSymbols.error(ctx.ID().getSymbol(), "no such function: "+funcName);
}
if ( meth instanceof VariableSymbol ) {
CheckSymbols.error(ctx.ID().getSymbol(), funcName+" is not a function");
}
}
}