Groovy Syntax Tree

Jeremy Rayner

Groovy Engineer

Member of JSR 241 Expert Group

Senior Developer, FT Interactive Data

Parser History

Java Syntax

Groovy Syntax

Grooviest Syntax

Lexer

JavaLexer

GroovyLexer

java -cp groovy-all-1.0.jar org.codehaus.groovy.antlr.LexerFrame

Parser

 

GroovyRecognizer

Java Syntax Tree

Java Syntax Tree

Groovy Syntax Tree

$ export JAVA_OPTS=-Dantlr.ast=mindmap
$ groovyc Foo.groovy
open
Foo.groovy.mm
in
freemind
( http://freemind.sf.net )

Pretty Printer

accept ast

accept()
no visit
CLASS_DEF ---> accept_FirstChild_v_RestOfTheChildren()
  MODIFIERS:

CLASS_DEF:                                             class
  IDENT: GroovyExample                                 GroovyExample
  EXTENDS_CLAUSE:  
  IMPLEMENTS_CLAUSE:  

  OBJBLOCK ---> accept_v_AllChildren_v()

  OBJBLOCK -> opening visit                            {
    VARIABLE_DEF: x                                    
    VARIABLE_DEF: y                                    ...
    CTOR_IDENT:
    METHOD_DEF: main
  OBJBLOCK -> closing visit                            }

accept class_def

accept_FirstChild_v_RestOfTheChildren()
no visit

accept & visit modifiers

accept_v_FirstChild_v_RestOfTheChildren()
public

visit class_def

accept_FirstChild_v_RestOfTheChildren()
public class

visit identifier

accept_v_FirstChild_v()
public class GroovyExample

visit extends clause

accept_v_FirstChild_v()
public class GroovyExample

visit implements clause

accept_v_FirstChild_v()
public class GroovyExample

opening visit to object block

accept_v_AllChildren_v()
public class GroovyExample {

... and so on...

 
public class GroovyExample {
    ...

closing visit to object block

accept_v_AllChildren_v()
public class GroovyExample {
    ...
}

Tree Walker and Visitor

Tree Walkers

traversals typically run in O(n) time

Visitors

More Visitors

Mutation Testing

Language Conversion - Java2Groovy

Book List






            Groovy Syntax

     Copyright 2007 Jeremy Rayner

     All Rights Reserved

Jeremy Rayner

Groovy Engineer

Member of JSR 241 Expert Group

Senior Developer, FT Interactive Data