Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Handling characters that return ecore::EChar and identifiers
Handling characters that return ecore::EChar and identifiers [message #1790263] Thu, 07 June 2018 16:26 Go to next message
Rohit Verma is currently offline Rohit VermaFriend
Messages: 21
Registered: August 2017
Junior Member
Hi,

(Please see attached ecore and Grammar files)

I am writing a grammar for expressions where I need to have support for Characters and Identifiers. A Character definition should return an ecore::EChar and Identifier definition should return ecore::EString.
The issue is that both Character and an Identifier could be a single alphabet, Eg: < x >

I know that a character is supposed to look like < 'x' > and an identifier will be just < x >, so they have different concrete syntax. However, I cannot have a rule for a character like " E_CHAR returns ecore::EChar : '\\'' SINGLE_CHAR '\\'' " because since it returns an ecore::EChar, the value assigned to this E_CHAR is the first character of <'x'> which is a single quote <'>. For this reason, I created a rule like {LiteralChar} "'" value = SINGLE_CHAR "'" which works fine for characters but now XText understands that characters are supposed to look like < x > and hence I can't now use identifiers like < x >.

How can I write a rule that handles this issue and lets me use both < 'x' > and < x > for Characters and Identifiers respectively?

So far I am thinking about removing the Character definition and only having identifiers, then in a custom program look for strings that are like <' SINGLE_CHAR '> and then create a new Char object from there.

Thanks.
Re: Handling characters that return ecore::EChar and identifiers [message #1790270 is a reply to message #1790263] Thu, 07 June 2018 17:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12233
Registered: July 2009
Senior Member
Quote:
because since it returns an ecore::EChar, the value assigned to this E_CHAR is the first character of <'x'> which is a single quote <'>. F


I don't understand your point with regard to this.
You can implement a custom value converter to address such things


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Handling characters that return ecore::EChar and identifiers [message #1790286 is a reply to message #1790270] Thu, 07 June 2018 20:11 Go to previous messageGo to next message
Rohit Verma is currently offline Rohit VermaFriend
Messages: 21
Registered: August 2017
Junior Member
Hi Christian,

In my MetaModel, I have a class "LiteralChar" with an attribute "value" which is of type EChar. When the parser encounters a token 'c' , I want it to instantiate a new object of type LiteralChar and assign it's "value" attribute the value c . If I wrote the following grammar, it would do is instantiate a new object of type LiteralChar and assign it's "value" attribute the value ' (the first single quote encountered) instead of c .

Atomic returns ValueSpecification:
{LiteralChar} value = E_CHAR

terminal E_CHAR returns ecore::EChar : ("'") ('a'..'z'|'A'..'Z') ("'") ;

====================================================

What do you mean by a custom value converter? That's something I probably don't know about. Could you guide me to a link for that?

Thanks.
Re: Handling characters that return ecore::EChar and identifiers [message #1790294 is a reply to message #1790286] Fri, 08 June 2018 05:04 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12233
Registered: July 2009
Senior Member
A value converter is the component that Xtext uses to translate strings to values and vice versa
Default implementation is inside defaultterminalconverters
It is used to register ivalueconverter for terminal and datatype rules

A forum or google search for Xtext and value converter should give you dozen of examples


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Scoping on an external EMF Model
Next Topic:Controlling generator behavior with parameters
Goto Forum:
  


Current Time: Fri Sep 21 11:26:56 GMT 2018

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

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

Back to the top