Showing items from Xtext-Tips

Xtext Tip: Adding importURI support

In a previous post I showed how to use the import namespace feature of Xtext. This feature helps to easily implement java like imports. However, Xtext also supports imports more in a C-like style, i.e. imports where a whole file imported, and its definitions made available to the current file.

For example, in our block language, it should be possible to import the name space from another Block and then use in our alias. We can for example define Block3 in file Block3.block

Continue Reading

Xtext Tip: Add custom code to your generated model

If you are into EMF modeling, you know that you can define custom operations and fields in EMF models. Xtext allows to make use of this feature in an elegant way. Let us start with the block language with a separate EMF metamodel from a previous tip. The full code for this example can be found here.

Prepare the model

We want to include a new computed field that returns the number of fields in our Block class. We call this attribute numberOfFields. We create an attribute with that name, and set the attributes as follows:

Continue Reading

Xtext Tip: Some reasons to use a separated EMF model

In a previous tip I showed how to migrate from a generated EMF model to a manually maintained one. Turning something automatic to manual sounds completely counter intuitive. We should automate more, not the contrary. However, there are some good reasons to do this. Let’s explore them:

Separation of concerns

I see the grammar is a presentation artifact. It represents the allowed inputs. However, the easiest way to input something is not always the best way to represent it for its intended purpose. The typical example, is the graph and the matrix. A matrix can perfectly represent a weighted graph. For certain computations it might be the optimal solution, but for others, a graphical representation of the graph is much better.

Continue Reading

Xtext Tip: Migrating to manual EMF model

Xtext is very good at inferring a metamodel from a grammar but it has its limitations. After reaching a certain complexity, I think it is important to decouple the grammar from the metamodel. This is allows to separate concerns: the metamodel becomes the “business model layer”, and the grammar becomes the “presentation layer”. To do this, we need to transform our project to this new setup. In this post I explain how. The full code for this example can be found here.

Continue Reading

Xtext Tip: Adding importedNamespace support

If you look at Xtext’s documentation here, there is a mention of the possibility of adding import name space support to your DSLs. This allows your DSL to have Java like imports. For example, in our block language, it should be possible to import the name space from another Block and then use in our alias. We can for example define Block3 in file Block3.block

block Block3 {
		field myField1
		field myField2
}

And then use it in the file Block1.block as follows:

Continue Reading