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
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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

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 a Packt Subscription?

Free for first 7 days. €18.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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
€18.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
€189.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
€264.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 120.97
Eclipse Plug-in Development Beginner's Guide
€41.99
Groovy for Domain-Specific Languages, Second Edition
€41.99
Implementing Domain-Specific Languages with Xtext and Xtend
€36.99
Total 120.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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.