com.threerings.expr
Class ExpressionParser<T>

java.lang.Object
  extended by com.threerings.expr.ExpressionParser<T>

public class ExpressionParser<T>
extends Object

Parses simple expressions using an implementation of the shunting-yard algorithm.


Nested Class Summary
protected static class ExpressionParser.ArrayIndex
          An array index identifier.
protected static class ExpressionParser.FunctionCall
          An function call identifier.
protected static class ExpressionParser.Identifier
          Wraps a string to identify it as an identifier.
protected static class ExpressionParser.Operator
          An operator identifier.
protected static class ExpressionParser.OperatorStreamTokenizer
          Extends StreamTokenizer slightly to handle multi-character operators.
 
Field Summary
protected  Stack<Object> _operation
          The operation stack.
protected  Stack<T> _output
          The output stack.
protected  ExpressionParser.OperatorStreamTokenizer _strtok
          The tokenizer from which we acquire tokens.
protected static Map<String,ExpressionParser.Operator> OPERATORS
          The operator map.
protected static Map<String,ExpressionParser.Operator> UNARY_ALTERNATES
           
 
Constructor Summary
ExpressionParser(Reader reader)
          Creates a new parser to read from the specified reader.
 
Method Summary
protected static void addOperator(String name, boolean rightAssociative, int precedence, int arity)
          Adds an operator to the map.
protected static void addUnaryAlternate(String name, boolean rightAssociative, int precedence, int arity)
          Adds an unary alternate for operators that can be both unary and binary.
protected  void handle(Object value)
          Handles the supplied output value.
protected  T handleArrayIndex(String array)
          Handles an array index.
protected  T handleFunctionCall(String function, int arity)
          Handles a function call.
protected  T handleIdentifier(String name)
          Handles an identifier.
protected  T handleNumber(double value)
          Handles a number.
protected  T handleOperator(String operator, int arity)
          Handles an operator.
protected  T handleString(String value)
          Handles a string.
static void main(String... args)
          Main method for testing.
 T parse()
          Parses the expression and returns the object at the top of the stack (or null if the stack is empty).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_strtok

protected ExpressionParser.OperatorStreamTokenizer _strtok
The tokenizer from which we acquire tokens.


_output

protected Stack<T> _output
The output stack.


_operation

protected Stack<Object> _operation
The operation stack.


OPERATORS

protected static final Map<String,ExpressionParser.Operator> OPERATORS
The operator map.


UNARY_ALTERNATES

protected static final Map<String,ExpressionParser.Operator> UNARY_ALTERNATES
Constructor Detail

ExpressionParser

public ExpressionParser(Reader reader)
Creates a new parser to read from the specified reader.

Method Detail

main

public static void main(String... args)
                 throws Exception
Main method for testing.

Throws:
Exception

parse

public T parse()
        throws Exception
Parses the expression and returns the object at the top of the stack (or null if the stack is empty).

Throws:
Exception

handle

protected void handle(Object value)
               throws Exception
Handles the supplied output value.

Throws:
Exception

handleNumber

protected T handleNumber(double value)
                  throws Exception
Handles a number.

Throws:
Exception

handleString

protected T handleString(String value)
                  throws Exception
Handles a string.

Throws:
Exception

handleOperator

protected T handleOperator(String operator,
                           int arity)
                    throws Exception
Handles an operator.

Throws:
Exception

handleFunctionCall

protected T handleFunctionCall(String function,
                               int arity)
                        throws Exception
Handles a function call.

Throws:
Exception

handleArrayIndex

protected T handleArrayIndex(String array)
                      throws Exception
Handles an array index.

Throws:
Exception

handleIdentifier

protected T handleIdentifier(String name)
                      throws Exception
Handles an identifier.

Throws:
Exception

addOperator

protected static void addOperator(String name,
                                  boolean rightAssociative,
                                  int precedence,
                                  int arity)
Adds an operator to the map.


addUnaryAlternate

protected static void addUnaryAlternate(String name,
                                        boolean rightAssociative,
                                        int precedence,
                                        int arity)
Adds an unary alternate for operators that can be both unary and binary.