#include <muParserBase.h>

Public Types | |
| typedef ParserError | exception_type |
| Type of the error class. | |
Public Member Functions | |
| ParserBase () | |
| Constructor. | |
| ParserBase (const ParserBase &a_Parser) | |
| Copy constructor. | |
| ParserBase & | operator= (const ParserBase &a_Parser) |
| Assignement operator. | |
| value_type | Eval () const |
| Calculate the result. | |
| void | SetExpr (const string_type &a_sExpr) |
| Set the formula. | |
| void | SetVarFactory (facfun_type a_pFactory, void *pUserData=NULL) |
| Set a function that can create variable pointer for unknown expression variables. | |
| void | EnableOptimizer (bool a_bIsOn=true) |
| Enable or disable the formula optimization feature. | |
| void | EnableByteCode (bool a_bIsOn=true) |
| Enable or disable parsing from Bytecode. | |
| void | EnableBuiltInOprt (bool a_bIsOn=true) |
| Enable or disable the built in binary operators. | |
| bool | HasBuiltInOprt () const |
| Query status of built in variables. | |
| void | AddValIdent (identfun_type a_pCallback) |
| Add a value parsing function. | |
| void | DefineOprt (const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, bool a_bAllowOpt=false) |
| Define a binary operator. | |
| void | DefineConst (const string_type &a_sName, value_type a_fVal) |
| Add a user defined constant. | |
| void | DefineStrConst (const string_type &a_sName, const string_type &a_strVal) |
| Define a new string constant. | |
| void | DefineVar (const string_type &a_sName, value_type *a_fVar) |
| Add a user defined variable. | |
| void | DefinePostfixOprt (const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true) |
| Add a user defined operator. | |
| void | DefineInfixOprt (const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true) |
| Add a user defined operator. | |
| void | ClearVar () |
| Clear all user defined variables. | |
| void | ClearFun () |
| Clear all functions. | |
| void | ClearConst () |
| Clear all user defined constants. | |
| void | ClearInfixOprt () |
| Clear the user defined Prefix operators. | |
| void | ClearPostfixOprt () |
| Clear all user defined postfix operators. | |
| void | ClearOprt () |
| Clear all user defined binary operators. | |
| void | RemoveVar (const string_type &a_strVarName) |
| Remove a variable from internal storage. | |
| const varmap_type & | GetUsedVar () const |
| Return a map containing the used variables only. | |
| const varmap_type & | GetVar () const |
| Return a map containing the used variables only. | |
| const valmap_type & | GetConst () const |
| Return a map containing all parser constants. | |
| const string_type & | GetExpr () const |
| Retrieve the formula. | |
| const funmap_type & | GetFunDef () const |
| Return prototypes of all parser functions. | |
| const char_type ** | GetOprtDef () const |
| Get the default symbols used for the built in operators. | |
| void | DefineNameChars (const char_type *a_szCharset) |
| Define the set of valid characters to be used in names of functions, variables, constants. | |
| void | DefineOprtChars (const char_type *a_szCharset) |
| Define the set of valid characters to be used in names of binary operators and postfix operators. | |
| void | DefineInfixOprtChars (const char_type *a_szCharset) |
| Define the set of valid characters to be used in names of infix operators. | |
| const char_type * | ValidNameChars () const |
| Virtual function that defines the characters allowed in name identifiers. | |
| const char_type * | ValidOprtChars () const |
| Virtual function that defines the characters allowed in operator definitions. | |
| const char_type * | ValidInfixOprtChars () const |
| Virtual function that defines the characters allowed in infix operator definitions. | |
| void | SetArgSep (char_type cArgSep) |
| Set argument separator. | |
| char_type | GetArgSep () const |
| Get the argument separator character. | |
| void | Error (EErrorCodes a_iErrc, int a_iPos=(int) mu::string_type::npos, const string_type &a_strTok=string_type()) const |
| Create an error containing the parse error position. | |
Protected Member Functions | |
| void | Init () |
| Initialize user defined functions. | |
| virtual void | InitCharSets ()=0 |
| virtual void | InitFun ()=0 |
| virtual void | InitConst ()=0 |
| virtual void | InitOprt ()=0 |
| virtual void | OnDetectVar (std::string *pExpr, int &nStart, int &nEnd) |
Static Protected Attributes | |
| static char_type * | c_DefaultOprt [] |
| Identifiers for built in binary operators. | |
Private Types | |
| typedef std::vector< value_type > | valbuf_type |
| Type used for storing an array of values. | |
| typedef std::vector< string_type > | stringbuf_type |
| Type for a vector of strings. | |
| typedef ParserTokenReader | token_reader_type |
| Typedef for the token reader. | |
|
typedef ParserToken < value_type, string_type > | token_type |
| Type used for parser tokens. | |
| typedef value_type(ParserBase::* | ParseFunction )() const |
| Typedef for the parse functions. | |
Private Member Functions | |
| void | Assign (const ParserBase &a_Parser) |
| Copy state of a parser object to this. | |
| void | InitTokenReader () |
| Initialize the token reader. | |
| void | ReInit () const |
| Reset parser to string parsing mode and clear internal buffers. | |
| void | AddCallback (const string_type &a_strName, const ParserCallback &a_Callback, funmap_type &a_Storage, const char_type *a_szCharSet) |
| Add a function or operator callback to the parser. | |
| void | ApplyBinOprt (ParserStack< token_type > &a_stOpt, ParserStack< token_type > &a_stVal) const |
| Apply a binary operator. | |
| void | ApplyFunc (ParserStack< token_type > &a_stOpt, ParserStack< token_type > &a_stVal, int iArgCount) const |
| Apply a function token. | |
| token_type | ApplyNumFunc (const token_type &a_FunTok, const std::vector< token_type > &a_vArg) const |
| token_type | ApplyStrFunc (const token_type &a_FunTok, const std::vector< token_type > &a_vArg) const |
| Execute a function that takes a single string argument. | |
| int | GetOprtPri (const token_type &a_Tok) const |
| Get operator priority. | |
| value_type | ParseString () const |
| One of the two main parse functions. | |
| value_type | ParseCmdCode () const |
| Parse the command code. | |
| value_type | ParseValue () const |
| Return result for constant functions. | |
| void | ClearFormula () |
| Clear the formula. | |
| void | CheckName (const string_type &a_strName, const string_type &a_CharSet) const |
| Check if a name contains invalid characters. | |
Private Attributes | |
| ParseFunction | m_pParseFormula |
| Pointer to the parser function. | |
| const ParserByteCode::map_type * | m_pCmdCode |
| Formula converted to bytecode, points to the data of the bytecode class. | |
| ParserByteCode | m_vByteCode |
| The Bytecode class. | |
| stringbuf_type | m_vStringBuf |
| String buffer, used for storing string function arguments. | |
| stringbuf_type | m_vStringVarBuf |
| std::auto_ptr< token_reader_type > | m_pTokenReader |
| Managed pointer to the token reader object. | |
| funmap_type | m_FunDef |
| Map of function names and pointers. | |
| funmap_type | m_PostOprtDef |
| Postfix operator callbacks. | |
| funmap_type | m_InfixOprtDef |
| unary infix operator. | |
| funmap_type | m_OprtDef |
| Binary operator callbacks. | |
| valmap_type | m_ConstDef |
| user constants. | |
| strmap_type | m_StrVarDef |
| user defined string constants | |
| varmap_type | m_VarDef |
| user defind variables. | |
| bool | m_bOptimize |
| Flag that indicates if the optimizer is on or off. | |
| bool | m_bUseByteCode |
| Flag that indicates if bytecode parsing is on or off. | |
| bool | m_bBuiltInOp |
| Flag that can be used for switching built in operators on and off. | |
| string_type | m_sNameChars |
| Charset for names. | |
| string_type | m_sOprtChars |
| Charset for postfix/ binary operator tokens. | |
| string_type | m_sInfixOprtChars |
| Charset for infix operator tokens. | |
| valbuf_type | m_vStackBuffer |
| This is merely a buffer used for the stack in the cmd parsing routine. | |
Friends | |
| class | ParserTokenReader |
Version 1.31 (20090112)
This is the implementation of a bytecode based mathematical expressions parser. The formula will be parsed from string and converted into a bytecode. Future calculations will be done with the bytecode instead the formula string resulting in a significant performance increase. Complementary to a set of internally implemented functions the parser is able to handle user defined functions and variables.
list_expr_var.cpp, and listvar.cpp.
Type of the error class.
Included for backwards compatibility.
typedef value_type(ParserBase::* mu::ParserBase::ParseFunction)() const [private] |
Typedef for the parse functions.
The parse function do the actual work. The parser exchanges the function pointer to the parser function depending on which state it is in. (i.e. bytecode parser vs. string parser)
| mu::ParserBase::ParserBase | ( | ) |
Constructor.
| a_szFormula | the formula to interpret. |
| ParserException | if a_szFormula is null. |
| mu::ParserBase::ParserBase | ( | const ParserBase & | a_Parser | ) |
Copy constructor.
Tha parser can be safely copy constructed but the bytecode is reset during copy construction.
| void mu::ParserBase::AddValIdent | ( | identfun_type | a_pCallback | ) |
Add a value parsing function.
When parsing an expression muParser tries to detect values in the expression string using different valident callbacks. Thuis it's possible to parse for hex values, binary values and floating point values.
| void mu::ParserBase::ApplyBinOprt | ( | ParserStack< token_type > & | a_stOpt, | |
| ParserStack< token_type > & | a_stVal | |||
| ) | const [private] |
Apply a binary operator.
| a_stOpt | The operator stack | |
| a_stVal | The value stack |
| void mu::ParserBase::ApplyFunc | ( | ParserStack< token_type > & | a_stOpt, | |
| ParserStack< token_type > & | a_stVal, | |||
| int | a_iArgCount | |||
| ) | const [private] |
Apply a function token.
| iArgCount | Number of Arguments actually gathered used only for multiarg functions. |
The function token is removed from the stack
| exception_type | if Argument count does not mach function requirements. |
| ParserBase::token_type mu::ParserBase::ApplyNumFunc | ( | const token_type & | a_FunTok, | |
| const std::vector< token_type > & | a_vArg | |||
| ) | const [private] |
(Attention: SetVal will reset Flags.)
| ParserBase::token_type mu::ParserBase::ApplyStrFunc | ( | const token_type & | a_FunTok, | |
| const std::vector< token_type > & | a_vArg | |||
| ) | const [private] |
Execute a function that takes a single string argument.
| a_FunTok | Function token. |
| exception_type | If the function token is not a string function |
(Attention: SetVal will reset Flags.)
| void mu::ParserBase::Assign | ( | const ParserBase & | a_Parser | ) | [private] |
Copy state of a parser object to this.
Clears Variables and Functions of this parser. Copies the states of all internal variables. Resets parse function to string parse mode.
| a_Parser | the source object. |
| void mu::ParserBase::CheckName | ( | const string_type & | a_sName, | |
| const string_type & | a_szCharSet | |||
| ) | const [private] |
Check if a name contains invalid characters.
| ParserException | if the name contains invalid charakters. |
| void mu::ParserBase::ClearConst | ( | ) |
Clear all user defined constants.
Both numeric and string constants will be removed from the internal storage.
| nothrow |
| void mu::ParserBase::ClearFormula | ( | ) | [private] |
Clear the formula.
| nothrow | Clear the formula and existing bytecode. |
| void mu::ParserBase::ClearFun | ( | ) |
Clear all functions.
| nothrow |
| void mu::ParserBase::ClearInfixOprt | ( | ) |
Clear the user defined Prefix operators.
| nothrow |
| void mu::ParserBase::ClearOprt | ( | ) |
Clear all user defined binary operators.
| nothrow |
| void mu::ParserBase::ClearPostfixOprt | ( | ) |
Clear all user defined postfix operators.
| nothrow |
| void mu::ParserBase::ClearVar | ( | ) |
Clear all user defined variables.
| nothrow | Resets the parser to string parsing mode by calling ReInit. |
| void mu::ParserBase::DefineConst | ( | const string_type & | a_sName, | |
| value_type | a_fVal | |||
| ) |
Add a user defined constant.
| [in] | a_sName | The name of the constant. |
| [in] | a_fVal | the value of the constant. |
| ParserException | in case the name contains invalid signs. |
| void mu::ParserBase::DefineInfixOprt | ( | const string_type & | a_sName, | |
| fun_type1 | a_pFun, | |||
| int | a_iPrec = prINFIX, |
|||
| bool | a_bAllowOpt = true | |||
| ) |
Add a user defined operator.
| [in] | a_sName | operator Identifier |
| [in] | a_pFun | Operator callback function |
| [in] | a_iPrec | Operator Precedence (default=prSIGN) |
| [in] | a_bAllowOpt | True if operator is volatile (default=false) |
| void mu::ParserBase::DefineOprt | ( | const string_type & | a_sName, | |
| fun_type2 | a_pFun, | |||
| unsigned | a_iPrec = 0, |
|||
| bool | a_bAllowOpt = false | |||
| ) |
Define a binary operator.
| [in] | a_pFun | Pointer to the callback function. |
| [in] | a_iPrec | Precedence of the operator. |
| [in] | a_bAllowOpt | If this is true the operator may be optimized away. |
| void mu::ParserBase::DefinePostfixOprt | ( | const string_type & | a_sName, | |
| fun_type1 | a_pFun, | |||
| bool | a_bAllowOpt = true | |||
| ) |
| void mu::ParserBase::DefineStrConst | ( | const string_type & | a_strName, | |
| const string_type & | a_strVal | |||
| ) |
Define a new string constant.
| [in] | a_strName | The name of the constant. |
| [in] | a_strVal | the value of the constant. |
| void mu::ParserBase::DefineVar | ( | const string_type & | a_sName, | |
| value_type * | a_pVar | |||
| ) |
| void mu::ParserBase::EnableBuiltInOprt | ( | bool | a_bIsOn = true |
) |
Enable or disable the built in binary operators.
| nothrow |
| void mu::ParserBase::EnableByteCode | ( | bool | a_bIsOn = true |
) |
Enable or disable parsing from Bytecode.
| void mu::ParserBase::EnableOptimizer | ( | bool | a_bIsOn = true |
) |
Enable or disable the formula optimization feature.
| nothrow |
| void mu::ParserBase::Error | ( | EErrorCodes | a_iErrc, | |
| int | a_iPos = (int)mu::string_type::npos, |
|||
| const string_type & | a_sTok = string_type() | |||
| ) | const |
Create an error containing the parse error position.
This function will create an Parser Exception object containing the error text and its position.
| a_iErrc | [in] The error code of type EErrorCodes. | |
| a_iPos | [in] The position where the error was detected. | |
| a_strTok | [in] The token string representation associated with the error. |
| ParserException | always throws thats the only purpose of this function. |
| value_type mu::ParserBase::Eval | ( | ) | const [inline] |
Calculate the result.
A note on const correctness: I consider it important that Calc is a const function. Due to caching operations Calc changes only the state of internal variables with one exception m_UsedVar this is reset during string parsing and accessible from the outside. Instead of making Calc non const GetUsedVar is non const because it explicitely calls Eval() forcing this update.
Variables must have been set (if needed)
| ParseException | if no Formula is set or in case of any other error related to the formula. |
| const funmap_type & mu::ParserBase::GetFunDef | ( | ) | const |
Return prototypes of all parser functions.
| nothrow | The return type is a map of the public type funmap_type containing the prototype definitions for all numerical parser functions. String functions are not part of this map. The Prototype definition is encapsulated in objects of the class FunProt one per parser function each associated with function names via a map construct. |
| const char_type ** mu::ParserBase::GetOprtDef | ( | ) | const |
| int mu::ParserBase::GetOprtPri | ( | const token_type & | a_Tok | ) | const [private] |
Get operator priority.
| ParserException | if a_Oprt is no operator code |
| bool mu::ParserBase::HasBuiltInOprt | ( | ) | const |
Query status of built in variables.
| nothrow |
| void mu::ParserBase::Init | ( | ) | [protected] |
Initialize user defined functions.
Calls the virtual functions InitFun(), InitConst() and InitOprt().
| void mu::ParserBase::InitTokenReader | ( | ) | [private] |
Initialize the token reader.
Create new token reader object and submit pointers to function, operator, constant and variable definitions.
| nothrow |
| ParserBase & mu::ParserBase::operator= | ( | const ParserBase & | a_Parser | ) |
Assignement operator.
Implemented by calling Assign(a_Parser). Self assignement is suppressed.
| a_Parser | Object to copy to this. |
| nothrow |
| value_type mu::ParserBase::ParseCmdCode | ( | ) | const [private] |
Parse the command code.
| value_type mu::ParserBase::ParseString | ( | ) | const [private] |
One of the two main parse functions.
Parse expression from input string. Perform syntax checking and create bytecode. After parsing the string and creating the bytecode the function pointer m_pParseFormula will be changed to the second parse routine the uses bytecode instead of string parsing.
| value_type mu::ParserBase::ParseValue | ( | ) | const [private] |
Return result for constant functions.
Seems pointless, but for parser functions that are made up of only a value, which occur in real world applications, this speeds up things by removing the parser overhead almost completely.
| void mu::ParserBase::ReInit | ( | ) | const [private] |
Reset parser to string parsing mode and clear internal buffers.
Clear bytecode, reset the token reader.
| nothrow |
| void mu::ParserBase::RemoveVar | ( | const string_type & | a_strVarName | ) |
Remove a variable from internal storage.
| nothrow | Removes a variable if it exists. If the Variable does not exist nothing will be done. |
| void mu::ParserBase::SetArgSep | ( | char_type | cArgSep | ) |
| void mu::ParserBase::SetExpr | ( | const string_type & | a_sExpr | ) |
Set the formula.
| a_strFormula | Formula as string_type |
| ParserException | in case of syntax errors. |
| void mu::ParserBase::SetVarFactory | ( | facfun_type | a_pFactory, | |
| void * | pUserData = NULL | |||
| ) |
Set a function that can create variable pointer for unknown expression variables.
| a_pFactory | A pointer to the variable factory. | |
| pUserData | A user defined context pointer. |
| const char_type * mu::ParserBase::ValidInfixOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in infix operator definitions.
| const char_type * mu::ParserBase::ValidNameChars | ( | ) | const |
Virtual function that defines the characters allowed in name identifiers.
| const char_type * mu::ParserBase::ValidOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in operator definitions.
char_type * mu::ParserBase::c_DefaultOprt [static, protected] |
Initial value:
{
_T("<="), _T(">="), _T("!="),
_T("=="), _T("<"), _T(">"),
_T("+"), _T("-"), _T("*"),
_T("/"), _T("^"), _T("and"),
_T("or"), _T("xor"), _T("="),
_T("("), _T(")"), 0
}
When defining custom binary operators with AddOprt(...) make sure not to choose names conflicting with these definitions.
ParseFunction mu::ParserBase::m_pParseFormula [mutable, private] |
Pointer to the parser function.
Eval() calls the function whose address is stored there.