Search code examples
xtextxpandmwe

How to configure Xtext mwe.Reader to fill the root element in a slot


I am using Xtext 2.0 with MWE 1 and XPand, but I guess the Problem for MWE 2 and XTend is exactly the same.

My Xtext grammer looks like this (excerpt):

grammer org.test.Test with org.eclipse.xtext.common.Terminals
generate test "http://www.test.org/test/Test

Model :
    "COMMON STUFF" 
    "{"
    (formatterDefs+=FormatterDef)*
    "}"
    ...

FormatterDef : "Formatter" name=ID  ":" formatter=STRING;

When I use this mwe definiton (excerpt):

<component class="org.eclipse.xtext.mwe.Reader" path="${project.src.directory}/xtext/model/" >      
    <register class="org.test.TestStandaloneSetup"/>        
<load slot='formatterDefs' type='FormatterDef'/>        
</component>

<component class="org.eclipse.xpand2.Generator">
    <metaModel class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>
    <expand value="templates::Formatter::formatterTxt FOREACH formatterDefs"/>
</component>

So the slot formatterDefs is filled with all FormaterDef, and then this is used for the template, and every think works fine.

But I have some templates that requires the model root element, named Model in the grammar. So I tryed to use

<load slot='formatterDefs' type='FormatterDef'/>

and

<expand value="templates::Main::main FOREACH model"/>

instead.

But then I get this warning:

org.eclipse.xtext.mwe.SlotEntry - Could not find any exported element of type 'Model' -> Slot 'model' is empty.

And the slot contains an empty list.

So my Question is: what do I need to do, to get the root Model into my templates?


Solution

  • I meant something like

    public class MyDslNameProvider extends DefaultDeclarativeQualifiedNameProvider {
    
        QualifiedName qualifiedName(Model m) {
            return QualifiedName.create(m.eResource().getURI().toString());
        }
    }
    
    public class MyDslRuntimeModule extends
                              org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
    
        @Override
        public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
            return MyDslNameProvider.class;
        }   
    }