Forward references

You should know by now that parsing is only the first stage when implementing a DSL and that it cannot detect all the errors from the programs. We need to implement additional checks in a validator.

One important thing we need to check in our Expressions DSL is that an expression does not refer to a variable defined after the very expression. Using an identifier before its declaration is usually called a forward reference.

Therefore, this program should not be considered valid:

var i = j + 1
var j = 0

Since the initialization expression of i refers to j, which is defined after. Of course, this is a design choice. Since we want to interpret the expressions, it makes sense to interpret them in the order they are defined.

This strategy ...

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.