How to exclude characters / characters using ANTLR grammar?

I am trying to write a grammar for various time formats (12:30, 0945, 1: 30-2: 45, ...) using ANTLR. While this works like a charm, until I type in characters that were not defined in the grammar file.

I use the following JUnit test, for example:

    final CharStream stream = new ANTLRStringStream("12:40-1300,15:123-18:59");
    final TimeGrammarLexer lexer = new TimeGrammarLexer(stream);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final TimeGrammarParser parser = new TimeGrammarParser(tokenStream);

    try {
        final timeGrammar_return tree = parser.timeGrammar();
        fail();
    } catch (final Exception e) {
        assertNotNull(e);
    }

An exception is thrown (as expected) because "15: 123" is not valid. If I try ("15: 23a"), then no exception will be thrown, and ANTLR treats it as a valid input.

Now, if I define the characters in my grammar, ANTLR seems to notice them, and again I get the exception that I want:

  CHAR: ('a'..'z')|('A'..'Z');

, , (äöü {% & < > !). - , : " ", "0..9,: -"

+3
2

...
- , : , "0..9,:-"

, ,, : -:

Foo
  :  ~('0'..'9' | ',' | ':' | '-')
  ;

(~ lexer)

: , , , . .

+5

, , . , antlr NonViableException.

:

 UTF8 :  ('\u0000'..'\u002A'     // ! to * 
     | '\u002E'..'\u002F'           // . / 
     | '\u003B'..'\u00FF'           // ; < = > ? @ as well as letters brackets and stuff
     ) 
     ;
+2

Source: https://habr.com/ru/post/1753861/


All Articles