Reputation: 3255
One example text I like to parse is like this -
@comment {
{ something }
{ something else }
}
Basically "@comment" is the key to search, after that is a pair of matching braces. I do not need to parse what is between the braces. Since this is similar to C' multi-line comment, I have my grammar based on that:
grammar tryit;
tryit : top_cmd
;
WS : ('\t' | ' ')+ {$channel = HIDDEN;};
New_Line : ('\r' | '\n')+ {$channel = HIDDEN;};
top_cmd :cmds
;
cmds
: cmd+
;
cmd
: Comment
;
Comment
: AtComment Open_Brace ( options {greedy = false; }: . )+ Close_Brace
;
AtComment
: '@comment'
;
Open_Brace
: '{'
;
Close_Brace
: '}'
;
But in testing in ANTLRWORKS, I get a EarlyExitException immediately.
Do you see what is wrong?
Upvotes: 1
Views: 78
Reputation: 170308
There are two issues that I see:
"@comment"
and the first "{"
. Note that the spaces are put on the HIDDEN channel for parser rules, not for lexer rules!( options {greedy = false; }: . )+
you're just matching the first "}"
, not balanced braces.Try something like this instead:
tryit
: top_cmd
;
top_cmd
: cmds
;
cmds
: cmd+
;
cmd
: Comment
;
Comment
: '@comment' ('\t' | ' ')* BalancedBraces
;
WS
: ('\t' | ' ')+ {$channel = HIDDEN;}
;
New_Line
: ('\r' | '\n')+ {$channel = HIDDEN;}
;
fragment
BalancedBraces
: '{' (~('{' | '}') | BalancedBraces)* '}'
;
Upvotes: 1