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

Bill Lear rael at zopyra.com
Fri Nov 26 13:50:01 PST 2010


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(); }
    ;


More information about the antlr-interest mailing list