Xtext Tip: Use validation to enforce constraints
Sometimes, you need some constraints for your DSL and you can use the grammar language for that. However, this is not a good idea. For example, in a previous post, I created a grammar rule to parse hexadecimal numbers with exactly two digits. To do this, we created the following rule:
terminal HEX_VALUE returns ecore::EInt:
'0x' (('0'..'9') | ('a'..'z') | ('A'..'Z')) (('0'..'9') | ('a'..'z') | ('A'..'Z'));
This rules does exactly that, but when it is not valid it shows a cryptic error. The error does not help the user to fix this.
Continue Reading