On-the-fly declaration in cross-reference [message #1840806] |
Sun, 25 April 2021 10:33 |
Bertalan Zoltán Péter Messages: 4 Registered: April 2021 |
Junior Member |
|
|
I am trying to create a grammar for an existing metamodel. Here is a relevant part of the grammar:
Ingredient: amount=INT name=EString ;
Container: 'container' name=EString ;
ContainmentRelation: ingredient=[Ingredient] '->' container=[Container] ;
I would want to be able to declare Containers and then write ContainmentRelations which describe Ingredients being contained inside the Containers. But I do not want to have to predeclare my Ingredients and then write the ContainmentRelations, I would like to be able to simply write a ContainmentRelation where on the left side of the arrow (->) I could declare a new Ingredient.
Here is an example; instead of this:
4 'carrot' // this is an ingredient
2 'potato' // this is another ingredient
container 'pot' // this is a container
// now the containment relation:
carrot -> pot
potato -> pot
I would want to be able to do this:
container 'pot'
4 'carrot' -> pot
2 'potato' -> pot
----
I naturally tried to change the ingredient=[Ingredient] cross reference to simply Ingredient, but understandably I get Cannot add supertype 'ContainmentRelation' to sealed type 'Ingredient'.
Do I need to change my metamodel? I suppose it would work if I added another relation between Ingredient and ContainmentRelation that would be a composition but this does not really make sense since the Ingredient should not be 'owned' by the ContainmentRelation but by some larger wrapping object to all thes called Recipe (not present in my examples for simplicity).
[Updated on: Sun, 25 April 2021 15:53] Report message to a moderator
|
|
|
|
|
|
|
|
Re: On-the-fly declaration in cross-reference [message #1840828 is a reply to message #1840817] |
Mon, 26 April 2021 09:05 |
Bertalan Zoltán Péter Messages: 4 Registered: April 2021 |
Junior Member |
|
|
Of course, that's what I would want. But how do I write in Xtext that an Ingredient can be declared inside a ContainmentRelation (and stored with the other Ingredients of the Recipe)? I cannot do
ContainmentRelation: Ingredient '->' container=[Container] ;
because in my metamodel, ContainmentRelation only has a reference (that is not a containment reference, marked with <> on my ASCII diagram) to an Ingredient. Is this where the post processor shown in the XtextCast you sent comes in?
[Updated on: Mon, 26 April 2021 09:06] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.04672 seconds