Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Keyword/name=ID conflicts
Keyword/name=ID conflicts [message #639647] Wed, 17 November 2010 05:50 Go to next message
Eclipse UserFriend
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 05:51] by Moderator

Re: Keyword/name=ID conflicts [message #639655 is a reply to message #639647] Wed, 17 November 2010 06:01 Go to previous messageGo to next message
Eclipse UserFriend
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 06:11 Go to previous messageGo to next message
Eclipse UserFriend
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 09:16 Go to previous messageGo to next message
Eclipse UserFriend
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 03:46 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Keyword/name=ID conflicts [message #1790776 is a reply to message #639873] Sat, 16 June 2018 07:04 Go to previous messageGo to next message
Eclipse UserFriend
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 07:15 Go to previous message
Eclipse UserFriend
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
Previous Topic:Compilation error inside the grammar access class when upgrading to Xtext 2.14
Next Topic:OneOf
Goto Forum:
  


Current Time: Sun Jun 15 15:31:10 EDT 2025

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

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

Back to the top