I want to achieve following behavior: User:class
should be parsed to Object - User; Type - class
, alsoUs:er:class
should result Object - Us:er; Type - class
. I can't make second part work, as soon as I add :
as a legal symbol for WORD
it parses whole input as an object Object - Us:er:class
.
My grammar:
grammar Sketch;
/*
* Parser Rules
*/
input : (object)+ EOF ;
object : objectName objectType? NEWLINE ;
objectType : ':' TYPE ;
objectName : WORD ;
/*
* Lexer Rules
*/
fragment LOWERCASE : [a-z] ;
fragment UPPERCASE : [A-Z] ;
fragment NUMBER : [0-9] ;
fragment WHITESPACE : (' ') ;
fragment SYMBOLS : [!-/:-@[-`] ;
fragment C : [cC] ;
fragment L : [lL] ;
fragment A : [aA] ;
fragment S : [sS] ;
fragment T : [tT] ;
fragment U : [uU] ;
fragment R : [rR] ;
TYPE : ((C L A S S) | (S T R U C T));
NEWLINE : ('\r'? '\n' | '\r')+ ;
WORD : (LOWERCASE | UPPERCASE | NUMBER | WHITESPACE | SYMBOLS)+ ;
Fragments for each letter are for case-insensitive parsing. As I understand, lexer gives priority to rules top-to-bottom, so TYPE should be picked over WORD, but I can't achieve it. I'm new to antlr4, maybe I'm missing something obvious.