我正在尝试创建行首标记:
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/