Skip to main content



      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 12:26 Go to next message
Eclipse UserFriend
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.
  • Attachment: ExpressionsDsl.xtext
    (Size: 2.30KB, Downloaded 101 times)
  • Attachment: RAM.aird
    (Size: 2.26MB, Downloaded 206 times)
  • Attachment: RAM.ecore
    (Size: 53.59KB, Downloaded 173 times)
  • Attachment: RAM.genmodel
    (Size: 27.95KB, Downloaded 233 times)
Re: Handling characters that return ecore::EChar and identifiers [message #1790270 is a reply to message #1790263] Thu, 07 June 2018 13:04 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Handling characters that return ecore::EChar and identifiers [message #1790286 is a reply to message #1790270] Thu, 07 June 2018 16:11 Go to previous messageGo to next message
Eclipse UserFriend
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 01:04 Go to previous message
Eclipse UserFriend
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
Previous Topic:Scoping on an external EMF Model
Next Topic:Controlling generator behavior with parameters
Goto Forum:
  


Current Time: Fri Jun 20 05:53:40 EDT 2025

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

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

Back to the top