Showing items from Eclipse

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: 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: Context dependent FQNs in Xtext

One of my favorite features of Xtext is the ability to have context dependant FQNs. One application of the feature is the this keyword, which is used to refer to the current instance in languages like Java. The full code for this example can be found here.

The basic block language

We are using the same block langague that we used in a previous tip. This time, however, we allow the aliases to use the this keyword in the alias field.

Continue Reading

Xtext Tip: Add custom values to your DSL

Sometimes we have a value that has different representations. For example, an integer can be represented in base 10, base 2, base 16 or any other. Depending of the domain, one of these representations might mean more to the experts. Xtext allows to do this very easily. The full code for this example can be found here.

The hex language

To illustrate this feature we are using a DSL to represent a binary file as a chain of hexadecimal values. A file of this language looks like follows:

Continue Reading

Xtext Tip: How to change the default delimiter of your FQNs

In a previous tip I showed how to add FQN support to your Xtext DSL. In this tip, we are going to customize our FQN to have a different separator. The full code for this example can be found here.

The basic block language

I am using the block language of the previous tip, but this time we are using the characters -> as separator

block Block1 {
	
	field field1
	field field2

	block SubBlock {
		field field2
	}
	alias field3 aliases field2

}
block Block2 {
	alias field1 aliases Block1->SubBlock->field2 // this is an FQN with a -> as separator
}

Customize the FQN separator

First we change the grammar. The new grammar looks like this:

Continue Reading