Can I write a rule where the initial token is partly fixed and partly generic?
rule: ID '=' NUMBER
;
ID: (A.. Z | a.. Z) +
NUMBER: (0 .. 9) +
But only if the token ID is in the form var* (var is fixed)
Thanks
Can I write a rule where the initial token is partly fixed and partly generic?
rule: ID '=' NUMBER
;
ID: (A.. Z | a.. Z) +
NUMBER: (0 .. 9) +
But only if the token ID is in the form var* (var is fixed)
Thanks
Do you mean this?
// Use this instead of ID in all parser rules that take either token
identifier
: VAR_ID | ID
;
VAR_ID
: 'var' ('A'..'Z' | 'a'..'z')*
;
ID
: ('A'..'Z' | 'a'..'z')+
;
Either way: when possible, do NOT use a strict lexer grammar for your error messages. They are slower and completely illegible to users. You should parse ID
in a relaxed form, then check later on if it's in precisely the correct form.