Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Keyword/name=ID conflicts
Keyword/name=ID conflicts [message #639647] Wed, 17 November 2010 10:50 Go to next message
Chris Ainsley is currently offline Chris AinsleyFriend
Messages: 77
Registered: March 2010
Location: UK
Member
I was just wondering if there is a way to specify an the value of an ID to the name of an existing keyword without using the '^' (or any other arbitrary prefix/suffix)?

A snippet follows:

RECORD:
'record ' name=ID '{' (fields+=Field)+ '}';


I want to be able to have a record ID of 'record' without having to specify a prefix of '^'. The reason is that the context makes it clear that I'm not trying to declare a sub-record and the grammar doesn't permit that at this point anyway.

I assume there must be very good technical reasons for the '^' value converter but I really dislike having to document an unergonomic convention ('^') unless I am sure that there is no alternative.

Note, I don't want to make fields into STRING types either if at all possible.

[Updated on: Wed, 17 November 2010 10:51]

Report message to a moderator

Re: Keyword/name=ID conflicts [message #639655 is a reply to message #639647] Wed, 17 November 2010 11:01 Go to previous messageGo to next message
Sylvain EVEILLARD is currently offline Sylvain EVEILLARDFriend
Messages: 555
Registered: July 2009
Senior Member
You can use a derived ID rule :

KEYWORDID :
ID | 'record'
;

RECORD:
'record ' name=KEYWORDID '{' (fields+=Field)+ '}';
Re: Keyword/name=ID conflicts [message #639659 is a reply to message #639655] Wed, 17 November 2010 11:11 Go to previous messageGo to next message
Chris Ainsley is currently offline Chris AinsleyFriend
Messages: 77
Registered: March 2010
Location: UK
Member
Sylvain EVEILLARD wrote on Wed, 17 November 2010 20:01
You can use a derived ID rule :

KEYWORDID :
ID | 'record'
;

RECORD:
'record ' name=KEYWORDID '{' (fields+=Field)+ '}';




Thanks for the fast and useful reply...

So, I understand this allows the parser to accept the 'record' as an id but the editor still highlights record as a keyword in the context of it being an ID (the 2nd 'record' in 'record record {...} '.

Can I solve this using a custom formatting rule or are keywords literally ALWAYS highlighted even in the context of an expected ID?
Re: Keyword/name=ID conflicts [message #639698 is a reply to message #639659] Wed, 17 November 2010 14:16 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

keywords are highlighted as such wherever they appear in the file (they are also tokenised as keywords, which you made working by introducing the datatype rule).
The lexical highlighting does not look for the context but only for the token type. You will have to adapt the semantic highlighting in order to "de-highlight" your record id.
See the documentation and also an old (outdated) blog post which might nevertheless serve as a source of inspiration.
http://blogs.itemis.de/stundzig/archives/467

Alex
Re: Keyword/name=ID conflicts [message #639873 is a reply to message #639647] Thu, 18 November 2010 08:46 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 759
Registered: July 2009
Location: Hamburg
Senior Member
You can define a datatype rule that enumerates all keywords or ID

MyString:
'record' | 'whatsoeverkeyword' ... | ID;

I'd recommend to also do semantic syntax highlighting to render keywords
which are strings as strings.


Am 17.11.10 11:50, schrieb Chris Ainsley:
> I was just wondering if there is a way to specify an the value of an ID
> to the name of an existing keyword without using the '^' (or any other
> arbitrary prefix/suffix)?
>
> A snippet follows:
>
>
> RECORD:
> 'record ' name=ID '{' (field+=Field)+ '}'
>
>
> I want to be able to have a record ID of 'record' without having to
> specify a prefix of '^'. The reason is that the context makes it clear
> that I'm not trying to declare a sub-record and the grammar doesn't
> permit that at this point anyway.
>
> I assume there must be very good technical reasons for the '^' value
> converter but I really dislike having to document an unergonomic
> convention ('^') unless I am sure that there is no alternative.
>
> Note, I don't want to make fields into STRING types either if at all
> possible.


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: Keyword/name=ID conflicts [message #1790776 is a reply to message #639873] Sat, 16 June 2018 11:04 Go to previous messageGo to next message
Alexandros Karypidis is currently offline Alexandros KarypidisFriend
Messages: 97
Registered: March 2010
Member
I came across this post while looking to achieve the same. In my case however, I am happy to use the circum (^) character as an escape which I saw mentioned above. Indeed ID is defined to have an optional circum at the start:

terminal ID: '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;


What I am not clear on though, is whether there is something which will remove that circum by default, or if one is supposed to do it manually. In my case when I tried it out it, the circum seemed to remain. Al my rules use "name=Name" to indirectly point to ID via this Name rule:

Name:  ID;


So in order to achieve that, I manually added a value converter as follows:

    @ValueConverter(rule="Name")
    def IValueConverter<String> IDValueConverter() {
        return new AbstractToStringConverter<String>() {
            override protected internalToValue(String value, INode node) throws ValueConverterException {
                if (value.startsWith("^")) value.substring(1)
                else value
            }
        };
    }


This way when a keyword is escaped using ^, the escape is dropped. Is this how escaping is supposed to work, or have I done something wrong? I am just not sure if there is already something off-the-shelf for doing this, which I have somehow "broken" forcing me to manually add a value converter...
Re: Keyword/name=ID conflicts [message #1790778 is a reply to message #1790776] Sat, 16 June 2018 11:15 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12237
Registered: July 2009
Senior Member
it will work automatically only if you directly use ID for the attribute e.g. name=ID.
if you use own datatype rules you need own valueconverters


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Compilation error inside the grammar access class when upgrading to Xtext 2.14
Next Topic:OneOf
Goto Forum:
  


Current Time: Fri Sep 21 23:15:28 GMT 2018

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

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

Back to the top