MADNESS
version 0.9
|
Mathematical expressions parser (base parser engine). More...
#include <muParserBase.h>
Public Types | |
typedef ParserError | exception_type |
Type of the error class. More... | |
Public Member Functions | |
ParserBase () | |
Constructor. More... | |
ParserBase (const ParserBase &a_Parser) | |
ParserBase & | operator= (const ParserBase &a_Parser) |
Assignement operator. More... | |
virtual | ~ParserBase () |
value_type | Eval () const |
Calculate the result. More... | |
void | SetExpr (const string_type &a_sExpr) |
Set the formula. More... | |
void | SetVarFactory (facfun_type a_pFactory, void *pUserData=NULL) |
void | EnableOptimizer (bool a_bIsOn=true) |
Enable or disable the formula optimization feature. More... | |
void | EnableByteCode (bool a_bIsOn=true) |
Enable or disable parsing from Bytecode. 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) |
void | DefineOprt (const string_type &a_strName, fun_type2 a_pFun, unsigned a_iPri=0, 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. More... | |
const varmap_type & | GetVar () const |
Return a map containing the used variables only. More... | |
const valmap_type & | GetConst () const |
Return a map containing all parser constants. More... | |
const string_type & | GetExpr () const |
Retrieve the formula. More... | |
const funmap_type & | GetFunDef () const |
Return prototypes of all parser functions. 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. More... | |
void | DefineOprtChars (const char_type *a_szCharset) |
Define the set of valid characters to be used in names of binary operators and postfix operators. More... | |
void | DefineInfixOprtChars (const char_type *a_szCharset) |
Define the set of valid characters to be used in names of infix operators. More... | |
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. More... | |
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... | |
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 |
Static Protected Attributes | |
static const char_type * | c_DefaultOprt [] |
Identifiers for built in binary operators. More... | |
Friends | |
class | ParserTokenReader |
Mathematical expressions parser (base parser engine).
Version 1.30 (20080413)
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.
Type of the error class.
Included for backwards compatibility.
mu::ParserBase::ParserBase | ( | ) |
Constructor.
a_szFormula | the formula to interpret. |
ParserException | if a_szFormula is null. |
mu::ParserBase::ParserBase | ( | const ParserBase & | a_Parser | ) |
|
virtual |
void mu::ParserBase::AddValIdent | ( | identfun_type | a_pCallback | ) |
Referenced by mu::Parser::Parser().
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::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. |
References a_fVal, and ValidNameChars().
Referenced by mu::Parser::InitConst().
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) |
References mu::cmOPRT_INFIX, and ValidOprtChars().
Referenced by mu::Parser::InitOprt().
void mu::ParserBase::DefineInfixOprtChars | ( | const char_type * | a_szCharset | ) |
Define the set of valid characters to be used in names of infix operators.
References a_szCharset.
Referenced by mu::Parser::InitCharSets().
void mu::ParserBase::DefineNameChars | ( | const char_type * | a_szCharset | ) |
Define the set of valid characters to be used in names of functions, variables, constants.
References a_szCharset.
Referenced by mu::Parser::InitCharSets().
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. |
References c_DefaultOprt, mu::cmARG_SEP, mu::cmOPRT_BIN, mu::ecBUILTIN_OVERLOAD, Error(), and ValidOprtChars().
void mu::ParserBase::DefineOprtChars | ( | const char_type * | a_szCharset | ) |
Define the set of valid characters to be used in names of binary operators and postfix operators.
References a_szCharset.
Referenced by mu::Parser::InitCharSets().
void mu::ParserBase::DefinePostfixOprt | ( | const string_type & | a_sName, |
fun_type1 | a_pFun, | ||
bool | a_bAllowOpt = true |
||
) |
Add a user defined operator.
References mu::cmOPRT_POSTFIX, mu::prPOSTFIX, and ValidOprtChars().
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. |
References mu::ecNAME_CONFLICT, Error(), and ValidNameChars().
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 vaule. |
ParserException | in case the name contains invalid signs. |
References a_pVar, mu::ecINVALID_VAR_PTR, mu::ecNAME_CONFLICT, Error(), and ValidNameChars().
Referenced by main(), and ParserHandler< T, NDIM >::ParserHandler().
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().
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. |
Referenced by DefineOprt(), DefineStrConst(), DefineVar(), and SetExpr().
|
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.
ParseException | if no Formula is set or in case of any other error related to the formula. |
Referenced by mu::Parser::Diff(), main(), and ParserHandler< T, NDIM >::operator()().
char_type mu::ParserBase::GetArgSep | ( | ) | const |
Get the argument separator character.
const valmap_type & mu::ParserBase::GetConst | ( | ) | const |
Return a map containing all parser constants.
const string_type & mu::ParserBase::GetExpr | ( | ) | const |
Retrieve 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 |
Get the default symbols used for the built in operators.
References c_DefaultOprt.
const varmap_type & mu::ParserBase::GetUsedVar | ( | ) | const |
Return a map containing the used variables only.
const varmap_type & mu::ParserBase::GetVar | ( | ) | const |
Return a map containing the used variables only.
bool mu::ParserBase::HasBuiltInOprt | ( | ) | const |
Query status of built in variables.
nothrow |
|
protected |
Initialize user defined functions.
Calls the virtual functions InitFun(), InitConst() and InitOprt().
References InitCharSets(), InitConst(), InitFun(), and InitOprt().
|
protectedpure virtual |
Implemented in mu::ParserInt, and mu::Parser.
Referenced by Init().
|
protectedpure virtual |
Implemented in mu::ParserInt, and mu::Parser.
Referenced by Init().
|
protectedpure virtual |
Implemented in mu::ParserInt, and mu::Parser.
Referenced by Init().
|
protectedpure virtual |
Implemented in mu::ParserInt, and mu::Parser.
Referenced by Init().
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 |
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 | ) |
Set argument separator.
cArgSep | the argument separator character. |
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.
References _T, mu::ecLOCALE, and Error().
Referenced by ParserHandler< T, NDIM >::changeExpr(), main(), and ParserHandler< T, NDIM >::ParserHandler().
void mu::ParserBase::SetVarFactory | ( | facfun_type | a_pFactory, |
void * | pUserData = NULL |
||
) |
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.
Referenced by DefineConst(), DefineStrConst(), DefineVar(), and mu::ParserTokenReader::ReadNextToken().
const char_type * mu::ParserBase::ValidOprtChars | ( | ) | const |
Virtual function that defines the characters allowed in operator definitions.
Referenced by DefineInfixOprt(), DefineOprt(), and DefinePostfixOprt().
|
friend |
|
staticprotected |
Identifiers for built in binary operators.
When defining custom binary operators with #AddOprt(...) make sure not to choose names conflicting with these definitions.
Referenced by DefineOprt(), and GetOprtDef().