Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Implementing Domain-Specific Languages with Xtext and Xtend
Implementing Domain-Specific Languages with Xtext and Xtend

Implementing Domain-Specific Languages with Xtext and Xtend: Learn how to implement a DSL with Xtext and Xtend using easy-to-understand examples and best practices. , Second Edition

eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Implementing Domain-Specific Languages with Xtext and Xtend

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 on to build the AST of a program.

This chapter will cover the following topics:

  • A DSL for entities
  • The Xtext generator
  • The Eclipse Modeling Framework
  • Improvements to the DSL

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
  3. Press Finish.

The wizard will create several projects and it will open the file Entities.xtext, which is the grammar definition.

The main dialog of the wizard is shown in the following screenshot...

The Xtext generator

Xtext uses the MWE2 (Modeling Workflow Engine 2) DSL to configure the generation of its artifacts. The 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.

Note

In order to deal with additional platforms besides Eclipse, such as IntelliJ and Web editors (Chapter 11 , Continuous Integration), in Xtext 2.9, a brand new generator infrastructure has been introduced, which also aims at simplifying the overall configuration of the generated artifacts. This new generator is completely different from the old one. However, the old generator is still present in Xtext so that projects created before Xtext 2.9 still work and do not need to migrate to the new generator immediately. This book will always use the new generator.

During the MWE2 workflow execution, Xtext...

The Eclipse Modeling Framework (EMF)

The EMF (Eclipse Modeling Framework) (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 mechanisms. The model specification (metamodel) can be described in XMI, XML Schema, Unified Modeling Language (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 similar to an implementation of a subset of UML class diagrams.

Note

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...

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 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

MyEntity[] myattribute;

is a valid statement of our DSL, while the one below (note the spaces between the square brackets):

MyEntity[  ] myattribute;

produces a syntax error.

This is not good, since we do not want spaces to be relevant (although there are languages such as 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...

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 programming language Xtend, which is shipped with Xtext and is implemented in Xtext itself. Xtend is a Java-like general purpose programming language, completely inter-operable 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 Xtext languages.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Leverage the latest features of Xtext and Xtend to develop a domain-specific language.
  • Integrate Xtext with popular third party IDEs and get the best out of both worlds.
  • Discover how to test a DSL implementation and how to customize runtime and IDE aspects of the DSL

Description

Xtext is an open source Eclipse framework for implementing domain-specific languages together with IDE functionalities. It lets you implement languages really quickly; most of all, it covers all aspects of a complete language infrastructure, including the parser, code generator, interpreter, and more. This book will enable you to implement Domain Specific Languages (DSL) efficiently, together with their IDE tooling, with Xtext and Xtend. Opening with brief coverage of Xtext features involved in DSL implementation, including integration in an IDE, the book will then introduce you to Xtend as this language will be used in all the examples throughout the book. You will then explore the typical programming development workflow with Xtext when we modify the grammar of the DSL. Further, the Xtend programming language (a fully-featured Java-like language tightly integrated with Java) will be introduced. We then explain the main concepts of Xtext, such as validation, code generation, and customizations of runtime and UI aspects. You will have learned how to test a DSL implemented in Xtext with JUnit and will progress to advanced concepts such as type checking and scoping. You will then integrate the typical Continuous Integration systems built in to Xtext DSLs and familiarize yourself with Xbase. By the end of the book, you will manually maintain the EMF model for an Xtext DSL and will see how an Xtext DSL can also be used in IntelliJ.

Who is this book for?

This book is targeted at programmers and developers who want to create a domain-specific language with Xtext. They should have a basic familiarity with Eclipse and its functionality. Previous experience with compiler implementation can be helpful but is not necessary since this book will explain all the development stages of a DSL.

What you will learn

  • Write Xtext grammar for a DSL;
  • Use Xtend as an alternative to Java to write cleaner, easier-to-read, and more maintainable code;
  • Build your Xtext DSLs easily with Maven/Tycho and Gradle;
  • Write a code generator and an interpreter for a DSL;
  • Explore the Xtext scoping mechanism for symbol resolution;
  • Test most aspects of the DSL implementation with JUnit;
  • Understand best practices in DSL implementations with Xtext and Xtend;
  • Develop your Xtext DSLs using Continuous Integration mechanisms;
  • Use an Xtext editor in a web application
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 31, 2016
Length: 426 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786464965
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Aug 31, 2016
Length: 426 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786464965
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 158.97
Eclipse Plug-in Development Beginner's Guide
$54.99
Groovy for Domain-Specific Languages, Second Edition
$54.99
Implementing Domain-Specific Languages with Xtext and Xtend
$48.99
Total $ 158.97 Stars icon
Banner background image

Table of Contents

16 Chapters
1. Implementing a DSL Chevron down icon Chevron up icon
2. Creating Your First Xtext Language Chevron down icon Chevron up icon
3. Working with the Xtend Programming Language Chevron down icon Chevron up icon
4. Validation Chevron down icon Chevron up icon
5. Code Generation Chevron down icon Chevron up icon
6. Customizing Xtext Components Chevron down icon Chevron up icon
7. Testing Chevron down icon Chevron up icon
8. An Expression Language Chevron down icon Chevron up icon
9. Type Checking Chevron down icon Chevron up icon
10. Scoping Chevron down icon Chevron up icon
11. Continuous Integration Chevron down icon Chevron up icon
12. Xbase Chevron down icon Chevron up icon
13. Advanced Topics Chevron down icon Chevron up icon
14. Conclusions Chevron down icon Chevron up icon
A. Bibliography Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(7 Ratings)
5 star 71.4%
4 star 28.6%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




L. Milligan May 28, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
“A theory should be as simple as possible, and no simpler.” Albert Einstein is supposed to have said that. Well, maybe. But whoever said it was right. And it applies to software as well, which is why Domain Specific Language (DSL) is so often the right solution to a software engineering problem. A well-designed DSL can simplify the architecture of a system while also providing the expressive power to match the peculiar complexities of the problem domain.This book is more than a tutorial; it is an introductory course in Xtext, a tool that provides an amazing level of automation in the production of a DSL. Xtext leverages other technologies, like ANTLR and EMF, and then provides all of the features that make a language easy to use: syntax coloring, content assist, outlines, validation and problem tracking, cross-reference linking, code-generation and integration with the Eclipse build mechanism. Lorenzo Bettini has done the hard work of presenting all of the features of Xtext in depth and with clear and meaningful examples. He explains the many different ways that an Xtext DSL can be customized, including sections on Xtend, itself a DSL which is adept at traversing the EMF Abstract Syntax Tree, and on the Google Guice injection framework. Code generation, interpreted language, test-driven development, build and distribution are all covered with enough detail to be meaningful without overwhelming the reader.In sum, I highly recommend this book to software developers who are looking for the right introduction to a powerful tool. Like scientific theories and software, a book should be as simple as possible, and no simpler. This is such a book. John Milligan
Amazon Verified review Amazon
Andrew Oct 03, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
An excellent up-to-date reference book for the Xtext framework and, more in general, for DSL design and implementation principles.This new version of the book streamlined the already solid contents of the first edition. The author still pursues a step-by-step presentation fashion that easily allows readers to get on to all Xtext-related functionalities. A really well-defined book sectioning permits grasping all the book contents; readers can efficiently use the book likewise a tool users' guide.The author strengthens tips and examples throughout the book and, most of all, proposes a new set of advanced topics that more trained users will definitely appreciate! The chapter on continuos integration should be always used as a reference guide for plugin development technologies!
Amazon Verified review Amazon
Enrico Denti Nov 10, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Libro ben scritto, aggiornato, chiaro, con molti esempi: un testo interessante su un argomento non banale. Unico piccolo neo, a volte potrebbe essere utile avere tutto il codice di un esempio raccolto in un box unico, anziché doverlo recuperare per parti dove viene spiegato, ma si tratta di un dettaglio.
Amazon Verified review Amazon
Cliente Amazon Feb 25, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Chiaro, con molti esempi. Ho trovato quello che cercavo.
Amazon Verified review Amazon
Max Feb 22, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Das Buch ist eine sehr gute Einführung in das Thema Xtext, behandelt aber auch alle wichtigen Themen in der notwendigen Tiefe. Ich habe mir nicht nur das Buch sondern auch des E-Book als PDF zugelegt und beides war jeden Euro wert.Wer eine DSL bauen möchte, die dann auch in der Praxis eingesetzt wird, ist nicht nur mit Xtext gute beraten sondern auch mit diesem Buch!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries wit