Class SourceUnit

java.lang.Object
org.codehaus.groovy.control.ProcessingUnit
org.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.
  • Field Details

    • source

      protected ReaderSource source
      Where we can get Readers for our source unit
    • name

      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.
    • cst

      protected Reduction cst
      A Concrete Syntax Tree of the source
    • ast

      protected ModuleNode ast
      The root of the Abstract Syntax Tree for the source
  • Constructor Details

  • Method Details

    • getName

      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
    • getCST

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

      public ModuleNode getAST()
      Returns the Abstract Syntax Tree produced during convert()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
    • buildAST

      public ModuleNode buildAST()
      Builds the AST.
    • getSample

      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.
    • addException

      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 - the exception that occurred
      Throws:
      CompilationFailedException - on error
    • addError

      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 - the exception, which should have line and column information
      Throws:
      CompilationFailedException - on error
    • addFatalError

      public void addFatalError(String text, ASTNode node) throws CompilationFailedException
      Parameters:
      text - the error message
      node - for locating the offending code
      Throws:
      CompilationFailedException - on error
      Since:
      3.0.0
    • addWarning

      public void addWarning(String text, ASTNode node)
      Since:
      4.0.7
    • addErrorAndContinue

      public void addErrorAndContinue(SyntaxException se)
    • getSource

      public ReaderSource getSource()
    • setSource

      public void setSource(ReaderSource source)