I need to write a XText grammar for a language that supports the hyphen '-' in variable names.
I tried with the toy example below where I define ID to be the pattern for variable names. What I am trying to specify here is that ID starts with a letter eventually followed by zero or more characters that are letters or '-' and ends with a letter (not an hyphen).
grammar org.xtext.example.mydsl.MyDsl hidden(WS)
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
lines+=Line*
;
Line:
(Variable | Expression) '.'
;
Variable:
name=ID
;
Expression:
'=' Atomic ({LinkedExpression.leftOperand=current} op=('->') rightOperand=Atomic)*
;
Atomic returns Expression:
ref=[Variable]
;
terminal ID: ('a'..'z'|'A'..'Z')(('a'..'z'|'A'..'Z'|'0'..'9'|'-')*('a'..'z'|'A'..'Z'|'0'..'9'))?;
terminal INT: ('0'..'9')+;
terminal WS: (' '|'\t'|'\r'|'\n')+;
The result is that if I try to parse the below text:
x.
y.
m-n.
=x->y.
=x -> y.
=m-n -> y.
=m-n->y.
The lines in bold fail as '-' in the '->' operator is read as part of a variable name; so for example:
=x->y.
is tokenised as:
= x- > y .
instead of:
= x -> y .
What am I doing wrong?