Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Working with INT optional features - default value feature
Working with INT optional features - default value feature [message #1790418] Sun, 10 June 2018 09:30 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Hello,

When I use an EInt to store an optional feature such as "length" below, the default value (when the feature is absent) is zero:

SimpleTypeDeclaration:
    'simpleType' name=Name ':' primitiveType=PrimitiveType ('[' length=INT ']')?
 ;


Since zero is actually a valid INT, it is impossible to determine (e.g. in a validator) whether the input is missing the modifier or it is just using the default. For instance:

// (A) this would store zero to length:
simpleType myTypeA : char

// (B) this would also store zero to length:
simpleType myTypeB : char[0]

// (C) this would store one to length:
simpleType myTypeC : char[1]

// (D) this would be illegal:
simpleType myTypeD : char[-1]


I would like to be able to differentiate between (A) and (B) in my code. A simple way to do that would be to default to "-1" for the length when it is absent. Is it possible to do that (even by defining my own terminal similar to INT):

terminal MY_INT returns ecore::EInt: ('0'..'9')+; // <== can I write something to default to -1 for the EInt here?


I have gotten around this by using an extra rule, but this just makes the code that uses it more convoluted:

SimpleTypeDeclaration:
    'simpleType' name=Name ':' primitiveType=PrimitiveType lengthModifiers=LengthModifiers?;
// now I can do a null check on lengthModifiers EAttribute to discriminate between (A) and (B)

LengthModifiers:
    '[' length=INT ']';
Re: Working with INT optional features - default value feature [message #1790421 is a reply to message #1790418] Sun, 10 June 2018 09:52 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

This question was asked a few weeks ago.

If you really want an optional integer use ecore::EIntegerObject.

But actually you are abusing your model.

You can use -1 to distinguish char from char[0], but how do you distinguish char[0][0]?

Your model should be type + possibly empty list of integers rather than type + optional integer and all the consequent problems.

Regards

Ed Willink
Re: Working with INT optional features - default value feature [message #1790423 is a reply to message #1790421] Sun, 10 June 2018 10:49 Go to previous message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Thanks, I've located the post you refer to and used EIntegerObject per your suggestion:

OptionalInt returns ecore::EIntegerObject: INT;


Not sure what you mean on your second point. I am not after multi-dimensional arrays, but if I needed them, would I not use something like:

Dimension: '[' length=OptionalInt ']';

SimpleTypeDeclaration: 'simpleType' name=Name ':' primitiveType=PrimitiveType (dimensions+=Dimension)?;

Previous Topic:The ?= operator
Next Topic: importURI with Xtext 2.14 and the new generator
Goto Forum:
  


Current Time: Thu Mar 28 14:10:05 GMT 2024

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

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

Back to the top