Wednesday, March 18, 2009

Xtext Corner #4 – Customizing the generator

TMF Xtext M6 has been released on Tuesday, 17 May 2009, and it comes with a bunch of handy new features. One of the most remarkable changes since milestone 5 is the redesigned generator infrastructure. A highly configurable, declarative framework has superseded the monolithic implementation, that we used previously.

The core idea behind the shiny new generator are so called generator fragments. They allow to contribute entries to various places during the generation process. It is possible to add extensions to the plugin.xml, declare exported packages for the MANIFEST.MF or list guice bindings. Furthermore and properly most important: generator fragments provide a mighty mechanism to create any kind of output files utilizing the Xpand template language.

Because the interface of fragments has been designed to be composable, it is easily possible to implement reusable components. Xtext itself ships with a neat set of default and often required fragments.
  • The generated grammar access provides strongly typed accessors for the various elements of your concrete syntax such as alternatives, assignments, cross references and keywords.
  • The Xtext parser that is responsable for reading text and instantiating a model. You'll get optionally an ANLTR-Parser, too.
  • A serializer, that is capable to transform the semantic model into its native textual form.
  • Xtext comes with a fragment, that creates a Check based validation stub. It allows to implement constraints for model instances declarativly. As there exists another fragment for java based validation, it is possible to use both and combine the expressivness of Check with the performance boost of native Java code for expensive validation.
  • A Java API for content assist is generated for the editor of your DSL. You can even choose to use an Xtend based API.
  • You'll get a custom EMF resource factory, that will create and configure your Xtext resources.
  • And the fragment that covers functionality that has not been in Xtext before, will generate Java classes for the infered metamodels. It uses EMF GenModels to output their corresponding Java API. Working with your semantic model is far more comfortable when there is no nead to deal with dynamic EMF models.
To create a library of generator fragments, we had to provide a simple way to parameterize the generator. Instead of instantiating objects the hard way and write a lot of Java code with constructor calls, getters and setters, we decided to use the Modeling Workflow Enging (MWE) to configure it. Clients plug the fragments they want to use, simply together by listing them in a workflow file. There is no need to write a single line of Java code to customize the behaviour or to execute the Xtext generator.

If someone has special needs for his language generation, he can implement a generator fragment that serves his very own interests well. Is your solution matching potential requirements of other parties, too? We will be happy to accept patches with reusable, customizable fragments to be packaged with Xtext.