First validation rules

Before getting to the main subject of this chapter, we will first implement some constraint checks that are complementary to type checking. Correct access to fields and methods will be checked in the next chapter, where we implement a custom scoping mechanism.

Checking cycles in class hierarchies

The SmallJava parser accepts input with cyclic class hierarchies, for instance:

class A extends C {}

class C extends B {}

class B extends A {}

This cannot be checked in the parser; we must write a validator @Check method to mark such situations as errors (this is similar to the validator for the Entities DSL in Chapter 4, Validation). As you will see later in this chapter, we will often need to traverse the inheritance hierarchy of ...

Get Implementing Domain-Specific Languages with Xtext and Xtend - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.