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

652 views Asked by At

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?

4

There are 4 answers

0
Christian Dietrich On BEST ANSWER

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;
    }   
}
0
Ralph On

A solution was to add an ID to the Grammer Definiton of the Model.

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

I understand that there was no existing Model element before, so after adding the ID the Model element must be created to contain the Id.

How ever may there is a better solution - I will accept it, as soon as it is posted.

0
Christian Dietrich On

you could customize the IQualifiedNameProvider to give the Model a name.

~Christian

1
robermorales On

Probably the best way is inserting the text {Model} just at the beginning of the rule, to ensure the creation.