Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Cross-references for multi-word identifiers
Cross-references for multi-word identifiers [message #708181] Tue, 02 August 2011 14:46 Go to next message
SWAT  is currently offline SWAT Friend
Messages: 4
Registered: August 2011
Junior Member
Hi,

One of the things I really need to be able to do with Xtext is to generate EMF entities whose names contain multiple (space-separated) words (the vocabulary for which is not known at design-time). That is, I want my input file to contain:

object what a nice EMF object;

Oh what a nice EMF object this is.

Have you seen what a nice EMF object it is?

and handle linking the occurrences of 'what a nice EMF object' together.

It seems maddeningly close to being possible, and I'm sure the reason it seems like it isn't is my own ignorance of the right way to do it.

What I have so far looks like this:

ObjectDeclaration : 'object' (name=ObjectName) ';';

OhThisIs : 'Oh' (argument=[ObjectDeclaration|STRING]) 'this is.';

HaveYouSeen: 'Have you seen' (argument=[ObjectDeclaration|STRING]) 'it is?';

ObjectDeclaration: ID (' ' ID)*;

I've tried various ways of specifying the cross-reference, but none of them work, usually giving "mismatched input '', expecting RULE_ID", and variants thereon.

As I say, I'm sure it's just that I'm missing something; I've read extensively and can't find anything which tells me how to solve it (or if I have found it, I haven't been able to recognise it when I've done so).

Could anyone help at all, please? Either with how best to specify the grammar to achieve it, or how best to override the linking mechanism to force the right result.

Many thanks!




Re: Cross-references for multi-word identifiers [message #708265 is a reply to message #708181] Tue, 02 August 2011 16:08 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Don't use the default 'name' attribute and write your own lookup.
IIRC there are several places where it is assumed that a "name" is an ID
when relying on the "built in" lookup.

- henrik

On 8/2/11 4:46 PM, SWAT wrote:
> Hi,
>
> One of the things I really need to be able to do with Xtext is to
> generate EMF entities whose names contain multiple (space-separated)
> words (the vocabulary for which is not known at design-time). That is, I
> want my input file to contain:
>
> object what a nice EMF object;
>
> Oh what a nice EMF object this is.
>
> Have you seen what a nice EMF object it is?
>
> and handle linking the occurrences of 'what a nice EMF object' together.
>
> It seems maddeningly close to being possible, and I'm sure the reason it
> seems like it isn't is my own ignorance of the right way to do it.
>
> What I have so far looks like this:
>
> ObjectDeclaration : 'object' (name=ObjectName) ';';
>
> OhThisIs : 'Oh' (argument=[ObjectDeclaration|STRING]) 'this is.';
>
> HaveYouSeen: 'Have you seen' (argument=[ObjectDeclaration|STRING]) 'it
> is?';
>
> ObjectDeclaration: ID (' ' ID)*;
>
> I've tried various ways of specifying the cross-reference, but none of
> them work, usually giving "mismatched input '', expecting RULE_ID", and
> variants thereon.
>
> As I say, I'm sure it's just that I'm missing something; I've read
> extensively and can't find anything which tells me how to solve it (or
> if I have found it, I haven't been able to recognise it when I've done so).
>
> Could anyone help at all, please? Either with how best to specify the
> grammar to achieve it, or how best to override the linking mechanism to
> force the right result.
>
> Many thanks!
>
>
>
>
>
Re: Cross-references for multi-word identifiers [message #708276 is a reply to message #708181] Tue, 02 August 2011 16:22 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

it is not trivial to make white spaces have a meaning (and get things working properly).
First, it is not recommended to have white spaces within keywords (as in 'Have you seen', normally one would write 'Have' 'you' 'seen').
Second, your ObjectName rule (which you called ObjectDeclaration) makes a white space a keyword (by having ' '). Thus, you have two overlapping rules that will cause problems (in particular as WS is hidden by default whereas keywords are not, you may have to overwrite the WS rule, taking out the space, introduce a new SPACE terminal that you hide or not as required).

I don't quite see how your grammar should match the given input file. You have argument=[ObjectDeclaration|STRING] but in you model snippet you "use" the ObjectName rule rather than a string, so I'd say use argument=[ObjectDeclaration|ObjectName].

Further, getting this working in the reduced example may not be too hard. There are only few keywords, basically a trivial grammar etc. Transferring this to a full blown grammar might turn out quite complicated.

A hint for "debugging". Check the "index" (Shift-Ctrl-F3) or set a break point in the name provider in order to ensure that the name of the object is as expected. You *can* use the string rule in the cross reference (I think), only then you have to write
Oh "what a nice EMF object" this is.
The value converter will remove the quotes, so the name will match.

Alex
Re: Cross-references for multi-word identifiers [message #708278 is a reply to message #708265] Tue, 02 August 2011 16:25 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
@Henrik: Do you know for sure that the name attribute is expected to be filled via the ID-rule. I always thought that it suffices that the rule used returns a string value, such as ID, STRING or the often used Fqn datatype rule.

Alex
Re: Cross-references for multi-word identifiers [message #708570 is a reply to message #708278] Wed, 03 August 2011 00:07 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 8/2/11 6:25 PM, Alexander Nittka wrote:
> @Henrik: Do you know for sure that the name attribute is expected to be
> filled via the ID-rule. I always thought that it suffices that the rule
> used returns a string value, such as ID, STRING or the often used Fqn
> datatype rule.
>
Not sure - I recall I had some issues (but it was with Xtext 1.x) when
trying to use a "name" feature that was neither ID, STRING, or FQN. In
fact, having a feature called "name" and not used for cross referencing
also gave me problems at that point. Maybe it depends on how the project
is configured...

Since then, I avoided using "name" for anything but the standard
linking. It works well when sticking to the conventions though.

Regards
- henrik
Re: Cross-references for multi-word identifiers [message #709040 is a reply to message #708570] Wed, 03 August 2011 12:41 Go to previous messageGo to next message
SWAT  is currently offline SWAT Friend
Messages: 4
Registered: August 2011
Junior Member
Hi,

Thanks for your replies, they've been extremely helpful!

I missed the fact that I'd written ObjectDeclaration instead of ObjectName, that can't have helped clarity, sorry.

So, taking on board what you've said, the modified version is:


ObjectDeclaration : 'object' (name=ObjectName) ';';

OhThisIs : 'Oh' (argument=[ObjectDeclaration|ObjectName]) 'this' 'is.';

HaveYouSeen: 'Have' 'you' 'seen' (argument=[ObjectDeclaration|ObjectName]) 'it' 'is?';

ObjectName hidden(WS): ID ( ID)*;

which seems to work fine. (I'm not sure I understand why the "hidden(WS)" is necessary but I seem to get errors without it, so in it goes.)

I think the stupid thing which I was missing was that the ' ' in the ObjectName rule was completely unnecessary (in fact, harmful).

I don't know whether it's to do with the use of "name" or not, but unless I use [ObjectDeclaration|ObjectName] for the cross-reference, it doesn't work.

I *think* that's it working as I wanted now, unless you can spot any hidden pitfalls I might have missed?
Thanks again for your help!
Re: Cross-references for multi-word identifiers [message #709118 is a reply to message #709040] Wed, 03 August 2011 14:11 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You probably want to separate the '.' and '?' into separate tokens too

OhThisIs : 'Oh' (argument=[ObjectDeclaration|ObjectName]) 'this' 'is' '.' ;

You probably want to set hidden(WS) on the root rule to make spaces
hidden from the grammar everywhere. If you want spaces to be included in
the ObjectName, you want to do something like:

ObjectName hidden(): ID (WS ID)*;

The way you have it now, if user inputs "the quick brown fox" your
ObjectName will contain "thequickbrownfox".

- henrik
On 8/3/11 2:41 PM, SWAT wrote:
> Hi,
>
> Thanks for your replies, they've been extremely helpful!
> I missed the fact that I'd written ObjectDeclaration instead of
> ObjectName, that can't have helped clarity, sorry.
>
> So, taking on board what you've said, the modified version is:
>
>
> ObjectDeclaration : 'object' (name=ObjectName) ';';
>
> OhThisIs : 'Oh' (argument=[ObjectDeclaration|ObjectName]) 'this' 'is.';
>
> HaveYouSeen: 'Have' 'you' 'seen'
> (argument=[ObjectDeclaration|ObjectName]) 'it' 'is?';
>
> ObjectName hidden(WS): ID ( ID)*;
>
> which seems to work fine. (I'm not sure I understand why the
> "hidden(WS)" is necessary but I seem to get errors without it, so in it
> goes.)
>
> I think the stupid thing which I was missing was that the ' ' in the
> ObjectName rule was completely unnecessary (in fact, harmful).
>
> I don't know whether it's to do with the use of "name" or not, but
> unless I use [ObjectDeclaration|ObjectName] for the cross-reference, it
> doesn't work.
>
> I *think* that's it working as I wanted now, unless you can spot any
> hidden pitfalls I might have missed? Thanks again for your help!
>
Previous Topic:(no subject)
Next Topic:Syntactic sugar
Goto Forum:
  


Current Time: Fri Apr 19 22:43:25 GMT 2024

Powered by FUDForum. Page generated in 0.03195 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top