|
|
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:
operators | Pointer to operator dictionary, longest strings first |
unaryOps | Pointer to unary operators dictionary, longest strings first |
explicit ExpEvaluator (Parser style)
| ExpEvaluator |
Constructs an evaluator from a parser style
Parameters:
style | Style of parsing to use |
ExpEvaluator (const ExpEvaluator& original)
| ExpEvaluator |
Copy constructor
Parameters:
original | Evaluator to copy the operation list from |
~ExpEvaluator ()
| ~ExpEvaluator |
[virtual]
Destructor
int compile (ParsePoint& expr, GenObject* context = 0)
| compile |
Parse and compile an expression
Parameters:
expr | Parsing context to compile |
context | Pointer 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:
results | List to fill with results row |
context | Pointer 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:
results | List to fill with results row |
context | Pointer 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:
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 |
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:
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 |
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:
codes | List of operation codes |
res | Result string representation of operations |
void dump (String& res)
| dump |
[const virtual]
Dump the postfix expression according to current operators dictionary
Parameters:
res | Result string representation of operations |
inline String dump (const ObjList& codes)
| dump |
[const]
Dump a list of operations according to current operators dictionary
Parameters:
codes | List 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:
ext | Pointer 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:
op1 | First operand |
op2 | Optional second operand |
op3 | Optional 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:
stack | Evaluation stack to remove the operand from |
oper | Operation 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:
stack | Evaluation 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:
stack | Evaluation 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:
stack | Evaluation stack to remove the operand from |
context | Pointer 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:
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 |
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:
stack | Evaluation stack to evaluate fields from |
context | Pointer 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:
expr | Current 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:
chr | Character to convert |
makeLower | True 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:
expr | Pointer to text to parse, gets advanced if succeeds |
operators | Pointer to operators table to use |
caseInsensitive | Match 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:
c | Character 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:
c | Character 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:
c | Character 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:
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, unsigned int line = 0)
| gotError |
[protected const]
Helper method to display debugging errors internally
Parameters:
error | Text of the error |
text | Optional text that caused the error |
line | Number 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:
error | Text of the error |
text | Optional text that caused the error |
line | Number 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:
error | Text of the error |
line | Number 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:
error | Text of the error |
line | Number 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:
buf | String buffer used to return the value |
line | Line number to format |
bool runCompile (ParsePoint& expr, char stop, GenObject* nested = 0)
| runCompile |
[protected]
Runs the parser and compiler for one (sub)expression
Parameters:
expr | Current parsing context, advances on expression to be compiled |
stop | Optional character expected after the expression |
nested | User 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:
expr | Current parsing context, advances on expression to be compiled |
stop | Optional list of possible characters expected after the expression |
nested | User 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:
expr | Current parsing context, advances on expression to be compiled |
context | Pointer 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:
expr | Current parsing context, advances on expression to be compiled |
context | Pointer 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:
expr | Current 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:
expr | Current 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:
expr | Current parsing context, advances on expression to be compiled if it succeeds |
precedence | The 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:
oper | Operator 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:
oper | Operator 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:
oper | Operator 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:
expr | Current parsing context to check, advances on expression to be compiled if asked to remove separator |
remove | True 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:
expr | Current parsing context, advances on expression to be compiled if it succeeds |
stop | Optional character expected after the instruction |
nested | User 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:
expr | Current parsing context, advances on expression to be compiled if it succeeds |
endOk | Consider reaching the end of string a success |
precedence | The 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:
expr | Current parsing context, advances on expression to be compiled if it succeeds |
constOnly | Return 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:
expr | Current 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:
expr | Current 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:
expr | Current 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:
expr | Pointer to string separator, gets advanced on success |
str | String 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:
expr | Pointer past escape character, gets advanced on success |
str | String in which the result is returned |
sep | String separator character |
Returns: True if succeeded
bool getField (ParsePoint& expr)
| getField |
[protected virtual]
Get a field keyword, advance parsing pointer past it
Parameters:
expr | Current 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:
oper | Operation to add |
line | Line 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:
oper | Operator code to add |
barrier | True 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:
oper | Operator code to add |
value | 64 bit nteger value to add |
barrier | True 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:
value | String 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:
value | Integer 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:
value | Boolean 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:
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 |
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:
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 |
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:
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 |
Returns: True if evaluation succeeded
bool runEvaluate (ObjList& stack, GenObject* context = 0)
| runEvaluate |
[protected const virtual]
Try to evaluate the expression
Parameters:
stack | Evaluation stack in use, results are left on stack |
context | Pointer 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:
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 |
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:
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 |
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:
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 |
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:
oper | Operation to dump |
res | Result 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. |