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

In this chapter we will introduce the concept of validation, and in particular, the Xtext mechanism to implement validation: the validator. With validation you can implement additional constraint checks of a DSL which cannot be done at parsing time. Xtext allows you to implement such constraint checks in an easy and declarative way; furthermore, you only need to communicate to Xtext the possible errors or warnings and it will take care of generating the error markers accordingly in the IDE. The validation will take place in the background while the user of the DSL is typing in the editor, so that an immediate feedback is provided. We will also show how to implement quickfixes corresponding to the errors and warnings generated during validation, so that we can help the user to solve problems due to validation errors.

Validation in Xtext


As we hinted in Chapter 1, Implementing a DSL, parsing a program is only the first stage in a programming language implementation. In particular, the overall correctness of a program cannot always be determined during parsing. Trying to embed additional constraint checks in the grammar specification could either make such specification more complex or it could be simply impossible as some additional static analysis can be performed only when other program parts are already parsed.

Actually, the best practice is to do as little as possible in the grammar and as much as possible in validation (we will use this practice in Chapter 9, Type Checking and Chapter 10, Scoping). This is because it is possible to provide far better error messages and to more precisely detect problems that are eligible for quickfixes.

The mechanism of validation will be used extensively in all example DSLs of this book. Typically, even for small DSLs, a validator has to be implemented to perform additional...

Quickfixes


As we said in Chapter 1, Implementing a DSL, a quickfix is a proposal to solve a problem in a program. Quickfixes are typically implemented by a context menu available from the error marker, and they are available both in the editor ruler and in the Problems view.

Note

Since quickfixes are tightly connected to validation, we describe them in this chapter. Moreover, they allow us to get familiar with the manipulation of the EMF model representing the AST of a program.

In our Entities DSL we can provide a quickfix for each warning and error issued by our validator; moreover, as we will see later, we can also provide quickfixes for errors issued by Xtext default validators.

Xtext provides an easy mechanism to implement a quickfix connected to an error or warning issued by a validator. The Xtext generator generates an Xtend stub class for quickfixes into the UI plug-in project; in our Entities DSL example, this class is org.example.entities.ui.quickfix.EntitiesQuickfixProvider.

A quickfix...

Summary


In this chapter you learned how to implement constraint checks, using the Xtext validator mechanism based on @Check annotated methods. Just by implementing a custom validator and calling the method error or warning with the appropriate information, Xtext produces error and warning markers that result in marking the text regions as well as showing the markers in the various views in Eclipse.

We also showed how to implement quickfixes. Since Xtext automatically synchronizes the DSL editor's contents with the EMF model of the AST, we can simply modify such model without dealing with the textual representation of the program.

In the next chapter we will write a code generator for the Entities DSL implemented in Xtend, relying on its advanced features for code generation: starting from a program written in our Entities DSL, we will generate the corresponding Java code. You will see that Xtext automatically integrates your code generator into the building infrastructure of Eclipse.

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}