Showing items from Xtext-Tips

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: How to make a DSL generate code from the code generated from another DSL

Sometimes it happens that you have an Xtext DSL that needs generate code to be consumed by another DSL. We can see an example of the need in the question in the Eclipse Forums. This is especially a problem when you use maven to build your projects.

There is a simple solution for that. You can make xtext-maven-plugin generate the code at different phases of the compilation.

In this example we have two DSLs: HelloGen that generates a hello file, and Hello that generates a txt file from a hello file.

Continue Reading

Xtext Tip: Use validation to enforce constraints

Sometimes, you need some constraints for your DSL and you can use the grammar language for that. However, this is not a good idea. For example, in a previous post, I created a grammar rule to parse hexadecimal numbers with exactly two digits. To do this, we created the following rule:

terminal HEX_VALUE returns ecore::EInt:
	'0x' (('0'..'9') | ('a'..'z') | ('A'..'Z')) (('0'..'9') | ('a'..'z') | ('A'..'Z'));

This rules does exactly that, but when it is not valid it shows a cryptic error. The error does not help the user to fix this.

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