Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Integrating It All


[ 326 ]

stmnt.statementLabel &&
stmnt.statementLabel == 'state'
}
boolean isNonStateBlock(Statement stmnt) {
stmnt instanceof ExpressionStatement &&
stmnt.expression instanceof ConstantExpression &&
stmnt.statementLabel &&
(stmnt.statementLabel == 'page' ||
stmnt.statementLabel == 'event' ||
stmnt.statementLabel == 'when')
}
boolean isDeclarationExpression(Statement stmnt) {
stmnt instanceof ExpressionStatement &&
stmnt.expression instanceof DeclarationExpression
}

Only declaration expressions are allowed in the state: block of the DSL. If we
encounter anything else, we raise a compile error via the addError method.
Collecting the event: blocks is more straightforward. We just add each
statement we encounter to the model:


def collectEventMethodBodies(node) {
def collecting = false
def event
node.code.statements.each { stmnt ->
if (!collecting && isEventBlock(stmnt)) {
collecting = true
event = getLabelParam(stmnt)
return
}
if (collecting && isNonEventBlock(stmnt)) {
collecting = false
}
if (collecting && isEventBlock(stmnt)) {
event = getLabelParam(stmnt)
}
if (collecting) {
model.addEventStatement event, stmnt
}
}

}
boolean isEventBlock(Statement stmnt) {
stmnt instanceof ExpressionStatement &&
stmnt.expression instanceof ConstantExpression &&

http://www.ebook3000.com
Free download pdf