c# - 如何检测行首,或 : "The name ' getCharPositionInLine' does not exist in the current context"

标签 c# antlr4 antlr4cs

我正在尝试创建行首标记:

lexer grammar ScriptLexer;

BOL : {getCharPositionInLine() == 0;}; // Beginning Of Line token

但是上面给出了错误

The name 'getCharPositionInLine' does not exist in the current context

当它创建这段代码时:

private void BOL_action(RuleContext _localctx, int actionIndex) {
    switch (actionIndex) {
    case 0: getCharPositionInLine() == 0; break;
    }
}

getCharPositionInLine() 方法不存在的地方...

最佳答案

最简单的方法是将 EOL 识别为相应的 BOL 代币。

BC  : '/*' .*? '*/' -> channel(HIDDEN) ;
LC  : '//' ~[\r\n]* -> channel(HIDDEN) ;
HWS : [ \t]*        -> channel(HIDDEN) ;
BOL : [\r\n\f]+ ;

像 block 评论规则这样的规则将在内部消耗 EOL,所以没有问题。行注释等规则不会消耗 EOL,因此会为紧随其后的行发出适当的 BOL。

一个潜在的问题是输入开始时不会发出 BOL。处理此问题的最简单方法是在将输入文本提供给词法分析器之前,强制在输入文本前加上行终端。

关于c# - 如何检测行首,或 : "The name ' getCharPositionInLine' does not exist in the current context",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31903723/

相关文章:

ANTLR4 相互左递归

c# - 从数据库和文件系统中删除文件

c# - 在 LINQ 中转换为 int 不起作用?

c# - 将 LeftBracket 之后的所有内容解释为字符串,直到下一个 RightBracket

java - 通过ErrorListener累积/收集错误以在解析后进行处理

antlr4 - 当我使用 BaseListener 遍历 ParserRuleContext 时,如何修改或插入 ParserRuleContext

Antlr 语义谓词未能找到可行的替代方案

c# - 如何向 MessageBox 添加扩展方法

c# - WPF 富文本框 : save/load in custom format