Trying to follow the example in definitive antlr 4 reference and stuck on page 147 where i cannot find the reference to parser.VarContext for my java grammar.** - I need to get to the ID and i cannot find VarContext, only a VarLocalContext which doesn't have the id. Did I miss something in the definitions ? I verified that the class is being generated, so its something else odd.
I've bolded the lines that get me in trouble but the VariableDeclaratorContext does exist but doesn't match what i need.
My source is below.
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");
}
}
}