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 Concrete Syntax Tree. Exceptions are collected during processing, and parsing will continue for while possible, in order to report as many problems as possible. module() is the primary entry point.

Author:
Bob McWhirter, James Strachan, Chris Poirier

Field Summary
private  SourceUnit controller
           
private static int[] EXPRESSION_REDUCE_HANDLERS
           
private static int[] EXPRESSION_SHIFT_HANDLERS
           
private  int nestCount
           
private  TokenStream tokenStream
           
 
Constructor Summary
Parser(SourceUnit controller, TokenStream tokenStream)
          Sets the Parser to process a TokenStream, under control of the specified SourceUnit.
 
Method Summary
protected  Reduction assertStatement()
          Processes an assert statement.
 Reduction bodyStatement()
          A synonym for typeBodyStatement( true, true, false ).
protected  Reduction breakStatement()
          Processes a break statement.
 Reduction classDeclaration(Reduction modifiers)
          Processes a class declaration.
protected  Reduction closureExpression()
          Processes a closure expression.
protected  Token consume()
          A synonym for consume( Types.ANY, false ).
protected  Token consume(boolean significantNewlines)
          A synonym for consume( Types.ANY, significantNewlines ).
protected  Token consume(int type)
          A synonym for consume( type, false ).
protected  Token consume(int type, boolean significantNewlines)
          Consumes (and returns) the next token if it is of the specified type.
protected  Reduction continueStatement()
          Processes a continue statement.
protected  CSTNode datatype()
          A synonym for datatype( true ).
protected  CSTNode datatype(boolean allowVoid)
          Processes a datatype specification.
 CSTNode dottedIdentifier()
          Processes a dotted identifer.
protected  Reduction 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  void error()
          A synonym for error( null, true, 1, null ).
protected  void error(int expectedType)
          A scalar synonym of error( expectedTypes ).
protected  void error(int[] expectedTypes)
          A synonym for error( expectedTypes, true, 1, null ).
protected  UnexpectedTokenException error(int[] expectedTypes, boolean throwIt, int k)
          A synonym for error( expectedTypes, throwIt, k, null ).
protected  UnexpectedTokenException error(int[] expectedTypes, boolean throwIt, int k, java.lang.String comment)
          Reports an error by generating and optionally throwing an UnexpectedTokenException.
protected  void error(java.lang.String comment)
          A synonym for error( null, true, 1, comment ).
protected  UnexpectedTokenException error(Token found, int[] expectedTypes, boolean throwIt, java.lang.String comment)
          Reports an error assembled from parts.
protected  void error(Token found, java.lang.String comment)
          A synonym for error( found, null, true, comment ).
protected  CSTNode expression()
          Processes a single (sub-)expression into a CSTNode.
protected  Reduction forStatement()
          Processes a for statement.
 TokenStream getTokenStream()
          Returns the TokenStream being parsed.
protected  Reduction gstring()
          Processes a GString.
protected  Reduction ifStatement()
          Processes an if statement.
 Reduction importStatement()
          Processes an import statement.
 Reduction interfaceDeclaration(Reduction modifiers)
          Processes a interface declaration.
protected  Token la()
          Synonym for la( 1, false ).
protected  Token la(boolean significantNewlines)
          Synonym for la( 1, significantNewlines ).
protected  Token la(ExpressionStack stack)
          Special la() used by the expression parser.
protected  Token la(int k)
          Synonym for la( k, false ).
protected  Token la(int k, boolean significantNewlines)
          Returns (without consuming) the next kth token in the underlying token stream.
protected  Reduction listOrMapExpression()
          Synonym for listOrMapExpression( false, false ).
protected  Reduction listOrMapExpression(boolean isMap, boolean insist)
          Processes a list or map expression.
protected  int lt()
          Returns the meaning of the la() token.
protected  int lt(boolean significantNewlines)
          Returns the meaning of the la( significantNewlines ) token.
protected  int lt(int k)
          Returns the meaning of the la( k ) token.
protected  int lt(int k, boolean significantNewlines)
          Returns the meaning of the la( k, significantNewlines ) token.
 Reduction methodDeclaration(Reduction modifiers, CSTNode type, Token identifier, boolean emptyOnly)
          Processes a class/interface method.
 Reduction 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.).
 Reduction module()
          The primary file-level parsing entry point.
protected  Token nameDeclaration(boolean significantNewlines)
          Processes a name that is valid for declarations.
protected  Token nameReference(boolean significantNewlines)
          Processes a reference to a declared name.
protected  Reduction newExpression()
          Processes a "new" expression.
protected  CSTNode optionalDatatype(boolean significantNewlines, boolean allowVoid)
          Processes an optional data type marker (for a parameter, method return type, etc.).
 void optionalNewlines()
          Eats any optional newlines.
 Reduction packageDeclaration()
          Processes a package declaration.
protected  Reduction parameterDeclaration()
          Processes a single parameter declaration, which can occur on methods and closures.
protected  Reduction parameterDeclarationList()
          Processes a parameter declaration list, which can occur on methods and closures.
protected  Reduction parameterList()
          Processes a NON-EMPTY parameter list, as supplied on either a method invokation or a closure invokation.
 Reduction parse()
          Synonym for module(), the primary entry point.
 Reduction propertyDeclaration(Reduction modifiers, CSTNode type, Token identifier)
          Processes a class/interface property, including the optional initialization clause.
 void recover()
          A synonym for recover( Types.ANY_END_OF_STATMENT, true ).
 void recover(int safe)
          A synonm for the scalar recover( safe, false ).
 void recover(int[] safe)
          A synonym for recover( safe, false ).
 void recover(int[] safe, boolean ignoreNewlines)
          Attempts to recover from an error by discarding input until a known token is found.
 void recover(int safe, boolean ignoreNewlines)
          A scalar version of recover( int[], boolean ).
protected  Reduction returnStatement()
          Processes a return statement.
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  Reduction statementsUntilRightCurly()
          Reads statements until a "}" is met.
protected  Reduction switchStatement()
          Processes a switch statement.
protected  Reduction synchronizedStatement()
          Processes a synchronized statement.
protected  Reduction throwStatement()
          Processes a throw statement.
 CSTNode topLevelStatement()
          Processes a top level statement (classes, interfaces, unattached methods, and unattached code).
protected  Reduction tryStatement()
          Processes a try statement.
protected  Reduction tupleExpression(int level, int depth)
          Processes a "new" array initializer expression.
 Reduction typeBody(boolean allowStatic, boolean allowAbstract, boolean requireAbstract)
          Processes the body of an interface or class.
 Reduction 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().
 Reduction typeList(int declarator, boolean optional, int limit)
          Processes a type list, like the ones that occur after "extends" or implements.
protected  Reduction variableDeclarationExpression(CSTNode datatype)
          Processes a typed variable declaration.
protected  Reduction 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

controller

private SourceUnit controller

tokenStream

private TokenStream tokenStream

nestCount

private int nestCount

EXPRESSION_SHIFT_HANDLERS

private static final int[] EXPRESSION_SHIFT_HANDLERS

EXPRESSION_REDUCE_HANDLERS

private static final int[] EXPRESSION_REDUCE_HANDLERS
Constructor Detail

Parser

public Parser(SourceUnit controller,
              TokenStream tokenStream)
Sets the Parser to process a TokenStream, under control of the specified SourceUnit.

Method Detail

parse

public Reduction parse()
                throws CompilationFailedException
Synonym for module(), the primary entry point.

Throws:
CompilationFailedException

getTokenStream

public TokenStream getTokenStream()
Returns the TokenStream being parsed.


optionalNewlines

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

Throws:
SyntaxException
CompilationFailedException

endOfStatement

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

Throws:
SyntaxException
CompilationFailedException

endOfStatement

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

Throws:
SyntaxException
CompilationFailedException

dottedIdentifier

public CSTNode dottedIdentifier()
                         throws SyntaxException,
                                CompilationFailedException
Processes a dotted identifer. Used all over the place.

Grammar:

     dottedIdentifier =  ("." )*
  

CST:

     dotted = { "." dotted  } | 
  

Throws:
SyntaxException
CompilationFailedException

module

public Reduction module()
                 throws SyntaxException,
                        CompilationFailedException
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:

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

CST:

     module = {  package imports (topLevelStatement)* }

     package           see packageDeclaration()
     imports           see importStatement()
     topLevelStatement see topLevelStatement()
  

Throws:
SyntaxException
CompilationFailedException

packageDeclaration

public Reduction packageDeclaration()
                             throws SyntaxException,
                                    CompilationFailedException
Processes a package declaration. Called by module().

Grammar:

     packageDeclaration = "package" dottedIdentifier 
  

CST:

     package = { "package" dottedIdentifier }

     see dottedIdentifier()
  

Throws:
SyntaxException
CompilationFailedException

importStatement

public Reduction importStatement()
                          throws SyntaxException,
                                 CompilationFailedException
Processes an import statement. Called by module().

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  } | 
     clause   = {  ? }
  

Throws:
SyntaxException
CompilationFailedException

topLevelStatement

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

Grammar:

     topLevelStatement
       = methodDeclaration
       | typeDeclaration
       | statement

     typeDeclaration = classDeclaration | interfaceDeclaration
  

Recognition:

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

CST:

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

Throws:
SyntaxException
CompilationFailedException

typeDeclaration

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

Throws:
SyntaxException
CompilationFailedException

modifierList

public Reduction modifierList(boolean allowStatic,
                              boolean allowAbstract)
                       throws CompilationFailedException,
                              SyntaxException
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:
CompilationFailedException
SyntaxException

classDeclaration

public Reduction classDeclaration(Reduction modifiers)
                           throws SyntaxException,
                                  CompilationFailedException
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      = { :SYNTH_CLASS modifiers extends implements body }
     extends    = { "extends"    datatype  } | {}
     implements = { "implements" datatype* } | {}

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

Throws:
SyntaxException
CompilationFailedException

interfaceDeclaration

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

Grammar:

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

CST:

     interface  = { :SYNTH_INTERFACE modifiers {} extends body }
     extends    = { "extends" datatype* } | {}

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

Throws:
SyntaxException
CompilationFailedException

typeList

public Reduction typeList(int declarator,
                          boolean optional,
                          int limit)
                   throws SyntaxException,
                          CompilationFailedException
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:
SyntaxException
CompilationFailedException

typeBody

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

Grammar:

     typeBody = "{" typeBodyStatement* "}"
  

CST:

     body = {  typeBodyStatement* }

     typeBodyStatement see typeBodyStatement()
  

Throws:
SyntaxException
CompilationFailedException

typeBodyStatement

public Reduction typeBodyStatement(boolean allowStatic,
                                   boolean allowAbstract,
                                   boolean requireAbstract)
                            throws SyntaxException,
                                   CompilationFailedException
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:
SyntaxException
CompilationFailedException

bodyStatement

public Reduction bodyStatement()
                        throws SyntaxException,
                               CompilationFailedException
A synonym for typeBodyStatement( true, true, false ).

Throws:
SyntaxException
CompilationFailedException

nameDeclaration

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

Grammar:

     nameDeclaration = 
  

CST:

     name = 
  

Throws:
SyntaxException
CompilationFailedException

nameReference

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

Grammar:

     nameReference =  | 
  

CST:

     name = 
  

Throws:
SyntaxException
CompilationFailedException

optionalDatatype

protected CSTNode optionalDatatype(boolean significantNewlines,
                                   boolean allowVoid)
                            throws SyntaxException,
                                   CompilationFailedException
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? (?=)
  
h

CST:

     result = datatype | {}

     see datatype()
  

Throws:
SyntaxException
CompilationFailedException

propertyDeclaration

public Reduction propertyDeclaration(Reduction modifiers,
                                     CSTNode type,
                                     Token identifier)
                              throws SyntaxException,
                                     CompilationFailedException
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 nameDeclaration ["=" expression]) 
  

CST:

     property = { :SYNTH_PROPERTY modifierList optionalDatatype expression? }

     see modifierList()
     see optionalDatatype()
     see expression()
  

Throws:
SyntaxException
CompilationFailedException

methodDeclaration

public Reduction methodDeclaration(Reduction modifiers,
                                   CSTNode type,
                                   Token identifier,
                                   boolean emptyOnly)
                            throws SyntaxException,
                                   CompilationFailedException
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:

     method = { :SYNTH_METHOD modifierList optionalDatatype
                 parameterDeclarationList throwsClause statementBody }

     throwsClause = { "throws" datatype+ } | {}

     see modifierList()
     see optionalDatatype()
     see parameterDeclarationList()
     see statementBody()
  

Throws:
SyntaxException
CompilationFailedException

parameterDeclarationList

protected Reduction parameterDeclarationList()
                                      throws SyntaxException,
                                             CompilationFailedException
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  = { :SYNTH_PARAMETER_DECLARATION optionalDatatype default? }
     default    = expression
  

Throws:
SyntaxException
CompilationFailedException

parameterDeclaration

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

Grammar:

     parameterDeclaration = optionalDatatype nameDeclaration
  

CST:

     parameter = { :SYNTH_PARAMETER_DECLARATION optionalDatatype }

     see optionalDatatype()
  

Throws:
SyntaxException
CompilationFailedException

datatype

protected CSTNode datatype(boolean allowVoid)
                    throws SyntaxException,
                           CompilationFailedException
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 = dottedIdentifier | "void" | "int" | ...
  

CST:

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

     see dottedIdentifier()
  

Throws:
SyntaxException
CompilationFailedException

datatype

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

Throws:
SyntaxException
CompilationFailedException

scalarDatatype

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

Grammar:

     scalarDatatype = dottedIdentifier | "void" | "int" | ...
  

CST:

     scalar    = dottedIdentifier | primitive
     primitive = "void" | "int" | ...

     see dottedIdentifier()
  

Throws:
SyntaxException
CompilationFailedException

statementBody

protected CSTNode statementBody(boolean requireBraces)
                         throws SyntaxException,
                                CompilationFailedException
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:
SyntaxException
CompilationFailedException

statementsUntilRightCurly

protected Reduction statementsUntilRightCurly()
                                       throws SyntaxException,
                                              CompilationFailedException
Reads statements until a "}" is met.

Grammar:

     statementsUntilRightCurly = statement* (?= "}")
  

CST:

     statements = {  statement* }
  

Throws:
SyntaxException
CompilationFailedException

statement

protected CSTNode statement(boolean allowUnlabelledBlocks)
                     throws SyntaxException,
                            CompilationFailedException
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|basicStatement|blockStatement)

     basicStatement = 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       = { :SYNTH_LABEL bareStatement }
     bareStatement  = emptyStatement | blockStatement | basicStatement
     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:
SyntaxException
CompilationFailedException

statement

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

Throws:
SyntaxException
CompilationFailedException

assertStatement

protected Reduction assertStatement()
                             throws SyntaxException,
                                    CompilationFailedException
Processes an assert statement.

Grammar:

     assertStatement = "assert" expression (":" expression) 
  

CST:

     assert = { "assert" expression expression? }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

breakStatement

protected Reduction breakStatement()
                            throws SyntaxException,
                                   CompilationFailedException
Processes a break statement. We require the label on the same line.

Grammar:

     breakStatement = "break" label? 

     label = 
  

CST:

     statement = { "break" label? }
     label     = 
  

Throws:
SyntaxException
CompilationFailedException

continueStatement

protected Reduction continueStatement()
                               throws SyntaxException,
                                      CompilationFailedException
Processes a continue statement. We require the label on the same line.

Grammar:

     continueStatement = "continue" label? 

     label = 
  

CST:

     statement = { "continue" label? }
     label     = 
  

Throws:
SyntaxException
CompilationFailedException

throwStatement

protected Reduction throwStatement()
                            throws SyntaxException,
                                   CompilationFailedException
Processes a throw statement.

Grammar:

     throwStatement = "throw" expression 
  

CST:

     statement = { "throw" expression }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

ifStatement

protected Reduction ifStatement()
                         throws SyntaxException,
                                CompilationFailedException
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:
SyntaxException
CompilationFailedException

returnStatement

protected Reduction returnStatement()
                             throws SyntaxException,
                                    CompilationFailedException
Processes a return statement. Any expression must start on the same line as the "return".

Grammar:

     returnStatement = "return" expression? 
  

CST:

     statement = { "return" expression? }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

switchStatement

protected Reduction switchStatement()
                             throws SyntaxException,
                                    CompilationFailedException
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:
SyntaxException
CompilationFailedException

synchronizedStatement

protected Reduction synchronizedStatement()
                                   throws SyntaxException,
                                          CompilationFailedException
Processes a synchronized statement.

Grammar:

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

CST:

     statement = { "synchronized" expression statementBody }

     see expression()
     see statementBody()
  

Throws:
SyntaxException
CompilationFailedException

tryStatement

protected Reduction tryStatement()
                          throws SyntaxException,
                                 CompilationFailedException
Processes a try statement.

Grammar:

     tryStatement  = "try" statementBody catchClause* finallyClause?

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

CST:

     try     = { "try" statementBody catches finally }

     catches = {  catch* }

     catch   = { "catch" datatype  statementBody }

     finally = {} | statementBody

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

Throws:
SyntaxException
CompilationFailedException

forStatement

protected Reduction forStatement()
                          throws SyntaxException,
                                 CompilationFailedException
Processes a for statement.

Grammar:

     forStatement = "for" "(" normal | each ")" statementBody

     normal = multi ";" expression ";" multi
     multi  = (expression ["," expression]*)

     each   = optionalDatatype nameDeclaration ("in"|":") expression
  

CST:

     for    = { "for" header statementBody }

     header = normal | each
     each   = { ("in"|":") optionalDatatype nameDeclaration expression }

     normal = {  init test incr }
     init   = {  expression* }
     test   = expression
     incr   = {  expression* }

     see expression()
     see nameDeclaration()
     see statementBody()
  

Throws:
SyntaxException
CompilationFailedException

doWhileStatement

protected Reduction doWhileStatement()
                              throws SyntaxException,
                                     CompilationFailedException
Processes a do ... while statement.

Grammar:

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

CST:

     do = { "do" statementBody expression }

     see expression()
     see statementBody()
  

Throws:
SyntaxException
CompilationFailedException

whileStatement

protected Reduction whileStatement()
                            throws SyntaxException,
                                   CompilationFailedException
Processes a while statement.

Grammar:

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

CST:

     while = { "while" expression statementBody }

     see expression()
     see statementBody()
  

Throws:
SyntaxException
CompilationFailedException

expression

protected CSTNode expression()
                      throws SyntaxException,
                             CompilationFailedException
Processes a single (sub-)expression into a CSTNode. No assumption is made about what follows the expression.

Note that the expression parser is rather stupid, in that it cannot resolve names. Therefore it is little more than a pre-filter, removing statements that can't possibly be right, but leaving everything that might be right for semantic analysis by the Analyzer (which has access to the symbol table. There was some thought given to eliminating the CSTs and going right to ASTs, but that option was rejected because inner classes mean that class name resolution won't work before parsing is complete.

Throws:
SyntaxException
CompilationFailedException

variableDeclarationExpression

protected Reduction variableDeclarationExpression(CSTNode datatype)
                                           throws SyntaxException,
                                                  CompilationFailedException
Processes a typed variable declaration. Without the type, it's a assignment expression instead (no comma support). The datatype has already been identified and is passed in.

Grammar:

     variableDeclarationExpression
        = datatype (nameDeclaration "=" expression)
                   ("," nameDeclaration "=" expression)*
  

CST:

     statement   = { : datatype declaration+ }
     declaration = {  expression }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

gstring

protected Reduction gstring()
                     throws SyntaxException,
                            CompilationFailedException
Processes a GString.

Grammar:

     gstring = (? "$" "{" expression "}" ?)*
  

CST:

     gstring = { :SYNTH_GSTRING (segment|expression)* }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

parameterList

protected Reduction parameterList()
                           throws SyntaxException,
                                  CompilationFailedException
Processes a NON-EMPTY parameter list, as supplied on either a method invokation or a closure invokation. Reads parameters until something that doesn't belong is found.

Grammar:

     parameterList = (regular "," named) | named
     regular = parameter ("," parameter)*
     named   = nameReference ":" parameter ("," nameReference ":" parameter)*

     parameter = expression
  

CST:

     parameterList = {  regular* named* }
     regular = expression
     named   = { ":"  expression }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

newExpression

protected Reduction newExpression()
                           throws SyntaxException,
                                  CompilationFailedException
Processes a "new" expression. Handles optional constructors, array initializations, closure arguments, and anonymous classes. In order to support anonymous classes, anonymous closures are not allowed.

Grammar:

     newExpression = "new" scalarDatatype (array|init)?
     array = ( "[" expression "]" )+ | ( ("[" "]")+ newArrayInitializer )
     init  = "(" parameterList? ")" (typeBody | closureExpression)?
  

CST:

     new = { "new" arrayType     dimensions newArrayInitializer? }
         | { "new" scalarDataype (parameterList|{}) typeBody? }

     arrayType  = { "{" (arrayType | scalarDatatype) }
     dimensions = {  expression+ } | {}

     see expression()
     see scalarDatatype()
     see typeBody()
  

Throws:
SyntaxException
CompilationFailedException

tupleExpression

protected Reduction tupleExpression(int level,
                                    int depth)
                             throws SyntaxException,
                                    CompilationFailedException
Processes a "new" array initializer expression.

Grammar:

     tupleExpression = "{" (tupleExpression | (expression ("," expression))? "}"
  

CST:

     initializer = { "{":SYNTH_TUPLE (initializer*|expression*) }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

closureExpression

protected Reduction closureExpression()
                               throws SyntaxException,
                                      CompilationFailedException
Processes a closure expression.

Grammar:

     closureExpression = "{" parameters statement* "}"
     parameters = ("|" parameterDeclarationList "|")?
  

CST:

     initializer = { "{":SYNTH_CLOSURE parameters statements }
     parameters  = parameterDeclarationList | {  }
     statements  = {  statement* }

     see parameterDeclarationList()
     see statement()
  

Throws:
SyntaxException
CompilationFailedException

listOrMapExpression

protected Reduction listOrMapExpression(boolean isMap,
                                        boolean insist)
                                 throws SyntaxException,
                                        CompilationFailedException
Processes a list or map expression.

Grammar:

     listOrMapExpression = list | map

     list = "[" (expression ("," expression)*)? "]"

     map     = "[" (":" | mapping+) "]"
     mapping = expression ":" expression
  

CST:

     list    = { "[":SYNTH_LIST expression* }
     map     = { "[":SYNTH_MAP  mapping* }
     mapping = { ":" expression expression }

     see expression()
  

Throws:
SyntaxException
CompilationFailedException

listOrMapExpression

protected Reduction listOrMapExpression()
                                 throws SyntaxException,
                                        CompilationFailedException
Synonym for listOrMapExpression( false, false ).

Throws:
SyntaxException
CompilationFailedException

error

protected UnexpectedTokenException error(Token found,
                                         int[] expectedTypes,
                                         boolean throwIt,
                                         java.lang.String comment)
                                  throws SyntaxException
Reports an error assembled from parts.

Throws:
SyntaxException

error

protected UnexpectedTokenException error(int[] expectedTypes,
                                         boolean throwIt,
                                         int k,
                                         java.lang.String comment)
                                  throws SyntaxException,
                                         CompilationFailedException
Reports an error by generating and optionally throwing an UnexpectedTokenException.

Throws:
SyntaxException
CompilationFailedException

error

protected UnexpectedTokenException error(int[] expectedTypes,
                                         boolean throwIt,
                                         int k)
                                  throws SyntaxException,
                                         CompilationFailedException
A synonym for error( expectedTypes, throwIt, k, null ).

Throws:
SyntaxException
CompilationFailedException

error

protected void error(int[] expectedTypes)
              throws SyntaxException,
                     CompilationFailedException
A synonym for error( expectedTypes, true, 1, null ).

Throws:
SyntaxException
CompilationFailedException

error

protected void error()
              throws SyntaxException,
                     CompilationFailedException
A synonym for error( null, true, 1, null ).

Throws:
SyntaxException
CompilationFailedException

error

protected void error(java.lang.String comment)
              throws SyntaxException,
                     CompilationFailedException
A synonym for error( null, true, 1, comment ).

Throws:
SyntaxException
CompilationFailedException

error

protected void error(Token found,
                     java.lang.String comment)
              throws SyntaxException
A synonym for error( found, null, true, comment ).

Throws:
SyntaxException

error

protected void error(int expectedType)
              throws SyntaxException,
                     CompilationFailedException
A scalar synonym of error( expectedTypes ).

Throws:
SyntaxException
CompilationFailedException

recover

public void recover(int[] safe,
                    boolean ignoreNewlines)
             throws SyntaxException,
                    CompilationFailedException
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:
SyntaxException
CompilationFailedException

recover

public void recover(int safe,
                    boolean ignoreNewlines)
             throws SyntaxException,
                    CompilationFailedException
A scalar version of recover( int[], boolean ).

Throws:
SyntaxException
CompilationFailedException

recover

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

Throws:
SyntaxException
CompilationFailedException

recover

public void recover(int safe)
             throws SyntaxException,
                    CompilationFailedException
A synonm for the scalar recover( safe, false ).

Throws:
SyntaxException
CompilationFailedException

recover

public void recover()
             throws SyntaxException,
                    CompilationFailedException
A synonym for recover( Types.ANY_END_OF_STATMENT, true ).

Throws:
SyntaxException
CompilationFailedException

la

protected Token la(int k,
                   boolean significantNewlines)
            throws SyntaxException,
                   CompilationFailedException
Returns (without consuming) the next kth token in the underlying token stream. You can make newlines significant as needed. Returns Token.EOF on end of stream. k is counted from 1.

Throws:
SyntaxException
CompilationFailedException

la

protected Token la(int k)
            throws SyntaxException,
                   CompilationFailedException
Synonym for la( k, false ).

Throws:
SyntaxException
CompilationFailedException

la

protected Token la(boolean significantNewlines)
            throws SyntaxException,
                   CompilationFailedException
Synonym for la( 1, significantNewlines ).

Throws:
SyntaxException
CompilationFailedException

la

protected Token la()
            throws SyntaxException,
                   CompilationFailedException
Synonym for la( 1, false ).

Throws:
SyntaxException
CompilationFailedException

la

protected Token la(ExpressionStack stack)
            throws SyntaxException,
                   CompilationFailedException
Special la() used by the expression parser. It will get the next token in the current statement. If the next token is past a line boundary and might be the start of the next statement, it won't cross the line to get it.

Throws:
SyntaxException
CompilationFailedException

lt

protected int lt(int k,
                 boolean significantNewlines)
          throws SyntaxException,
                 CompilationFailedException
Returns the meaning of the la( k, significantNewlines ) token.

Throws:
SyntaxException
CompilationFailedException

lt

protected int lt(int k)
          throws SyntaxException,
                 CompilationFailedException
Returns the meaning of the la( k ) token.

Throws:
SyntaxException
CompilationFailedException

lt

protected int lt(boolean significantNewlines)
          throws SyntaxException,
                 CompilationFailedException
Returns the meaning of the la( significantNewlines ) token.

Throws:
SyntaxException
CompilationFailedException

lt

protected int lt()
          throws SyntaxException,
                 CompilationFailedException
Returns the meaning of the la() token.

Throws:
SyntaxException
CompilationFailedException

consume

protected Token consume(int type,
                        boolean significantNewlines)
                 throws SyntaxException,
                        CompilationFailedException
Consumes (and returns) the next token if it is of the specified type. If significantNewlines is set, newlines will not automatically be consumed; otherwise they will. Throws UnexpectedTokenException if the type doesn't match.

Throws:
SyntaxException
CompilationFailedException

consume

protected Token consume(int type)
                 throws SyntaxException,
                        CompilationFailedException
A synonym for consume( type, false ). If type is Types.NEWLINE, equivalent to consume( Types.NEWLINE, true ).

Throws:
SyntaxException
CompilationFailedException

consume

protected Token consume()
                 throws SyntaxException,
                        CompilationFailedException
A synonym for consume( Types.ANY, false ).

Throws:
SyntaxException
CompilationFailedException

consume

protected Token consume(boolean significantNewlines)
                 throws SyntaxException,
                        CompilationFailedException
A synonym for consume( Types.ANY, significantNewlines ). If you pass true, it will consume exactly the next token from the stream.

Throws:
SyntaxException
CompilationFailedException


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