Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Solved] Referencing to two separate types
[Solved] Referencing to two separate types [message #1780268] Fri, 19 January 2018 12:56 Go to next message
Ray Kerstens is currently offline Ray KerstensFriend
Messages: 2
Registered: January 2018
Junior Member
Hi all,

I'm working on my first DSL and I'm running into a problem. The goal is that ultimately one is able to define the following:
Supermarket
    HealthyProductTypes
        HealtyProductType Fruit
        HealthyProductType Vegetable
    UnHealthyProductTypes
        UnHealtyProductType Soda
        UnHealthyProductType Snack
    Products
        Product Coke Type Soda
        Product Apple Type Fruit
        Product Crisps Type Snack


The grammar I have for now is:
Model:
    supermarkets += Supermarket
;
Supermarket:
    "Supermarket"
    "HealthyProductTypes" healthyproducttypes += HealthyProductType+
    "UnHealthyProductTypes" uhealthyproducttypes += UnHealthyProductType+
    "Products" products += Product+
;
 HealthyProductType:
    "HealthyProductType" name=ID
;
 UnHealthyProductType:
    "UnHealthyProductType" name=ID
;
Product:
    "Product" name=ID
    "Type" 
    type=[HealthyProductType] | [UnHealthyProductType]
; 


That final part (type=[HealthyProductType] | [UnHealthyProductType]) yields the error "A rule's name has to be unique". I also tried doing it in the following manner
Product:
    "Product" name=ID
    "Type" 
    type= HealthyReference | UnhealthyReference
; 
HealthyReference :
    reference = [HealthyProductType]
;
UnhealthyReference:
    reference = [UnHealthyProductType]


But that yields an error when generating "Decision can match input such as "RULE_ID" using multiple alternatives: 1, 2"

What would be the way to get the functionality I want?


Edit:
Christian DietrichFriend Posted the functionality I wanted. For future reference, this is the resulting grammar:

Model:
    supermarkets += Supermarket
;
Supermarket:
    "Supermarket"
    "HealthyProductTypes" healthyproducttypes += HealthyProductType+
    "UnHealthyProductTypes" uhealthyproducttypes += UnHealthyProductType+
    "Products" products += Product+
;
 HealthyProductType:
    "HealthyProductType" name=ID
;
 UnHealthyProductType:
    "UnHealthyProductType" name=ID
;
Product:
    "Product" name=ID
    "Type" 
    type= [ProductType] 
; 
ProductType: 
    HealthyProductType | UnHealthyProductType
;


Kind regards,
Ray

[Updated on: Mon, 22 January 2018 08:28]

Report message to a moderator

Re: Referencing to two separate types [message #1780304 is a reply to message #1780268] Fri, 19 January 2018 17:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11726
Registered: July 2009
Senior Member
this is not possible you need to introduce a common supertype like

ProductType: HealthyProductType | UnHealthyProductType;

and then ref=[ProductType]


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Referencing to two separate types [message #1780305 is a reply to message #1780304] Fri, 19 January 2018 17:21 Go to previous messageGo to next message
Axel Guckelsberger is currently offline Axel GuckelsbergerFriend
Messages: 336
Registered: July 2009
Senior Member
Maybe also consider a more concise syntax like:

    Products
        Soda Coke
        Fruit Apple
        Snack Crisps


or

    Products: Coke (Soda), Apple (Fruit), Crisps (Snack)

[Updated on: Fri, 19 January 2018 17:23]

Report message to a moderator

Re: Referencing to two separate types [message #1780402 is a reply to message #1780304] Mon, 22 January 2018 08:32 Go to previous message
Ray Kerstens is currently offline Ray KerstensFriend
Messages: 2
Registered: January 2018
Junior Member
Christian Dietrich wrote on Fri, 19 January 2018 17:17
this is not possible you need to introduce a common supertype like

ProductType: HealthyProductType | UnHealthyProductType;

and then ref=[ProductType]

Thanks, this indeed does the trick. I edited my main post to include the solution and marked the topic as solved.

Axel Guckelsberger wrote on Fri, 19 January 2018 17:21
Maybe also consider a more concise syntax like:

    Products
        Soda Coke
        Fruit Apple
        Snack Crisps


or

    Products: Coke (Soda), Apple (Fruit), Crisps (Snack)

Noted, I will surely edit that in a later version. Thank you for your input.
Previous Topic:LSP: resolving types
Next Topic:Incorrect "Type mismatch" error due to duplicate type hierarchy in editor
Goto Forum:
  


Current Time: Mon Feb 19 08:43:21 GMT 2018

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

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