Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Implementing Domain-Specific Languages with Xtext and Xtend

You're reading from  Implementing Domain-Specific Languages with Xtext and Xtend

Product type Book
Published in Aug 2013
Publisher Packt
ISBN-13 9781782160304
Pages 342 pages
Edition 1st Edition
Languages
Author (1):
Lorenzo Bettini Lorenzo Bettini
Profile icon Lorenzo Bettini

Table of Contents (21) Chapters

Implementing Domain-Specific Languages with Xtext and Xtend
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Implementing a DSL 2. Creating Your First Xtext Language 3. The Xtend Programming Language 4. Validation 5. Code Generation 6. Customizations 7. Testing 8. An Expression Language 9. Type Checking 10. Scoping 11. Building and Releasing 12. Xbase 13. Bibliography
Index

Chapter 2. Creating Your First Xtext Language

In this chapter we will develop a DSL with Xtext and learn how the Xtext grammar language works. We will see the typical development workflow of programming with Xtext when we modify the grammar of the DSL. The chapter will also provide a small introduction to EMF (Eclipse Modeling Framework), a framework that Xtext relies upon to build the AST (Abstract Syntax Tree) of a program.

A DSL for entities


We will now implement a simple DSL to model entities, which can be seen as simple Java classes; each entity can have a super type entity (you can think of it as a Java superclass) and some attributes (similar to Java fields). This example is a variant of the domain model example that can be found in the Xtext documentation.

Creating the project

First of all, we will use the Xtext project wizard to create the projects for our DSL (we have already experimented with this at the end of Chapter 1, Implementing a DSL).

  1. Start Eclipse and navigate to File | New | Project.... In the dialog navigate to the Xtext category and select Xtext Project.

  2. In the next dialog you should specify the following names:

    • Project name: org.example.entities

    • Name: org.example.entities.Entities

    • Extensions: entities

    • Uncheck the option Create SDK feature project (we will use the Create SDK feature project only in Chapter 11, Building and Releasing)

The wizard will create three projects and it will open the...

The Xtext generator


Xtext uses the MWE2 DSL to configure the generation of its artifacts; the default generated .mwe2 file already comes with good defaults, thus, for the moment, we will not modify it. However, it is interesting to know that by tweaking this file we can request the Xtext generator to generate support for additional features, as we will see later in this book.

During the MWE2 workflow execution, Xtext will generate artifacts related to the UI editor for your DSL, but most important of all, it will derive an ANTLR specification from the Xtext grammar with all the actions to create the AST while parsing. The classes for the nodes of the AST will be generated using the EMF framework (as explained in the next section).

The generator must be run after every modification to the grammar (the .xtext file). The whole generator infrastructure relies on the Generation Gap Pattern (Vlissides 1996). Indeed, code generators are fine, but when you have to customize the generated code: subsequent...

The Eclipse Modeling Framework (EMF)


The Eclipse Modeling Framework (EMF) (Steinberg et al., 2008), http://www.eclipse.org/modeling/emf, provides code generation facilities for building tools and applications based on structured data models. Most of the Eclipse projects that in some way deal with modeling are based on EMF since it simplifies the development of complex software applications with its modeling mechanisms. The model specification ( metamodel) can be described in XMI, XML Schema, UML, Rational Rose, or annotated Java. It is also possible to specify the metamodel programmatically using Xcore, which was implemented in Xtext. Typically, a metamodel is defined in the Ecore format, which is basically an implementation of a subset of UML class diagrams.

Tip

Pay attention to the meta levels in this context: an Ecore model is a metamodel, since it is a model describing a model. Using the metamodel EMF produces a set of Java classes for the model. If you are not familiar with modeling...

Improvements to the DSL


Now that we have a working DSL, we can do some improvements and modifications to the grammar.

After every modification to the grammar, as we said in the section The Xtext generator, we must run the MWE2 workflow so that Xtext will generate the new ANTLR parser and the updated EMF classes.

First of all, while experimenting with the editor, you might have noted that while

MyEntity[] myattribute;

is a valid sentence of our DSL, this one (note the spaces between the square brackets)

MyEntity[  ] myattribute;

produces a syntax error.

This is not good, since spaces should not be relevant in a DSL (although there are languages like Python and Haskell where spaces are indeed relevant).

The problem is due to the fact that in the Attribute rule, we specified [], thus, no space is allowed between the square brackets; we can modify the rule as follows:

Attribute: type=[Entity] (array?='[' ']')? name=ID ';';

Since we split the two square brackets into two separate tokens, spaces between...

Summary


In this chapter, you learned how to implement a simple DSL with Xtext and you saw that, starting from a grammar definition, Xtext automatically generates many artifacts for the DSL, including IDE tooling.

You also started to learn the EMF API that allows you to programmatically manipulate a model representing a program AST. Being able to programmatically access models is crucial to perform additional checks on a program that has been parsed and also to perform code generation, as we will see in the rest of the book.

In the next chapter, we will introduce the new programming language, Xtend (which is shipped with Xtext, and is implemented in Xtext itself): a Java-like general purpose programming language tightly integrated with Java that allows you to write much simpler and much cleaner programs. We will use Xtend in the rest of the book to implement all the aspects of languages implemented in Xtext.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Implementing Domain-Specific Languages with Xtext and Xtend
Published in: Aug 2013 Publisher: Packt ISBN-13: 9781782160304
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}