Reputation: 5108
As part of a larger grammar I'm trying to define rules to describe "method calls". I ran into trouble and I think I reduced the problem to my lack of knowledge regarding Terminals.
Here's a simple grammar describing my problem:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model: methodCalls+=MethodCall*;
MethodCall: 'call' ID '.' ID;
With that grammar I can write something like
call variable.method
call foo.bar
Now I would like to allow wildcard characters in the method name. I changed the MethodCall-rule to
MethodCall: 'call' ID '.' WildcardName;
and to the end of the grammar I added
terminal WildcardName : ('a'..'z'|'A'..'Z'|'_'|'*'|'?') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'*'|'?')*;
Trying
call variable.method
call foo.bar
again I got the error messages:
mismatched input 'foo' expecting RULE_ID
mismatched input 'variable' expecting RULE_ID
Why are 'foo' and 'variable' not being matched by Terminal ID? And more importantly, why does even adding the new Terminal without actually using it cause this error message?
Upvotes: 0
Views: 1194
Reputation: 11868
the parsing is done in two steps: lexing and parsing. terminal rules are done in the lexing phase => at the places you expect an ID a WildcardName is recognized => you have to use a Datatype rule for this as well
WildcardName : (ID | '*')+;
Upvotes: 3