civquest.parser.condition
Class ConditionParser

java.lang.Object
  extended bycivquest.parser.condition.ConditionParser
Direct Known Subclasses:
ComplexConditionParser, SimpleConditionParser

public abstract class ConditionParser
extends java.lang.Object

A ConditionParser is capable of processing one-line boolean expressions in its own Java-like language, and evaluating their truth values. This is useful because Java has no built-in mechanism for evaluating strings of its own grammar, like the eval() functions of scripting languages like Perl or Python. The partial grammar for the condition language looks like this:

 Operand ::= <Var> | <Int>
 RelOp   ::= '<' | '<=' | '=' | '>' | '>='
 Expr    ::= <Operand> <RelOp> <Operand>
           | '(' && ')'
           | '(' || ')'
 
In addition, although of less importance, line comments start with a ';' and block comments are embraced between '/*' and '*\/'. For referring to variables within the expressions, they must be bounded (passed to the parser in name/value pairs) prior to evaluation. This part is abstracted, to allow concrete sub-classes to deal with them in a manner most befitting its specialized purpose.

See Also:
SimpleConditionParser, ComplexConditionParser

Field Summary
protected  Token current
           
protected  boolean err
           
protected  java.lang.String expr
           
protected  int OP_AND
           
protected  int OP_EQ
           
protected  int OP_GE
           
protected  int OP_GT
           
protected  int OP_LE
           
protected  int OP_LT
           
protected  int OP_OR
           
protected  civquest.parser.condition.ConditionTokenizer tokenizer
           
protected  civquest.parser.condition.BoolExpression tree
           
 
Constructor Summary
ConditionParser(java.lang.String expr)
          Creates a parser for the given expression, for which a parse-tree is immediately constructed.
 
Method Summary
abstract  void bind(java.lang.String name, int val)
          Binds a value to a variable name.
protected  void error(java.lang.String msg)
           
 boolean eval()
          Evaluates the expression according to the most recently bound values for variables.
abstract  int getBinding(java.lang.String name)
          Gets the value for a bound variable.
protected  void next()
           
protected  civquest.parser.condition.BoolExpression parseCompoundExpr()
           
protected  civquest.parser.condition.BoolExpression parseExpr()
           
protected  int parseLogicOperator()
           
protected  civquest.parser.condition.AritExpression parseOperand()
           
protected  int parseRelativeOperator()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tokenizer

protected civquest.parser.condition.ConditionTokenizer tokenizer

current

protected Token current

err

protected boolean err

expr

protected java.lang.String expr

tree

protected civquest.parser.condition.BoolExpression tree

OP_EQ

protected final int OP_EQ
See Also:
Constant Field Values

OP_LT

protected final int OP_LT
See Also:
Constant Field Values

OP_LE

protected final int OP_LE
See Also:
Constant Field Values

OP_GT

protected final int OP_GT
See Also:
Constant Field Values

OP_GE

protected final int OP_GE
See Also:
Constant Field Values

OP_AND

protected final int OP_AND
See Also:
Constant Field Values

OP_OR

protected final int OP_OR
See Also:
Constant Field Values
Constructor Detail

ConditionParser

public ConditionParser(java.lang.String expr)
Creates a parser for the given expression, for which a parse-tree is immediately constructed.

Parameters:
expr - The conditional expression to be evaluated.
Method Detail

bind

public abstract void bind(java.lang.String name,
                          int val)
Binds a value to a variable name. Note that a concrete ConditionParser may have other preferred means for this.

Parameters:
name - The name of the variable to be bound
val - The value to bind to the name

getBinding

public abstract int getBinding(java.lang.String name)
Gets the value for a bound variable. Note that a concrete ConditionParser may have other preferred means for this.

Parameters:
name - The name of the variable
Returns:
the current value for the variable

eval

public boolean eval()
Evaluates the expression according to the most recently bound values for variables.

Returns:
true if the condition holds, false otherwise.

parseExpr

protected civquest.parser.condition.BoolExpression parseExpr()

parseCompoundExpr

protected civquest.parser.condition.BoolExpression parseCompoundExpr()

parseOperand

protected civquest.parser.condition.AritExpression parseOperand()

parseLogicOperator

protected int parseLogicOperator()

parseRelativeOperator

protected int parseRelativeOperator()

error

protected void error(java.lang.String msg)

next

protected void next()