Oak
Oak

Reputation: 26898

How can I create cross-reference when a sigil is required for referring but not for defining?

In my DSL, I have something similar to:

x = 14
y = $x + 1

So an element is defined with just its name, but when referred to, some sigil must be added. Any whitespace between the sigil and the name is forbidden when referencing the element.

How can I do this in Xtext, while still allowing cross-reference between these elements?

Because it seems to me that I either have to use two different terminals for this - one to match x and the other to match $x - but then how would the cross-reference mechanism associate them together? Or alternatively, if I define:

ElementRef: '$' [Element|ELEMENT_NAME];

then Xtext will allow whitespace between the sigil and the name, which is illegal in my DSL. I guess an option such as "do not accept whitespace at this point" would be great, but I could not find anything in the Xtext documentation about something like that.

Upvotes: 0

Views: 228

Answers (1)

Sebastian Zarnekow
Sebastian Zarnekow

Reputation: 6729

You have to use a datatype rule for the cross-reference token and register a value converter that strips the $ sign.

ElementRef: [Element|ReferenceID];
ReferenceID hidden(): '$' ID;

The value converter is responsible for the conversion between the abstract syntax (the ID) and the concrete syntax ($ID) for your tokens. Please refer to the docs for details.

Upvotes: 1

Related Questions