I'd like to add a keyword to my language.
This keyword would only have to be matched during one particular parser grammar rule.
Due to backward compatibility I'd like to allow this keyword to continue to be used as a variable name, ie it can be matched by the lexer rule that determines if a token is suitable for a variable name.
The Lexer matches the new rule whenever it is found in the file.
Is the appropriate way of working around this to modify the var_declaration rule so that it matches either an IDENT or the new KEYWORD tokens?
protected
modified_var_declaration:
VAR (IDENT|KEYWORD);
;
The relevant rules are:
IDENT // matches variable names
options { testLiterals=true; }
: ( '_' | 'a'..'z' | 'A'..'Z' ) ( '_' | 'a'..'z' | 'A'..'Z' | DIGIT )*
;
KEYWORD: // my new keyword
"key"
;
The parser rule for creating a variable is:
protected
var_declaration:
VAR IDENT;
;