Top Description Fields Constructors Methods
org.codehaus.groovy.control

public Class SourceUnit

extends ProcessingUnit
Class Inheritance
Imports
antlr.CharScanner, .MismatchedCharException, .MismatchedTokenException, .NoViableAltException, .NoViableAltForCharException, groovy.lang.GroovyClassLoader, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.ASTNode, .ModuleNode, org.codehaus.groovy.control.io.FileReaderSource, .ReaderSource, .StringReaderSource, .URLReaderSource, org.codehaus.groovy.control.messages.Message, .SimpleMessage, .SyntaxErrorMessage, org.codehaus.groovy.syntax.Reduction, .SyntaxException, org.codehaus.groovy.tools.Utilities, java.io.File, .IOException, .Reader, java.net.URL, java.security.AccessController, .PrivilegedAction

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

Field Summary

Modifier and TypeField and Description
protected ModuleNode
ast

The root of the Abstract Syntax Tree for the source

protected Reduction
cst

A Concrete Syntax Tree of the source

protected String
name

A descriptive name of the source unit.

private ParserPlugin
parserPlugin

The pluggable parser used to generate the AST - we allow pluggability currently as we need to have Classic and JSR support

protected ReaderSource
source

Where we can get Readers for our source unit

Inherited from org.codehaus.groovy.control.ProcessingUnit:
classLoaderconfigurationerrorCollectorphasephaseComplete

Constructor Summary

AccessConstructor and Description
public
SourceUnit(String name, ReaderSource source, CompilerConfiguration flags, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from existing machinery.

public
SourceUnit(File source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from the specified file.

public
SourceUnit(URL source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from the specified URL.

public
SourceUnit(String name, String source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit for a string of source.

Method Summary

Modifier and TypeMethod and Description
public void
addError(SyntaxException
the exception, which should have line and column information
se
)

This method adds a SyntaxException to the error collector.

public void
public void
addException(Exception
the exception that occurred
e
)

This method adds an exception to the error collector.

public void
addFatalError(String
the error message
msg
,
ASTNode
the AST node
node
)

Convenience wrapper for ErrorCollector#addFatalError(org.codehaus.groovy.control.messages.Message).

public ModuleNode
buildAST()

Builds the AST.

public void
convert()

Generates an AST from the CST.

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.

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.

public boolean
failedWithUnexpectedEOF()

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

public ModuleNode
getAST()

Returns the Abstract Syntax Tree produced during convert()ing and expanded during later phases.

public Reduction
getCST()

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

public String
getName()

Returns the name for the SourceUnit.

public String
getSample(int line, int column, Janitor janitor)

Returns a sampling of the source at the specified line and column, or null if it is unavailable.

public ReaderSource
protected boolean
isEofToken(Token token)

Deprecated
public void
parse()

Parses the source to a CST.

public void
Inherited from org.codehaus.groovy.control.ProcessingUnit:
completePhaseconfiguregetClassLoadergetConfigurationgetErrorCollectorgetPhasegetPhaseDescriptiongotoPhaseisPhaseCompletenextPhasesetClassLoadersetConfiguration

Field Detail

astback to summary
protected ModuleNode ast

The root of the Abstract Syntax Tree for the source

cstback to summary
protected Reduction cst

A Concrete Syntax Tree of the source

nameback to summary
protected String name

A descriptive name of the source unit. This name shouldn't be used for controlling the SourceUnit, it is only for error messages and to determine the name of the class for a script.

parserPluginback to summary
private ParserPlugin parserPlugin

The pluggable parser used to generate the AST - we allow pluggability currently as we need to have Classic and JSR support

sourceback to summary
protected ReaderSource source

Where we can get Readers for our source unit

Constructor Detail

SourceUnitback to summary
public SourceUnit(String name, ReaderSource source, CompilerConfiguration flags, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from existing machinery.

SourceUnitback to summary
public SourceUnit(File source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from the specified file.

SourceUnitback to summary
public SourceUnit(URL source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit from the specified URL.

SourceUnitback to summary
public SourceUnit(String name, String source, CompilerConfiguration configuration, GroovyClassLoader loader, ErrorCollector er)

Initializes the SourceUnit for a string of source.

Method Detail

addErrorback to summary
public void addError(SyntaxException se) throws CompilationFailedException

This method adds a SyntaxException to the error collector. The exception should specify the line and column number of the error. This method should be reserved for real errors in the syntax of the SourceUnit. If your error is not in syntax, and is a semantic error, or more general error, then use addException or use the error collector directly by retrieving it with getErrorCollector().

Parameters
se:SyntaxException

the exception, which should have line and column information

Exceptions
CompilationFailedException:
on error
addErrorAndContinueback to summary
public void addErrorAndContinue(SyntaxException se) throws CompilationFailedException
addExceptionback to summary
public void addException(Exception e) throws CompilationFailedException

This method adds an exception to the error collector. The Exception most likely has no line number attached to it. For this reason you should use this method sparingly. Prefer using addError for syntax errors or add an error to the ErrorCollector directly by retrieving it with getErrorCollector().

Parameters
e:Exception

the exception that occurred

Exceptions
CompilationFailedException:
on error
addFatalErrorback to summary
public void addFatalError(String msg, ASTNode node) throws CompilationFailedException

Convenience wrapper for ErrorCollector#addFatalError(org.codehaus.groovy.control.messages.Message).

Parameters
msg:String

the error message

node:ASTNode

the AST node

Exceptions
CompilationFailedException:
on error
Since
3.0.0
buildASTback to summary
public ModuleNode buildAST()

Builds the AST.

convertback to summary
public void convert() throws CompilationFailedException

Generates an AST from the CST. You can retrieve it with getAST().

createback to summary
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.

createback to summary
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.

failedWithUnexpectedEOFback to summary
public boolean failedWithUnexpectedEOF()

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

getASTback to summary
public ModuleNode getAST()

Returns the Abstract Syntax Tree produced during convert()ing and expanded during later phases.

getCSTback to summary
public Reduction getCST()

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

getNameback to summary
public String getName()

Returns the name for the SourceUnit. This name shouldn't be used for controlling the SourceUnit, it is only for error messages

getSampleback to summary
public String getSample(int line, int column, Janitor janitor)

Returns a sampling of the source at the specified line and column, or null if it is unavailable.

getSourceback to summary
public ReaderSource getSource()
isEofTokenback to summary
protected boolean isEofToken(Token token)

Deprecated

Annotations
@Deprecated
parseback to summary
public void parse() throws CompilationFailedException

Parses the source to a CST. You can retrieve it with getCST().

setSourceback to summary
public void setSource(ReaderSource source)