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: If you know Eclipse then learning how to implement a DSL using Xtext is a natural progression. And this guide makes it easy to get started through a step-by-step approach accompanied with simple examples.

Arrow left icon
Profile Icon Lorenzo Bettini
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9 (15 Ratings)
Paperback Aug 2013 342 pages 1st Edition
eBook
Mex$811.99 Mex$902.99
Paperback
Mex$1128.99
Subscription
Free Trial
Arrow left icon
Profile Icon Lorenzo Bettini
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.9 (15 Ratings)
Paperback Aug 2013 342 pages 1st Edition
eBook
Mex$811.99 Mex$902.99
Paperback
Mex$1128.99
Subscription
Free Trial
eBook
Mex$811.99 Mex$902.99
Paperback
Mex$1128.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.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 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.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn to quickly develop a domain-specific language with Xtext Implement any aspect of a DSL using Xtend, a fully featured Java-like programming language 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 its IDE functionalities. It lets you implement languages really quickly, and, most of all, it covers all aspects of a complete language infrastructure, starting from the parser, code generator, interpreter, and more. "Implementing Domain-Specific Languages with Xtext and Xtend" will teach you how to develop a DSL with Xtext, an Eclipse framework for implementing domain-specific languages. The chapters are like tutorials that describe the main concepts of Xtext such as grammar definition, validation, code generation, customizations, and many more, through uncomplicated and easy-to-understand examples. Starting with briefly covering the features of Xtext that are involved in a 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. We then proceed by explaining the main concepts of Xtext, such as validation, code generation, and customizations of runtime and UI aspects. By the end of the book, you will have learned how to test a DSL implemented in Xtext with Junit, in order to follow a test-driven development strategy that will help the developer implement maintainable code that is much faster and cleaner. A test-driven approach is used throughout the book when presenting advanced concepts such as type checking and scoping. The book also shows you how to build and release a DSL so that it can be installed in Eclipse, and gives you hints on how to build the DSL headlessly in a continuous integration server. "Implementing Domain-Specific Languages with Xtext and Xtend" aims to complement the official Xtext documentation to explain the main concepts through simplified examples and to teach the best practices for a DSL implementation in Xtext. It is a Beginner's Guide which should set you up for professional development DSL and its Eclipse IDE tooling.

Who is this book for?

This book is for programmers who want to learn about Xtext and how to use it to implement a DSL (or a programming language) together with Eclipse IDE tooling. It assumes that the user is familiar with Eclipse and its functionality. Existing basic knowledge of a compiler implementation would be useful, though not strictly required, since the book will explain all the stages of the development of a DSL.

What you will learn

  • Learn to write an Xtext grammar for a DSL Use Xtend as an alternative to Java to write cleaner, easier-to-read, and more maintainable code Write constraint checks for a DSL using the validator mechanism Learn how to write a code generator and an interpreter for a DSL Discover how automatic symbol resolution works in Xtext and how to customize it Build and deploy a DSL implementation so that others can install it Get well versed with the typical Xtext development workflow

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 21, 2013
Length: 342 pages
Edition : 1st
Language : English
ISBN-13 : 9781782160304
Category :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.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 21, 2013
Length: 342 pages
Edition : 1st
Language : English
ISBN-13 : 9781782160304
Category :
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 Mex$85 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 Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 2,133.98
Data Visualization with D3.js Cookbook
Mex$1004.99
Implementing Domain-Specific Languages with Xtext and Xtend
Mex$1128.99
Total Mex$ 2,133.98 Stars icon

Table of Contents

13 Chapters
Implementing a DSL Chevron down icon Chevron up icon
Creating Your First Xtext Language Chevron down icon Chevron up icon
The Xtend Programming Language Chevron down icon Chevron up icon
Validation Chevron down icon Chevron up icon
Code Generation Chevron down icon Chevron up icon
Customizations Chevron down icon Chevron up icon
Testing Chevron down icon Chevron up icon
An Expression Language Chevron down icon Chevron up icon
Type Checking Chevron down icon Chevron up icon
Scoping Chevron down icon Chevron up icon
Building and Releasing Chevron down icon Chevron up icon
Xbase Chevron down icon Chevron up icon
Bibliography 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.9
(15 Ratings)
5 star 93.3%
4 star 0%
3 star 6.7%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




LuiX Nov 19, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent! It's an excellent book, with great examples and very didactic text. I recommend it. The introduction to Xtend is very good.
Amazon Verified review Amazon
OPCoach Oct 30, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book gives a full overview of all the powerful features of XText and Xtend, explaining how to manage the auto completion or quick fixes (for instance), and how to test it using JUnit !If you plan to develop a text editor, don't miss this book. It is easy to read, clearly explained and all the examples can be reused for any project.
Amazon Verified review Amazon
Alexander Marek Feb 24, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have been struggeling quite a lot with Xtext as it is a huge framework and I was neither familiar with Xtext, the Eclipse plugin framework and all their naming conventions.Additionally I am currently developing my first textual DSL.This book has helped me a lot!Not only did it give a good introduction into the field of textual DSLs with all the things that belong there, they also do it with simple and well explained examples. I learned how to introduce type systems, do code generation and even write an interpreter.Additionally it gives a lot of insights into the internals of Xtext like how linking works, etc. which also saved me tons of hours as I finally understood the errors and warnings in the Eclipse console that came from Xtext.I can only recommend this book to every interested reader.
Amazon Verified review Amazon
Axl Mattheus Nov 06, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is extensive without being long winded. It covers most, if not all of the functionality locked away in Xtext. It does a respectable job of introducing Xtend. It is well organized, which makes it easy to skip chapters not of interest to the reader without losing context in later chapters. Examples are concise, focussed, practical and accurate - and the examples work too!Compared to some of the works on other language construction tools, this book is a shining example of what a book on language construction should look like.Caveat: don't take on this book if you do not have some knowledge of language construction. A little knowledge of ANTLR will go a long way as it will make a big difference in dealing with the ANTLR errors/warnings when constructing a new language.Well done Lorenzo and the editors of this book!
Amazon Verified review Amazon
Axel Sep 25, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Although I have been working with both Xtext and Xtend for several months already I bought the new book from Lorenzo Bettini as it promised to give a far better total overview regarding advanced use cases and best practices than the official documentation. After having read the whole book I can recommend it to new users interested in model-driven development and also to advanced DSL engineers.The book copes with teaching things in building steps whereby a number of examples is implemented and extended. Even quite complex topics like type systems and scoping are very-well described so readers are not lost in the nowhere.If you think you know already all important aspects of Xtext and Xtend note the book is updated for the 2.4.x series already. Be sure that you will discover some details you missed before, but serve for further improvement of your productivity.
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.

Modal Close icon
Modal Close icon