muParser API -  1.35
Classes | Public Types | Public Member Functions | Static 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]

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...
 
ParserBaseoperator= (const ParserBase &a_Parser)
 Assignment operator. More...
 
value_type Eval () const
 Calculate the result. More...
 
value_typeEval (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=nullptr)
 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...
 
template<typename T >
void DefineFunUserData (const string_type &a_strName, T a_pFun, void *a_pUserData, bool a_bAllowOpt=true)
 Define a parser function with user data (not null). 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_typeGetUsedVar () const
 Return a map containing the used variables only.
 
const varmap_typeGetVar () const
 Return a map containing the used variables only.
 
const valmap_typeGetConst () const
 Return a map containing all parser constants.
 
const string_typeGetExpr () const
 Retrieve the formula.
 
const funmap_typeGetFunDef () const
 Return prototypes of all parser functions. More...
 
string_type GetVersion (EParserVersionInfo eInfo=pviFULL) const
 Returns the version of muparser. More...
 
const ParserByteCodeGetByteCode () const
 Returns the bytecode of the current expression.
 
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_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.
 

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...
 
void Error (EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
 Create an error containing the parse error position. 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_typec_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
 

Detailed Description

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 68 of file muParserBase.h.

Member Typedef Documentation

Type of the error class.

Included for backwards compatibility.

Definition at line 103 of file muParserBase.h.

Constructor & Destructor Documentation

mu::ParserBase::ParserBase ( )

Constructor.

Parameters
a_szFormulathe formula to interpret.
Exceptions
ParserExceptionif a_szFormula is nullptr.

Definition at line 90 of file muParserBase.cpp.

91  : m_pParseFormula(&ParserBase::ParseString)
92  , m_vRPN()
93  , m_vStringBuf()
94  , m_pTokenReader()
95  , m_FunDef()
96  , m_PostOprtDef()
97  , m_InfixOprtDef()
98  , m_OprtDef()
99  , m_ConstDef()
100  , m_StrVarDef()
101  , m_VarDef()
102  , m_bBuiltInOp(true)
103  , m_sNameChars()
104  , m_sOprtChars()
105  , m_sInfixOprtChars()
106  , m_vStackBuffer()
107  , m_nFinalResultIdx(0)
108  {
109  InitTokenReader();
110  }
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 118 of file muParserBase.cpp.

119  : m_pParseFormula(&ParserBase::ParseString)
120  , m_vRPN()
121  , m_vStringBuf()
122  , m_pTokenReader()
123  , m_FunDef()
124  , m_PostOprtDef()
125  , m_InfixOprtDef()
126  , m_OprtDef()
127  , m_ConstDef()
128  , m_StrVarDef()
129  , m_VarDef()
130  , m_bBuiltInOp(true)
131  , m_sNameChars()
132  , m_sOprtChars()
133  , m_sInfixOprtChars()
134  {
135  m_pTokenReader.reset(new token_reader_type(this));
136  Assign(a_Parser);
137  }

Member Function Documentation

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 328 of file muParserBase.cpp.

Referenced by mu::Parser::Parser(), and mu::ParserInt::ParserInt().

329  {
330  m_pTokenReader->AddValIdent(a_pCallback);
331  }
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

Definition at line 1614 of file muParserBase.cpp.

1615  {
1616  m_ConstDef.clear();
1617  m_StrVarDef.clear();
1618  ReInit();
1619  }
void mu::ParserBase::ClearFun ( )

Clear all functions.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1601 of file muParserBase.cpp.

1602  {
1603  m_FunDef.clear();
1604  ReInit();
1605  }
void mu::ParserBase::ClearInfixOprt ( )

Clear the user defined Prefix operators.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow

Definition at line 1648 of file muParserBase.cpp.

1649  {
1650  m_InfixOprtDef.clear();
1651  ReInit();
1652  }
void mu::ParserBase::ClearOprt ( )

Clear all user defined binary operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1637 of file muParserBase.cpp.

1638  {
1639  m_OprtDef.clear();
1640  ReInit();
1641  }
void mu::ParserBase::ClearPostfixOprt ( )

Clear all user defined postfix operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1626 of file muParserBase.cpp.

1627  {
1628  m_PostOprtDef.clear();
1629  ReInit();
1630  }
void mu::ParserBase::ClearVar ( )

Clear all user defined variables.

Exceptions
nothrowResets the parser to string parsing mode by calling #ReInit.

Definition at line 1574 of file muParserBase.cpp.

1575  {
1576  m_VarDef.clear();
1577  ReInit();
1578  }
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.

Definition at line 617 of file muParserBase.cpp.

Referenced by mu::Parser::InitConst().

618  {
619  if (a_sName.length() > MaxLenIdentifier)
621 
622  CheckName(a_sName, ValidNameChars());
623  m_ConstDef[a_sName] = a_fVal;
624  ReInit();
625  }
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
template<typename T >
void mu::ParserBase::DefineFun ( const string_type a_strName,
a_pFun,
bool  a_bAllowOpt = true 
)
inline

Define a parser function without arguments.

Parameters
a_strNameName of the function
a_pFunPointer to the callback function
a_bAllowOptA flag indicating this function may be optimized

Definition at line 139 of file muParserBase.h.

Referenced by mu::Parser::InitFun(), and mu::ParserInt::InitFun().

140  {
141  AddCallback(a_strName, ParserCallback(a_pFun, a_bAllowOpt), m_FunDef, ValidNameChars());
142  }
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
template<typename T >
void mu::ParserBase::DefineFunUserData ( const string_type a_strName,
a_pFun,
void *  a_pUserData,
bool  a_bAllowOpt = true 
)
inline

Define a parser function with user data (not null).

Parameters
a_strNameName of the function
a_pFunPointer to the callback function
a_pUserDataPointer that will be passed back to callback (shall not be nullptr)
a_bAllowOptA flag indicating this function may be optimized

Definition at line 152 of file muParserBase.h.

153  {
154  AddCallback(a_strName, ParserCallback(a_pFun, a_pUserData, a_bAllowOpt), m_FunDef, ValidNameChars());
155  }
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
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

Definition at line 531 of file muParserBase.cpp.

Referenced by mu::Parser::InitOprt(), and mu::ParserInt::InitOprt().

532  {
533  if (a_sName.length() > MaxLenIdentifier)
535 
536  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, cmOPRT_INFIX), m_InfixOprtDef, ValidInfixOprtChars());
537  }
code for infix operators
Definition: muParserDef.h:176
const char_type * ValidInfixOprtChars() const
Virtual function that defines the characters allowed in infix operator definitions.
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
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.

Parameters
[in]a_sNameThe identifier of the operator.
[in]a_pFunPointer to the callback function.
[in]a_iPrecPrecedence of the operator.
[in]a_eAssociativityThe associativity of the operator.
[in]a_bAllowOptIf this is true the operator may be optimized away.

Adds a new Binary operator the the parser instance.

Definition at line 550 of file muParserBase.cpp.

Referenced by mu::ParserInt::InitOprt().

551  {
552  if (a_sName.length() > MaxLenIdentifier)
554 
555  // Check for conflicts with built in operator names
556  for (int i = 0; m_bBuiltInOp && i < cmENDIF; ++i)
557  {
558  if (a_sName == string_type(c_DefaultOprt[i]))
559  {
560  Error(ecBUILTIN_OVERLOAD, -1, a_sName);
561  }
562  }
563 
564  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, a_eAssociativity), m_OprtDef, ValidOprtChars());
565  }
Trying to overload builtin operator.
Definition: muParserDef.h:254
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:205
const char_type * ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
For use in the ternary if-then-else operator.
Definition: muParserDef.h:158
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:300
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
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.

Definition at line 501 of file muParserBase.cpp.

502  {
503  if (a_sName.length() > MaxLenIdentifier)
505 
506  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, prPOSTFIX, cmOPRT_POSTFIX), m_PostOprtDef, ValidOprtChars());
507  }
Postfix operator priority (currently unused)
Definition: muParserDef.h:221
code for postfix operators
Definition: muParserDef.h:175
const char_type * ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
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.

Definition at line 572 of file muParserBase.cpp.

573  {
574  // Test if a constant with that names already exists
575  if (m_StrVarDef.find(a_strName) != m_StrVarDef.end())
577 
578  CheckName(a_strName, ValidNameChars());
579 
580  m_vStringVarBuf.push_back(a_strVal); // Store variable string in internal buffer
581  m_StrVarDef[a_strName] = m_vStringVarBuf.size() - 1; // bind buffer index to variable name
582 
583  ReInit();
584  }
Name conflict.
Definition: muParserDef.h:258
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
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 value.
Postcondition
Will reset the Parser to string parsing mode.
Exceptions
ParserExceptionin case the name contains invalid signs or a_pVar is nullptr.

Definition at line 593 of file muParserBase.cpp.

594  {
595  if (a_pVar == 0)
597 
598  if (a_sName.length() > MaxLenIdentifier)
600 
601  // Test if a constant with that names already exists
602  if (m_ConstDef.find(a_sName) != m_ConstDef.end())
604 
605  CheckName(a_sName, ValidNameChars());
606  m_VarDef[a_sName] = a_pVar;
607  ReInit();
608  }
Name conflict.
Definition: muParserDef.h:258
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
Invalid variable pointer.
Definition: muParserDef.h:256
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
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().

Definition at line 1688 of file muParserBase.cpp.

Referenced by mu::ParserInt::InitOprt().

1689  {
1690  m_bBuiltInOp = a_bIsOn;
1691  ReInit();
1692  }
void mu::ParserBase::EnableDebugDump ( bool  bDumpCmd,
bool  bDumpStack 
)
static

Enable the dumping of bytecode and stack content on the console.

Parameters
bDumpCmdFlag to enable dumping of the current bytecode to the console.
bDumpStackFlag to enable dumping of the stack content is written to the console.

This function is for debug purposes only!

Definition at line 1672 of file muParserBase.cpp.

1673  {
1674  ParserBase::g_DbgDumpCmdCode = bDumpCmd;
1675  ParserBase::g_DbgDumpStack = bDumpStack;
1676  }
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

Definition at line 1659 of file muParserBase.cpp.

1660  {
1661  m_vRPN.EnableOptimizer(a_bIsOn);
1662  ReInit();
1663  }
void mu::ParserBase::Error ( EErrorCodes  a_iErrc,
int  a_iPos = static_cast<int>(mu::string_type::npos),
const string_type a_sTok = string_type() 
) const
protected

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 that's the only purpose of this function.

Definition at line 1563 of file muParserBase.cpp.

Referenced by DefineConst(), DefineInfixOprt(), DefineOprt(), DefinePostfixOprt(), DefineStrConst(), DefineVar(), and SetExpr().

1564  {
1565  throw exception_type(a_iErrc, a_sTok, m_pTokenReader->GetExpr(), a_iPos);
1566  }
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:103
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.

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.

Definition at line 1816 of file muParserBase.cpp.

Referenced by mu::Parser::Diff().

1817  {
1818  return (this->*m_pParseFormula)();
1819  }
value_type * mu::ParserBase::Eval ( int &  nStackSize) const

Evaluate an expression containing comma separated subexpressions.

Parameters
[out]nStackSizeThe total number of results available
Returns
Pointer to the array containing all expression results

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 1829 of file muParserBase.cpp.

1830  {
1831  if (m_vRPN.GetSize() > 0)
1832  {
1833  ParseCmdCode();
1834  }
1835  else
1836  {
1837  ParseString();
1838  }
1839 
1840  nStackSize = m_nFinalResultIdx;
1841 
1842  // (for historic reasons the stack starts at position 1)
1843  return &m_vStackBuffer[1];
1844  }
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.

Definition at line 739 of file muParserBase.cpp.

740  {
741  return m_FunDef;
742  }
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 1853 of file muParserBase.cpp.

1854  {
1855  return m_nFinalResultIdx;
1856  }
const char_type ** mu::ParserBase::GetOprtDef ( ) const

Get the default symbols used for the built in operators.

See also
c_DefaultOprt

Definition at line 435 of file muParserBase.cpp.

436  {
437  return (const char_type**)(&c_DefaultOprt[0]);
438  }
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:205
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
string_type mu::ParserBase::GetVersion ( EParserVersionInfo  eInfo = pviFULL) const

Returns the version of muparser.

Parameters
eInfoA 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 284 of file muParserBase.cpp.

285  {
287 
288  ss << ParserVersion;
289 
290  if (eInfo == pviFULL)
291  {
292  ss << _T(" (") << ParserVersionDate;
293  ss << std::dec << _T("; ") << sizeof(void*) * 8 << _T("BIT");
294 
295 #ifdef _DEBUG
296  ss << _T("; DEBUG");
297 #else
298  ss << _T("; RELEASE");
299 #endif
300 
301 #ifdef _UNICODE
302  ss << _T("; UNICODE");
303 #else
304 #ifdef _MBCS
305  ss << _T("; MBCS");
306 #else
307  ss << _T("; ASCII");
308 #endif
309 #endif
310 
311 #ifdef MUP_USE_OPENMP
312  ss << _T("; OPENMP");
313 #endif
314 
315  ss << _T(")");
316  }
317 
318  return ss.str();
319  }
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
std::basic_stringstream< char_type, std::char_traits< char_type >, std::allocator< char_type > > stringstream_type
Typedef for easily using stringstream that respect the parser stringtype.
Definition: muParserDef.h:309
bool mu::ParserBase::HasBuiltInOprt ( ) const

Query status of built in variables.

Returns
#m_bBuiltInOp; true if built in operators are enabled.
Exceptions
nothrow

Definition at line 1699 of file muParserBase.cpp.

1700  {
1701  return m_bBuiltInOp;
1702  }
void mu::ParserBase::Init ( )
protected

Initialize user defined functions.

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

Definition at line 514 of file muParserBase.cpp.

515  {
516  InitCharSets();
517  InitFun();
518  InitConst();
519  InitOprt();
520  }
ParserBase & mu::ParserBase::operator= ( const ParserBase a_Parser)

Assignment operator.

Implemented by calling Assign(a_Parser). Self assignment is suppressed.

Parameters
a_ParserObject to copy to this.
Returns
*this
Exceptions
nothrow

Definition at line 151 of file muParserBase.cpp.

152  {
153  Assign(a_Parser);
154  return *this;
155  }
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.

Definition at line 1586 of file muParserBase.cpp.

1587  {
1588  varmap_type::iterator item = m_VarDef.find(a_strVarName);
1589  if (item != m_VarDef.end())
1590  {
1591  m_VarDef.erase(item);
1592  ReInit();
1593  }
1594  }
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 230 of file muParserBase.cpp.

231  {
232  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>('.'));
233  SetArgSep(',');
234  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:206
void SetArgSep(char_type cArgSep)
Set argument separator.
void mu::ParserBase::SetArgSep ( char_type  cArgSep)

Set argument separator.

Parameters
cArgSepthe argument separator character.

Definition at line 1716 of file muParserBase.cpp.

Referenced by ResetLocale().

1717  {
1718  m_pTokenReader->SetArgSep(cArgSep);
1719  }
void mu::ParserBase::SetDecSep ( char_type  cDecSep)

Set the decimal separator.

Parameters
cDecSepDecimal separator as a character value.
See also
SetThousandsSep

By default muparser uses the "C" locale. The decimal separator of this locale is overwritten by the one provided here.

Definition at line 204 of file muParserBase.cpp.

205  {
206  char_type cThousandsSep = std::use_facet< change_dec_sep<char_type> >(s_locale).thousands_sep();
207  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
208  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:206
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
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.

Definition at line 417 of file muParserBase.cpp.

418  {
419  // Check locale compatibility
420  if (m_pTokenReader->GetArgSep() == std::use_facet<numpunct<char_type> >(s_locale).decimal_point())
421  Error(ecLOCALE);
422 
423  // Check maximum allowed expression length. An arbitrary value small enough so i can debug expressions sent to me
424  if (a_sExpr.length() >= MaxLenExpression)
425  Error(ecEXPRESSION_TOO_LONG, 0, a_sExpr);
426 
427  m_pTokenReader->SetFormula(a_sExpr + _T(" "));
428  ReInit();
429  }
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
Throw an exception if the expression has more than 10000 characters. (an arbitrary limit) ...
Definition: muParserDef.h:274
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:206
Conflict with current locale.
Definition: muParserDef.h:264
void Error(EErrorCodes a_iErrc, int a_iPos=static_cast< int >(mu::string_type::npos), const string_type &a_strTok=string_type()) const
Create an error containing the parse error position.
void mu::ParserBase::SetThousandsSep ( char_type  cThousandsSep = 0)

Sets the thousands operator.

Parameters
cThousandsSepThe thousands separator as a character
See also
SetDecSep

By default muparser uses the "C" locale. The thousands separator of this locale is overwritten by the one provided here.

Definition at line 218 of file muParserBase.cpp.

219  {
220  char_type cDecSep = std::use_facet< change_dec_sep<char_type> >(s_locale).decimal_point();
221  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
222  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:206
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
void mu::ParserBase::SetVarFactory ( facfun_type  a_pFactory,
void *  pUserData = nullptr 
)

Set a function that can create variable pointer for unknown expression variables.

Parameters
a_pFactoryA pointer to the variable factory.
pUserDataA user defined context pointer.

Definition at line 338 of file muParserBase.cpp.

339  {
340  m_pTokenReader->SetVarCreator(a_pFactory, pUserData);
341  }
const char_type * mu::ParserBase::ValidInfixOprtChars ( ) const

Virtual function that defines the characters allowed in infix operator definitions.

See also
ValidNameChars, ValidOprtChars

Definition at line 491 of file muParserBase.cpp.

Referenced by DefineInfixOprt().

492  {
493  MUP_ASSERT(m_sInfixOprtChars.size());
494  return m_sInfixOprtChars.c_str();
495  }
#define MUP_ASSERT(COND)
An assertion that does not kill the program.
Definition: muParserDef.h:77
const char_type * mu::ParserBase::ValidNameChars ( ) const

Virtual function that defines the characters allowed in name identifiers.

See also
ValidOprtChars, #ValidPrefixOprtChars

Definition at line 471 of file muParserBase.cpp.

Referenced by DefineConst(), DefineStrConst(), DefineVar(), and mu::ParserTokenReader::ReadNextToken().

472  {
473  MUP_ASSERT(m_sNameChars.size());
474  return m_sNameChars.c_str();
475  }
#define MUP_ASSERT(COND)
An assertion that does not kill the program.
Definition: muParserDef.h:77
const char_type * mu::ParserBase::ValidOprtChars ( ) const

Virtual function that defines the characters allowed in operator definitions.

See also
ValidNameChars, #ValidPrefixOprtChars

Definition at line 481 of file muParserBase.cpp.

Referenced by DefineOprt(), and DefinePostfixOprt().

482  {
483  MUP_ASSERT(m_sOprtChars.size());
484  return m_sOprtChars.c_str();
485  }
#define MUP_ASSERT(COND)
An assertion that does not kill the program.
Definition: muParserDef.h:77

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("&&"),
_T("||"), _T("="), _T("("),
_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.

Definition at line 205 of file muParserBase.h.

Referenced by DefineOprt(), and GetOprtDef().


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