我现在正在使用基于this grammar file的Antlr 3编写C#语法 .
但是,我发现了一些我无法理解的定义 .
NUMBER:
Decimal_digits INTEGER_TYPE_SUFFIX? ;
// For the rare case where 0.ToString() etc is used.
GooBall
@after
{
CommonToken int_literal = new CommonToken(NUMBER, $dil.text);
CommonToken dot = new CommonToken(DOT, ".");
CommonToken iden = new CommonToken(IDENTIFIER, $s.text);
Emit(int_literal);
Emit(dot);
Emit(iden);
Console.Error.WriteLine("\tFound GooBall {0}", $text);
}
:
dil = Decimal_integer_literal d = '.' s=GooBallIdentifier
;
fragment GooBallIdentifier
: IdentifierStart IdentifierPart* ;
上面的片段包含'GooBall'的定义 . 我对这个定义有一些疑问 .
为什么需要GooBall?为什么这个语法定义lexer规则来解析'0.ToString()'而不是解析器规则?
2 回答
这是因为这是一个没有被任何其他规则处理的有效表达式 - 我猜你称之为匿名对象,因为缺少一个更好的术语 . 类似于“你好世界”.ToUpper() . 通常,方法调用仅对变量标识符有效或返回值ala GetThing() . Method()或其他方式 .
抱歉 . 我找到了the official FAQ pages的原因 .