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.

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 a class to perform additional checks. Also in this case it is our job to avoid an infinite loop in case of cycles. We write ...

Get Implementing Domain-Specific Languages with Xtext and Xtend now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.