Skip to main content



      Home
Home » Modeling » TMF (Xtext) » C-like multiple declaration
C-like multiple declaration [message #1793679] Tue, 14 August 2018 00:29 Go to next message
Eclipse UserFriend
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 00:34 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: C-like multiple declaration [message #1793738 is a reply to message #1793680] Tue, 14 August 2018 19:47 Go to previous messageGo to next message
Eclipse UserFriend
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 01:25] by Moderator

Re: C-like multiple declaration [message #1793747 is a reply to message #1793738] Wed, 15 August 2018 01:16 Go to previous messageGo to next message
Eclipse UserFriend
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?

[Updated on: Wed, 15 August 2018 01:19] by Moderator

Re: C-like multiple declaration [message #1793748 is a reply to message #1793747] Wed, 15 August 2018 02:58 Go to previous messageGo to next message
Eclipse UserFriend
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 03:03 Go to previous messageGo to next message
Eclipse UserFriend
which bindings did you do?
Re: C-like multiple declaration [message #1793753 is a reply to message #1793750] Wed, 15 August 2018 04:36 Go to previous messageGo to next message
Eclipse UserFriend
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 05:01 Go to previous messageGo to next message
Eclipse UserFriend
i miss proper return types on the defs e.g. for bindIResourceDescriptionManager and bindIDerivedStateComputer

Re: C-like multiple declaration [message #1793799 is a reply to message #1793756] Wed, 15 August 2018 22:27 Go to previous message
Eclipse UserFriend
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 Jun 20 07:33:35 EDT 2025

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

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

Back to the top