Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problem adding custom feature & operations with implementations to Xtext generated ECore model(Is there a way not modifying generated code every time?)
icon12.gif  Problem adding custom feature & operations with implementations to Xtext generated ECore model [message #1609854] Tue, 10 February 2015 11:30 Go to next message
Evermind Mising name is currently offline Evermind Mising nameFriend
Messages: 7
Registered: July 2009
Junior Member
Hi everybody,
I am new to Xtext and EMF. I have a simple Xtext syntax which is simplified from real case:

Expression:
PrimaryExpression | GroupExpression;

PrimaryExpression:
variable=ID '= ' value=ID ';' ;

GroupExpression:
'(' exprs+=Expression+ ')';

So the syntax accepts code like:
((((x=y;)e=f;g=h;(a=b;)c=d;)))

What I want to do is:
1. adding a feature 'index' to generated 'GroupExpression' Java interface, so that I can traverse AST later and assign a sequence number to each GroupExpression instance;
I managed to achieve this by adding a "phantom" syntax fragment into GroupExpression, just like:

GroupIndex: '00000000000000000'; // Recognize an identifier that pratically cannot be matched accidently

GroupExpression:
'(' index=GroupIndex? exprs+=Expression+ ')'; // index can never be matched, but Xtext will generate a feature for it, so I can use it to store calculated index value.

This can get THIS thing done, however I think it is limited and not elegant. Wonder if there is a better way.

2.I want to add an operation "void evaluate(String expr)" to generated interface Expression, and provide a default implementation that can be baked into generated code. I didn't find a way to achieve this though. I found some article saying "Generation gap pattern" which is using EcoreGenerator and it will
automatically search for "ImplCustom" classes and generate factory that actually create custom impl class, however I tried and got incomplete generated code. I also read this issue 402669 [ht tps://bugs.eclipse.org/bugs/show_bug.cgi?id=402669] and found that EcoreGeneratorFragment used in generated workflow doesn't support ImplCustom classes at all. So currently I have no way to deal with this problem.

So could someone please tell me is there any official or elegant way to achieve this? Thanks!

[Updated on: Tue, 10 February 2015 11:32]

Report message to a moderator

Re: Problem adding custom feature & operations with implementations to Xtext generated ECore mod [message #1609872 is a reply to message #1609854] Tue, 10 February 2015 11:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

search for IXtext2EcorePostProcessor


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem adding custom feature & operations with implementations to Xtext generated ECore mod [message #1610001 is a reply to message #1609872] Tue, 10 February 2015 13:36 Go to previous messageGo to next message
Evermind Mising name is currently offline Evermind Mising nameFriend
Messages: 7
Registered: July 2009
Junior Member
Hi Christian!
Thank you for pointing out the right direction and now I have achieved above 2 goals utilizing IXtext2EcorePostProcessor which is a great step for me! Now I wonder, since I can modify metamodels generated by Xtext, is it also possible to modify generated code of MyDSLFactoryImpl so that it can return my customized model implementation class? If so I don't have to write method bodies in java and in string, and can use my favorite Xtend to write implementations? I looked at the API and searched and didn't find a clue.
Re: Problem adding custom feature & operations with implementations to Xtext generated ECore mod [message #1610002 is a reply to message #1610001] Tue, 10 February 2015 13:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no thats not possible. but it is not neccessary anyway.

but maybe you should consider to use a manually maintained metamodel anyway


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problem adding custom feature & operations with implementations to Xtext generated ECore mod [message #1611516 is a reply to message #1610002] Wed, 11 February 2015 12:17 Go to previous message
Evermind Mising name is currently offline Evermind Mising nameFriend
Messages: 7
Registered: July 2009
Junior Member
Hi! I managed to add a visitBy(MyDslSwitch switch_) method to Expression interface, so that I can visit the AST with any custom switch implementation, which is sufficient for now. Manually maintained metamodel way is a little heavy for me since I know little about EMF. I'm going to play with Xtext for a while first. Thanks anyway!
Previous Topic:Problem Importing a DSL into another
Next Topic:Xtext model
Goto Forum:
  


Current Time: Tue Apr 23 07:14:58 GMT 2024

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

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

Back to the top