Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 326298

Summary: Setting default values for features in the grammar
Product: [Modeling] TMF Reporter: Sébastien Gandon <sgandon>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: enhancement    
Priority: P5 CC: bojidar.dimitrov, foremans, moritz.eysholdt, robert, sven.efftinge
Version: 0.7.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Sébastien Gandon CLA 2010-09-27 08:55:57 EDT
Stating from an existing emf metamodel I need to create instances of classes in a textual manner using xtext.
But to make the grammar specific for a given usage some of the classes attributes needs to have default values that I would like to specify in the grammar.
Let say we have in our model a class called Fruit with 2 attributes name and color.
We want to define in the xtext grammar a syntax specific from apples 
say
-------xtext code
Apple returns Fruit:
'apple' color=STRING;
-------
Here I obviously forgot to set the name of the Fruit instance to "apple", and the Fruit instance will be created with name to null and color to whatever the user specify.


Would it be possible to enhance the xtext grammar definition to define default values to attributes right in the grammar definition.
something such as
-------xtext code
Apple returns Fruit(name='apple'):
'apple' color=STRING;
-------

One way around this issue seems to be using the DefaultEcoreElementFactory as mentioned in the forum (http://www.eclipse.org/forums/index.php?t=msg&goto=629133&S=673ad8a446fe60274a6f7df081928177) but that would really be nice to have this feature in the grammar because it is really dependent on the grammar definition.
Comment 1 Sven Efftinge CLA 2010-09-27 09:07:37 EDT
*** Bug 316862 has been marked as a duplicate of this bug. ***
Comment 2 Moritz Eysholdt CLA 2011-01-11 08:30:49 EST
I'd say default values for EAttributes are not something Xtext's grammar should handle. Ecore provides many features of which only some can be inferred from the grammar. This is intentional, since otherwise the grammar would become more complex than Ecore itself.

To set default values, you can 

a) Import the Ecore model (instead of generating it) in the grammar. Thereby, you can edit the Ecore file directly.

b) Use MWE's EcoreGenerator. It allows to use the generation gap pattern... so you can override the Java Methodes generated by EMF. With this approach even dynamic defaults become possible. :) (aka if feature a is not set, use feature b's value as default).

c) Use the processor that allows to modify the Ecore model that is inferred from the grammar.

proposing to close this one as "won't fix".
Comment 3 Sven Efftinge CLA 2011-01-11 08:53:15 EST
The request is not to declare default values for EAttributes but to set values based on decisions the parser made during parsing.
Comment 4 Steve Foreman CLA 2013-07-29 18:24:22 EDT
This is 2 years and old and probably long forgotten about by now.

Any update on this? Is there a way to specify the what the default value should be for missing attributes within a rule?

Here is the essence of what I'm trying to do:

Main:
  A | B;

A:
  'value' type=EnumType name=Name '=' value=INT;

B returns A(property=EnumType.special):
  'special' name=Name '=' value=INT;

EnumType:
  normal | extended | special | ...
Comment 5 Sven Efftinge CLA 2013-07-30 02:45:32 EDT
Thanks for the reminder. 
This requirement comes up rarely and can be solved by post-processing the parsed model, which is why I close this bug. 
Sorry for having it laying around for such a long time.