Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtext] Format Converter for Enums
[Xtext] Format Converter for Enums [message #1710570] Wed, 07 October 2015 15:10 Go to next message
Hauke Fuhrmann is currently offline Hauke FuhrmannFriend
Messages: 30
Registered: January 2015
Member
Hi there,

I have a simple legacy language for key/value Pairs, where all values
are written in double quotes, independent of their type.

E.g.

Hallo="Hallo"
Number="123"
Direction="Left"

For an integer I wrote a simple value converter that strips the quotes
or adds them when serializing.

What would be the approach for enums?
My first approach is to define a Literal with double quotes, which is
quite ugly for further processing of EObjects but works for
serialization and parsing.

enum Direction:
Left='"Left"' | Right='"Right"'
;

How to do it right? Can it be done by a value converter? How would it
look like? Can it be a generic converter for all kinds of enums?

Cheers,
Hauke
Re: [Xtext] Format Converter for Enums [message #1710658 is a reply to message #1710570] Thu, 08 October 2015 08:58 Go to previous messageGo to next message
Hauke Fuhrmann is currently offline Hauke FuhrmannFriend
Messages: 30
Registered: January 2015
Member
Am 07.10.2015 17:10, schrieb Hauke Fuhrmann:
> I have a simple legacy language for key/value Pairs, where all values
> are written in double quotes, independent of their type.
>
> E.g.
>
> Hallo="Hallo"
> Number="123"
> Direction="Left"
>
> For an integer I wrote a simple value converter that strips the quotes
> or adds them when serializing.
>
> What would be the approach for enums?
> My first approach is to define a Literal with double quotes, which is
> quite ugly for further processing of EObjects but works for
> serialization and parsing.
>
> enum Direction:
> Left='"Left"' | Right='"Right"'
> ;
>
> How to do it right? Can it be done by a value converter? How would it
> look like? Can it be a generic converter for all kinds of enums?

I also tried the straight-forward approach to define the quotes in the
grammar, but it does not work...

Entry:
'{' number=STRINGINT ',' '\"' direction=Direction '\"' '}'
;

No matter whether I use the escaping '\' character or not, I always get
parsing errors like

{ "1" , "Left" } gives: mismatched input '"Left"' expecting '"'

So I assumed "Left" might be a problem due to the quotes without any
whitespace, so the parser has a problem. However:

{ "1" , " Left " } gives: mismatched input '" Left "' expecting '"'

Even with whitespace this cannot be parsed. Why not?

Cheers,
Hauke
Re: [Xtext] Format Converter for Enums [message #1710740 is a reply to message #1710658] Thu, 08 October 2015 19:22 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
hi is the context of the enums restricted or is it mixed with other stuff.

if at the place where the enum can occur only the enum can occur you could use a datatype rule + a value converter


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:User friendly grammar editing
Next Topic:Validator inheritance doesn't seem to work - fails to add inherited EPackage to getEPackages
Goto Forum:
  


Current Time: Fri Apr 26 20:05:06 GMT 2024

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

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

Back to the top