antlr4 - 找不到 Parser.varContext
问题描述
试图遵循权威 antlr 4 参考中的示例并停留在第 147 页,我无法找到我的 java 语法对 parser.VarContext 的引用。** - 我需要获取 ID,但我找不到 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");
}
}
}
解决方案
推荐阅读
- sql - pgsql DELETE + LIMIT + JOIN + ORDER
- javascript - Webpack 混淆器问题:无法读取未定义的属性“tap”
- flutter - Flutter:如何制作自定义头像
- scala - 为什么运算符关联性对于 Scala 3 扩展方法和常规方法的工作方式不同?
- ruby-on-rails - Rails 多个评级星形式
- javascript - 角度无法从事件处理程序调用服务
- c# - 如何阅读 Apache 日志
- python-3.x - 如何使用 selenium webdriver 在 Google 学者中滚动滑块
- python - 网页抓取时返回空白括号
- flutter - 使用https而不是http进行flutter开发