Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Sharing rules' bodies
Sharing rules' bodies [message #1717217] Thu, 10 December 2015 19:37 Go to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 397
Registered: March 2011
Senior Member
I have a language where I want expressions like this:

thing { part x;  part y; }

stuff Name { part x;  part y; }


where 'thing' and 'stuff' are separate constructs whose body shares the same syntax.

The language is created from an existing Ecore meta-model where "Thing" has a "stuff" property of type "Stuff". "Stuff" has a list of "parts".

I could write the following grammar:

ThingContents return Stuff:
  '{' (parts+=Part)* '}'

Part:
  'part' name=ID ';';

Thing:
  'thing' stuff=ThingContents;

Stuff:
  'stuff' name=ID '{' (parts+=Part)* '}';


But this grammar duplicates the contents of "thing" and "stuff".

I would like to be able to avoid duplication in my grammar. Can this be accomplished? If so how?

I tried the following:

Contents return Stuff:
  '{' (parts+=Part)* '}'

Part:
  'part' name=ID ';';

Thing:
  'thing' stuff=Contents;

Stuff:
  'stuff' name=ID Contents;


but it fails on the "Stuff" rule with "An unassigned rule call is not allowed, when the 'current' was already created."

Is there any way to share the body of a rule among different rules?
Re: Sharing rules' bodies [message #1717222 is a reply to message #1717217] Thu, 10 December 2015 19:59 Go to previous messageGo to next message
Stefan Oehme is currently offline Stefan OehmeFriend
Messages: 159
Registered: April 2010
Location: Kiel
Senior Member

Hi Ernesto,

in Xtext 2.9 you can define a parser rule fragment

fragment Contents :
  '{' (parts+=Part)* '}'
;


And then use it like this:

Thing:
  'thing' stuff=ThingContents;

ThingContents:
  Contents
;

Stuff:
  'stuff' name=ID Contents;


Ideally, I would get rid of the "ThingContents" indirection in the ecore model, but maybe it's needed for reasons I don't see =)

Like this:


Thing:
  'thing' Contents

Stuff:
  'stuff' name=ID Contents;

Cheers,
Stefan

[Updated on: Thu, 10 December 2015 20:01]

Report message to a moderator

Re: Sharing rules' bodies [message #1717226 is a reply to message #1717222] Thu, 10 December 2015 20:37 Go to previous messageGo to next message
Ernesto Posse is currently offline Ernesto PosseFriend
Messages: 397
Registered: March 2011
Senior Member
Great! But that's not available in 2.8.4, is it?
Re: Sharing rules' bodies [message #1717250 is a reply to message #1717226] Fri, 11 December 2015 05:27 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13930
Registered: July 2009
Senior Member
No this is a xtext 2.9 feature

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Defining Cardinality Elegantly
Next Topic:Fatal problem with TreeAppendable
Goto Forum:
  


Current Time: Sat May 08 14:20:23 GMT 2021

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

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

Back to the top