Groovy Documentation

kotka.groovy.zweig
[Groovy] Class ZweigBuilder

java.lang.Object
  kotka.groovy.zweig.ZweigBuilder

class ZweigBuilder

ZweigBuilder translates a high-level AST description into the actual groovy AST representation. The are entry points for statements as well as expressions and nodes. All of the below described structures nest arbitrarily.

All entry points are idempotent. So you may provide your custom created ASTNodes directly should the need arise. In this case ZweigBuilder will leave them alone.

Simple constants

The following constants are immediately translated to constant expressions:

Complex and compound AST nodes

More complicated specifications are described by maps. The following describes the available structures. Each key in the map describes part of the information required to build the structure.

Note: The first key described is always the one used to decide which type of structure is actually meant.

Note: In case there is the default value given for a keyword, this keyword is optional. If it is not contained in the specification map, then the default value will apply.

List expressions

list
a list of AST specifications

Example:

   // spec
   [list: ["foo", "bar"]]

   // corresponding code
   ["foo", "bar"]
 

Map expressions

map
a map of AST specifications

Example:

   // spec
   [map: [foo: "bar", baz: 5]]

   // corresponding code
   [foo: "bar", baz: 5]
 

Variable expressions

variable
the name of the variable

Example:

   // spec
   [variable: "foo"]

   // corresponding code
   foo
 

Return statement

return
the value to be returned

Example:

   //spec
   [return: [variable "foo"]]

   // corresponding code
   return foo
 

Assignment

set
the left hand side of the assignment
to
the value

Example:

   // spec
   [set: [variable: foo]
    to:  5]

   // corresponding code
   foo = 5
 

Property access (via getter/setter)

property
the name of the property
of
the receiver of the accessor call
Default: this

Example:

   // spec
   [property: "bar"
    of:       [variable: "foo"]]

   // corresponding code
   foo.bar
 

Field access on this (not via getter/setter)

field
the name of the field
of
the class of this

Example:

   // spec
   [field: "size"
    of:    String]

   // corresponding code
   this.size
 

Method call

call
the name of method to call
on
the receiver of the method call
with
a list of arguments
Default: []

Example:

   // spec
   [call: "bar",
    on:   [variable: "foo"],
    with: ["baz", 5]]

   // corresponding code
   foo.bar("baz", 5)
 

Static method call

staticCall
the name of method to call
on
the class to call the static method on
with
a list of arguments
Default: []

Example:

   // spec
   [staticCall: "format",
    on:         String,
    with:       ["%d", 5]]

   // corresponding code
   String.format("%d", 5)
 

Constructor call

construct
the class
with
a list of arguments
Default: []

Example:

   // spec
   [construct: String,
    with:      [[variable: "inputBytes"]]]

   // corresponding code
   new String(inputBytes)
 

Method definition

method
the name of the method to define
returnType
the class of the return value.
Default: Object
modifier
modifiers like public or static. This can be a string for a single modifier, an integer (as per java.lang.reflect.Modifier) or a list of thereof.
Default: "public"
arguments
a list of maps with one key/value pair. The key being the name of the argument, the value the argument type.
Default: []
exceptions
a list of exceptions this method may throw
Default: []
body
a list of expressions comprising the body of the method
Default: []

Example:

   // spec
   [method:     "frobnicateFoo"
    modifier:   "static"
    returnType: Boolean,
    arguments:  [[foo: AFoo], [bar: Integer], [baz: Integer]],
    body: [
       [call: "frobnicate",
        on:   [variable: "foo"],
        with  [[variable: "bar"]]],
       [call: "frobnicate",
        on:   [variable: "foo"],
        with: [[variable: "baz"]]],
       true
    ]]

   // corresponding code
   def static Boolean frobnicateFoo(Integer bar, Integer baz) {
       foo.frobnicate bar
       foo.frobnicate baz
       true
   }
 

Constructor definition

constructor
the class
modifier
modifiers like public or static. This can be a string for a single modifier, an integer (as per java.lang.reflect.Modifier) or a list of thereof.
Default: "public"
arguments
a list of maps with one key/value pair. The key being the name of the argument, the value the argument type.
Default: []
exceptions
a list of exceptions this constructor may throw
Default: []
body
a list of expressions comprising the body of the constructor
Default: []

Example:

   // spec
   [construct:  "Foo"
    arguments:  [[bar: Integer]],
    body: [
       [construct: ClassNode.SUPER,
        with       ["foo", [variable: "bar"]]]
    ]]

   // corresponding code
   Foo(Integer bar) {
       super("foo", bar)
   }
 
Authors:
Meikel Brandmeyer


Method Summary
static java.lang.Object toExpression(java.lang.Object spec)

Converts a high-level description of the desired outcome into the actual groovy expression AST structure.

static java.lang.Object toNode(java.lang.Object spec)

Converts a high-level description of the desired outcome into the actual groovy node AST structure.

static java.lang.Object toStatement(java.lang.Object spec)

Converts a high-level description of the desired outcome into the actual groovy statement AST structure.

static java.lang.Object withCategories(groovy.lang.Closure f)

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Method Detail

toExpression

static java.lang.Object toExpression(java.lang.Object spec)
Converts a high-level description of the desired outcome into the actual groovy expression AST structure. toExpression is idempotent.
Parameters:
spec - a high-level AST specification of the expression
Returns:
the corresponding expression AST data structure


toNode

static java.lang.Object toNode(java.lang.Object spec)
Converts a high-level description of the desired outcome into the actual groovy node AST structure. toNode is idempotent.
Parameters:
spec - a high-level AST specification of the node
Returns:
the corresponding node AST data structure


toStatement

static java.lang.Object toStatement(java.lang.Object spec)
Converts a high-level description of the desired outcome into the actual groovy statement AST structure. toStatement is idempotent.
Parameters:
spec - a high-level AST specification of the statement
Returns:
the corresponding statement AST data structure


withCategories

static java.lang.Object withCategories(groovy.lang.Closure f)


 

Groovy Documentation