Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 8

[ 203 ]

The StateMachineModel class is really just a wrapper on some lists and maps that
contain a representation of the state machine. It has convenience methods for adding
the states, events, and transitions as they are encountered during parsing and for
accessing these while building the new AST nodes. Next, we will look at how the
parser builds the StateMachineModel object.


The StateMachineParser class is a concrete implementation of the
GroovyClassVisitor interface. The StateMachineASTTransformation.visit
method passes the parser each class node it finds to the visitContents method:


class StatePatternParser implements GroovyClassVisitor {
def model
def source

StatePatternParser(stateMachineModel, sourceUnit) {
this.model = stateMachineModel
this.source = sourceUnit
}
@Override
void visitClass(ClassNode node) {
}
@Override
void visitMethod(MethodNode node) {
// we only need this one
}
@Override
void visitConstructor(ConstructorNode node) {
}
@Override
void visitField(FieldNode node) {
}
@Override
void visitProperty(PropertyNode node) {
}
}

StateMachineParser must override all the visit methods available from
GroovyClassVisitor, but for our purposes, the only one we need to really
implement is visitMethod. This visitor will be invoked for each method that is
encountered in the class.

Free download pdf