Implementing Domain:Specific Languages with Xtext and Xtend
上QQ阅读APP看书,第一时间看更新

An introduction to Xtend

The Xtend programming language comes with very nice documentation, which can be found on its website, http://www.eclipse.org/xtend. We will give an overview of Xtend in this chapter, but we strongly suggest that you then go through the Xtend documentation thoroughly. Xtend itself is implemented in Xtext and it is a proof of concept of how involved a language implemented in Xtext can be.

We will use Xtend throughout this book to write all parts of a DSL implementation. Namely, we will use it to customize UI features, to write tests, to implement constraint checks, and to write code generators or interpreters for all the example DSLs we will develop in this book. In particular, starting with version 2.4, all the stub classes generated by Xtext for your DSL projects are Xtend classes by default (instead of Java, as in the previous versions).

You can still generate Java stub classes by customizing the MWE2 workflow, but in this book we will always use Xtend classes. Xtend, besides providing useful mechanisms for writing code generators (most of all, multi-line template expressions), also provides powerful features that make model visiting and traversing really easy, straightforward, and natural to read and maintain. Indeed, besides the grammar definition, for the rest of the time when implementing a DSL, you will have to visit the AST model. Xtend programs are translated into Java, and Xtend code can access all the Java libraries, thus Xtend and Java can coexist seamlessly.

Using Xtend in your projects

You can use Xtend in your Eclipse Java projects (both plain Java and plug-in projects).

In a plug-in project, you can right-click on your source folder and select New | Xtend Class; you will see that this wizard is similar to the standard New Java Class wizard, so you can choose the package, the class Name, Superclass, and Interfaces (refer the following screenshot):

As soon as the class is created, you will get an error marker with the message "Mandatory library bundle 'org.eclipse.xtext.xbase.lib' 2.4.0 or higher not found on the classpath". You just need to use the quickfix "Add Xtend libs to classpath" and the required Xtend bundles will be added to your project's dependencies.

A new source folder will be created in your plug-in project, xtend-gen, where the Java code corresponding to your Xtend code will be automatically generated (using the building infrastructure of Eclipse) as soon as you save an .xtend file. Just like src-gen created by the Xtext generator (as seen in the previous chapter), the files in xtend-gen must not be modified by the programmer, since they will be overwritten by the Xtend compiler.

Tip

The folder xtend-gen is not automatically added to the build source folders of your plug-in project, and therefore, you should add it manually in your build.properties file (the file has a warning marker, and the editor will provide you with a quickfix to add that folder). This is requested only for plug-in projects.

You can use the same steps to create an Xtend class in a plain Java project (again, you will have to use the quickfix to add the Xtend libraries to the classpath). Of course, in this case, there is no build.properties to adjust.

Note

Starting from version 2.4.0, Xtext automatically generates Xtend stub classes for your DSL (instead of the Java ones), thus, the runtime and UI plug-in projects are already setup to use Xtend and its libraries. However, this does not hold for the tests plug-in project; thus, when starting to write Xtend classes in the tests plug-in project (as we will see in Chapter 7, Testing), you will need to perform the setup steps described in this section.