ExpEvaluator Class Reference

An expression parser and evaluator. More...

#include <yateclass.h>

List of all members.

Public Types

enum  Parser { C, SQL }
enum  Opcode {
  OpcNone, OpcNull, OpcPush, OpcDrop,
  OpcDup, OpcSwap, OpcRot, OpcOver,
  OpcAdd, OpcSub, OpcMul, OpcDiv,
  OpcMod, OpcNeg, OpcAnd, OpcOr,
  OpcXor, OpcNot, OpcShl, OpcShr,
  OpcLAnd, OpcLOr, OpcLXor, OpcLNot,
  OpcCat, OpcReM, OpcReIM, OpcReNm,
  OpcReINm, OpcLike, OpcILike, OpcNLike,
  OpcNIlike, OpcEq, OpcNe, OpcGt,
  OpcLt, OpcGe, OpcLe, OpcCond,
  OpcAs, OpcField, OpcFunc
}

Public Member Functions

 ExpEvaluator (const TokenDict *operators=0)
 ExpEvaluator (Parser style)
 ExpEvaluator (const ExpEvaluator &original)
virtual ~ExpEvaluator ()
int compile (const char *expr)
bool evaluate (ObjList *results)
bool evaluate (ObjList &results)
int evaluate (NamedList &results, unsigned int index=0, const char *prefix=0)
int evaluate (Array &results, unsigned int index)
bool simplify ()
bool null () const
String dump () const
const TokenDictoperators ()
ExpExtenderextender () const
void extender (ExpExtender *ext)

Protected Member Functions

char skipWhites (const char *&expr) const
int getKeyword (const char *str) const
bool gotError (const char *error=0, const char *text=0)
virtual bool runCompile (const char *&expr)
virtual Opcode getOperator (const char *&expr) const
virtual const char * getOperator (Opcode oper) const
virtual int getPrecedence (Opcode oper)
virtual bool getSeparator (const char *&expr, bool remove)
virtual bool getOperand (const char *&expr)
virtual bool getNumber (const char *&expr)
virtual bool getString (const char *&expr)
virtual bool getFunction (const char *&expr)
virtual bool getField (const char *&expr)
void addOpcode (Opcode oper)
void addOpcode (const String &value)
void addOpcode (long int value)
void addOpcode (Opcode oper, const String &name, long int value=0)
ExpOperationpopOne (ObjList &stack)
virtual bool trySimplify ()
virtual bool runEvaluate (ObjList &stack)
virtual bool runOperation (ObjList &stack, const ExpOperation &oper)
virtual bool runFunction (ObjList &stack, const ExpOperation &oper)
virtual bool runField (ObjList &stack, const ExpOperation &oper)

Protected Attributes

const TokenDictm_operators
ObjList m_opcodes


Detailed Description

An expression parser and evaluator.

A class used to build stack based (posifix) expression parsers and evaluators


Member Enumeration Documentation

enum Parser

Parsing styles

enum Opcode

Operation codes


Constructor & Destructor Documentation

ExpEvaluator ( const TokenDict operators = 0  )  [explicit]

Constructs an evaluator from an operator dictionary

Parameters:
operators Pointer to operator dictionary, longest strings first

ExpEvaluator ( Parser  style  )  [explicit]

Constructs an evaluator from a parser style

Parameters:
style Style of parsing to use

ExpEvaluator ( const ExpEvaluator original  ) 

Copy constructor

Parameters:
original Evaluator to copy the operation list from

virtual ~ExpEvaluator (  )  [virtual]

Destructor


Member Function Documentation

int compile ( const char *  expr  ) 

Parse and compile an expression

Parameters:
expr Pointer to expression to compile
Returns:
Number of expressions compiled, zero on error

bool evaluate ( ObjList results  ) 

Evaluate the expression, optionally return results

Parameters:
results List to fill with results row
Returns:
True if expression evaluation succeeded, false on failure

bool evaluate ( ObjList results  )  [inline]

Evaluate the expression, return computed results

Parameters:
results List to fill with results row
Returns:
True if expression evaluation succeeded, false on failure

int evaluate ( NamedList results,
unsigned int  index = 0,
const char *  prefix = 0 
)

Evaluate the expression, return computed results

Parameters:
results List of parameters to populate with results row
index Index of result row, zero to not include an index
prefix Prefix to prepend to parameter names
Returns:
Number of result columns, -1 on failure

int evaluate ( Array results,
unsigned int  index 
)

Evaluate the expression, return computed results

Parameters:
results Array of result rows to populate
index Index of result row, zero to just set column headers
Returns:
Number of result columns, -1 on failure

bool simplify (  )  [inline]

Simplify the expression, performs constant folding

Returns:
True if the expression was simplified

bool null (  )  const [inline]

Check if the expression is empty (no operands or operators)

Returns:
True if the expression is completely empty

String dump (  )  const

Dump the postfix expression according to current operators dictionary

Returns:
String representation of operations

const TokenDict* operators (  )  [inline]

Retrieve the internally used operator dictionary

Returns:
Pointer to operators dictionary in use

ExpExtender* extender (  )  const [inline]

Retrieve the internally used expression extender

Returns:
Pointer to the extender in use, NULL if none

void extender ( ExpExtender ext  ) 

Set the expression extender to use in evaluation

Parameters:
ext Pointer to the extender to use, NULL to remove current

char skipWhites ( const char *&  expr  )  const [protected]

Helper method to skip over whitespaces

Parameters:
expr Pointer to expression cursor, gets advanced
Returns:
First character after whitespaces where expr points

int getKeyword ( const char *  str  )  const [protected]

Helper method to count characters making a keyword

Parameters:
str Pointer to text without whitespaces in front
Returns:
Length of the keyword, 0 if a valid keyword doesn't follow

bool gotError ( const char *  error = 0,
const char *  text = 0 
) [protected]

Helper method to display debugging errors internally

Parameters:
error Text of the error
text Optional text that caused the error
Returns:
Always returns false

virtual bool runCompile ( const char *&  expr  )  [protected, virtual]

Runs the parser and compiler for one (sub)expression

Parameters:
expr Pointer to text to parse, gets advanced
Returns:
True if one expression was compiled and a separator follows

virtual Opcode getOperator ( const char *&  expr  )  const [protected, virtual]

Returns next operator in the parsed text

Parameters:
expr Pointer to text to parse, gets advanced if succeeds
Returns:
Operator code, OpcNone on failure

virtual const char* getOperator ( Opcode  oper  )  const [protected, virtual]

Helper method to get the canonical name of an operator

Parameters:
oper Operator code
Returns:
name of the operator, NULL if it doesn't have one

virtual int getPrecedence ( Opcode  oper  )  [protected, virtual]

Get the precedence of an operator

Parameters:
oper Operator code
Returns:
Precedence of the operator, zero (lowest) if unknown

virtual bool getSeparator ( const char *&  expr,
bool  remove 
) [protected, virtual]

Check if we are at an expression separator and optionally skip past it

Parameters:
expr Pointer to text to check, gets advanced if asked to remove separator
remove True to skip past the found separator
Returns:
True if a separator was found

virtual bool getOperand ( const char *&  expr  )  [protected, virtual]

Get an operand, advance parsing pointer past it

Parameters:
expr Pointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally

virtual bool getNumber ( const char *&  expr  )  [protected, virtual]

Get a numerical operand, advance parsing pointer past it

Parameters:
expr Pointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally

virtual bool getString ( const char *&  expr  )  [protected, virtual]

Get a string operand, advance parsing pointer past it

Parameters:
expr Pointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally

virtual bool getFunction ( const char *&  expr  )  [protected, virtual]

Get a function call, advance parsing pointer past it

Parameters:
expr Pointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally

virtual bool getField ( const char *&  expr  )  [protected, virtual]

Get a field keyword, advance parsing pointer past it

Parameters:
expr Pointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally

void addOpcode ( Opcode  oper  )  [protected]

Add a simple operator to the expression

Parameters:
oper Operator code to add

void addOpcode ( const String value  )  [protected]

Add a string constant to the expression

Parameters:
value String value to add, will be pushed on execution

void addOpcode ( long int  value  )  [protected]

Add an integer constant to the expression

Parameters:
value Integer value to add, will be pushed on execution

void addOpcode ( Opcode  oper,
const String name,
long int  value = 0 
) [protected]

Add a function or field to the expression

Parameters:
oper Operator code to add, must be OpcField or OpcFunc
name Name of the field or function, case sensitive
value Numerical value used as parameter count to functions

ExpOperation* popOne ( ObjList stack  )  [protected]

Pops an operand off the evaluation stack

Parameters:
stack Evaluation stack to remove the operand from
Returns:
Operator removed from stack, NULL if stack underflow

virtual bool trySimplify (  )  [protected, virtual]

Try to apply simplification to the expression

Returns:
True if the expression was simplified

virtual bool runEvaluate ( ObjList stack  )  [protected, virtual]

Try to evaluate the expression

Parameters:
stack Evaluation stack in use, results are left on stack
Returns:
True if evaluation succeeded

virtual bool runOperation ( ObjList stack,
const ExpOperation oper 
) [protected, virtual]

Try to evaluate a single operation

Parameters:
stack Evaluation stack in use, operands are popped off this stack and results are pushed back on stack
oper Operation to execute
Returns:
True if evaluation succeeded

virtual bool runFunction ( ObjList stack,
const ExpOperation oper 
) [protected, virtual]

Try to evaluate a single function

Parameters:
stack Evaluation stack in use, parameters are popped off this stack and results are pushed back on stack
oper Function to evaluate
Returns:
True if evaluation succeeded

virtual bool runField ( ObjList stack,
const ExpOperation oper 
) [protected, virtual]

Try to evaluate a single field

Parameters:
stack Evaluation stack in use, field value must be pushed on it
oper Field to evaluate
Returns:
True if evaluation succeeded


Member Data Documentation

const TokenDict* m_operators [protected]

Internally used operator dictionary

ObjList m_opcodes [protected]

Internally used list of operands and operator codes


The documentation for this class was generated from the following file:

Generated on Sat May 28 18:28:23 2011 for Yate by  doxygen 1.5.6