我需要在Antlr4中解析Delphi指令。例如,
{$MODE Delphi}{$ifdef net}
net,
{$else}
NewKernel,
{$ifndef st}
{$ifndef neter}
hyper,
{$endif}
{$endif}
{$endif}
但是同时,在Delphi中,卷曲括号内部{}可能是注释。因此,如何创建antlr4语法,它正在工作类似的事情:"文件:不要解析文本{解析}不要解析文本;"?
很难从Q的措辞中确定。听起来您正在寻找:
directive : LDrctv .... RBrace ; // replace .... with appropriate rule terms
comment : LBrace .*? RBrace ;
skip : . ; // aggregates all tokens not included in above rules
LDrctv : '{$' ;
LBrace : '{' ;
RBrace : '}' ;
// other token rules
更新:skip
规则捕获了其他规则未消耗的所有令牌。如果仅关注directive
规则,则只需要评估解析树中的相应DirectiveContext
节点。