How to parse variables and raw text

19 views
Skip to first unread message

Garry

unread,
Feb 12, 2015, 11:27:44 AM2/12/15
to antlr-di...@googlegroups.com

Hello.

In my language I have variables in format $ID$, all that doesn't match this pattern should be "raw text".
Also I have statements in format "<!--STATEMENT_NAME -->" and comments in format "<!-- -->".

Trying to make ez grammar for it, but it doesn't work:
grammar gram;

program : st*;

st : sqlSt | commentSt;

sqlSt :
'<!--SQL' commentText '-->';

commentSt :
'<!--' commentText '-->';

var : '$' Identifier '$';

commentText
: var commentText | raw commentText;

raw : Raw+;

Identifier : [a-zA-Z]+[a-zA-Z0-9_-]*;
Raw : .+?;

Newline :('\r' '\n'? | '\n') -> skip;

test input:
<!--SQL SOME TEXT -->
<!-- $var123$ COMMENT!!!$var123$ -->


 Tree:


Console output:

line 1:8 extraneous input 'SOME' expecting {'$', Raw}
line 1:13 extraneous input 'TEXT' expecting {'$', Raw}
line 1:18 extraneous input '-->' expecting {'$', Raw}
line 2:14 extraneous input 'COMMENT' expecting {'$', Raw}
line 2:33 extraneous input '-->' expecting {'$', Raw}


Maybe I should't use recursion here, but why "SOME" and "TEXT" are red? Help me please parse it.
Thank you in advance. 
Reply all
Reply to author
Forward
0 new messages