Showing items from Language Engineering

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

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