0
votes

I have a text "ON something blah blah blah could be anything"

I want to match the ON, something, and "blah blah blah could be anything"

I have my tokens as

ON      :   ('O'|'o')('N'|'n');
INDEX: (options {greedy=false;}: ESC | .)*
WS  :
    (   ' '
        |   '\t'
        |   '\r'
        |   '\n'
        )+
        { $channel=HIDDEN; }
        ;  
ESC :
    '\\' ('"'|'\''|'\\')
    ;

and I normally want whitespace to be thrown away so channel is hidden but in this case, I want to match the rest of the string WITH whitespace. how do I do this in a rule? Something like

myRule: ON INDEX REST;

but not sure what REST should be? (and not sure if I need imaginary token or not).

thanks, Dean

1
An imaginary token has nothing to do with it. What you're trying to do (in the way you're thinking of it) is probably not possible. Are you parsing an existing language? If so, which one? If note, please explain in more detail what (real world) input you're trying to parse, not a single small use case.Bart Kiers

1 Answers

1
votes

If I correctly understand your issue, so your grammar will be something like this (works correctly for your example):

grammar so1;

myRule: ON INDEX rest;

rest    :   ~('\r'|'\n')*;

ON      :   ('O'|'o')('N'|'n');

INDEX   :   (LETTER | DIGIT)+;

WS  :
(   ' '
    |   '\t'
    |   '\r'
    |   '\n'
    )+
    { $channel=HIDDEN; }
    ;  

fragment DIGIT : '0'..'9' ;
fragment LETTER : ('a'..'z' | 'A'..'Z' | '_');