Validation in Xtext
As we anticipated 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...