Yate
|
An expression parser and evaluator. More...
#include <yatescript.h>
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 TokenDict * | operators () const |
const TokenDict * | unaryOps () const |
ExpExtender * | extender () const |
void | extender (ExpExtender *ext) |
virtual ExpOperation * | popValue (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 ExpOperation * | popOne (ObjList &stack) |
static ExpOperation * | popAny (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) |
ExpOperation * | addOpcode (Opcode oper, bool barrier=false) |
ExpOperation * | addOpcode (Opcode oper, long int value, bool barrier=false) |
ExpOperation * | addOpcode (const String &value) |
ExpOperation * | addOpcode (long int value) |
ExpOperation * | addOpcode (bool value) |
ExpOperation * | addOpcode (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 TokenDict * | m_operators |
const TokenDict * | m_unaryOps |
ObjList | m_opcodes |
bool | m_inError |
An expression parser and evaluator.
A class used to build stack based (posifix) expression parsers and evaluators
enum Opcode |
Operation codes
enum Parser |
Parsing styles
ExpEvaluator | ( | const TokenDict * | operators = 0 , |
const TokenDict * | unaryOps = 0 |
||
) | [explicit] |
Constructs an evaluator from an operator dictionary
operators | Pointer to operator dictionary, longest strings first |
unaryOps | Pointer to unary operators dictionary, longest strings first |
ExpEvaluator | ( | Parser | style | ) | [explicit] |
Constructs an evaluator from a parser style
style | Style of parsing to use |
ExpEvaluator | ( | const ExpEvaluator & | original | ) |
Copy constructor
original | Evaluator to copy the operation list from |
virtual ~ExpEvaluator | ( | ) | [virtual] |
Destructor
ExpOperation* addOpcode | ( | Opcode | oper, |
bool | barrier = false |
||
) | [protected] |
Add a simple operator to the expression
oper | Operator code to add |
barrier | True to create an evaluator stack barrier |
ExpOperation* addOpcode | ( | Opcode | oper, |
long int | value, | ||
bool | barrier = false |
||
) | [protected] |
Add a simple operator to the expression
oper | Operator code to add |
value | Integer value to add |
barrier | True to create an evaluator stack barrier |
ExpOperation* addOpcode | ( | const String & | value | ) | [protected] |
Add a string constant to the expression
value | String value to add, will be pushed on execution |
ExpOperation* addOpcode | ( | long int | value | ) | [protected] |
Add an integer constant to the expression
value | Integer value to add, will be pushed on execution |
ExpOperation* addOpcode | ( | bool | value | ) | [protected] |
Add a boolean constant to the expression
value | Boolean 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
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 |
barrier | True to create an exavuator stack barrier |
Parse and compile an expression
expr | Pointer to expression to compile |
context | Pointer to arbitrary object to be passed to called methods |
static char condLower | ( | char | chr, |
bool | makeLower | ||
) | [inline, static, protected] |
Helper method to conditionally convert to lower case
chr | Character to convert |
makeLower | True to convert chr to lower case |
Dump a list of operations according to current operators dictionary
codes | List of operation codes |
res | Result string representation of operations |
Dump the postfix expression according to current operators dictionary
res | Result string representation of operations |
References ExpEvaluator::dump().
Referenced by ExpEvaluator::dump().
Dump a list of operations according to current operators dictionary
codes | List of operation codes |
References ExpEvaluator::dump().
Referenced by ExpEvaluator::dump().
Dump the postfix expression according to current operators dictionary
References ExpEvaluator::dump().
Referenced by ExpEvaluator::dump().
Evaluate the expression, optionally return results
results | List to fill with results row |
context | Pointer to arbitrary object to be passed to called methods |
Evaluate the expression, return computed results
results | List to fill with results row |
context | Pointer to arbitrary object to be passed to called methods |
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
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 |
context | Pointer to arbitrary object to be passed to called methods |
Evaluate the expression, return computed results
results | Array of result rows to populate |
index | Index of result row, zero to just set column headers |
context | Pointer to arbitrary object to be passed to called methods |
ExpExtender* extender | ( | ) | const [inline] |
Retrieve the internally used expression extender
void extender | ( | ExpExtender * | ext | ) |
Set the expression extender to use in evaluation
ext | Pointer 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
expr | Pointer to text to parse, gets advanced on success |
virtual bool getFunction | ( | const char *& | expr | ) | [protected, virtual] |
Get a function call, advance parsing pointer past it
expr | Pointer to text to parse, gets advanced on success |
virtual bool getInstruction | ( | const char *& | expr, |
Opcode | nested = OpcNone |
||
) | [protected, virtual] |
Get an instruction or block, advance parsing pointer past it
expr | Pointer to text to parse, gets advanced on success |
nested | The instruction within this one is nested |
virtual int getKeyword | ( | const char * | str | ) | const [protected, virtual] |
Helper method to count characters making a keyword
str | Pointer to text without whitespaces in front |
virtual bool getNumber | ( | const char *& | expr | ) | [protected, virtual] |
Get a numerical operand, advance parsing pointer past it
expr | Pointer to text to parse, gets advanced on success |
virtual bool getOperand | ( | const char *& | expr, |
bool | endOk = true |
||
) | [protected, virtual] |
Get an operand, advance parsing pointer past it
expr | Pointer to text to parse, gets advanced on success |
endOk | Consider reaching the end of string a success |
Opcode getOperator | ( | const char *& | expr, |
const TokenDict * | operators, | ||
bool | caseInsensitive = false |
||
) | const [protected] |
Helper method to return next operator in the parsed text
expr | Pointer to text to parse, gets advanced if succeeds |
operators | Pointer to operators table to use |
caseInsensitive | Match case-insensitive if set |
virtual Opcode getOperator | ( | const char *& | expr | ) | [protected, virtual] |
Returns next operator in the parsed text
expr | Pointer to text to parse, gets advanced if succeeds |
virtual const char* getOperator | ( | Opcode | oper | ) | const [protected, virtual] |
Helper method to get the canonical name of an operator
oper | Operator code |
virtual Opcode getPostfixOperator | ( | const char *& | expr | ) | [protected, virtual] |
Returns next unary postfix operator in the parsed text
expr | Pointer to text to parse, gets advanced if succeeds |
virtual int getPrecedence | ( | Opcode | oper | ) | const [protected, virtual] |
Get the precedence of an operator
oper | Operator code |
virtual bool getRightAssoc | ( | Opcode | oper | ) | const [protected, virtual] |
Get the associativity of an operator
oper | Operator code |
virtual bool getSeparator | ( | const char *& | expr, |
bool | remove | ||
) | [protected, virtual] |
Check if we are at an expression separator and optionally skip past it
expr | Pointer to text to check, gets advanced if asked to remove separator |
remove | True to skip past the found separator |
virtual bool getString | ( | const char *& | expr | ) | [protected, virtual] |
Get a string operand, advance parsing pointer past it
expr | Pointer to text to parse, gets advanced on success |
virtual Opcode getUnaryOperator | ( | const char *& | expr | ) | [protected, virtual] |
Returns next unary operator in the parsed text
expr | Pointer to text to parse, gets advanced if succeeds |
bool gotError | ( | const char * | error = 0 , |
const char * | text = 0 |
||
) | const [protected] |
Helper method to display debugging errors internally
error | Text of the error |
text | Optional text that caused the error |
bool gotError | ( | const char * | error = 0 , |
const char * | text = 0 |
||
) | [protected] |
Helper method to set error flag and display debugging errors internally
error | Text of the error |
text | Optional text that caused the error |
bool inError | ( | ) | const [inline] |
Check if a parse or compile error was encountered
virtual bool keywordChar | ( | char | c | ) | const [protected, virtual] |
Check if a character can be part of a keyword or identifier
c | Character to check |
bool null | ( | ) | const [inline] |
Check if the expression is empty (no operands or operators)
Retrieve the internally used operator dictionary
static ExpOperation* popAny | ( | ObjList & | stack | ) | [static] |
Pops any operand (including barriers) off an evaluation stack
stack | Evaluation stack to remove the operand from |
static ExpOperation* popOne | ( | ObjList & | stack | ) | [static] |
Pops an operand off an evaluation stack, does not pop a barrier
stack | Evaluation stack to remove the operand from |
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
stack | Evaluation stack to remove the operand from |
context | Pointer to arbitrary object to be passed to called methods |
virtual int preProcess | ( | const char *& | expr, |
GenObject * | context = 0 |
||
) | [protected, virtual] |
Process top-level preprocessor directives
expr | Pointer to expression cursor, gets advanced |
context | Pointer to arbitrary object to be passed to called methods |
static void pushOne | ( | ObjList & | stack, |
ExpOperation * | oper | ||
) | [static] |
Push an operand on an evaluation stack
stack | Evaluation stack to remove the operand from |
oper | Operation 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
stack | Evaluation stack to evaluate fields from |
context | Pointer to arbitrary object to be passed to called methods |
virtual bool runAssign | ( | ObjList & | stack, |
const ExpOperation & | oper, | ||
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Try to assign a value to a single field
stack | Evaluation stack in use |
oper | Field to assign to, contains the field name and new value |
context | Pointer to arbitrary object to be passed to called methods |
virtual bool runCompile | ( | const char *& | expr, |
char | stop = 0 , |
||
Opcode | nested = OpcNone |
||
) | [protected, virtual] |
Runs the parser and compiler for one (sub)expression
expr | Pointer to text to parse, gets advanced |
stop | Optional character expected after the expression |
nested | The instruction within this expression is nested |
virtual bool runEvaluate | ( | const ObjList & | opcodes, |
ObjList & | stack, | ||
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Try to evaluate a list of operation codes
opcodes | List of operation codes to evaluate |
stack | Evaluation stack in use, results are left on stack |
context | Pointer to arbitrary object to be passed to called methods |
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
opcodes | ObjVector of operation codes to evaluate |
stack | Evaluation stack in use, results are left on stack |
context | Pointer to arbitrary object to be passed to called methods |
index | Index in operation codes to start evaluation from |
virtual bool runEvaluate | ( | ObjList & | stack, |
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Try to evaluate the expression
stack | Evaluation stack in use, results are left on stack |
context | Pointer to arbitrary object to be passed to called methods |
virtual bool runField | ( | ObjList & | stack, |
const ExpOperation & | oper, | ||
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Try to evaluate a single field
stack | Evaluation stack in use, field value must be pushed on it |
oper | Field to evaluate |
context | Pointer to arbitrary object to be passed to called methods |
virtual bool runFunction | ( | ObjList & | stack, |
const ExpOperation & | oper, | ||
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Try to evaluate a single function
stack | Evaluation stack in use, parameters are popped off this stack and results are pushed back on stack |
oper | Function to evaluate |
context | Pointer to arbitrary object to be passed to called methods |
virtual bool runOperation | ( | ObjList & | stack, |
const ExpOperation & | oper, | ||
GenObject * | context = 0 |
||
) | const [virtual] |
Try to evaluate a single operation
stack | Evaluation stack in use, operands are popped off this stack and results are pushed back on stack |
oper | Operation to execute |
context | Pointer to arbitrary object to be passed to called methods |
bool simplify | ( | ) | [inline] |
Simplify the expression, performs constant folding
virtual char skipComments | ( | const char *& | expr, |
GenObject * | context = 0 |
||
) | const [protected, virtual] |
Skip over comments and whitespaces
expr | Pointer to expression cursor, gets advanced |
context | Pointer to arbitrary object to be passed to called methods |
static char skipWhites | ( | const char *& | expr | ) | [static, protected] |
Helper method to skip over whitespaces
expr | Pointer to expression cursor, gets advanced |
virtual bool trySimplify | ( | ) | [protected, virtual] |
Try to apply simplification to the expression
Retrieve the internally used unary operators dictionary
bool m_inError [protected] |
Flag that we encountered a parse or compile error
const TokenDict* m_operators [protected] |
Internally used operator dictionary
const TokenDict* m_unaryOps [protected] |
Internally used unary operators dictionary