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

Martijn Reuvers martijn.reuvers at gmail.com
Sun Nov 28 23:08:16 PST 2010


Hello Bill,

As said before most likely your newline is causing problems. I'd
recommend you to get rid of it first (completely including the \r \n
constructs), to the point where your grammar does not give any error
anymore. After that introduce it step by step.

As a side note avoid using text constructs in parser rules (e.g.
'cleanLogs', use real tokens for it). Sooner or later these will cause
trouble, plus that with real tokens you can have better error
reporting.

Regards,
Martijn

On Fri, Nov 26, 2010 at 10:50 PM, Bill Lear <rael at zopyra.com> wrote:
> On Friday, November 26, 2010 at 20:15:19 (+0100) Massimiliano Donini writes:
>>
>>Can you send complete grammar?
>>That error is about lexer rules order.
>
> Ok, added below my sig.
>
>
> Bill
>
> grammar Command;
>
> @header {
>    import java.util.ArrayList;
>    import java.util.List;
> }
>
> @parser::members {
>    private StageParameters stageParameters;
>    private List<Command> commandList = new ArrayList<Command>();
>
>    public void setStageParameters(final StageParameters params) {
>        stageParameters = params;
>    }
>
>    public List<Command> getCommandList() {
>        return commandList;
>    }
> }
>
> commands: command+
>    ;
>
> command
> scope {
>    int timeout;
>    List<String> notifyList;
> }
> @init {
>    $command::timeout = -1;
>    $command::notifyList = new ArrayList<String>();
> }
>    : (cleanlogs|cleanup) (NEWLINE | EOF)
>    | NEWLINE
>    ;
>
> cleanlogs
> @init {
>    $command::timeout = CleanlogCommand.defaultMinutesToSleep;
> }
>    : 'cleanLogs' command_options? {
>        commandList.add(new CleanlogCommand(stageParameters,
>                                            $command::timeout,
>                                            $command::notifyList));
>    }
>    ;
>
> cleanup
> @init {
>    $command::timeout = CleanlogCommand.defaultMinutesToSleep;
> }
>    : 'cleanup' command_options ? {
>        commandList.add(new CleanupCommand(stageParameters,
>                                           $command::timeout,
>                                           $command::notifyList));
>    }
>    ;
>
> shellCommand
> @init {
>    $command::timeout = ShellCommand.defaultMinutesToSleep;
> }
>    : 'shellCommand' timeoutOption? COMMAND_TEXT {
>        commandList.add(new ShellCommand(stageParameters,
>                                         $COMMAND_TEXT.text));
>    }
>    ;
>
> command_options:
>    timeoutOption
>    | notifyOption
>    | timeoutOption notifyOption
>    | notifyOption timeoutOption
>    ;
>
> timeoutOption:
>    '-timeout' INT { $command::timeout = Integer.parseInt($INT.text); }
>    ;
>
> notifyOption: '-notify' EMAIL {
>        $command::notifyList.add($EMAIL.text);
>    }
>    | '-notify' QUOTED_STRING {
>        String[] l = $QUOTED_STRING.text.split("\\s+");
>
>        for (int i = 0; i < l.length; i++) {
>            $command::notifyList.add(l[i]);
>        }
>    }
>    ;
>
> COMMAND_TEXT: (options {greedy=false;} : . )* '\r'? '\n' {
>        setText(getText().trim());
>    }
>    ;
>
> QUOTED_STRING:
>    '"' ( EscapeSequence | ~('\\'|'"') )* '"' {
>        setText(getText().substring(1, getText().length() - 1));
>    }
>    | '\'' ( EscapeSequence | ~('\\'|'\'') )* '\'' {
>        setText(getText().substring(1, getText().length() - 1));
>    }
>    ;
>
> fragment
> EscapeSequence : '\\' ('\"'|'\''|'\\') ;
>
> INT: '0'..'9'+
>    ;
>
> ID: 'a'..'z'+
>    ;
>
> EMAIL: ~('\n' | '\r' | ' ' | '"')+
>    ;
>
> NEWLINE:
>    '\r'? '\n'
>    ;
>
> COMMENT
>    : '//' ~('\n'|'\r')* '\r'? '\n' { skip(); }
>    | '/*' ( options {greedy=false;} : . )* '*/' { skip(); }
>    ;
>
> WS: (' ' | '\t')+ { skip(); }
>    ;
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list