The only way I see how you could go about doing this, is define an Echo
rule in your lexer grammar that matches the characters echo
followed by all other characters except \r
and \n
:
Echo
: 'echo' ~('\r' | '\n')+
;
and make sure that rule is before the rule that matches identifiers and keywords (like for
).
A quick demo of a possible start would be:
grammar Test;
parse
: (echo | for)*
;
echo
: Echo (NewLine | EOF)
;
for
: For Identifier In range NewLine
Do NewLine
echo
Done (NewLine | EOF)
;
range
: '{' Integer '..' Integer ('..' Integer)? '}'
;
Echo
: 'echo' ~('\r' | '\n')+
;
For : 'for';
In : 'in';
Do : 'do';
Done : 'done';
Identifier
: ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*
;
Integer
: '0'..'9'+
;
NewLine
: '\r' '\n'
| '\n'
| '\r'
;
Space
: (' ' | '\t') {skip();}
;
If you'd parse the input:
echo for print example
for i in {0..10..2}
do
echo "Welcome $i times"
done
echo the end for now!
with it, it would look like:
(I had to rotate the image a bit, otherwise it wouldn't be visible at all!)
HTH.