class ExpEvaluator

An expression parser and evaluator. More...

Full nameTelEngine::ExpEvaluator
Definition#include <libs/yscript/yatescript.h>
InheritsTelEngine::DebugEnabler [public ]
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Types

Public Methods

Public Static Methods

Protected Methods

Protected Static Methods

Protected Members


Detailed Description

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

enum Parser { C, SQL, }

Parser

Parsing styles

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, OpcCopy, OpcAssign = 0x0100, OpcPrivate = 0x1000 }

Opcode

Operation codes

explicit  ExpEvaluator (const TokenDict* operators = 0, const TokenDict* unaryOps = 0)

ExpEvaluator

Constructs an evaluator from an operator dictionary

Parameters:
operatorsPointer to operator dictionary, longest strings first
unaryOpsPointer to unary operators dictionary, longest strings first

explicit  ExpEvaluator (Parser style)

ExpEvaluator

Constructs an evaluator from a parser style

Parameters:
styleStyle of parsing to use

 ExpEvaluator (const ExpEvaluator& original)

ExpEvaluator

Copy constructor

Parameters:
originalEvaluator to copy the operation list from

 ~ExpEvaluator ()

~ExpEvaluator

[virtual]

Destructor

int  compile (ParsePoint& expr, GenObject* context = 0)

compile

Parse and compile an expression

Parameters:
exprParsing context to compile
contextPointer to arbitrary object to be passed to called methods

Returns: Number of expressions compiled, zero on error

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

evaluate

[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

inline bool  evaluate (ObjList& results, GenObject* context = 0)

evaluate

[const]

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

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

evaluate

[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)

evaluate

[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

inline bool  simplify ()

simplify

Simplify the expression, performs constant folding

Returns: True if the expression was simplified

inline bool  inError ()

inError

[const]

Check if a parse or compile error was encountered

Returns: True if the evaluator encountered an error

inline unsigned int  lineNumber ()

lineNumber

[const]

Retrieve the number of line currently being parsed

Returns: Number of current parsed line, 1 is the first line

bool  null ()

null

[const virtual]

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

Returns: True if the expression is completely empty

void  dump (const ObjList& codes, String& res)

dump

[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)

dump

[const virtual]

Dump the postfix expression according to current operators dictionary

Parameters:
resResult string representation of operations

inline String  dump (const ObjList& codes)

dump

[const]

Dump a list of operations according to current operators dictionary

Parameters:
codesList of operation codes

Returns: String representation of operations

inline String  dump ()

dump

[const]

Dump the postfix expression according to current operators dictionary

Returns: String representation of operations

inline const TokenDict*  operators ()

operators

[const]

Retrieve the internally used operator dictionary

Returns: Pointer to operators dictionary in use

inline const TokenDict*  unaryOps ()

unaryOps

[const]

Retrieve the internally used unary operators dictionary

Returns: Pointer to unary operators dictionary in use

inline ExpExtender*  extender ()

extender

[const]

Retrieve the internally used expression extender

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

void  extender (ExpExtender* ext)

extender

Set the expression extender to use in evaluation

Parameters:
extPointer to the extender to use, NULL to remove current

unsigned int  getLineOf (ExpOperation* op1, ExpOperation* op2 = 0, ExpOperation* op3 = 0)

getLineOf

[static]

Retrieve the line number from one to three operands

Parameters:
op1First operand
op2Optional second operand
op3Optional third operand

Returns: Line number at compile time, zero if not found

void  pushOne (ObjList& stack, ExpOperation* oper)

pushOne

[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

ExpOperation*  popOne (ObjList& stack)

popOne

[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

ExpOperation*  popAny (ObjList& stack)

popAny

[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

ExpOperation*  popValue (ObjList& stack, GenObject* context = 0)

popValue

[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

bool  runOperation (ObjList& stack, const ExpOperation& oper, GenObject* context = 0)

runOperation

[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  runAllFields (ObjList& stack, GenObject* context = 0)

runAllFields

[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

char  skipWhites (ParsePoint& expr)

skipWhites

[protected virtual]

Method to skip over whitespaces, count parsed lines too

Parameters:
exprCurrent parsing context, advances on expression to be compiled

Returns: First character after whitespaces where expr points

inline char  condLower (char chr, bool makeLower)

condLower

[protected static]

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

Opcode  getOperator (const char*& expr, const TokenDict* operators, bool caseInsensitive = false)

getOperator

[protected const]

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

bool  keywordLetter (char c)

keywordLetter

[protected const virtual]

Check if a character can be a letter character in a keyword or identifier

Parameters:
cCharacter to check

Returns: True if the character can be part of a keyword or identifier

bool  keywordDigit (char c)

keywordDigit

[protected const virtual]

Check if a character can be can be a digit character in a keyword or identifier

Parameters:
cCharacter to check

Returns: True if the character can be part of a keyword or identifier

bool  keywordChar (char c)

keywordChar

[protected const 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

int  getKeyword (const char* str)

getKeyword

[protected const 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

bool  gotError (const char* error = 0, const char* text = 0, unsigned int line = 0)

gotError

[protected const]

Helper method to display debugging errors internally

Parameters:
errorText of the error
textOptional text that caused the error
lineNumber of line generating the error, zero for parsing errors

Returns: Always returns false

bool  gotError (const char* error = 0, const char* text = 0, unsigned int line = 0)

gotError

[protected]

Helper method to set error flag and display debugging errors internally

Parameters:
errorText of the error
textOptional text that caused the error
lineNumber of line generating the error, zero for parsing errors

Returns: Always returns false

inline bool  gotError (const char* error, unsigned int line)

gotError

[protected const]

Helper method to display debugging errors internally

Parameters:
errorText of the error
lineNumber of line generating the error, zero for parsing errors

Returns: Always returns false

inline bool  gotError (const char* error, unsigned int line)

gotError

[protected]

Helper method to set error flag and display debugging errors internally

Parameters:
errorText of the error
lineNumber of line generating the error, zero for parsing errors

Returns: Always returns false

void  formatLineNo (String& buf, unsigned int line)

formatLineNo

[protected const virtual]

Formats a line number to display in error messages

Parameters:
bufString buffer used to return the value
lineLine number to format

bool  runCompile (ParsePoint& expr, char stop, GenObject* nested = 0)

runCompile

[protected]

Runs the parser and compiler for one (sub)expression

Parameters:
exprCurrent parsing context, advances on expression to be compiled
stopOptional character expected after the expression
nestedUser defined object to pass for nested parsing

Returns: True if one expression was compiled and a separator follows

bool  runCompile (ParsePoint& expr, const char* stop = 0, GenObject* nested = 0)

runCompile

[protected virtual]

Runs the parser and compiler for one (sub)expression

Parameters:
exprCurrent parsing context, advances on expression to be compiled
stopOptional list of possible characters expected after the expression
nestedUser defined object to pass for nested parsing

Returns: True if one expression was compiled and a separator follows

char  skipComments (ParsePoint& expr, GenObject* context = 0)

skipComments

[protected virtual]

Skip over comments and whitespaces

Parameters:
exprCurrent parsing context, advances on expression to be compiled
contextPointer to arbitrary object to be passed to called methods

Returns: First character after comments or whitespaces where expr points

int  preProcess (ParsePoint& expr, GenObject* context = 0)

preProcess

[protected virtual]

Process top-level preprocessor directives

Parameters:
exprCurrent parsing context, advances on expression to be compiled
contextPointer to arbitrary object to be passed to called methods

Returns: Number of expressions compiled, negative if no more directives

Opcode  getOperator (ParsePoint& expr)

getOperator

[protected virtual]

Returns next operator in the parsed text

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: Operator code, OpcNone on failure

Opcode  getUnaryOperator (ParsePoint& expr)

getUnaryOperator

[protected virtual]

Returns next unary operator in the parsed text

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: Operator code, OpcNone on failure

Opcode  getPostfixOperator (ParsePoint& expr, int precedence = 0)

getPostfixOperator

[protected virtual]

Returns next unary postfix operator in the parsed text

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds
precedenceThe precedence of the previous operator

Returns: Operator code, OpcNone on failure

const char*  getOperator (Opcode oper)

getOperator

[protected const 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

int  getPrecedence (Opcode oper)

getPrecedence

[protected const virtual]

Get the precedence of an operator

Parameters:
operOperator code

Returns: Precedence of the operator, zero (lowest) if unknown

bool  getRightAssoc (Opcode oper)

getRightAssoc

[protected const 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

bool  getSeparator (ParsePoint& expr, bool remove)

getSeparator

[protected virtual]

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

Parameters:
exprCurrent parsing context to check, advances on expression to be compiled if asked to remove separator
removeTrue to skip past the found separator

Returns: True if a separator was found

bool  getInstruction (ParsePoint& expr, char stop = 0, GenObject* nested = 0)

getInstruction

[protected virtual]

Get an instruction or block, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds
stopOptional character expected after the instruction
nestedUser defined object passed from nested parsing

Returns: True if succeeded, must add the operands internally

bool  getOperand (ParsePoint& expr, bool endOk = true, int precedence = 0)

getOperand

[protected virtual]

Get an operand, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds
endOkConsider reaching the end of string a success
precedenceThe precedence of the previous operator

Returns: True if succeeded, must add the operand internally

bool  getSimple (ParsePoint& expr, bool constOnly = false)

getSimple

[protected virtual]

Get an inline simple type, usually string or number

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds
constOnlyReturn only inline constants

Returns: True if succeeded, must add the operand internally

bool  getNumber (ParsePoint& expr)

getNumber

[protected virtual]

Get a numerical operand, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: True if succeeded, must add the operand internally

bool  getString (ParsePoint& expr)

getString

[protected virtual]

Get a string operand, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: True if succeeded, must add the operand internally

bool  getFunction (ParsePoint& expr)

getFunction

[protected virtual]

Get a function call, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: True if succeeded, must add the operand internally

bool  getString (const char*& expr, String& str)

getString

[protected virtual]

Helper method - get a string, advance parsing pointer past it

Parameters:
exprPointer to string separator, gets advanced on success
strString in which the result is returned

Returns: True if succeeded

bool  getEscape (const char*& expr, String& str, char sep)

getEscape

[protected virtual]

Helper method - get an escaped component of a string

Parameters:
exprPointer past escape character, gets advanced on success
strString in which the result is returned
sepString separator character

Returns: True if succeeded

bool  getField (ParsePoint& expr)

getField

[protected virtual]

Get a field keyword, advance parsing pointer past it

Parameters:
exprCurrent parsing context, advances on expression to be compiled if it succeeds

Returns: True if succeeded, must add the operand internally

void  addOpcode (ExpOperation* oper, unsigned int line = 0)

addOpcode

[protected]

Add an aready built operation to the expression and set its line number

Parameters:
operOperation to add
lineLine number where operation was compiled, zero to used parsing point

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

addOpcode

[protected]

Add a simple operator to the expression

Parameters:
operOperator code to add
barrierTrue to create an evaluator stack barrier

Returns: Newly added operation

ExpOperation*  addOpcode (Opcode oper, int64_t value, bool barrier = false)

addOpcode

[protected]

Add a simple operator to the expression

Parameters:
operOperator code to add
value64 bit nteger value to add
barrierTrue to create an evaluator stack barrier

Returns: Newly added operation

ExpOperation*  addOpcode (const String& value)

addOpcode

[protected]

Add a string constant to the expression

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

Returns: Newly added operation

ExpOperation*  addOpcode (int64_t value)

addOpcode

[protected]

Add an integer constant to the expression

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

Returns: Newly added operation

ExpOperation*  addOpcode (bool value)

addOpcode

[protected]

Add a boolean constant to the expression

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

Returns: Newly added operation

ExpOperation*  addOpcode (Opcode oper, const String& name, int64_t value = 0, bool barrier = false)

addOpcode

[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

Returns: Newly added operation

ExpOperation*  popOpcode ()

popOpcode

[protected]

Remove from the code and return the last operation

Returns: Operation removed from end of code, NULL if no operations remaining

bool  trySimplify ()

trySimplify

[protected virtual]

Try to apply simplification to the expression

Returns: True if the expression was simplified

bool  runEvaluate (const ObjList& opcodes, ObjList& stack, GenObject* context = 0)

runEvaluate

[protected const 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

bool  runEvaluate (const ObjVector& opcodes, ObjList& stack, GenObject* context = 0, unsigned int index = 0)

runEvaluate

[protected const 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

bool  runEvaluate (ObjList& stack, GenObject* context = 0)

runEvaluate

[protected const 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

bool  runFunction (ObjList& stack, const ExpOperation& oper, GenObject* context = 0)

runFunction

[protected const 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

bool  runField (ObjList& stack, const ExpOperation& oper, GenObject* context = 0)

runField

[protected const 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

bool  runAssign (ObjList& stack, const ExpOperation& oper, GenObject* context = 0)

runAssign

[protected const 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

void  dump (const ExpOperation& oper, String& res)

dump

[protected const virtual]

Dump a single operation according to current operators dictionary

Parameters:
operOperation to dump
resResult string representation of operations

const TokenDict* m_operators

m_operators

[protected]

const TokenDict* m_unaryOps

m_unaryOps

[protected]

ObjList m_opcodes

m_opcodes

[protected]

ObjList* m_lastOpcode

m_lastOpcode

[protected]

bool m_inError

m_inError

[protected]

unsigned int m_lineNo

m_lineNo

[protected]


Generated by: paulc on bussard on Sun Oct 20 21:06:06 2013, using kdoc 2.0a54.