|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcivquest.parser.condition.ConditionParser
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.&& ')' | '(' || ')'
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 |
protected civquest.parser.condition.ConditionTokenizer tokenizer
protected Token current
protected boolean err
protected java.lang.String expr
protected civquest.parser.condition.BoolExpression tree
protected final int OP_EQ
protected final int OP_LT
protected final int OP_LE
protected final int OP_GT
protected final int OP_GE
protected final int OP_AND
protected final int OP_OR
Constructor Detail |
public ConditionParser(java.lang.String expr)
expr
- The conditional expression to be evaluated.Method Detail |
public abstract void bind(java.lang.String name, int val)
name
- The name of the variable to be boundval
- The value to bind to the namepublic abstract int getBinding(java.lang.String name)
name
- The name of the variable
public boolean eval()
protected civquest.parser.condition.BoolExpression parseExpr()
protected civquest.parser.condition.BoolExpression parseCompoundExpr()
protected civquest.parser.condition.AritExpression parseOperand()
protected int parseLogicOperator()
protected int parseRelativeOperator()
protected void error(java.lang.String msg)
protected void next()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |