Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 8

[ 209 ]

state: "ON"

when: "ON"

event: "toggle"
when:
next = "ON"
when: "OFF"
next = "ON"

This will provide the following output:


ErrorState.groovy: 5: when: must follow event:


@ line 5, column 7.


when: "ON"


^


ErrorState.groovy: 9: Expected state value after when:


@ line 9, column 9.


next = "ON"


^


ErrorState.groovy: 10: Cannot transition from a non existant state


@ line 10, column 11.


when: "OFF"


^


Providing any expression for a transition other than one in the form
next = 'state' should cause an appropriate error:


def handleBadWhenExpression(next, expr, state) {
def oper = expr.operation
if (!next instanceof VariableExpression ||
next.accessedVariable.name != 'next') {
addError "assignment to next is only allowed",
next, source
} else if (oper instanceof Token || oper.rootText != "=") {
addError "assignment operator is only allowed",
expr, source
} else if (!state instanceof ConstantExpression) {
addError "assigning a valid state only allowed",
state, source
}
Free download pdf