MADNESS  version 0.9
Public Types | Public Member Functions | Protected Member Functions | Static Protected Attributes | Friends | List of all members
mu::ParserBase Class Referenceabstract

Mathematical expressions parser (base parser engine). More...

#include <muParserBase.h>

Inheritance diagram for mu::ParserBase:
Inheritance graph
[legend]

Public Types

typedef ParserError exception_type
 Type of the error class. More...
 

Public Member Functions

 ParserBase ()
 Constructor. More...
 
 ParserBase (const ParserBase &a_Parser)
 
ParserBaseoperator= (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_typeGetUsedVar () const
 Return a map containing the used variables only. More...
 
const varmap_typeGetVar () const
 Return a map containing the used variables only. More...
 
const valmap_typeGetConst () const
 Return a map containing all parser constants. More...
 
const string_typeGetExpr () const
 Retrieve the formula. More...
 
const funmap_typeGetFunDef () 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_typeValidNameChars () const
 Virtual function that defines the characters allowed in name identifiers. More...
 
const char_typeValidOprtChars () const
 Virtual function that defines the characters allowed in operator definitions. More...
 
const char_typeValidInfixOprtChars () 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_typec_DefaultOprt []
 Identifiers for built in binary operators. More...
 

Friends

class ParserTokenReader
 

Detailed Description

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.

Author
(C) 2004-2008 Ingo Berg

Member Typedef Documentation

Type of the error class.

Included for backwards compatibility.

Constructor & Destructor Documentation

mu::ParserBase::ParserBase ( )

Constructor.

Parameters
a_szFormulathe formula to interpret.
Exceptions
ParserExceptionif a_szFormula is null.
mu::ParserBase::ParserBase ( const ParserBase a_Parser)
mu::ParserBase::~ParserBase ( )
virtual

Member Function Documentation

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.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow
void mu::ParserBase::ClearFun ( )

Clear all functions.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow
void mu::ParserBase::ClearInfixOprt ( )

Clear the user defined Prefix operators.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow
void mu::ParserBase::ClearOprt ( )

Clear all user defined binary operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow
void mu::ParserBase::ClearPostfixOprt ( )

Clear all user defined postfix operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow
void mu::ParserBase::ClearVar ( )

Clear all user defined variables.

Exceptions
nothrowResets 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.

Parameters
[in]a_sNameThe name of the constant.
[in]a_fValthe value of the constant.
Postcondition
Will reset the Parser to string parsing mode.
Exceptions
ParserExceptionin 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.

Postcondition
Will reset the Parser to string parsing mode.
Parameters
[in]a_sNameoperator Identifier
[in]a_pFunOperator callback function
[in]a_iPrecOperator Precedence (default=prSIGN)
[in]a_bAllowOptTrue if operator is volatile (default=false)
See also
EPrec

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.

Parameters
[in]a_pFunPointer to the callback function.
[in]a_iPrecPrecedence of the operator.
[in]a_bAllowOptIf 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.

Postcondition
Will reset the Parser to string parsing mode.

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.

Parameters
[in]a_strNameThe name of the constant.
[in]a_strValthe 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.

Parameters
[in]a_sNamethe variable name
[in]a_pVarA pointer to the variable vaule.
Postcondition
Will reset the Parser to string parsing mode.
Precondition
[assert] a_fVar!=0
Exceptions
ParserExceptionin 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.

Exceptions
nothrow
See also
m_bBuiltInOp, ReInit()

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.

Attention
There is no reason to disable bytecode. It will drastically decrease parsing speed.
void mu::ParserBase::EnableOptimizer ( bool  a_bIsOn = true)

Enable or disable the formula optimization feature.

Postcondition
Resets the parser to string parser mode.
Exceptions
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.

Parameters
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.
Exceptions
ParserExceptionalways throws thats the only purpose of this function.

Referenced by DefineOprt(), DefineStrConst(), DefineVar(), and SetExpr().

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.

Precondition
A formula must be set.
Variables must have been set (if needed)
See also
#m_pParseFormula
Returns
The evaluation result
Exceptions
ParseExceptionif 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.

Returns
#m_FunDef
See also
FunProt
Exceptions
nothrowThe 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.

See also
c_DefaultOprt

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.

Returns
#m_bBuiltInOp; true if built in operators are enabled.
Exceptions
nothrow
void mu::ParserBase::Init ( )
protected

Initialize user defined functions.

Calls the virtual functions InitFun(), InitConst() and InitOprt().

References InitCharSets(), InitConst(), InitFun(), and InitOprt().

virtual void mu::ParserBase::InitCharSets ( )
protectedpure virtual

Implemented in mu::ParserInt, and mu::Parser.

Referenced by Init().

virtual void mu::ParserBase::InitConst ( )
protectedpure virtual

Implemented in mu::ParserInt, and mu::Parser.

Referenced by Init().

virtual void mu::ParserBase::InitFun ( )
protectedpure virtual

Implemented in mu::ParserInt, and mu::Parser.

Referenced by Init().

virtual void mu::ParserBase::InitOprt ( )
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.

Parameters
a_ParserObject to copy to this.
Returns
*this
Exceptions
nothrow
void mu::ParserBase::RemoveVar ( const string_type a_strVarName)

Remove a variable from internal storage.

Exceptions
nothrowRemoves 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.

Parameters
cArgSepthe argument separator character.
void mu::ParserBase::SetExpr ( const string_type a_sExpr)

Set the formula.

Parameters
a_strFormulaFormula as string_type
Exceptions
ParserExceptionin 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.

See also
ValidNameChars, ValidOprtChars
const char_type * mu::ParserBase::ValidNameChars ( ) const

Virtual function that defines the characters allowed in name identifiers.

See also
ValidOprtChars, #ValidPrefixOprtChars

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.

See also
ValidNameChars, #ValidPrefixOprtChars

Referenced by DefineInfixOprt(), DefineOprt(), and DefinePostfixOprt().

Friends And Related Function Documentation

friend class ParserTokenReader
friend

Member Data Documentation

const char_type * mu::ParserBase::c_DefaultOprt
staticprotected
Initial value:
=
{
_T("<="), _T(">="), _T("!="),
_T("=="), _T("<"), _T(">"),
_T("+"), _T("-"), _T("*"),
_T("/"), _T("^"), _T("and"),
_T("or"), _T("xor"), _T("="),
_T("("), _T(")"), 0
}

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().


The documentation for this class was generated from the following files: