Yate
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
ExpEvaluator Class Reference

An expression parser and evaluator. More...

#include <yatescript.h>

Inheritance diagram for ExpEvaluator:
DebugEnabler

List of all members.

Public Types

enum  Parser { C, SQL }
enum  Opcode {
  OpcNone = 0, OpcNull, OpcPush, OpcDrop,
  OpcDup, OpcSwap, OpcRot, OpcOver,
  OpcAdd, OpcSub, OpcMul, OpcDiv,
  OpcMod, OpcNeg, OpcIncPre, OpcDecPre,
  OpcIncPost, OpcDecPost, 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, OpcLabel,
  OpcAssign = 0x0100, OpcPrivate = 0x1000
}

Public Member Functions

 ExpEvaluator (const TokenDict *operators=0, const TokenDict *unaryOps=0)
 ExpEvaluator (Parser style)
 ExpEvaluator (const ExpEvaluator &original)
virtual ~ExpEvaluator ()
int compile (const char *expr, GenObject *context=0)
bool evaluate (ObjList *results, GenObject *context=0) const
bool evaluate (ObjList &results, GenObject *context=0) const
int evaluate (NamedList &results, unsigned int index=0, const char *prefix=0, GenObject *context=0) const
int evaluate (Array &results, unsigned int index, GenObject *context=0) const
bool simplify ()
bool inError () const
bool null () const
void dump (const ObjList &codes, String &res) const
void dump (String &res) const
String dump (const ObjList &codes) const
String dump () const
const TokenDictoperators () const
const TokenDictunaryOps () const
ExpExtenderextender () const
void extender (ExpExtender *ext)
virtual ExpOperationpopValue (ObjList &stack, GenObject *context=0) const
virtual bool runOperation (ObjList &stack, const ExpOperation &oper, GenObject *context=0) const
virtual bool runAllFields (ObjList &stack, GenObject *context=0) const

Static Public Member Functions

static void pushOne (ObjList &stack, ExpOperation *oper)
static ExpOperationpopOne (ObjList &stack)
static ExpOperationpopAny (ObjList &stack)

Protected Member Functions

Opcode getOperator (const char *&expr, const TokenDict *operators, bool caseInsensitive=false) const
virtual bool keywordChar (char c) const
virtual int getKeyword (const char *str) const
bool gotError (const char *error=0, const char *text=0) const
bool gotError (const char *error=0, const char *text=0)
virtual bool runCompile (const char *&expr, char stop=0, Opcode nested=OpcNone)
virtual char skipComments (const char *&expr, GenObject *context=0) const
virtual int preProcess (const char *&expr, GenObject *context=0)
virtual Opcode getOperator (const char *&expr)
virtual Opcode getUnaryOperator (const char *&expr)
virtual Opcode getPostfixOperator (const char *&expr)
virtual const char * getOperator (Opcode oper) const
virtual int getPrecedence (Opcode oper) const
virtual bool getRightAssoc (Opcode oper) const
virtual bool getSeparator (const char *&expr, bool remove)
virtual bool getInstruction (const char *&expr, Opcode nested=OpcNone)
virtual bool getOperand (const char *&expr, bool endOk=true)
virtual bool getNumber (const char *&expr)
virtual bool getString (const char *&expr)
virtual bool getFunction (const char *&expr)
virtual bool getField (const char *&expr)
ExpOperationaddOpcode (Opcode oper, bool barrier=false)
ExpOperationaddOpcode (Opcode oper, long int value, bool barrier=false)
ExpOperationaddOpcode (const String &value)
ExpOperationaddOpcode (long int value)
ExpOperationaddOpcode (bool value)
ExpOperationaddOpcode (Opcode oper, const String &name, long int value=0, bool barrier=false)
virtual bool trySimplify ()
virtual bool runEvaluate (const ObjList &opcodes, ObjList &stack, GenObject *context=0) const
virtual bool runEvaluate (const ObjVector &opcodes, ObjList &stack, GenObject *context=0, unsigned int index=0) const
virtual bool runEvaluate (ObjList &stack, GenObject *context=0) const
virtual bool runFunction (ObjList &stack, const ExpOperation &oper, GenObject *context=0) const
virtual bool runField (ObjList &stack, const ExpOperation &oper, GenObject *context=0) const
virtual bool runAssign (ObjList &stack, const ExpOperation &oper, GenObject *context=0) const

Static Protected Member Functions

static char skipWhites (const char *&expr)
static char condLower (char chr, bool makeLower)

Protected Attributes

const TokenDictm_operators
const TokenDictm_unaryOps
ObjList m_opcodes
bool m_inError

Detailed Description

An expression parser and evaluator.

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


Member Enumeration Documentation

enum Opcode

Operation codes

enum Parser

Parsing styles


Constructor & Destructor Documentation

ExpEvaluator ( const TokenDict operators = 0,
const TokenDict unaryOps = 0 
) [explicit]

Constructs an evaluator from an operator dictionary

Parameters:
operatorsPointer to operator dictionary, longest strings first
unaryOpsPointer to unary operators dictionary, longest strings first
ExpEvaluator ( Parser  style) [explicit]

Constructs an evaluator from a parser style

Parameters:
styleStyle of parsing to use
ExpEvaluator ( const ExpEvaluator original)

Copy constructor

Parameters:
originalEvaluator to copy the operation list from
virtual ~ExpEvaluator ( ) [virtual]

Destructor


Member Function Documentation

ExpOperation* addOpcode ( Opcode  oper,
bool  barrier = false 
) [protected]

Add a simple operator to the expression

Parameters:
operOperator code to add
barrierTrue to create an evaluator stack barrier
ExpOperation* addOpcode ( Opcode  oper,
long int  value,
bool  barrier = false 
) [protected]

Add a simple operator to the expression

Parameters:
operOperator code to add
valueInteger value to add
barrierTrue to create an evaluator stack barrier
ExpOperation* addOpcode ( const String value) [protected]

Add a string constant to the expression

Parameters:
valueString value to add, will be pushed on execution
ExpOperation* addOpcode ( long int  value) [protected]

Add an integer constant to the expression

Parameters:
valueInteger value to add, will be pushed on execution
ExpOperation* addOpcode ( bool  value) [protected]

Add a boolean constant to the expression

Parameters:
valueBoolean value to add, will be pushed on execution
ExpOperation* addOpcode ( Opcode  oper,
const String name,
long int  value = 0,
bool  barrier = false 
) [protected]

Add a function or field to the expression

Parameters:
operOperator code to add, must be OpcField or OpcFunc
nameName of the field or function, case sensitive
valueNumerical value used as parameter count to functions
barrierTrue to create an exavuator stack barrier
int compile ( const char *  expr,
GenObject context = 0 
)

Parse and compile an expression

Parameters:
exprPointer to expression to compile
contextPointer to arbitrary object to be passed to called methods
Returns:
Number of expressions compiled, zero on error
static char condLower ( char  chr,
bool  makeLower 
) [inline, static, protected]

Helper method to conditionally convert to lower case

Parameters:
chrCharacter to convert
makeLowerTrue to convert chr to lower case
Returns:
Converted character or original if conversion not requested
void dump ( const ObjList codes,
String res 
) const

Dump a list of operations according to current operators dictionary

Parameters:
codesList of operation codes
resResult string representation of operations
void dump ( String res) const [inline]

Dump the postfix expression according to current operators dictionary

Parameters:
resResult string representation of operations

References ExpEvaluator::dump().

Referenced by ExpEvaluator::dump().

String dump ( const ObjList codes) const [inline]

Dump a list of operations according to current operators dictionary

Parameters:
codesList of operation codes
Returns:
String representation of operations

References ExpEvaluator::dump().

Referenced by ExpEvaluator::dump().

String dump ( ) const [inline]

Dump the postfix expression according to current operators dictionary

Returns:
String representation of operations

References ExpEvaluator::dump().

Referenced by ExpEvaluator::dump().

bool evaluate ( ObjList results,
GenObject context = 0 
) const

Evaluate the expression, optionally return results

Parameters:
resultsList to fill with results row
contextPointer to arbitrary object to be passed to called methods
Returns:
True if expression evaluation succeeded, false on failure
bool evaluate ( ObjList results,
GenObject context = 0 
) const [inline]

Evaluate the expression, return computed results

Parameters:
resultsList to fill with results row
contextPointer to arbitrary object to be passed to called methods
Returns:
True if expression evaluation succeeded, false on failure

References ExpEvaluator::evaluate().

Referenced by ExpEvaluator::evaluate().

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

Evaluate the expression, return computed results

Parameters:
resultsList of parameters to populate with results row
indexIndex of result row, zero to not include an index
prefixPrefix to prepend to parameter names
contextPointer to arbitrary object to be passed to called methods
Returns:
Number of result columns, -1 on failure
int evaluate ( Array results,
unsigned int  index,
GenObject context = 0 
) const

Evaluate the expression, return computed results

Parameters:
resultsArray of result rows to populate
indexIndex of result row, zero to just set column headers
contextPointer to arbitrary object to be passed to called methods
Returns:
Number of result columns, -1 on failure
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:
extPointer to the extender to use, NULL to remove current
virtual bool getField ( const char *&  expr) [protected, virtual]

Get a field keyword, advance parsing pointer past it

Parameters:
exprPointer 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:
exprPointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally
virtual bool getInstruction ( const char *&  expr,
Opcode  nested = OpcNone 
) [protected, virtual]

Get an instruction or block, advance parsing pointer past it

Parameters:
exprPointer to text to parse, gets advanced on success
nestedThe instruction within this one is nested
Returns:
True if succeeded, must add the operands internally
virtual int getKeyword ( const char *  str) const [protected, virtual]

Helper method to count characters making a keyword

Parameters:
strPointer to text without whitespaces in front
Returns:
Length of the keyword, 0 if a valid keyword doesn't follow
virtual bool getNumber ( const char *&  expr) [protected, virtual]

Get a numerical operand, advance parsing pointer past it

Parameters:
exprPointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally
virtual bool getOperand ( const char *&  expr,
bool  endOk = true 
) [protected, virtual]

Get an operand, advance parsing pointer past it

Parameters:
exprPointer to text to parse, gets advanced on success
endOkConsider reaching the end of string a success
Returns:
True if succeeded, must add the operand internally
Opcode getOperator ( const char *&  expr,
const TokenDict operators,
bool  caseInsensitive = false 
) const [protected]

Helper method to return next operator in the parsed text

Parameters:
exprPointer to text to parse, gets advanced if succeeds
operatorsPointer to operators table to use
caseInsensitiveMatch case-insensitive if set
Returns:
Operator code, OpcNone on failure
virtual Opcode getOperator ( const char *&  expr) [protected, virtual]

Returns next operator in the parsed text

Parameters:
exprPointer 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:
operOperator code
Returns:
name of the operator, NULL if it doesn't have one
virtual Opcode getPostfixOperator ( const char *&  expr) [protected, virtual]

Returns next unary postfix operator in the parsed text

Parameters:
exprPointer to text to parse, gets advanced if succeeds
Returns:
Operator code, OpcNone on failure
virtual int getPrecedence ( Opcode  oper) const [protected, virtual]

Get the precedence of an operator

Parameters:
operOperator code
Returns:
Precedence of the operator, zero (lowest) if unknown
virtual bool getRightAssoc ( Opcode  oper) const [protected, virtual]

Get the associativity of an operator

Parameters:
operOperator code
Returns:
True if the operator is right-to-left associative, false if left-to-right
virtual bool getSeparator ( const char *&  expr,
bool  remove 
) [protected, virtual]

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

Parameters:
exprPointer to text to check, gets advanced if asked to remove separator
removeTrue to skip past the found separator
Returns:
True if a separator was found
virtual bool getString ( const char *&  expr) [protected, virtual]

Get a string operand, advance parsing pointer past it

Parameters:
exprPointer to text to parse, gets advanced on success
Returns:
True if succeeded, must add the operand internally
virtual Opcode getUnaryOperator ( const char *&  expr) [protected, virtual]

Returns next unary operator in the parsed text

Parameters:
exprPointer to text to parse, gets advanced if succeeds
Returns:
Operator code, OpcNone on failure
bool gotError ( const char *  error = 0,
const char *  text = 0 
) const [protected]

Helper method to display debugging errors internally

Parameters:
errorText of the error
textOptional text that caused the error
Returns:
Always returns false
bool gotError ( const char *  error = 0,
const char *  text = 0 
) [protected]

Helper method to set error flag and display debugging errors internally

Parameters:
errorText of the error
textOptional text that caused the error
Returns:
Always returns false
bool inError ( ) const [inline]

Check if a parse or compile error was encountered

Returns:
True if the evaluator encountered an error
virtual bool keywordChar ( char  c) const [protected, virtual]

Check if a character can be part of a keyword or identifier

Parameters:
cCharacter to check
Returns:
True if the character can be part of a keyword or identifier
bool null ( ) const [inline]

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

Returns:
True if the expression is completely empty
const TokenDict* operators ( ) const [inline]

Retrieve the internally used operator dictionary

Returns:
Pointer to operators dictionary in use
static ExpOperation* popAny ( ObjList stack) [static]

Pops any operand (including barriers) off an evaluation stack

Parameters:
stackEvaluation stack to remove the operand from
Returns:
Operator removed from stack, NULL if stack underflow
static ExpOperation* popOne ( ObjList stack) [static]

Pops an operand off an evaluation stack, does not pop a barrier

Parameters:
stackEvaluation stack to remove the operand from
Returns:
Operator removed from stack, NULL if stack underflow
virtual ExpOperation* popValue ( ObjList stack,
GenObject context = 0 
) const [virtual]

Pops and evaluate the value of an operand off an evaluation stack, does not pop a barrier

Parameters:
stackEvaluation stack to remove the operand from
contextPointer to arbitrary object to be passed to called methods
Returns:
Value removed from stack, NULL if stack underflow or field not evaluable
virtual int preProcess ( const char *&  expr,
GenObject context = 0 
) [protected, virtual]

Process top-level preprocessor directives

Parameters:
exprPointer to expression cursor, gets advanced
contextPointer to arbitrary object to be passed to called methods
Returns:
Number of expressions compiled, negative if no more directives
static void pushOne ( ObjList stack,
ExpOperation oper 
) [static]

Push an operand on an evaluation stack

Parameters:
stackEvaluation stack to remove the operand from
operOperation to push on stack, NULL will not be pushed
virtual bool runAllFields ( ObjList stack,
GenObject context = 0 
) const [virtual]

Convert all fields on the evaluation stack to their values

Parameters:
stackEvaluation stack to evaluate fields from
contextPointer to arbitrary object to be passed to called methods
Returns:
True if all fields on the stack were evaluated properly
virtual bool runAssign ( ObjList stack,
const ExpOperation oper,
GenObject context = 0 
) const [protected, virtual]

Try to assign a value to a single field

Parameters:
stackEvaluation stack in use
operField to assign to, contains the field name and new value
contextPointer to arbitrary object to be passed to called methods
Returns:
True if assignment succeeded
virtual bool runCompile ( const char *&  expr,
char  stop = 0,
Opcode  nested = OpcNone 
) [protected, virtual]

Runs the parser and compiler for one (sub)expression

Parameters:
exprPointer to text to parse, gets advanced
stopOptional character expected after the expression
nestedThe instruction within this expression is nested
Returns:
True if one expression was compiled and a separator follows
virtual bool runEvaluate ( const ObjList opcodes,
ObjList stack,
GenObject context = 0 
) const [protected, virtual]

Try to evaluate a list of operation codes

Parameters:
opcodesList of operation codes to evaluate
stackEvaluation stack in use, results are left on stack
contextPointer to arbitrary object to be passed to called methods
Returns:
True if evaluation succeeded
virtual bool runEvaluate ( const ObjVector opcodes,
ObjList stack,
GenObject context = 0,
unsigned int  index = 0 
) const [protected, virtual]

Try to evaluate a vector of operation codes

Parameters:
opcodesObjVector of operation codes to evaluate
stackEvaluation stack in use, results are left on stack
contextPointer to arbitrary object to be passed to called methods
indexIndex in operation codes to start evaluation from
Returns:
True if evaluation succeeded
virtual bool runEvaluate ( ObjList stack,
GenObject context = 0 
) const [protected, virtual]

Try to evaluate the expression

Parameters:
stackEvaluation stack in use, results are left on stack
contextPointer to arbitrary object to be passed to called methods
Returns:
True if evaluation succeeded
virtual bool runField ( ObjList stack,
const ExpOperation oper,
GenObject context = 0 
) const [protected, virtual]

Try to evaluate a single field

Parameters:
stackEvaluation stack in use, field value must be pushed on it
operField to evaluate
contextPointer to arbitrary object to be passed to called methods
Returns:
True if evaluation succeeded
virtual bool runFunction ( ObjList stack,
const ExpOperation oper,
GenObject context = 0 
) const [protected, virtual]

Try to evaluate a single function

Parameters:
stackEvaluation stack in use, parameters are popped off this stack and results are pushed back on stack
operFunction to evaluate
contextPointer to arbitrary object to be passed to called methods
Returns:
True if evaluation succeeded
virtual bool runOperation ( ObjList stack,
const ExpOperation oper,
GenObject context = 0 
) const [virtual]

Try to evaluate a single operation

Parameters:
stackEvaluation stack in use, operands are popped off this stack and results are pushed back on stack
operOperation to execute
contextPointer to arbitrary object to be passed to called methods
Returns:
True if evaluation succeeded
bool simplify ( ) [inline]

Simplify the expression, performs constant folding

Returns:
True if the expression was simplified
virtual char skipComments ( const char *&  expr,
GenObject context = 0 
) const [protected, virtual]

Skip over comments and whitespaces

Parameters:
exprPointer to expression cursor, gets advanced
contextPointer to arbitrary object to be passed to called methods
Returns:
First character after comments or whitespaces where expr points
static char skipWhites ( const char *&  expr) [static, protected]

Helper method to skip over whitespaces

Parameters:
exprPointer to expression cursor, gets advanced
Returns:
First character after whitespaces where expr points
virtual bool trySimplify ( ) [protected, virtual]

Try to apply simplification to the expression

Returns:
True if the expression was simplified
const TokenDict* unaryOps ( ) const [inline]

Retrieve the internally used unary operators dictionary

Returns:
Pointer to unary operators dictionary in use

Member Data Documentation

bool m_inError [protected]

Flag that we encountered a parse or compile error

ObjList m_opcodes [protected]

Internally used list of operands and operator codes

const TokenDict* m_operators [protected]

Internally used operator dictionary

const TokenDict* m_unaryOps [protected]

Internally used unary operators dictionary


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