org.codehaus.groovy.syntax.parser
Class ExpressionStack

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

public class ExpressionStack
extends java.lang.Object

A combination stack and helper class for parsing groovy expression.

Expressions are a little trickier to parse than the statements above. As a result, we are now doing a hybrid LL/LR parse at the expression level.

Author:
Chris Poirier

Field Summary
private  int open
           
private  Parser parser
           
private  java.util.ArrayList stack
           
 
Constructor Summary
(package private) ExpressionStack(Parser context)
           
 
Method Summary
(package private)  boolean atStartOfExpression()
          Return true if the stack is at the start of an expression.
 boolean canComplete()
          Returns true if the stack can be completed without further shifts.
 boolean isComplete()
          Returns true if the stack is in a state that can be considered a complete expression, provided lookahead is amenable, of course.
(package private)  boolean isEmpty()
          Returns true if the stack is empty.
(package private)  CSTNode pop()
          Pops the node from the top of the stack.
(package private)  void push(CSTNode node)
          Pushes a node onto the stack.
(package private)  void reduce(int count, int rootOffset, boolean mark)
          Performs a reduce by taking some number of CSTNodes from the top of the stack, and making one of them a Reduction with the others as children, then pushes that new node back onto the stack.
(package private)  void shift()
          Shifts a token from the stream to the top of the stack.
(package private)  void shift(int count)
          Shifts some number of (non-newline) tokens from the stream to the top of the stack.
(package private)  void shiftIf(boolean flag, java.lang.String error)
          Shifts if the specified flag is true, reports an error otherwise.
(package private)  void shiftIfTopIsAnExpression(java.lang.String error)
          Shifts if the top of the stack is an expression, reports an error otherwise.
(package private)  void shiftIfTopIsAnOperator(java.lang.String error)
          Shifts if the top of the stack is a operator, reports an error otherwise.
(package private)  void shiftUnless(boolean flag, java.lang.String error)
          Shifts unless the specified flag is true, reports an error otherwise.
(package private)  void shiftUnlessTopIsAnExpression(java.lang.String error)
          Shifts unless the top of the stack is an expression, reports an error otherwise.
(package private)  void shiftUnlessTopIsAnOperator(java.lang.String error)
          Shifts unless the top of the stack is an operator, reports an error otherwise.
(package private)  int size()
          Returns the number of elements in the stack.
(package private)  CSTNode top()
          Returns the top node from the stack without removing it.
(package private)  CSTNode top(int offset)
          Returns some node from the stack.
(package private)  boolean topIsAModifiableExpression()
          Returns true if the top element of the stack is a modifiable expression.
(package private)  boolean topIsAnExpression()
          Returns true if the top element of the stack is an expression.
(package private)  boolean topIsAnOperator()
          Returns true if the top element of the stack is an operator.
(package private)  boolean topIsAnOperator(int offset, boolean unknownReturns)
          Returns true if the element at the specified offset from the top of the stack is an operator.
 java.lang.String toString()
          Creates a string representation of the stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

stack

private java.util.ArrayList stack

parser

private Parser parser

open

private int open
Constructor Detail

ExpressionStack

ExpressionStack(Parser context)
Method Detail

isEmpty

boolean isEmpty()
Returns true if the stack is empty.


isComplete

public boolean isComplete()
Returns true if the stack is in a state that can be considered a complete expression, provided lookahead is amenable, of course.


canComplete

public boolean canComplete()
Returns true if the stack can be completed without further shifts. Used by Parser.la(ExpressionStack) to determine when ambiguous tokens can't be read across a newline. The algorithm will guess true if it isn't sure. If it returns false, you can rely on that analysis.


size

int size()
Returns the number of elements in the stack.


push

void push(CSTNode node)
Pushes a node onto the stack.


pop

CSTNode pop()
Pops the node from the top of the stack.


top

CSTNode top()
Returns the top node from the stack without removing it.


top

CSTNode top(int offset)
Returns some node from the stack. offset is counted from the top of the stack.


shift

void shift(int count)
     throws SyntaxException,
            CompilationFailedException
Shifts some number of (non-newline) tokens from the stream to the top of the stack. They are pushed in order.

Throws:
SyntaxException
CompilationFailedException

shift

void shift()
     throws SyntaxException,
            CompilationFailedException
Shifts a token from the stream to the top of the stack.

Throws:
SyntaxException
CompilationFailedException

reduce

void reduce(int count,
            int rootOffset,
            boolean mark)
Performs a reduce by taking some number of CSTNodes from the top of the stack, and making one of them a Reduction with the others as children, then pushes that new node back onto the stack.


atStartOfExpression

boolean atStartOfExpression()
Return true if the stack is at the start of an expression. True if either the stack is empty or the top token is a left parenthesis.


topIsAnOperator

boolean topIsAnOperator()
Returns true if the top element of the stack is an operator.


topIsAnOperator

boolean topIsAnOperator(int offset,
                        boolean unknownReturns)
Returns true if the element at the specified offset from the top of the stack is an operator.


topIsAModifiableExpression

boolean topIsAModifiableExpression()
Returns true if the top element of the stack is a modifiable expression.


topIsAnExpression

boolean topIsAnExpression()
Returns true if the top element of the stack is an expression.


shiftIf

void shiftIf(boolean flag,
             java.lang.String error)
       throws SyntaxException,
              CompilationFailedException
Shifts if the specified flag is true, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

shiftUnless

void shiftUnless(boolean flag,
                 java.lang.String error)
           throws SyntaxException,
                  CompilationFailedException
Shifts unless the specified flag is true, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

shiftIfTopIsAnExpression

void shiftIfTopIsAnExpression(java.lang.String error)
                        throws SyntaxException,
                               CompilationFailedException
Shifts if the top of the stack is an expression, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

shiftIfTopIsAnOperator

void shiftIfTopIsAnOperator(java.lang.String error)
                      throws SyntaxException,
                             CompilationFailedException
Shifts if the top of the stack is a operator, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

shiftUnlessTopIsAnExpression

void shiftUnlessTopIsAnExpression(java.lang.String error)
                            throws SyntaxException,
                                   CompilationFailedException
Shifts unless the top of the stack is an expression, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

shiftUnlessTopIsAnOperator

void shiftUnlessTopIsAnOperator(java.lang.String error)
                          throws SyntaxException,
                                 CompilationFailedException
Shifts unless the top of the stack is an operator, reports an error otherwise.

Throws:
SyntaxException
CompilationFailedException

toString

public java.lang.String toString()
Creates a string representation of the stack.



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