gov.sns.tools.formula
Class FormulaGrammarBase

java.lang.Object
  |
  +--gov.sns.tools.formula.FormulaGrammarBase
Direct Known Subclasses:
FormulaGrammar

public abstract class FormulaGrammarBase
extends java.lang.Object

FormulaGrammarBase provides the foundation for the formula grammar. It defines the constructs used by the grammar and is the abstract superclass for the FormulaGrammar which is generated by the javacc tool.


Field Summary
protected  java.util.Map functionTable
          A table of functions keyed by the function name.
protected  java.util.List operatorStack
          Holds a list of operators in the order in which they should be evaluated.
protected  java.util.Map variableTable
          A table of variables keyed by the variable name.
 
Constructor Summary
FormulaGrammarBase()
          Creates a new instance of FormulaParserBase.
 
Method Summary
protected  void addBaseFunctions()
          Instantiate common math functions and add them to the function table.
 void addFunction(Function function)
          Add a function a function to the function lookup table.
 java.util.List compile(java.lang.String formulaStr)
          Parse the formula and compile the operations for performance.
abstract  int formula()
          parse one line of code i.e.
 gov.sns.tools.formula.Variable getVariable(java.lang.String name)
          Lookup the variable with the given name.
 boolean hasVariable(java.lang.String name)
          Determine if a variable of the specified name exists.
abstract  void ReInit(java.io.Reader reader)
          re-initialize the parser
 void setVariable(java.lang.String name, double value)
          Set the named variable to have the specified value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

operatorStack

protected java.util.List operatorStack
Holds a list of operators in the order in which they should be evaluated.


variableTable

protected java.util.Map variableTable
A table of variables keyed by the variable name.


functionTable

protected java.util.Map functionTable
A table of functions keyed by the function name.

Constructor Detail

FormulaGrammarBase

public FormulaGrammarBase()
Creates a new instance of FormulaParserBase.

Method Detail

addBaseFunctions

protected void addBaseFunctions()
Instantiate common math functions and add them to the function table.


addFunction

public final void addFunction(Function function)
Add a function a function to the function lookup table.

Parameters:
function - the function to add to the function lookup table

setVariable

public final void setVariable(java.lang.String name,
                              double value)
Set the named variable to have the specified value.

Parameters:
name - the name of the variable
value - the value to assign to the variable

getVariable

public final gov.sns.tools.formula.Variable getVariable(java.lang.String name)
Lookup the variable with the given name. If the variable is found, then return it, otherwise create a new variable, add it to the variable lookup table and return the variable.

Parameters:
name - the name of the variable to fetch

hasVariable

public final boolean hasVariable(java.lang.String name)
Determine if a variable of the specified name exists.

Parameters:
name - The name of the variable for which to check.
Returns:
true if the variable exists; false otherwise.

compile

public final java.util.List compile(java.lang.String formulaStr)
                             throws ParseException
Parse the formula and compile the operations for performance. By compile we mean parsing the formula string and generating an ordered list of operators which represents the formula in a form suitable for fast evaluation.

Parameters:
formulaStr - String representation of the formula.
Returns:
The compiled operator stack.
Throws:
ParseException - Exception if there is a parse error while parsing the formula.

formula

public abstract int formula()
                     throws ParseException
parse one line of code i.e. the formula

ParseException

ReInit

public abstract void ReInit(java.io.Reader reader)
re-initialize the parser