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 a copy of 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...
 
void SetByteCode (const ParserByteCode &a_ByteCode)
 Restore a previously saved bytecode.
 
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 352 of file muParserBase.cpp.

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

353  {
354  m_pTokenReader->AddValIdent(a_pCallback);
355  }
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 1640 of file muParserBase.cpp.

1641  {
1642  m_ConstDef.clear();
1643  m_StrVarDef.clear();
1644  ReInit();
1645  }
void mu::ParserBase::ClearFun ( )

Clear all functions.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1627 of file muParserBase.cpp.

1628  {
1629  m_FunDef.clear();
1630  ReInit();
1631  }
void mu::ParserBase::ClearInfixOprt ( )

Clear the user defined Prefix operators.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow

Definition at line 1674 of file muParserBase.cpp.

1675  {
1676  m_InfixOprtDef.clear();
1677  ReInit();
1678  }
void mu::ParserBase::ClearOprt ( )

Clear all user defined binary operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1663 of file muParserBase.cpp.

1664  {
1665  m_OprtDef.clear();
1666  ReInit();
1667  }
void mu::ParserBase::ClearPostfixOprt ( )

Clear all user defined postfix operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1652 of file muParserBase.cpp.

1653  {
1654  m_PostOprtDef.clear();
1655  ReInit();
1656  }
void mu::ParserBase::ClearVar ( )

Clear all user defined variables.

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

Definition at line 1600 of file muParserBase.cpp.

1601  {
1602  m_VarDef.clear();
1603  ReInit();
1604  }
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 641 of file muParserBase.cpp.

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

642  {
643  if (a_sName.length() > MaxLenIdentifier)
645 
646  CheckName(a_sName, ValidNameChars());
647  m_ConstDef[a_sName] = a_fVal;
648  ReInit();
649  }
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 555 of file muParserBase.cpp.

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

556  {
557  if (a_sName.length() > MaxLenIdentifier)
559 
560  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, cmOPRT_INFIX), m_InfixOprtDef, ValidInfixOprtChars());
561  }
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 574 of file muParserBase.cpp.

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

575  {
576  if (a_sName.length() > MaxLenIdentifier)
578 
579  // Check for conflicts with built in operator names
580  for (int i = 0; m_bBuiltInOp && i < cmENDIF; ++i)
581  {
582  if (a_sName == string_type(c_DefaultOprt[i]))
583  {
584  Error(ecBUILTIN_OVERLOAD, -1, a_sName);
585  }
586  }
587 
588  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, a_eAssociativity), m_OprtDef, ValidOprtChars());
589  }
Trying to overload builtin operator.
Definition: muParserDef.h:254
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:207
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:302
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 525 of file muParserBase.cpp.

526  {
527  if (a_sName.length() > MaxLenIdentifier)
529 
530  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, prPOSTFIX, cmOPRT_POSTFIX), m_PostOprtDef, ValidOprtChars());
531  }
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 596 of file muParserBase.cpp.

597  {
598  // Test if a constant with that names already exists
599  if (m_StrVarDef.find(a_strName) != m_StrVarDef.end())
601 
602  CheckName(a_strName, ValidNameChars());
603 
604  m_vStringVarBuf.push_back(a_strVal); // Store variable string in internal buffer
605  m_StrVarDef[a_strName] = m_vStringVarBuf.size() - 1; // bind buffer index to variable name
606 
607  ReInit();
608  }
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 617 of file muParserBase.cpp.

618  {
619  if (a_pVar == 0)
621 
622  if (a_sName.length() > MaxLenIdentifier)
624 
625  // Test if a constant with that names already exists
626  if (m_ConstDef.find(a_sName) != m_ConstDef.end())
628 
629  CheckName(a_sName, ValidNameChars());
630  m_VarDef[a_sName] = a_pVar;
631  ReInit();
632  }
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 1714 of file muParserBase.cpp.

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

1715  {
1716  m_bBuiltInOp = a_bIsOn;
1717  ReInit();
1718  }
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 1698 of file muParserBase.cpp.

1699  {
1700  ParserBase::g_DbgDumpCmdCode = bDumpCmd;
1701  ParserBase::g_DbgDumpStack = bDumpStack;
1702  }
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 1685 of file muParserBase.cpp.

1686  {
1687  m_vRPN.EnableOptimizer(a_bIsOn);
1688  ReInit();
1689  }
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 1589 of file muParserBase.cpp.

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

1590  {
1591  throw exception_type(a_iErrc, a_sTok, m_pTokenReader->GetExpr(), a_iPos);
1592  }
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 1842 of file muParserBase.cpp.

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

1843  {
1844  return (this->*m_pParseFormula)();
1845  }
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 1855 of file muParserBase.cpp.

1856  {
1857  if (m_vRPN.GetSize() > 0)
1858  {
1859  ParseCmdCode();
1860  }
1861  else
1862  {
1863  ParseString();
1864  }
1865 
1866  nStackSize = m_nFinalResultIdx;
1867 
1868  // (for historic reasons the stack starts at position 1)
1869  return &m_vStackBuffer[1];
1870  }
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 763 of file muParserBase.cpp.

764  {
765  return m_FunDef;
766  }
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 1879 of file muParserBase.cpp.

1880  {
1881  return m_nFinalResultIdx;
1882  }
const char_type ** mu::ParserBase::GetOprtDef ( ) const

Get the default symbols used for the built in operators.

See also
c_DefaultOprt

Definition at line 459 of file muParserBase.cpp.

460  {
461  return (const char_type**)(&c_DefaultOprt[0]);
462  }
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:207
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:308
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 308 of file muParserBase.cpp.

309  {
311 
312  ss << ParserVersion;
313 
314  if (eInfo == pviFULL)
315  {
316  ss << _T(" (") << ParserVersionDate;
317  ss << std::dec << _T("; ") << sizeof(void*) * 8 << _T("BIT");
318 
319 #ifdef _DEBUG
320  ss << _T("; DEBUG");
321 #else
322  ss << _T("; RELEASE");
323 #endif
324 
325 #ifdef _UNICODE
326  ss << _T("; UNICODE");
327 #else
328 #ifdef _MBCS
329  ss << _T("; MBCS");
330 #else
331  ss << _T("; ASCII");
332 #endif
333 #endif
334 
335 #ifdef MUP_USE_OPENMP
336  ss << _T("; OPENMP");
337 #endif
338 
339  ss << _T(")");
340  }
341 
342  return ss.str();
343  }
#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:311
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 1725 of file muParserBase.cpp.

1726  {
1727  return m_bBuiltInOp;
1728  }
void mu::ParserBase::Init ( )
protected

Initialize user defined functions.

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

Definition at line 538 of file muParserBase.cpp.

539  {
540  InitCharSets();
541  InitFun();
542  InitConst();
543  InitOprt();
544  }
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 1612 of file muParserBase.cpp.

1613  {
1614  varmap_type::iterator item = m_VarDef.find(a_strVarName);
1615  if (item != m_VarDef.end())
1616  {
1617  m_VarDef.erase(item);
1618  ReInit();
1619  }
1620  }
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:208
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 1742 of file muParserBase.cpp.

Referenced by ResetLocale().

1743  {
1744  m_pTokenReader->SetArgSep(cArgSep);
1745  }
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:208
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:308
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 441 of file muParserBase.cpp.

442  {
443  // Check locale compatibility
444  if (m_pTokenReader->GetArgSep() == std::use_facet<numpunct<char_type> >(s_locale).decimal_point())
445  Error(ecLOCALE);
446 
447  // Check maximum allowed expression length. An arbitrary value small enough so i can debug expressions sent to me
448  if (a_sExpr.length() >= MaxLenExpression)
449  Error(ecEXPRESSION_TOO_LONG, 0, a_sExpr);
450 
451  m_pTokenReader->SetFormula(a_sExpr + _T(" "));
452  ReInit();
453  }
#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:208
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:208
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:308
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 362 of file muParserBase.cpp.

363  {
364  m_pTokenReader->SetVarCreator(a_pFactory, pUserData);
365  }
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 515 of file muParserBase.cpp.

Referenced by DefineInfixOprt().

516  {
517  MUP_ASSERT(m_sInfixOprtChars.size());
518  return m_sInfixOprtChars.c_str();
519  }
#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 495 of file muParserBase.cpp.

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

496  {
497  MUP_ASSERT(m_sNameChars.size());
498  return m_sNameChars.c_str();
499  }
#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 505 of file muParserBase.cpp.

Referenced by DefineOprt(), and DefinePostfixOprt().

506  {
507  MUP_ASSERT(m_sOprtChars.size());
508  return m_sOprtChars.c_str();
509  }
#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 207 of file muParserBase.h.

Referenced by DefineOprt(), and GetOprtDef().


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