muParser API -
1.35
|
Mathematical expressions parser (base parser engine). More...
#include <muParserBase.h>
Classes | |
class | change_dec_sep |
A facet class used to change decimal and thousands separator. More... | |
Public Types | |
typedef ParserError | exception_type |
Type of the error class. More... | |
Public Member Functions | |
ParserBase () | |
Constructor. More... | |
ParserBase (const ParserBase &a_Parser) | |
Copy constructor. More... | |
ParserBase & | operator= (const ParserBase &a_Parser) |
Assignment operator. More... | |
value_type | Eval () const |
Calculate the result. More... | |
value_type * | Eval (int &nStackSize) const |
Evaluate an expression containing comma separated subexpressions. More... | |
void | Eval (value_type *results, int nBulkSize) |
int | GetNumResults () const |
Return the number of results on the calculation stack. More... | |
void | SetExpr (const string_type &a_sExpr) |
Set the formula. More... | |
void | SetVarFactory (facfun_type a_pFactory, void *pUserData=NULL) |
Set a function that can create variable pointer for unknown expression variables. More... | |
void | SetDecSep (char_type cDecSep) |
Set the decimal separator. More... | |
void | SetThousandsSep (char_type cThousandsSep=0) |
Sets the thousands operator. More... | |
void | ResetLocale () |
Resets the locale. More... | |
void | EnableOptimizer (bool a_bIsOn=true) |
Enable or disable the formula optimization feature. More... | |
void | EnableBuiltInOprt (bool a_bIsOn=true) |
Enable or disable the built in binary operators. More... | |
bool | HasBuiltInOprt () const |
Query status of built in variables. More... | |
void | AddValIdent (identfun_type a_pCallback) |
Add a value parsing function. More... | |
template<typename T > | |
void | DefineFun (const string_type &a_strName, T a_pFun, bool a_bAllowOpt=true) |
Define a parser function without arguments. More... | |
void | DefineOprt (const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, EOprtAssociativity a_eAssociativity=oaLEFT, bool a_bAllowOpt=false) |
Define a binary operator. More... | |
void | DefineConst (const string_type &a_sName, value_type a_fVal) |
Add a user defined constant. More... | |
void | DefineStrConst (const string_type &a_sName, const string_type &a_strVal) |
Define a new string constant. More... | |
void | DefineVar (const string_type &a_sName, value_type *a_fVar) |
Add a user defined variable. More... | |
void | DefinePostfixOprt (const string_type &a_strFun, fun_type1 a_pOprt, bool a_bAllowOpt=true) |
Add a user defined operator. More... | |
void | DefineInfixOprt (const string_type &a_strName, fun_type1 a_pOprt, int a_iPrec=prINFIX, bool a_bAllowOpt=true) |
Add a user defined operator. More... | |
void | ClearVar () |
Clear all user defined variables. More... | |
void | ClearFun () |
Clear all functions. More... | |
void | ClearConst () |
Clear all user defined constants. More... | |
void | ClearInfixOprt () |
Clear the user defined Prefix operators. More... | |
void | ClearPostfixOprt () |
Clear all user defined postfix operators. More... | |
void | ClearOprt () |
Clear all user defined binary operators. More... | |
void | RemoveVar (const string_type &a_strVarName) |
Remove a variable from internal storage. More... | |
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. More... | |
string_type | GetVersion (EParserVersionInfo eInfo=pviFULL) const |
Returns the version of muparser. More... | |
const char_type ** | GetOprtDef () const |
Get the default symbols used for the built in operators. More... | |
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. More... | |
const char_type * | ValidOprtChars () const |
Virtual function that defines the characters allowed in operator definitions. More... | |
const char_type * | ValidInfixOprtChars () const |
Virtual function that defines the characters allowed in infix operator definitions. More... | |
void | SetArgSep (char_type cArgSep) |
Set argument separator. More... | |
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. More... | |
Static Public Member Functions | |
static void | EnableDebugDump (bool bDumpCmd, bool bDumpStack) |
Enable the dumping of bytecode and stack content on the console. More... | |
Protected Member Functions | |
void | Init () |
Initialize user defined functions. More... | |
virtual void | InitCharSets ()=0 |
virtual void | InitFun ()=0 |
virtual void | InitConst ()=0 |
virtual void | InitOprt ()=0 |
virtual void | OnDetectVar (string_type *pExpr, int &nStart, int &nEnd) |
Static Protected Attributes | |
static const char_type * | c_DefaultOprt [] |
Identifiers for built in binary operators. More... | |
static std::locale | s_locale = std::locale(std::locale::classic(), new change_dec_sep<char_type>('.')) |
The locale used by the parser. | |
static bool | g_DbgDumpCmdCode = false |
static bool | g_DbgDumpStack = false |
Friends | |
class | ParserTokenReader |
Mathematical expressions parser (base parser engine).
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.
Definition at line 62 of file muParserBase.h.
Type of the error class.
Included for backwards compatibility.
Definition at line 97 of file muParserBase.h.
mu::ParserBase::ParserBase | ( | ) |
Constructor.
a_szFormula | the formula to interpret. |
ParserException | if a_szFormula is null. |
Definition at line 77 of file muParserBase.cpp.
mu::ParserBase::ParserBase | ( | const ParserBase & | a_Parser | ) |
Copy constructor.
The parser can be safely copy constructed but the bytecode is reset during copy construction.
Definition at line 106 of file muParserBase.cpp.
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. Thus it's possible to parse for hex values, binary values and floating point values.
Definition at line 319 of file muParserBase.cpp.
Referenced by mu::Parser::Parser(), and mu::ParserInt::ParserInt().
void mu::ParserBase::ClearConst | ( | ) |
Clear all user defined constants.
Both numeric and string constants will be removed from the internal storage.
nothrow |
Definition at line 1491 of file muParserBase.cpp.
void mu::ParserBase::ClearFun | ( | ) |
Clear all functions.
nothrow |
Definition at line 1478 of file muParserBase.cpp.
void mu::ParserBase::ClearInfixOprt | ( | ) |
Clear the user defined Prefix operators.
nothrow |
Definition at line 1525 of file muParserBase.cpp.
void mu::ParserBase::ClearOprt | ( | ) |
Clear all user defined binary operators.
nothrow |
Definition at line 1514 of file muParserBase.cpp.
void mu::ParserBase::ClearPostfixOprt | ( | ) |
Clear all user defined postfix operators.
nothrow |
Definition at line 1503 of file muParserBase.cpp.
void mu::ParserBase::ClearVar | ( | ) |
Clear all user defined variables.
nothrow | Resets the parser to string parsing mode by calling #ReInit. |
Definition at line 1451 of file muParserBase.cpp.
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. |
Definition at line 615 of file muParserBase.cpp.
Referenced by mu::Parser::InitConst().
|
inline |
Define a parser function without arguments.
a_strName | Name of the function |
a_pFun | Pointer to the callback function |
a_bAllowOpt | A flag indicating this function may be optimized |
Definition at line 133 of file muParserBase.h.
Referenced by mu::Parser::InitFun(), and mu::ParserInt::InitFun().
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) |
Definition at line 529 of file muParserBase.cpp.
Referenced by mu::Parser::InitOprt(), and mu::ParserInt::InitOprt().
void mu::ParserBase::DefineOprt | ( | const string_type & | a_sName, |
fun_type2 | a_pFun, | ||
unsigned | a_iPrec = 0 , |
||
EOprtAssociativity | a_eAssociativity = oaLEFT , |
||
bool | a_bAllowOpt = false |
||
) |
Define a binary operator.
[in] | a_sName | The identifier of the operator. |
[in] | a_pFun | Pointer to the callback function. |
[in] | a_iPrec | Precedence of the operator. |
[in] | a_eAssociativity | The associativity of the operator. |
[in] | a_bAllowOpt | If this is true the operator may be optimized away. |
Adds a new Binary operator the the parser instance.
Definition at line 551 of file muParserBase.cpp.
Referenced by mu::ParserInt::InitOprt().
void mu::ParserBase::DefinePostfixOprt | ( | const string_type & | a_sName, |
fun_type1 | a_pFun, | ||
bool | a_bAllowOpt = true |
||
) |
Add a user defined operator.
Definition at line 497 of file muParserBase.cpp.
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. |
Definition at line 573 of file muParserBase.cpp.
void mu::ParserBase::DefineVar | ( | const string_type & | a_sName, |
value_type * | a_pVar | ||
) |
Add a user defined variable.
[in] | a_sName | the variable name |
[in] | a_pVar | A pointer to the variable value. |
ParserException | in case the name contains invalid signs or a_pVar is NULL. |
Definition at line 594 of file muParserBase.cpp.
void mu::ParserBase::EnableBuiltInOprt | ( | bool | a_bIsOn = true | ) |
Enable or disable the built in binary operators.
nothrow |
If you disable the built in binary operators there will be no binary operators defined. Thus you must add them manually one by one. It is not possible to disable built in operators selectively. This function will Reinitialize the parser by calling ReInit().
Definition at line 1565 of file muParserBase.cpp.
Referenced by mu::ParserInt::InitOprt().
|
static |
Enable the dumping of bytecode and stack content on the console.
bDumpCmd | Flag to enable dumping of the current bytecode to the console. |
bDumpStack | Flag to enable dumping of the stack content is written to the console. |
This function is for debug purposes only!
Definition at line 1549 of file muParserBase.cpp.
void mu::ParserBase::EnableOptimizer | ( | bool | a_bIsOn = true | ) |
Enable or disable the formula optimization feature.
nothrow |
Definition at line 1536 of file muParserBase.cpp.
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. |
Definition at line 1440 of file muParserBase.cpp.
Referenced by DefineOprt(), DefineStrConst(), DefineVar(), and SetExpr().
value_type mu::ParserBase::Eval | ( | ) | const |
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 explicitly calls Eval() forcing this update.
ParseException | if no Formula is set or in case of any other error related to the formula. |
Definition at line 1708 of file muParserBase.cpp.
Referenced by mu::Parser::Diff().
value_type * mu::ParserBase::Eval | ( | int & | nStackSize | ) | const |
Evaluate an expression containing comma separated subexpressions.
[out] | nStackSize | The total number of results available |
This member function can be used to retrieve all results of an expression made up of multiple comma separated subexpressions (i.e. "x+y,sin(x),cos(y)")
Definition at line 1671 of file muParserBase.cpp.
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. |
Definition at line 734 of file muParserBase.cpp.
int mu::ParserBase::GetNumResults | ( | ) | const |
Return the number of results on the calculation stack.
If the expression contains comma separated subexpressions (i.e. "sin(y), x+y"). There may be more than one return value. This function returns the number of available results.
Definition at line 1687 of file muParserBase.cpp.
const char_type ** mu::ParserBase::GetOprtDef | ( | ) | const |
Get the default symbols used for the built in operators.
Definition at line 431 of file muParserBase.cpp.
string_type mu::ParserBase::GetVersion | ( | EParserVersionInfo | eInfo = pviFULL | ) | const |
Returns the version of muparser.
eInfo | A flag indicating whether the full version info should be returned or not. |
Format is as follows: "MAJOR.MINOR (COMPILER_FLAGS)" The COMPILER_FLAGS are returned only if eInfo==pviFULL.
Definition at line 267 of file muParserBase.cpp.
bool mu::ParserBase::HasBuiltInOprt | ( | ) | const |
Query status of built in variables.
nothrow |
Definition at line 1576 of file muParserBase.cpp.
|
protected |
Initialize user defined functions.
Calls the virtual functions InitFun(), InitConst() and InitOprt().
Definition at line 512 of file muParserBase.cpp.
ParserBase & mu::ParserBase::operator= | ( | const ParserBase & | a_Parser | ) |
Assignment operator.
Implemented by calling Assign(a_Parser). Self assignment is suppressed.
a_Parser | Object to copy to this. |
nothrow |
Definition at line 140 of file muParserBase.cpp.
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. |
Definition at line 1463 of file muParserBase.cpp.
void mu::ParserBase::ResetLocale | ( | ) |
Resets the locale.
The default locale used "." as decimal separator, no thousands separator and "," as function argument separator.
Definition at line 220 of file muParserBase.cpp.
void mu::ParserBase::SetArgSep | ( | char_type | cArgSep | ) |
Set argument separator.
cArgSep | the argument separator character. |
Definition at line 1593 of file muParserBase.cpp.
Referenced by ResetLocale().
void mu::ParserBase::SetDecSep | ( | char_type | cDecSep | ) |
Set the decimal separator.
cDecSep | Decimal separator as a character value. |
By default muparser uses the "C" locale. The decimal separator of this locale is overwritten by the one provided here.
Definition at line 194 of file muParserBase.cpp.
void mu::ParserBase::SetExpr | ( | const string_type & | a_sExpr | ) |
Set the formula.
a_strFormula | Formula as string_type |
ParserException | in case of syntax errors. |
Triggers first time calculation thus the creation of the bytecode and scanning of used variables.
Definition at line 410 of file muParserBase.cpp.
void mu::ParserBase::SetThousandsSep | ( | char_type | cThousandsSep = 0 | ) |
Sets the thousands operator.
cThousandsSep | The thousands separator as a character |
By default muparser uses the "C" locale. The thousands separator of this locale is overwritten by the one provided here.
Definition at line 208 of file muParserBase.cpp.
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. |
Definition at line 329 of file muParserBase.cpp.
const char_type * mu::ParserBase::ValidInfixOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in infix operator definitions.
Definition at line 487 of file muParserBase.cpp.
Referenced by DefineInfixOprt().
const char_type * mu::ParserBase::ValidNameChars | ( | ) | const |
Virtual function that defines the characters allowed in name identifiers.
Definition at line 467 of file muParserBase.cpp.
Referenced by DefineConst(), DefineFun(), DefineStrConst(), DefineVar(), and mu::ParserTokenReader::ReadNextToken().
const char_type * mu::ParserBase::ValidOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in operator definitions.
Definition at line 477 of file muParserBase.cpp.
Referenced by DefineOprt(), and DefinePostfixOprt().
|
staticprotected |
Identifiers for built in binary operators.
When defining custom binary operators with #AddOprt(...) make sure not to choose names conflicting with these definitions.
Definition at line 192 of file muParserBase.h.
Referenced by DefineOprt(), and GetOprtDef().