org.codehaus.groovy.syntax.parser
Class ASTBuilder

java.lang.Object
  extended byorg.codehaus.groovy.syntax.parser.ASTBuilder

public class ASTBuilder
extends Object

Builds an Abstract Syntax Tree from the Concrete Syntax Tree produced by the Parser. The resulting AST is very preliminary, and must still be validated and massaged before it is ready to be used. build() is the primary entry point.

Author:
James Strachan, Bob McWhirter, Sam Pullara, Chris Poirier

Field Summary
static int[] EXPRESSION_HANDLERS
           
 
Constructor Summary
ASTBuilder(SourceUnit sourceUnit, ClassLoader classLoader)
          Initializes the ASTBuilder.
 
Method Summary
protected  void addPropertyDeclaration(ClassNode classNode, CSTNode reduction)
          A synonym for addPropertyDeclaration( classNode, reduction, 0 ).
protected  void addPropertyDeclaration(ClassNode classNode, CSTNode reduction, int extraModifiers)
          Processes the Reduction produced by Parser.propertyDeclaration().
protected  AssertStatement assertStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.assertStatement().
protected  BreakStatement breakStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.breakStatement().
 ModuleNode build(CSTNode input)
          Builds an AST ModuleNode from a Parser.module() Reduction.
protected  CaseStatement caseStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.switchStatement() for cases.
protected  ClassNode classDeclaration(ClassNode context, CSTNode reduction)
          Processes the Reduction produced by Parser.classDeclaration().
protected  ClassExpression classExpression(CSTNode reduction)
          Converts a CSTNode into a ClassExpression.
protected  ClosureExpression closureExpression(CSTNode reduction)
          Processes the Reduction produced by Parser.closureExpression().
protected  ContinueStatement continueStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.continueStatement().
protected  String dot(String base)
          A synonym for dot( base, "" ).
protected  String dot(String base, String name)
          Returns two names joined by a dot.
protected  DoWhileStatement doWhileStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.doWhileStatement().
protected  void error(String description, CSTNode node)
          Throws a ParserException.
protected  Expression expression(CSTNode reduction)
          Processes the CSTNode produced by Parser.expression().
protected  Expression[] expressions(CSTNode reduction)
          Processes a series of expression to an Expression[].
protected  Statement expressionStatement(CSTNode node)
          Processes any expression that forms a complete statement.
protected  ForStatement forStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.forStatement().
 ClassLoader getClassLoader()
          Returns our class loader (as supplied on construction).
protected  Expression gstringExpression(CSTNode reduction)
          Processes the Reduction produced by Parser.gstring().
protected  String identifier(CSTNode identifier)
          Returns the text of an identifier.
protected  IfStatement ifStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.ifStatement().
protected  void importStatement(ModuleNode module, CSTNode reduction)
          Processes the Reduction produced by Parser.importStatement().
protected  void importStatements(ModuleNode module, CSTNode container)
          Processes the imports Reduction produced by Parser.module().
 Expression infixExpression(CSTNode reduction)
          Processes most infix operators.
protected  ClassNode interfaceDeclaration(ClassNode context, CSTNode reduction)
          Processes the Reduction produced by Parser.interfaceDeclaration().
protected  boolean isDatatype(String name)
          Returns true if the specified name is a known type name.
protected  ListExpression listExpression(CSTNode reduction)
          Processes one of the Reductions produced by Parser.listOrMapExpression().
protected  String makeName(CSTNode root)
          A synonym for makeName( root, "java.lang.Object" ).
protected  String makeName(CSTNode root, String defaultName)
          Converts a CSTNode representation of a type name back into a string.
protected  MapExpression mapExpression(CSTNode reduction)
          Processes the other Reduction produced by Parser.listOrMapExpression().
protected  MethodCallExpression methodCallExpression(CSTNode reduction)
          Processes a SYNTH_METHOD_CALL Reduction produced by Parser.expression().
protected  MethodNode methodDeclaration(ClassNode classNode, CSTNode reduction)
          A synonym for methodDeclaration( classNode, reduction, 0 ).
protected  MethodNode methodDeclaration(ClassNode classNode, CSTNode reduction, int extraModifiers)
          Processes the Reduction produced by Parser.methodDeclaration().
protected  int modifiers(CSTNode list)
          Returns the ASM Constant bits for the specified modifiers.
protected  Expression newExpression(CSTNode reduction)
          Processes the Reduction produced by Parser.newExpression().
 Expression numericExpression(CSTNode reduction, boolean negate)
          Processes numeric literals.
protected  String packageDeclaration(CSTNode reduction)
          Processes the Reduction produced by Parser.packageDeclaration().
protected  Parameter[] parameterDeclarations(CSTNode reduction)
          Processes the Reduction produced by Parser.parameterDeclarationList().
protected  Expression parameterList(CSTNode reduction)
          Processes the Reduction produced by Parser.parameterList().
 Expression prefixExpression(CSTNode reduction)
          Processes most prefix operators.
protected  String resolveName(CSTNode root)
          A synonym for resolveName( root, true ).
protected  String resolveName(CSTNode root, boolean safe)
          Builds a name from a CSTNode, then resolves it.
protected  String resolveName(String name, boolean safe)
          Returns a fully qualified name for any given potential type name.
protected  ReturnStatement returnStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.returnStatement().
 Expression simpleExpression(CSTNode reduction)
          Processes most simple expressions.
protected  Statement statement(CSTNode reduction)
          Processes the Reduction produced by Parser.statement().
protected  BlockStatement statementBlock(CSTNode reduction)
          Processes any statement block.
protected  BlockStatement statementBody(CSTNode reduction)
          Processes the Reduction that results from Parser.statementBody().
protected  BlockStatement statements(CSTNode reduction, int first)
          Processes any series of statements, starting at the specified offset and running to the end of the CSTNode.
protected  SwitchStatement switchStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.switchStatement().
protected  SynchronizedStatement synchronizedStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.synchronizedStatement().
 Expression syntheticExpression(CSTNode reduction)
          Processes most synthetic expressions.
protected  ThrowStatement throwStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.throwStatement().
protected  void topLevelStatement(ModuleNode module, CSTNode reduction)
          Processes the Reduction produced by Parser.topLevelStatement().
protected  TryCatchStatement tryStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.tryStatement().
protected  TupleExpression tupleExpression(CSTNode reduction)
          Processes the Reduction produced by Parser.newArrayInitializer().
protected  void typeBody(ClassNode classNode, CSTNode body, int propertyModifiers, int methodModifiers)
          Processes a type body for classDeclaration() and others.
protected  Type typeExpression(CSTNode reduction)
          Converts an (possibly optional) type expression to a Type.
protected  Expression variableDeclarationExpression(CSTNode reduction)
          Processes the Reduction produced by parsing a typed variable declaration.
protected  VariableExpression variableExpression(CSTNode reduction)
          Converts a (typically IDENTIFIER) CSTNode to a VariableExpression, if valid.
protected  VariableExpression variableExpression(CSTNode reduction, String type)
           
protected  Expression variableOrClassExpression(CSTNode reduction)
          Converts a (typically IDENTIFIER) CSTNode to a ClassExpression, if valid, or a VariableExpression otherwise.
protected  WhileStatement whileStatement(CSTNode reduction)
          Processes the Reduction produced by Parser.whileStatement().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXPRESSION_HANDLERS

public static final int[] EXPRESSION_HANDLERS
Constructor Detail

ASTBuilder

public ASTBuilder(SourceUnit sourceUnit,
                  ClassLoader classLoader)
Initializes the ASTBuilder.

Method Detail

getClassLoader

public ClassLoader getClassLoader()
Returns our class loader (as supplied on construction).


build

public ModuleNode build(CSTNode input)
                 throws ParserException
Builds an AST ModuleNode from a Parser.module() Reduction.

Throws:
ParserException

packageDeclaration

protected String packageDeclaration(CSTNode reduction)
Processes the Reduction produced by Parser.packageDeclaration().


importStatements

protected void importStatements(ModuleNode module,
                                CSTNode container)
Processes the imports Reduction produced by Parser.module().


importStatement

protected void importStatement(ModuleNode module,
                               CSTNode reduction)
Processes the Reduction produced by Parser.importStatement().


topLevelStatement

protected void topLevelStatement(ModuleNode module,
                                 CSTNode reduction)
                          throws ParserException
Processes the Reduction produced by Parser.topLevelStatement().

Throws:
ParserException

classDeclaration

protected ClassNode classDeclaration(ClassNode context,
                                     CSTNode reduction)
                              throws ParserException
Processes the Reduction produced by Parser.classDeclaration().

Throws:
ParserException

typeBody

protected void typeBody(ClassNode classNode,
                        CSTNode body,
                        int propertyModifiers,
                        int methodModifiers)
                 throws ParserException
Processes a type body for classDeclaration() and others.

Throws:
ParserException

addPropertyDeclaration

protected void addPropertyDeclaration(ClassNode classNode,
                                      CSTNode reduction,
                                      int extraModifiers)
                               throws ParserException
Processes the Reduction produced by Parser.propertyDeclaration(). Adds the property to the supplied class.

Throws:
ParserException

addPropertyDeclaration

protected void addPropertyDeclaration(ClassNode classNode,
                                      CSTNode reduction)
                               throws ParserException
A synonym for addPropertyDeclaration( classNode, reduction, 0 ).

Throws:
ParserException

methodDeclaration

protected MethodNode methodDeclaration(ClassNode classNode,
                                       CSTNode reduction,
                                       int extraModifiers)
                                throws ParserException
Processes the Reduction produced by Parser.methodDeclaration(). Adds the method to the supplied class.

Throws:
ParserException

methodDeclaration

protected MethodNode methodDeclaration(ClassNode classNode,
                                       CSTNode reduction)
                                throws ParserException
A synonym for methodDeclaration( classNode, reduction, 0 ).

Throws:
ParserException

parameterDeclarations

protected Parameter[] parameterDeclarations(CSTNode reduction)
                                     throws ParserException
Processes the Reduction produced by Parser.parameterDeclarationList().

Throws:
ParserException

interfaceDeclaration

protected ClassNode interfaceDeclaration(ClassNode context,
                                         CSTNode reduction)
                                  throws ParserException
Processes the Reduction produced by Parser.interfaceDeclaration().

Throws:
ParserException

statementBody

protected BlockStatement statementBody(CSTNode reduction)
                                throws ParserException
Processes the Reduction that results from Parser.statementBody().

Throws:
ParserException

statements

protected BlockStatement statements(CSTNode reduction,
                                    int first)
                             throws ParserException
Processes any series of statements, starting at the specified offset and running to the end of the CSTNode.

Throws:
ParserException

statementBlock

protected BlockStatement statementBlock(CSTNode reduction)
                                 throws ParserException
Processes any statement block.

Throws:
ParserException

statement

protected Statement statement(CSTNode reduction)
                       throws ParserException
Processes the Reduction produced by Parser.statement().

Throws:
ParserException

assertStatement

protected AssertStatement assertStatement(CSTNode reduction)
                                   throws ParserException
Processes the Reduction produced by Parser.assertStatement().

Throws:
ParserException

breakStatement

protected BreakStatement breakStatement(CSTNode reduction)
                                 throws ParserException
Processes the Reduction produced by Parser.breakStatement().

Throws:
ParserException

continueStatement

protected ContinueStatement continueStatement(CSTNode reduction)
                                       throws ParserException
Processes the Reduction produced by Parser.continueStatement().

Throws:
ParserException

ifStatement

protected IfStatement ifStatement(CSTNode reduction)
                           throws ParserException
Processes the Reduction produced by Parser.ifStatement().

Throws:
ParserException

returnStatement

protected ReturnStatement returnStatement(CSTNode reduction)
                                   throws ParserException
Processes the Reduction produced by Parser.returnStatement().

Throws:
ParserException

switchStatement

protected SwitchStatement switchStatement(CSTNode reduction)
                                   throws ParserException
Processes the Reduction produced by Parser.switchStatement().

Throws:
ParserException

caseStatement

protected CaseStatement caseStatement(CSTNode reduction)
                               throws ParserException
Processes the Reduction produced by Parser.switchStatement() for cases.

Throws:
ParserException

synchronizedStatement

protected SynchronizedStatement synchronizedStatement(CSTNode reduction)
                                               throws ParserException
Processes the Reduction produced by Parser.synchronizedStatement().

Throws:
ParserException

throwStatement

protected ThrowStatement throwStatement(CSTNode reduction)
                                 throws ParserException
Processes the Reduction produced by Parser.throwStatement().

Throws:
ParserException

tryStatement

protected TryCatchStatement tryStatement(CSTNode reduction)
                                  throws ParserException
Processes the Reduction produced by Parser.tryStatement().

Throws:
ParserException

forStatement

protected ForStatement forStatement(CSTNode reduction)
                             throws ParserException
Processes the Reduction produced by Parser.forStatement().

Throws:
ParserException

doWhileStatement

protected DoWhileStatement doWhileStatement(CSTNode reduction)
                                     throws ParserException
Processes the Reduction produced by Parser.doWhileStatement().

Throws:
ParserException

whileStatement

protected WhileStatement whileStatement(CSTNode reduction)
                                 throws ParserException
Processes the Reduction produced by Parser.whileStatement().

Throws:
ParserException

expressionStatement

protected Statement expressionStatement(CSTNode node)
                                 throws ParserException
Processes any expression that forms a complete statement.

Throws:
ParserException

expressions

protected Expression[] expressions(CSTNode reduction)
                            throws ParserException
Processes a series of expression to an Expression[].

Throws:
ParserException

expression

protected Expression expression(CSTNode reduction)
                         throws ParserException
Processes the CSTNode produced by Parser.expression().

Throws:
ParserException

infixExpression

public Expression infixExpression(CSTNode reduction)
                           throws ParserException
Processes most infix operators.

Throws:
ParserException

prefixExpression

public Expression prefixExpression(CSTNode reduction)
                            throws ParserException
Processes most prefix operators.

Throws:
ParserException

simpleExpression

public Expression simpleExpression(CSTNode reduction)
                            throws ParserException
Processes most simple expressions.

Throws:
ParserException

numericExpression

public Expression numericExpression(CSTNode reduction,
                                    boolean negate)
                             throws ParserException
Processes numeric literals.

Throws:
ParserException

syntheticExpression

public Expression syntheticExpression(CSTNode reduction)
                               throws ParserException
Processes most synthetic expressions.

Throws:
ParserException

variableOrClassExpression

protected Expression variableOrClassExpression(CSTNode reduction)
                                        throws ParserException
Converts a (typically IDENTIFIER) CSTNode to a ClassExpression, if valid, or a VariableExpression otherwise.

Throws:
ParserException

classExpression

protected ClassExpression classExpression(CSTNode reduction)
                                   throws ParserException
Converts a CSTNode into a ClassExpression.

Throws:
ParserException

variableExpression

protected VariableExpression variableExpression(CSTNode reduction)
Converts a (typically IDENTIFIER) CSTNode to a VariableExpression, if valid.


variableExpression

protected VariableExpression variableExpression(CSTNode reduction,
                                                String type)

typeExpression

protected Type typeExpression(CSTNode reduction)
Converts an (possibly optional) type expression to a Type.


variableDeclarationExpression

protected Expression variableDeclarationExpression(CSTNode reduction)
                                            throws ParserException
Processes the Reduction produced by parsing a typed variable declaration.

Throws:
ParserException

methodCallExpression

protected MethodCallExpression methodCallExpression(CSTNode reduction)
                                             throws ParserException
Processes a SYNTH_METHOD_CALL Reduction produced by Parser.expression().

Throws:
ParserException

closureExpression

protected ClosureExpression closureExpression(CSTNode reduction)
                                       throws ParserException
Processes the Reduction produced by Parser.closureExpression().

Throws:
ParserException

parameterList

protected Expression parameterList(CSTNode reduction)
                            throws ParserException
Processes the Reduction produced by Parser.parameterList().

Throws:
ParserException

newExpression

protected Expression newExpression(CSTNode reduction)
                            throws ParserException
Processes the Reduction produced by Parser.newExpression().

Throws:
ParserException

tupleExpression

protected TupleExpression tupleExpression(CSTNode reduction)
                                   throws ParserException
Processes the Reduction produced by Parser.newArrayInitializer().

Throws:
ParserException

gstringExpression

protected Expression gstringExpression(CSTNode reduction)
                                throws ParserException
Processes the Reduction produced by Parser.gstring().

Throws:
ParserException

listExpression

protected ListExpression listExpression(CSTNode reduction)
                                 throws ParserException
Processes one of the Reductions produced by Parser.listOrMapExpression().

Throws:
ParserException

mapExpression

protected MapExpression mapExpression(CSTNode reduction)
                               throws ParserException
Processes the other Reduction produced by Parser.listOrMapExpression().

Throws:
ParserException

makeName

protected String makeName(CSTNode root,
                          String defaultName)
Converts a CSTNode representation of a type name back into a string.


makeName

protected String makeName(CSTNode root)
A synonym for makeName( root, "java.lang.Object" ).


identifier

protected String identifier(CSTNode identifier)
Returns the text of an identifier.


resolveName

protected String resolveName(String name,
                             boolean safe)
Returns a fully qualified name for any given potential type name. Returns null if no qualified name could be determined.


resolveName

protected String resolveName(CSTNode root,
                             boolean safe)
Builds a name from a CSTNode, then resolves it. Returns the resolved name if available, or null, unless safe is set, in which case the built name is returned instead of null.


resolveName

protected String resolveName(CSTNode root)
A synonym for resolveName( root, true ).


isDatatype

protected boolean isDatatype(String name)
Returns true if the specified name is a known type name.


dot

protected String dot(String base,
                     String name)
Returns two names joined by a dot. If the base name is empty, returns the name unchanged.


dot

protected String dot(String base)
A synonym for dot( base, "" ).


modifiers

protected int modifiers(CSTNode list)
Returns the ASM Constant bits for the specified modifiers.


error

protected void error(String description,
                     CSTNode node)
              throws ParserException
Throws a ParserException.

Throws:
ParserException


Copyright © 2003-2004 The Codehaus. All Rights Reserved.