[antlr-interest] Reading all text to end-of-line in a rule

Bill Lear rael at zopyra.com
Fri Nov 26 11:09:53 PST 2010


On Friday, November 26, 2010 at 19:23:24 (+0100) Martijn Reuvers writes:
>Hello Bill,
>
>The grammar below does the trick for your command (I stripped it
>somewhat, to quickly get it working). See the SHELL_COMMAND token,
>notice the options part with the greedy identifier. It reads until it
>encounters the newline stuff. You need to have something in front of
>it, like SHELL in this example - otherwise it would match anything,
>which is not what you want.

Thanks Martijn.  I tried replacing my original COMMAND_TEXT with
your suggestion:

COMMAND_TEXT: (options {greedy=false;} : . )* '\r'? '\n' {
        setText(getText().trim());
    }
    ;

and though this reduces the conflict, I still have one I don't know how
to resolve:

error(208): Command.g:124:1: The following token definitions can never be matched because prior tokens match the same input: NEWLINE

I need the NEWLINE so I can limit the commands to a single line.  Here is
one part of the grammar I did not share that is relevant, along with
the parts I changed with your help:

command
    : (clean|shell) (NEWLINE | EOF)
    | NEWLINE
    ;

[clean as before]

shell : 'shell' timeoutOption? COMMAND_TEXT ;

[timeoutOption as before, etc.]

COMMAND_TEXT: (options {greedy=false;} : . )* '\r'? '\n' {
        setText(getText().trim());
    }
    ;
EMAIL: ~('\n' | '\r' | ' ' | '"')+ ;
NEWLINE: '\r'? '\n' ;
WS: (' ' | '\t')+ { skip(); } ;

So, how do I preserve the single-line command idiom with the shell command
"read-to-end-of" line part?


Bill


More information about the antlr-interest mailing list