Groovy for Domain-specific Languages - Second Edition
AST Transformations [ 198 ] Similarly, placing a string literal in our code without an assignment is mostly a useless exercise. ...
Chapter 8 [ 199 ] Concrete state classes: We will have one concrete state class per available state in the machine. Each state ...
AST Transformations [ 200 ] def toggle() { context.state = new OffState(context) } String toString() { "ON" } } Finally, we need ...
Chapter 8 [ 201 ] A state machine AST transformation Unlike the previous examples, we won't need an annotation class for a globa ...
AST Transformations [ 202 ] Global AST transformations are potentially expensive in terms of CPU resources. The visit method of ...
Chapter 8 [ 203 ] The StateMachineModel class is really just a wrapper on some lists and maps that contain a representation of t ...
AST Transformations [ 204 ] In order to understand this code better, let's first remind ourselves of the syntax of our state mac ...
Chapter 8 [ 205 ] } } } } The state machine DSL should contain at least two state declarations and at least one event declaratio ...
AST Transformations [ 206 ] As we traverse the statements in the module, we also record the occurrence of event: and when: claus ...
Chapter 8 [ 207 ] Here, we are looking in the AST for a binary expression where the left operand is a variable called next, the ...
AST Transformations [ 208 ] We should have two or more states and at least one event declared. You will also notice in the handl ...
Chapter 8 [ 209 ] state: "ON" when: "ON" event: "toggle" when: next = "ON" when: "OFF" next = "ON" This will provide the followi ...
AST Transformations [ 210 ] } state: "ON" state: "OFF" event: "toggle" when: "ON" prev = "OFF" when: "OFF" next * "OFF" This wil ...
Chapter 8 [ 211 ] ModuleNode moduleNode ClassNode classNode String className ClassNode contextClass String contextClassName Stat ...
AST Transformations [ 212 ] contextClassName = "${className}Context" contextClassNode[1].addProperty( new PropertyNode( "state", ...
Chapter 8 [ 213 ] What we have is a classic chicken and egg situation. There is nowhere in the pattern that we can start where w ...
AST Transformations [ 214 ] ClassHelper.make(contextClassName), ArgumentListExpression.EMPTY_ARGUMENTS ), null, null ) ) } The f ...
Chapter 8 [ 215 ] | |_ConstantExpression getState | |_ArgumentListExpression |_ArgumentListExpression Note how the toString() an ...
AST Transformations [ 216 ] This builder method is very similar to the buildClientStateGetter method. It iterates all events in ...
Chapter 8 [ 217 ] methodStatement = new ExpressionStatement( new BinaryExpression( new PropertyExpression( new VariableExpressio ...
«
7
8
9
10
11
12
13
14
15
16
»
Free download pdf