org.codehaus.groovy.control
Class SourceUnit

java.lang.Object
  extended byorg.codehaus.groovy.control.ProcessingUnit
      extended byorg.codehaus.groovy.control.SourceUnit

public class SourceUnit
extends ProcessingUnit

Provides an anchor for a single source unit (usually a script file) as it passes through the compiler system.

Version:
$Id: SourceUnit.java,v 1.2 2004/07/10 03:31:41 bran Exp $
Author:
Chris Poirier, Bing Ran

Field Summary
protected  ModuleNode ast
           
protected  Reduction cst
           
protected  String name
           
protected  ReaderSource source
           
 
Fields inherited from class org.codehaus.groovy.control.ProcessingUnit
classLoader, configuration, errors, fatal, output, phase, phaseComplete, tolerance, warningLevel, warnings
 
Constructor Summary
SourceUnit(File source, CompilerConfiguration configuration, ClassLoader loader)
          Initializes the SourceUnit from the specified file.
SourceUnit(String name, ReaderSource source, CompilerConfiguration flags, ClassLoader loader)
          Initializes the SourceUnit from existing machinery.
SourceUnit(String name, String source, CompilerConfiguration configuration, ClassLoader loader)
          Initializes the SourceUnit for a string of source.
SourceUnit(URL source, CompilerConfiguration configuration, ClassLoader loader)
          Initializes the SourceUnit from the specified URL.
 
Method Summary
 void addError(String text, CSTNode context)
          Convenience wrapper for addError().
 void addError(SyntaxException error)
          Convenience wrapper for addError().
 void addWarning(int importance, String text, CSTNode context)
          Convenience wrapper for addWarning() that won't create an object unless it is relevant.
 void addWarning(int importance, String text, Object data, CSTNode context)
          Convenience wrapper for addWarning() that won't create an object unless it is relevant.
 void convert()
          Generates an AST from the CST.
static SourceUnit create(String name, String source)
          A convenience routine to create a standalone SourceUnit on a String with defaults for almost everything that is configurable.
static SourceUnit create(String name, String source, int tolerance)
          A convenience routine to create a standalone SourceUnit on a String with defaults for almost everything that is configurable.
static ClassNode createClassNode(String code)
           
static FieldNode createFieldNode(String code)
          Takes a field definition statement and wrap it in class definition.
 MethodNode createMethodNode(String code)
           
static ModuleNode createModule(String code)
          to quickly create a ModuleNode from a piece of Groovy code
 Statement createStatement(String code)
           
 boolean failedWithUnexpectedEOF()
          Convenience routine, primarily for use by the InteractiveShell, that returns true if parse() failed with an unexpected EOF.
 ModuleNode getAST()
          Returns the Abstract Syntax Tree produced during parse()ing and expanded during later phases.
 Reduction getCST()
          Returns the Concrete Syntax Tree produced during parse()ing.
 String getName()
          Returns the name for the SourceUnit.
 String getSample(int line, int column, Janitor janitor)
          Returns a sampling of the source at the specified line and column, of null if it is unavailable.
 void parse()
          Parses the source to a CST.
 
Methods inherited from class org.codehaus.groovy.control.ProcessingUnit
addError, addError, addFatalError, addWarning, completePhase, configure, fail, getClassLoader, getError, getErrorCount, getErrors, getException, getPhase, getPhaseDescription, getSyntaxError, getWarning, getWarningCount, getWarnings, gotoPhase, hasErrors, nextPhase, setClassLoader, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

protected ReaderSource source

name

protected String name

cst

protected Reduction cst

ast

protected ModuleNode ast
Constructor Detail

SourceUnit

public SourceUnit(String name,
                  ReaderSource source,
                  CompilerConfiguration flags,
                  ClassLoader loader)
Initializes the SourceUnit from existing machinery.


SourceUnit

public SourceUnit(File source,
                  CompilerConfiguration configuration,
                  ClassLoader loader)
Initializes the SourceUnit from the specified file.


SourceUnit

public SourceUnit(URL source,
                  CompilerConfiguration configuration,
                  ClassLoader loader)
Initializes the SourceUnit from the specified URL.


SourceUnit

public SourceUnit(String name,
                  String source,
                  CompilerConfiguration configuration,
                  ClassLoader loader)
Initializes the SourceUnit for a string of source.

Method Detail

getName

public String getName()
Returns the name for the SourceUnit.


getCST

public Reduction getCST()
Returns the Concrete Syntax Tree produced during parse()ing.


getAST

public ModuleNode getAST()
Returns the Abstract Syntax Tree produced during parse()ing and expanded during later phases.


failedWithUnexpectedEOF

public boolean failedWithUnexpectedEOF()
Convenience routine, primarily for use by the InteractiveShell, that returns true if parse() failed with an unexpected EOF.


create

public static SourceUnit create(String name,
                                String source)
A convenience routine to create a standalone SourceUnit on a String with defaults for almost everything that is configurable.


create

public static SourceUnit create(String name,
                                String source,
                                int tolerance)
A convenience routine to create a standalone SourceUnit on a String with defaults for almost everything that is configurable.


parse

public void parse()
           throws CompilationFailedException
Parses the source to a CST. You can retrieve it with getCST().

Throws:
CompilationFailedException

convert

public void convert()
             throws CompilationFailedException
Generates an AST from the CST. You can retrieve it with getAST().

Throws:
CompilationFailedException

addWarning

public void addWarning(int importance,
                       String text,
                       CSTNode context)
Convenience wrapper for addWarning() that won't create an object unless it is relevant.


addWarning

public void addWarning(int importance,
                       String text,
                       Object data,
                       CSTNode context)
Convenience wrapper for addWarning() that won't create an object unless it is relevant.


addError

public void addError(SyntaxException error)
              throws CompilationFailedException
Convenience wrapper for addError().

Throws:
CompilationFailedException

addError

public void addError(String text,
                     CSTNode context)
              throws CompilationFailedException
Convenience wrapper for addError().

Throws:
CompilationFailedException

getSample

public String getSample(int line,
                        int column,
                        Janitor janitor)
Returns a sampling of the source at the specified line and column, of null if it is unavailable.


createModule

public static ModuleNode createModule(String code)
                               throws CompilationFailedException
to quickly create a ModuleNode from a piece of Groovy code

Parameters:
code -
Returns:
Throws:
CompilationFailedException

createClassNode

public static ClassNode createClassNode(String code)
                                 throws CompilationFailedException
Throws:
CompilationFailedException

createFieldNode

public static FieldNode createFieldNode(String code)
                                 throws CompilationFailedException
Takes a field definition statement and wrap it in class definition. The FieldNode object representing the field is extracted and returned, Types need to be fully qualified.

Parameters:
code - a naked statement to define a field, such as: String prop = "hello"
Returns:
a FieldNode object.
Throws:
CompilationFailedException

createStatement

public Statement createStatement(String code)
                          throws CompilationFailedException
Throws:
CompilationFailedException

createMethodNode

public MethodNode createMethodNode(String code)
                            throws CompilationFailedException
Throws:
CompilationFailedException


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