ANTLR Explorer
ANTLR Explorer

Reputation: 3

ANTLR4 comments, preprocessor or directives parsing

I need to parse Delphi directives in ANTLR4. For example,

{$MODE Delphi}{$ifdef net}
net,
{$else}
NewKernel,
{$ifndef st}
{$ifndef neter}
hyper,
{$endif}
{$endif}
{$endif}
But simultaneously, in Delphi there are inside curly braces { } could be comments. So, How to create ANTLR4 grammar, which is working something like this: "file: not to parse text { parsing } not to parse text ;" ?

Upvotes: 0

Views: 440

Answers (1)

GRosenberg
GRosenberg

Reputation: 5991

Hard to be certain from how the Q is phrased. Sounds like you are looking for:

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

Update: the skip rule catches all tokens that are not consumed by the other rules. If only the directive rule is of interest, then only the corresponding DirectiveContext nodes in the parse tree need be evaluated.

Upvotes: 1

Related Questions