org.codehaus.groovy.syntax.parser
Class Parser

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

public class Parser
extends java.lang.Object

Reads the source text and produces a hierarchy of Concrete Syntax Trees (CSTs). Exceptions are collected during processing, and parsing will continue for while possible, in order to report as many problems as possible. compilationUnit() is the primary entry point.

Author:
Bob McWhirter, James Strachan, Chris Poirier

Field Summary
protected static int[] ARRAY_ITEM_TERMINATORS
           
protected static int[] EXTENDS_CLAUSE_TERMINATORS
          End markers for use by classDeclaration(), interfaceDeclaration(), and methodDeclaration().
protected static int[] GENERAL_CLAUSE_TERMINATOR
           
protected static int[] IMPLEMENTS_CLAUSE_TERMINATORS
           
static int[] OPTIONAL_DATATYPE_FOLLOWER
           
protected static int[] PARAMETER_TERMINATORS
           
protected static int[] STATEMENT_TERMINATORS
           
static int[] SWITCH_STATEMENT_BLOCK_TERMINATORS
           
protected static int[] THROWS_CLAUSE_TERMINATORS
           
protected static int[] TYPE_DEFINERS
          Various generally useful type sets.
 
Constructor Summary
Parser(TokenStream tokenStream)
          Sets the Parser to process a TokenStream.
Parser(TokenStream tokenStream, ExceptionCollector collector)
          Sets the Parser to process a TokenStream.
 
Method Summary
protected  CSTNode additiveExpression()
           
protected  CSTNode argumentList()
           
protected  CSTNode assertStatement()
           
protected  CSTNode assignmentExpression()
           
 CSTNode bodyStatement()
          A synonym for typeBodyStatement( true, true, false ).
protected  CSTNode breakStatement()
          Processes a break statement.
 CSTNode classDeclaration(CSTNode modifiers)
          Processes a class declaration.
protected  CSTNode closureExpression()
           
protected  CSTNode closureExpression(boolean pipeRequired)
           
 CSTNode compilationUnit()
          The primary file-level parsing entry point.
protected  void consume_bare(CSTNode root, int type)
          Analogous to consume(root, type), exception consumes with consume_bare.
protected  Token consume_bare(int type)
          Consumes (and returns) the next token if it is of the specified type, or throws UnexpectedTokenException.
protected  void consume(CSTNode root, int type)
          Adds a CSTNode of the result of consume(type) as a child of root.
protected  Token consume(int type)
          Consumes (and returns) the next token if it is of the specified type, or throws an UnexpectedTokenException.
protected  void consumeUntil_bare(int type)
          Consumes tokens until one of the specified type is consumed.
protected  void consumeUntil(int type)
          Consumes tokens until one of the specified type is consumed.
protected  CSTNode continueStatement()
          Processes a continue statement.
static Parser create(java.lang.String text, int errorTolerance)
          Convenience routine to wrap a String in a Parser.
protected  CSTNode datatype()
          A synonym for datatype( true ).
protected  CSTNode datatype(boolean allowVoid)
          Processes a datatype specification.
protected  CSTNode doubleQuotedString()
           
protected  CSTNode doWhileStatement()
          Processes a do ...
 void endOfStatement()
          A synonym for endOfStatement( true ).
 void endOfStatement(boolean allowRightCurlyBrace)
          Eats a required end-of-statement (semicolon or newline) from the stream.
protected  CSTNode equalityExpression()
           
protected  CSTNode expression()
           
protected  CSTNode forStatement()
          Processes a for statement.
 TokenStream getTokenStream()
          Returns the TokenStream being parsed.
protected  CSTNode ifStatement()
          Processes an if statement.
 CSTNode importStatement()
          Processes an import statement.
 CSTNode interfaceDeclaration(CSTNode modifiers)
          Processes a interface declaration.
protected  Token la_bare()
          Returns (without consuming) the next token in the underlying token stream (newlines included).
protected  Token la_bare(int k)
          Returns (without consuming any tokens) the next kth token from the underlying token stream (newlines included).
protected  Token la()
          Returns (without consuming) the next non-newline token in the underlying token stream.
protected  Token la(int k)
          Returns (without consuming any tokens) the next kth non-newline token from the underlying token stream.
protected  CSTNode listExpression(CSTNode entry)
           
protected  CSTNode listOrMapExpression()
           
protected  CSTNode logicalAndExpression()
           
protected  CSTNode logicalOrExpression()
           
protected  boolean lookAheadForMethodCall()
           
protected  int lt_bare()
          Returns the type of the la_bare() token, or -1.
protected  int lt_bare(int k)
          Returns the type of the la_bare(k) token, or -1.
protected  int lt()
          Returns the type of the la() token, or -1.
protected  int lt(int k)
          Returns the type of the la(k) token, or -1.
protected  CSTNode mapExpression(CSTNode key)
           
protected  CSTNode methodCallOrPropertyExpression(CSTNode expr)
           
protected  CSTNode methodCallWithoutParenthesis(CSTNode expr, CSTNode identifier)
           
 CSTNode methodDeclaration(CSTNode modifiers, CSTNode type, CSTNode identifier, boolean emptyOnly)
          Processes a class/interface method.
 CSTNode modifierList(boolean allowStatic, boolean allowAbstract)
          Processes the modifiers list that can appear on top- and class-level method and class-level variable names (public, private, abstract, etc.).
protected  CSTNode multiplicativeExpression()
           
protected  CSTNode nameDeclaration(boolean useBare)
          Processes a name that is valid for declarations.
protected  CSTNode namedParameterList(int endOfListDemarc)
           
protected  CSTNode nameReference(boolean useBare)
          Processes a reference to a declared name.
protected  CSTNode newExpression()
           
protected  CSTNode optionalDatatype(boolean useBare, boolean allowVoid)
          Processes an optional data type marker (for a parameter, method return type, etc.).
 void optionalNewlines()
          Eats any optional newlines.
 CSTNode packageDeclaration()
          Processes a package declaration.
protected  CSTNode parameterDeclaration()
          Processes a single parameter declaration, which can occur on methods and closures.
protected  CSTNode parameterDeclarationList()
          Processes a parameter declaration list, which can occur on methods and closures.
protected  CSTNode parameterList(int endOfListDemarc)
           
protected  CSTNode parentheticalExpression()
           
protected  CSTNode postfixExpression()
           
protected  CSTNode primaryExpression()
           
 CSTNode propertyDeclaration(CSTNode modifiers, CSTNode type, CSTNode identifier)
          Processes a class/interface property, including the optional initialization clause.
protected  CSTNode rangeExpression()
           
 void recover()
          A synonym for recover( STATEMENT_TERMNINATORS, true ).
 void recover(int[] safe)
          A synonym for recover( safe, false ).
 void recover(int[] safe, boolean useBare)
          Attempts to recover from an error by discarding input until a known token is found.
protected  CSTNode regexPattern()
           
protected  CSTNode relationalExpression()
           
protected  Token require(Token token)
          Ensures that a Token is not null.
protected  CSTNode returnStatement()
          Processes a return statement.
protected  CSTNode rootNode_bare(int type)
          Analagous to rootNode(type), except consumes with consume_bare.
protected  CSTNode rootNode_bare(int type, CSTNode child)
          Analagous to rootNode(type, child), except consumes with consume_bare().
protected  CSTNode rootNode(int type)
          Returns a new CSTNode that holds the result of consume(type).
protected  CSTNode rootNode(int type, CSTNode child)
          Identical to rootNode(type), but adds child as a child of the newly created node.
protected  CSTNode scalarDatatype(boolean allowVoid)
          Processes a scalar datatype specification.
protected  CSTNode statement()
          Synonym for statement( false ).
protected  CSTNode statement(boolean allowUnlabelledBlocks)
          Processes a single statement.
protected  CSTNode statementBody(boolean requireBraces)
          Processes the body of a complex statement (like "if", "for", etc.).
protected  CSTNode statementsUntilRightCurly(CSTNode root)
          Reads statements until a "}" is met.
protected  CSTNode subscriptExpression(CSTNode expr)
           
protected  CSTNode sugaryMethodCallExpression(CSTNode expr, CSTNode identifier, CSTNode dotExpr)
           
protected  CSTNode switchStatement()
          Processes a switch statement.
protected  CSTNode synchronizedStatement()
          Processes a synchronized statement.
protected  CSTNode ternaryExpression()
           
protected  CSTNode thisExpression()
           
protected  void throwExpected(int[] expectedTypes)
          Throws an UnexpectedTokenException.
protected  CSTNode throwStatement()
          Processes a throw statement.
 CSTNode topLevelStatement()
          Processes a top level statement (classes, interfaces, unattached methods, and unattached code).
protected  CSTNode tryParseMethodCallWithoutParenthesis(CSTNode expr, CSTNode identifier)
           
protected  CSTNode tryStatement()
          Processes an try statement.
 CSTNode typeBody(boolean allowStatic, boolean allowAbstract, boolean requireAbstract)
          Processes the body of an interface or class.
 CSTNode typeBodyStatement(boolean allowStatic, boolean allowAbstract, boolean requireAbstract)
          Processes a single entry in the the body of an interface or class.
 CSTNode typeDeclaration()
          A synomym for topLevelStatement().
 CSTNode typeList(int declarator, int[] until, boolean optional, int limit)
          Processes a type list, like the ones that occur after "extends" or implements.
protected  CSTNode unaryExpression()
           
protected  CSTNode whileStatement()
          Processes a while statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_DEFINERS

protected static final int[] TYPE_DEFINERS
Various generally useful type sets.


STATEMENT_TERMINATORS

protected static final int[] STATEMENT_TERMINATORS

GENERAL_CLAUSE_TERMINATOR

protected static final int[] GENERAL_CLAUSE_TERMINATOR

PARAMETER_TERMINATORS

protected static final int[] PARAMETER_TERMINATORS

ARRAY_ITEM_TERMINATORS

protected static final int[] ARRAY_ITEM_TERMINATORS

EXTENDS_CLAUSE_TERMINATORS

protected static final int[] EXTENDS_CLAUSE_TERMINATORS
End markers for use by classDeclaration(), interfaceDeclaration(), and methodDeclaration().


IMPLEMENTS_CLAUSE_TERMINATORS

protected static final int[] IMPLEMENTS_CLAUSE_TERMINATORS

THROWS_CLAUSE_TERMINATORS

protected static final int[] THROWS_CLAUSE_TERMINATORS

OPTIONAL_DATATYPE_FOLLOWER

public static final int[] OPTIONAL_DATATYPE_FOLLOWER

SWITCH_STATEMENT_BLOCK_TERMINATORS

public static final int[] SWITCH_STATEMENT_BLOCK_TERMINATORS
Constructor Detail

Parser

public Parser(TokenStream tokenStream)
Sets the Parser to process a TokenStream.


Parser

public Parser(TokenStream tokenStream,
              ExceptionCollector collector)
Sets the Parser to process a TokenStream. Exceptions will be collected in the specified collector.

Method Detail

create

public static Parser create(java.lang.String text,
                            int errorTolerance)
Convenience routine to wrap a String in a Parser.


getTokenStream

public TokenStream getTokenStream()
Returns the TokenStream being parsed.


optionalNewlines

public void optionalNewlines()
                      throws ReadException,
                             SyntaxException
Eats any optional newlines.

Throws:
ReadException
SyntaxException

endOfStatement

public void endOfStatement(boolean allowRightCurlyBrace)
                    throws ReadException,
                           SyntaxException
Eats a required end-of-statement (semicolon or newline) from the stream. Throws an UnexpectedTokenException if anything else is found.

Throws:
ReadException
SyntaxException

endOfStatement

public void endOfStatement()
                    throws ReadException,
                           SyntaxException
A synonym for endOfStatement( true ).

Throws:
ReadException
SyntaxException

recover

public void recover(int[] safe,
                    boolean useBare)
             throws ReadException,
                    SyntaxException
Attempts to recover from an error by discarding input until a known token is found. It further guarantees that /at least/ one token will be eaten.

Throws:
ReadException
SyntaxException

recover

public void recover(int[] safe)
             throws ReadException,
                    SyntaxException
A synonym for recover( safe, false ).

Throws:
ReadException
SyntaxException

recover

public void recover()
             throws ReadException,
                    SyntaxException
A synonym for recover( STATEMENT_TERMNINATORS, true ).

Throws:
ReadException
SyntaxException

compilationUnit

public CSTNode compilationUnit()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector,
                               ExceptionCollector
The primary file-level parsing entry point. The returned CST represents the content in a single class file. Collects most exceptions and attempts to continue.

Grammar:

     compilationUnit = [packageStatement]
                       (usingStatement)*
                       (topLevelStatement)*
                       
  

CST:

     compilationUnit = {  package imports (topLevelStatement)* }
     
     package           see packageDeclaration()
     imports           see importStatement()
     topLevelStatement see topLevelStatement()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

packageDeclaration

public CSTNode packageDeclaration()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Processes a package declaration. Called by compilationUnit().

Grammar:

     packageDeclaration = "package"  ("." )* 
  

CST:

     package = { "package" classes }
     classes = { "." classes class } | class
     class   = {  }
  

Throws:
ReadException
SyntaxException
ExceptionCollector

importStatement

public CSTNode importStatement()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
Processes an import statement. Called by compilationUnit().

Grammar:

     importStatement = "import" (all|specific) 
 
     all      = package "." (package ".")* "*"

     specific = (package "." (package ".")*)? classes
     classes  = class ["as" alias] ("," class ["as" alias])*

     package  = 
     class    = 
     alias    = 
  

CST:

     import   = { "import" (package|{}) ({"*"} | clause*) }

     package  = { "." package element } | element
     element  = {  }

     clause   = {  alias }
     alias    = {  } | {}
  

Throws:
ReadException
SyntaxException
ExceptionCollector

topLevelStatement

public CSTNode topLevelStatement()
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a top level statement (classes, interfaces, unattached methods, and unattached code). Called by compilationUnit().

Grammar:

     topLevelStatement
       = methodDeclaration 
       | typeDeclaration
       | statement
     
     typeDeclaration = classDefinition | interfaceDeclaration
  

Recognition:

     "def"                    => methodDeclaration
     modifierList "class"     => classDeclaration
     modifierList "interface" => interfaceDeclaration
     modifierList             => 
     *                        => statement
  

CST:

     see methodDeclaration()
     see classDeclaration()
     see interfaceDeclaration()
     see statement()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

typeDeclaration

public CSTNode typeDeclaration()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
A synomym for topLevelStatement().

Throws:
ReadException
SyntaxException
ExceptionCollector

modifierList

public CSTNode modifierList(boolean allowStatic,
                            boolean allowAbstract)
                     throws ReadException,
                            SyntaxException,
                            ExceptionCollector
Processes the modifiers list that can appear on top- and class-level method and class-level variable names (public, private, abstract, etc.).

Grammar:

     modifierList = *
  

CST:

     modifiers = {  {}* }
  

Throws:
ReadException
SyntaxException
ExceptionCollector

classDeclaration

public CSTNode classDeclaration(CSTNode modifiers)
                         throws ReadException,
                                SyntaxException,
                                ExceptionCollector
Processes a class declaration. Caller has already processed the declaration modifiers, and passes them in.

Grammar:

     classDeclaration = * "class" 
                        ["extends" datatype]
                        ["implements" datatype (, datatype)*]
                        typeBody
  

CST:

     class      = { "class" modifiers {} extends implements body }
     extends    = { "extends"    datatype  } | {}
     implements = { "implements" datatype* } | {}

     modifiers see modifierList()
     datatype  see datatype()
     body      see typeBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

interfaceDeclaration

public CSTNode interfaceDeclaration(CSTNode modifiers)
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Processes a interface declaration. Caller has already processed the declaration modifiers, and passes them in.

Grammar:

     interfaceDeclaration = * "interface" 
                            ["extends" typeList]
                            typeBody
  

CST:

     class      = { "interface" modifiers {} {} extends body }
     extends    = { "extends" datatype* } | {}

     modifiers see modifierList()
     datatype  see datatype()
     body      see typeBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

typeList

public CSTNode typeList(int declarator,
                        int[] until,
                        boolean optional,
                        int limit)
                 throws ReadException,
                        SyntaxException,
                        ExceptionCollector
Processes a type list, like the ones that occur after "extends" or implements. If the list is optional, the returned CSTNode will be empty.

Grammar:

     typeList = datatype (, datatype)*
  

CST:

     typeList = {  datatype+ } | {}

     datatype see datatype()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

typeBody

public CSTNode typeBody(boolean allowStatic,
                        boolean allowAbstract,
                        boolean requireAbstract)
                 throws ReadException,
                        SyntaxException,
                        ExceptionCollector
Processes the body of an interface or class.

Grammar:

     typeBody = "{" typeBodyStatement* "}"
  

CST:

     body = {  typeBodyStatement* }

     typeBodyStatement see typeBodyStatement()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

typeBodyStatement

public CSTNode typeBodyStatement(boolean allowStatic,
                                 boolean allowAbstract,
                                 boolean requireAbstract)
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a single entry in the the body of an interface or class. Valid objects are constructors, methods, properties, static initializers, and inner classes or interfaces.

Grammar:

     typeBodyStatement
       = staticInitializer 
       | classDeclaration
       | interfaceDeclaration
       | propertyDeclaration
       | methodDeclaration

     staticInitializer = ("static" "{" statement* "}")
  

Recognition:

     "static" "{"             => staticInitializer
     modifierList "class"     => classDeclaration
     modifierList "interface" => interfaceDeclaration
     modifierList ["property"] optionalDatatype identifier "("               => methodDeclaration
     modifierList ["property"] optionalDatatype identifier ("="|";"|"\n"|"}" => propertyDeclaration
     *                        => 
  

CST:

     see classDeclaration()
     see interfaceDeclaration()
     see methodDeclaration()
     see propertyDeclaration()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

bodyStatement

public CSTNode bodyStatement()
                      throws ReadException,
                             SyntaxException,
                             ExceptionCollector
A synonym for typeBodyStatement( true, true, false ).

Throws:
ReadException
SyntaxException
ExceptionCollector

nameDeclaration

protected CSTNode nameDeclaration(boolean useBare)
                           throws ReadException,
                                  SyntaxException
Processes a name that is valid for declarations. Newlines can be made significant, if required for disambiguation.

Grammar:

     nameDeclaration = 
  

CST:

     name = {  }
  

Throws:
ReadException
SyntaxException

nameReference

protected CSTNode nameReference(boolean useBare)
                         throws ReadException,
                                SyntaxException
Processes a reference to a declared name. Newlines can be made significant, if required for disambiguation.

Grammar:

     nameReference =  | 
  

CST:

     name = {  }
  

Throws:
ReadException
SyntaxException

optionalDatatype

protected CSTNode optionalDatatype(boolean useBare,
                                   boolean allowVoid)
                            throws ReadException,
                                   SyntaxException,
                                   ExceptionCollector
Processes an optional data type marker (for a parameter, method return type, etc.). Newlines can be made significant, if required for disambiguation.

Grammar:

     optionalDatatype = datatype? (?=)
  

CST:

     result = datatype | {}

     see datatype()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

propertyDeclaration

public CSTNode propertyDeclaration(CSTNode modifiers,
                                   CSTNode type,
                                   CSTNode identifier)
                            throws ReadException,
                                   SyntaxException,
                                   ExceptionCollector
Processes a class/interface property, including the optional initialization clause. The modifiers, type, and identifier have already been identified by the caller, and are passed in.

Grammar:

     propertyDeclaration = (modifierList optionalDatatype identifier ["=" expression])
  

CST:

     property = { "property" modifierList methodIdentifier methodReturnType expression? }
     
     see modifierList()
     see methodIdentifier()
     see methodReturnType()
     see expression()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

methodDeclaration

public CSTNode methodDeclaration(CSTNode modifiers,
                                 CSTNode type,
                                 CSTNode identifier,
                                 boolean emptyOnly)
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a class/interface method. The modifiers, type, and identifier have already been identified by the caller, and are passed in. If emptyOnly is set, no method body will be allowed.

Grammar:

     methodDeclaration = modifierList optionalDatatype identifier 
                         "(" parameterDeclarationList ")" 
                         [ "throws" typeList ]
                         ( statementBody |  )?
  

CST:

     property = { "method" modifierList methodIdentifier methodReturnType 
                   parameterDeclarationList statementBody throwsClause }

     throwsClause = { "throws" datatype+ } | {}
     
     see modifierList()
     see methodIdentifier()
     see methodReturnType()
     see parameterDeclarationList()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

parameterDeclarationList

protected CSTNode parameterDeclarationList()
                                    throws ReadException,
                                           SyntaxException,
                                           ExceptionCollector
Processes a parameter declaration list, which can occur on methods and closures. It loops as long as it finds a comma as the next token.

Grammar:

     parameterDeclarationList 
        = (parameterDeclaration ("," parameterDeclaration)* ("," parameterDeclaration "=" expression)* )?
        | (parameterDeclaration "=" expression ("," parameterDeclaration "=" expression)* )?
  

CST:

     parameters = {  parameter* }
     parameter  = {  datatype nameDeclaration default }
     default    = { } | expression
  

Throws:
ReadException
SyntaxException
ExceptionCollector

parameterDeclaration

protected CSTNode parameterDeclaration()
                                throws ReadException,
                                       SyntaxException,
                                       ExceptionCollector
Processes a single parameter declaration, which can occur on methods and closures.

Grammar:

     parameterDeclaration = optionalDatatype nameDeclaration
  

CST:

     parameter = {  datatype nameDeclaration }
     
     see datatype()
     see nameDeclaration()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

datatype

protected CSTNode datatype(boolean allowVoid)
                    throws ReadException,
                           SyntaxException,
                           ExceptionCollector
Processes a datatype specification. For reasons of disambiguation, the array marker ([]) must never be on a separate line from the base datatype.

Grammar:

     datatype = scalarDatatype ( "[" "]" )*
     
     scalarDatatype 
       = ( ("." )*)
       | "void" | "int" | ...
  

CST:

     datatype  = { "[" datatype } | scalar
     scalar    = typename | primitive
     typename  = { "." typename name } | name 
     name      = {  }
     primitive = { "void" } | { "int" } | ...
  

Throws:
ReadException
SyntaxException
ExceptionCollector

datatype

protected CSTNode datatype()
                    throws ReadException,
                           SyntaxException,
                           ExceptionCollector
A synonym for datatype( true ).

Throws:
ReadException
SyntaxException
ExceptionCollector

scalarDatatype

protected CSTNode scalarDatatype(boolean allowVoid)
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a scalar datatype specification.

Grammar:

     scalarDatatype 
       = ( ("." )*)
       | "void" | "int" | ...
  

CST:

     scalar    = typename | primitive
     typename  = { "." typename name } | name 
     name      = {  }
     primitive = { "void" } | { "int" } | ...
  

Throws:
ReadException
SyntaxException
ExceptionCollector

statementBody

protected CSTNode statementBody(boolean requireBraces)
                         throws ReadException,
                                SyntaxException,
                                ExceptionCollector
Processes the body of a complex statement (like "if", "for", etc.). Set requireBraces if the body must not be just a single statement.

Grammar:

     statementBody = ("{" statement* "}")
                   | statement
  

CST:

     complex = { "{" statement* }
     simple  = statement

     see statement()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

statementsUntilRightCurly

protected CSTNode statementsUntilRightCurly(CSTNode root)
                                     throws ReadException,
                                            SyntaxException,
                                            ExceptionCollector
Reads statements until a "}" is met. Adds each statement as a child of the root node, which you must supply.

Grammar:

     statementsUntilRightCurly = statement* (?= "}")
  

CST:

     statements added as children of supplied root node
  

Throws:
ReadException
SyntaxException
ExceptionCollector

statement

protected CSTNode statement(boolean allowUnlabelledBlocks)
                     throws ReadException,
                            SyntaxException,
                            ExceptionCollector
Processes a single statement. Statements include: loop constructs, branch constructs, flow control constructs, exception constructs, expressions of a variety of types, and pretty much anything you can put inside a method.

Grammar:

     statement     = (label ":")? bareStatement
     bareStatement = (emptyStatement|pureStatement|blockStatement)

     pureStatement = forStatement 
                   | whileStatement
                   | doStatement
                   | continueStatement
                   | breakStatement
                   | ifStatement
                   | tryStatement
                   | throwStatement
                   | synchronizedStatement
                   | switchStatement
                   | returnStatement
                   | assertStatement
                   | expression 

     label          = 
     blockStatement = "{" statement* "}" 
     emptyStatement = ";"
  

Recognition:

     ";"       => emptyStatement
      => Statement
     "{"       => expression, then:
                    if it is a closureExpression and has no parameters => blockStatement
     *         => expression
  

CST:

     labelled       = { ":"  bareStatement }
     bareStatement  = emptyStatement | blockStatement | pureStatement
     emptyStatement = { "{" }
     blockStatement = { "{" statement* }

     see forStatement()
     see whileStatement()
     see doStatement()
     see continueStatement()
     see breakStatement()
     see ifStatement()
     see tryStatement()
     see throwStatement()
     see synchronizedStatement()
     see switchStatement()
     see returnStatement()
     see assertStatement()
     see expression()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

statement

protected CSTNode statement()
                     throws ReadException,
                            SyntaxException,
                            ExceptionCollector
Synonym for statement( false ).

Throws:
ReadException
SyntaxException
ExceptionCollector

switchStatement

protected CSTNode switchStatement()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Processes a switch statement.

Grammar:

     switchStatment = "switch" "(" expression ")" "{" switchBody "}"

     switchBody = caseSet*
     caseSet = (("case" expression ":")+ | ("default" ":")) statement+
  

CST:

     switch = { "switch" expression case* }
     case   = { "case" expression statement* } 
            | { "default" statement* }

     see expression()
     see statement()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

breakStatement

protected CSTNode breakStatement()
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a break statement.

Grammar:

     breakStatement = "break" label? 
     
     label = 
  

CST:

     statement = { "break" label? }
     
     label = {  }
  

Throws:
ReadException
SyntaxException
ExceptionCollector

continueStatement

protected CSTNode continueStatement()
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Processes a continue statement.

Grammar:

     continueStatement = "continue" label? 
     
     label = 
  

CST:

     statement = { "continue" label? }
     
     label = {  }
  

Throws:
ReadException
SyntaxException
ExceptionCollector

throwStatement

protected CSTNode throwStatement()
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a throw statement.

Grammar:

     throwStatement = "throw" expression 
  

CST:

     statement = { "throw" expression }

     see expression()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

synchronizedStatement

protected CSTNode synchronizedStatement()
                                 throws ReadException,
                                        SyntaxException,
                                        ExceptionCollector
Processes a synchronized statement.

Grammar:

     synchronizedStatement = "synchronized" "(" expression ")" statementBody 
  

CST:

     statement = { "synchronized" expression statementBody }

     see expression()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

ifStatement

protected CSTNode ifStatement()
                       throws ReadException,
                              SyntaxException,
                              ExceptionCollector
Processes an if statement.

Grammar:

     ifStatement  = ifClause elseIfClause* elseClause?

     ifClause     = "if" "(" expression ")" statementBody
     elseIfClause = "else" "if" "(" expression ")" statementBody
     elseClause   = "else" statementBody
  

CST:

     if   = { "if" expression statementBody else? }
     else = if | { "else" statementBody } 

     see expression()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

tryStatement

protected CSTNode tryStatement()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
Processes an try statement.

Grammar:

     tryStatement  = "try" statementBody catchClause* finallyClause?

     catchClause   = "catch" "(" datatype identifier ")" statementBody
     finallyClause = "finally" statementBody
  

CST:

     try     = { "try" statementBody finally catches }

     catches = {  catch* }

     catch   = { "catch" datatype identifier statementBody }

     finally = {} | statementBody

     see datatype()
     see identifier()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

returnStatement

protected CSTNode returnStatement()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Processes a return statement.

Grammar:

     returnStatement = "return" expression? 
  

CST:

     statement = { "return" expression? }

     see expression()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

whileStatement

protected CSTNode whileStatement()
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Processes a while statement.

Grammar:

     whileStatement = "while" "(" expression ")" statementBody
  

CST:

     while = { "while" expression statementBody }

     see expression()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

doWhileStatement

protected CSTNode doWhileStatement()
                            throws ReadException,
                                   SyntaxException,
                                   ExceptionCollector
Processes a do ... while statement.

Grammar:

     doWhileStatement = "do" statementBody "while" "(" expression ")" 
  

CST:

     do = { "do" statementBody expression }

     see expression()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

forStatement

protected CSTNode forStatement()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
Processes a for statement.

Grammar:

     forStatement = "for" statementBody "while" "(" expression ")" 
  

CST:

     do = { "do" statementBody expression }

     see expression()
     see statementBody()
  

Throws:
ReadException
SyntaxException
ExceptionCollector

assertStatement

protected CSTNode assertStatement()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

expression

protected CSTNode expression()
                      throws ReadException,
                             SyntaxException,
                             ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

assignmentExpression

protected CSTNode assignmentExpression()
                                throws ReadException,
                                       SyntaxException,
                                       ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

ternaryExpression

protected CSTNode ternaryExpression()
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

logicalOrExpression

protected CSTNode logicalOrExpression()
                               throws ReadException,
                                      SyntaxException,
                                      ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

logicalAndExpression

protected CSTNode logicalAndExpression()
                                throws ReadException,
                                       SyntaxException,
                                       ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

equalityExpression

protected CSTNode equalityExpression()
                              throws ReadException,
                                     SyntaxException,
                                     ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

relationalExpression

protected CSTNode relationalExpression()
                                throws ReadException,
                                       SyntaxException,
                                       ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

rangeExpression

protected CSTNode rangeExpression()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

additiveExpression

protected CSTNode additiveExpression()
                              throws ReadException,
                                     SyntaxException,
                                     ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

multiplicativeExpression

protected CSTNode multiplicativeExpression()
                                    throws ReadException,
                                           SyntaxException,
                                           ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

unaryExpression

protected CSTNode unaryExpression()
                           throws ReadException,
                                  SyntaxException,
                                  ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

postfixExpression

protected CSTNode postfixExpression()
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

primaryExpression

protected CSTNode primaryExpression()
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

thisExpression

protected CSTNode thisExpression()

subscriptExpression

protected CSTNode subscriptExpression(CSTNode expr)
                               throws ReadException,
                                      SyntaxException,
                                      ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

methodCallOrPropertyExpression

protected CSTNode methodCallOrPropertyExpression(CSTNode expr)
                                          throws ReadException,
                                                 SyntaxException,
                                                 ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

sugaryMethodCallExpression

protected CSTNode sugaryMethodCallExpression(CSTNode expr,
                                             CSTNode identifier,
                                             CSTNode dotExpr)
                                      throws ReadException,
                                             SyntaxException,
                                             ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

tryParseMethodCallWithoutParenthesis

protected CSTNode tryParseMethodCallWithoutParenthesis(CSTNode expr,
                                                       CSTNode identifier)
                                                throws SyntaxException,
                                                       ReadException
Throws:
SyntaxException
ReadException

methodCallWithoutParenthesis

protected CSTNode methodCallWithoutParenthesis(CSTNode expr,
                                               CSTNode identifier)
                                        throws ReadException,
                                               SyntaxException,
                                               ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

lookAheadForMethodCall

protected boolean lookAheadForMethodCall()
                                  throws ReadException,
                                         SyntaxException,
                                         ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

regexPattern

protected CSTNode regexPattern()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

doubleQuotedString

protected CSTNode doubleQuotedString()
                              throws ReadException,
                                     SyntaxException,
                                     ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

parentheticalExpression

protected CSTNode parentheticalExpression()
                                   throws ReadException,
                                          SyntaxException,
                                          ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

parameterList

protected CSTNode parameterList(int endOfListDemarc)
                         throws ReadException,
                                SyntaxException,
                                ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

namedParameterList

protected CSTNode namedParameterList(int endOfListDemarc)
                              throws ReadException,
                                     SyntaxException,
                                     ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

newExpression

protected CSTNode newExpression()
                         throws ReadException,
                                SyntaxException,
                                ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

closureExpression

protected CSTNode closureExpression()
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

closureExpression

protected CSTNode closureExpression(boolean pipeRequired)
                             throws ReadException,
                                    SyntaxException,
                                    ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

listOrMapExpression

protected CSTNode listOrMapExpression()
                               throws ReadException,
                                      SyntaxException,
                                      ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

mapExpression

protected CSTNode mapExpression(CSTNode key)
                         throws ReadException,
                                SyntaxException,
                                ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

listExpression

protected CSTNode listExpression(CSTNode entry)
                          throws ReadException,
                                 SyntaxException,
                                 ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

argumentList

protected CSTNode argumentList()
                        throws ReadException,
                               SyntaxException,
                               ExceptionCollector
Throws:
ReadException
SyntaxException
ExceptionCollector

throwExpected

protected void throwExpected(int[] expectedTypes)
                      throws ReadException,
                             SyntaxException
Throws an UnexpectedTokenException.

Throws:
ReadException
SyntaxException

require

protected Token require(Token token)
                 throws ReadException,
                        SyntaxException
Ensures that a Token is not null. Throws a ParserException on error.

Throws:
ReadException
SyntaxException

la

protected Token la()
            throws ReadException,
                   SyntaxException
Returns (without consuming) the next non-newline token in the underlying token stream.

Throws:
ReadException
SyntaxException

la

protected Token la(int k)
            throws ReadException,
                   SyntaxException
Returns (without consuming any tokens) the next kth non-newline token from the underlying token stream.

Throws:
ReadException
SyntaxException

lt

protected int lt()
          throws ReadException,
                 SyntaxException
Returns the type of the la() token, or -1.

Throws:
ReadException
SyntaxException

lt

protected int lt(int k)
          throws ReadException,
                 SyntaxException
Returns the type of the la(k) token, or -1.

Throws:
ReadException
SyntaxException

consumeUntil

protected void consumeUntil(int type)
                     throws ReadException,
                            SyntaxException
Consumes tokens until one of the specified type is consumed.

Throws:
ReadException
SyntaxException

consume

protected Token consume(int type)
                 throws ReadException,
                        SyntaxException
Consumes (and returns) the next token if it is of the specified type, or throws an UnexpectedTokenException. If the specified type is Token.NEWLINE, eats all available newlines, and consumes (and returns) the next (non-newline) token.

Throws:
ReadException
SyntaxException

consume

protected void consume(CSTNode root,
                       int type)
                throws ReadException,
                       SyntaxException
Adds a CSTNode of the result of consume(type) as a child of root. Throws UnexpectedTokenException if the next token is not of the correct type.

Throws:
ReadException
SyntaxException

rootNode

protected CSTNode rootNode(int type)
                    throws ReadException,
                           SyntaxException
Returns a new CSTNode that holds the result of consume(type). Throws UnexpectedTokenException if the next token is not of the correct type.

Throws:
ReadException
SyntaxException

rootNode

protected CSTNode rootNode(int type,
                           CSTNode child)
                    throws ReadException,
                           SyntaxException
Identical to rootNode(type), but adds child as a child of the newly created node.

Throws:
ReadException
SyntaxException

la_bare

protected Token la_bare()
                 throws ReadException,
                        SyntaxException
Returns (without consuming) the next token in the underlying token stream (newlines included).

Throws:
ReadException
SyntaxException

la_bare

protected Token la_bare(int k)
                 throws ReadException,
                        SyntaxException
Returns (without consuming any tokens) the next kth token from the underlying token stream (newlines included).

Throws:
ReadException
SyntaxException

lt_bare

protected int lt_bare()
               throws ReadException,
                      SyntaxException
Returns the type of the la_bare() token, or -1.

Throws:
ReadException
SyntaxException

lt_bare

protected int lt_bare(int k)
               throws ReadException,
                      SyntaxException
Returns the type of the la_bare(k) token, or -1.

Throws:
ReadException
SyntaxException

consumeUntil_bare

protected void consumeUntil_bare(int type)
                          throws ReadException,
                                 SyntaxException
Consumes tokens until one of the specified type is consumed. Newlines are treated as normal tokens.

Throws:
ReadException
SyntaxException

consume_bare

protected Token consume_bare(int type)
                      throws ReadException,
                             SyntaxException
Consumes (and returns) the next token if it is of the specified type, or throws UnexpectedTokenException.

Throws:
ReadException
SyntaxException

consume_bare

protected void consume_bare(CSTNode root,
                            int type)
                     throws ReadException,
                            SyntaxException
Analogous to consume(root, type), exception consumes with consume_bare.

Throws:
ReadException
SyntaxException

rootNode_bare

protected CSTNode rootNode_bare(int type)
                         throws ReadException,
                                SyntaxException
Analagous to rootNode(type), except consumes with consume_bare.

Throws:
ReadException
SyntaxException

rootNode_bare

protected CSTNode rootNode_bare(int type,
                                CSTNode child)
                         throws ReadException,
                                SyntaxException
Analagous to rootNode(type, child), except consumes with consume_bare().

Throws:
ReadException
SyntaxException


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