Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » C-like multiple declaration
C-like multiple declaration [message #1793679] Tue, 14 August 2018 04:29 Go to next message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
Hi,

I'm trying to allow multiple declarations of the same type, a bit like in C or Java. However, I can't find a clean way to do it directly in the grammar. At the moment I have the "usual" way (snippet):

Declaration returns Declaration:  
	'var' name=ID ':' type = [Type];


So, when declaring stuff, considering that I declared a type Integer, I write:
var anInteger : Integer
var another : Integer


The idea is to simply have
var anInteger, another : Integer


But I'd like to instantiate as many Declaration EObjects as I declare elements, i.e, I do not want to have a list of names for one Declaration (using the usual parameter-like pattern below), but triggering the creation of multiple Declaration from the grammar.

Declaration returns Declaration:  
	'var' name+=ID (',' name+=ID)* ':' type = [Type];


Is there a way to do so directly from the grammar ? Or, if not possible, where do I have the hand to transform my list of names into a list of EObjects?

Thanks,
Fabian
Re: C-like multiple declaration [message #1793680 is a reply to message #1793679] Tue, 14 August 2018 04:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Yes you can einher adapt scoping indexing generation etc to deal with the multiple names
Or you use a IDerivedStateComputer to do a inline model to model transformation
See http://xtextcasts.org/episodes/18-model-optimization


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: C-like multiple declaration [message #1793738 is a reply to message #1793680] Tue, 14 August 2018 23:47 Go to previous messageGo to next message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
As suggested in your post (method 2), I went for the inline M2M approach but despite the binding of my own IDerivedStateComputer seems to work (at least the bindIDerivedStateComputer get called in the RuntimeModule), the methods in there are not (debugged with println, got nuts). The editor/validator is complaining because there is no value in the type feature. The code below doesn't get called:
class IoTDSLDerivedState implements IDerivedStateComputer { 
  override discardDerivedState(DerivedStateAwareResource resource) {
    println("called discardDerivedState")
    resource.allContents.filter(typeof(DeclaredElement)).forEach [
      type = null
    ]
  }
  
  override installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
    println("called installDerivedState")
    resource.allContents.filter(typeof(DeclaredElement)).forEach [
      type = (eContainer as Declaration).type
    ]
  }
}


The grammar snippet:
Declaration returns Declaration:
	'var' instances += DeclaredElement (',' instances += DeclaredElement)* ':' type = [Type]
;
DeclaredElement returns DeclaredElement:
  name=ID
;


Also, I believe I do not need to add the structural features in my own postprocessor since I rely on an existing MM where the type relationship already exists for the DeclaredElement and I just want it to be populated at runtime.

Any idea? Thanks!

[Updated on: Wed, 15 August 2018 05:25]

Report message to a moderator

Re: C-like multiple declaration [message #1793747 is a reply to message #1793738] Wed, 15 August 2018 05:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
you can not do it metamodel agnostic.
if there is a name list in your metamodel you have to use that anyway correct?
but wont you rather have a Declaration list in the "right place" and the name list next to it?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 15 August 2018 05:19]

Report message to a moderator

Re: C-like multiple declaration [message #1793748 is a reply to message #1793747] Wed, 15 August 2018 06:58 Go to previous messageGo to next message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
Our messages cross themselves. In my previous answer I show what I did, but the methods in DerivedStateComputer seems to not being called. Any idea ?
Re: C-like multiple declaration [message #1793750 is a reply to message #1793748] Wed, 15 August 2018 07:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
which bindings did you do?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: C-like multiple declaration [message #1793753 is a reply to message #1793750] Wed, 15 August 2018 08:36 Go to previous messageGo to next message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
class IoTDSLRuntimeModule extends AbstractIoTDSLRuntimeModule {	
  def bindIDerivedStateComputer() {
    // here is my derived state computer
    return IoTDSLDerivedState
  }

  override Class<? extends XtextResource> bindXtextResource() {
    return DerivedStateAwareResource;
  }

  def bindIResourceDescriptionManager() {
    return DerivedStateAwareResourceDescriptionManager;
  }
}

But, I'm not using Xbase, do I have to re-implement/extend something in the last 2?
Re: C-like multiple declaration [message #1793756 is a reply to message #1793753] Wed, 15 August 2018 09:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
i miss proper return types on the defs e.g. for bindIResourceDescriptionManager and bindIDerivedStateComputer



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: C-like multiple declaration [message #1793799 is a reply to message #1793756] Thu, 16 August 2018 02:27 Go to previous message
Fabian G. is currently offline Fabian G.Friend
Messages: 60
Registered: May 2010
Location: Christchurch (NZ)
Member
Thanks working like a charm, now. For further reference, if needed:
  def Class<? extends IDerivedStateComputer> bindIDerivedStateComputer() {
    return IoTDSLDerivedState
  }

  override Class<? extends XtextResource> bindXtextResource() {
    return DerivedStateAwareResource;
  }

  def Class<? extends IResourceDescription.Manager> bindIResourceDescriptionManager() {
    return DerivedStateAwareResourceDescriptionManager;
  }


Also, even if I didn't need it at the end, I think the new generator to override is org.eclipse.xtext.xtext.generator.XtextGenerator (the post you mentioned is using a deprecated generator, but I'm still unsure since doc in the class discourages from extending it).

public class IoTDSLExtendedGenerator extends XtextGenerator {  
  public IoTDSLExtendedGenerator() {    
    new XtextStandaloneSetup() {
      @Override
      public Injector createInjector() {
        return Guice.createInjector(new XtextRuntimeModule() {
          @Override
          public void configureIXtext2EcorePostProcessor(Binder binder) {
            try {
              Class.forName("org.eclipse.xtend.expression.ExecutionContext");
              binder.bind(IXtext2EcorePostProcessor.class).to(IoTDSLPostProcessor.class);
            } catch (ClassNotFoundException e) {
              logger.error(e.getMessage(), e);
            }
          }
        });
      }
    }.createInjectorAndDoEMFRegistration();
  }
}
Previous Topic:Trigger validation manually
Next Topic:Acces a single element in a list of events
Goto Forum:
  


Current Time: Fri Apr 26 23:45:43 GMT 2024

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

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

Back to the top