I'm writing an Xtext grammar and I want to support cross-reference for usage of variables or parameters in my target language.
So the target language looks something like:
function(p1) {
x1 = eat(p1) // line 1
x2 = eat(x1) // line 2
}
And for that, I've defined:
Instruction:
name=Value '=' 'eat' '(' ref=[Instruction|Value] ')'
This works for line 2, but fails for line 1 because a parameter is not an instruction. I've tried defining it like:
Instruction:
name=Value '=' 'eat' '(' ref=([Instruction|Value] | [Parameter|Value]) ')'
Or various variations of this - e.g. using a single Ref
rule which can be one of the two possible reference possibilities - but all of these are rejected by Xtext. Is this doable, and if so, how?
(I guess an alternative will be to define a single element which represents both a parameter and an instruction and then refer to it, but I could not figure out how to do that without allowing the syntax or an entire instruction to appear in the parameter list)