Search code examples
javaxtextxtend

Xtext Adding values to the grammar manually


I have a grammar that can parse a list of containers and modules, then generate these containers in alphabetical order. But, I also want to add the module names into the container list.

For example:

With the configuration:
CONTAINER cont1;
CONTAINER cont2;
MODULE external WITH PRIORITY 1;
MODULE internal WITH PRIORITY 2;


The generated file should have:
main()
{
  Container(cont1);
  Container(cont2);
  Container(external);
  Container(internal);
}

I was able to generate what is in the CONTAINER configuration. But I want to merge the module names "internal" and "external" into the container. Then sort them alphabetically.

Is there a way to do this in xtext? And does this belong to the generator or scoping part?


Solution

  • This should go into the generator. What you need it generate a list of all CONTAINER and MODULE nodes. I assume that you've written the generator in Xtend:

    val list = Lists.newArrayList( model.containers )
    list.addAll( model.modules )
    

    iterate over the sorted list:

    for( part : list.sortBy(e|e.name) ) {
        part.generateContainer()
    }
    

    and then one generateContainer() method per type.