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...
 
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 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=(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 63 of file muParserBase.h.

Member Typedef Documentation

Type of the error class.

Included for backwards compatibility.

Definition at line 98 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 88 of file muParserBase.cpp.

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

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

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

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

319  {
320  m_pTokenReader->AddValIdent(a_pCallback);
321  }
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 1588 of file muParserBase.cpp.

1589  {
1590  m_ConstDef.clear();
1591  m_StrVarDef.clear();
1592  ReInit();
1593  }
void mu::ParserBase::ClearFun ( )

Clear all functions.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1575 of file muParserBase.cpp.

1576  {
1577  m_FunDef.clear();
1578  ReInit();
1579  }
void mu::ParserBase::ClearInfixOprt ( )

Clear the user defined Prefix operators.

Postcondition
Resets the parser to string parser mode.
Exceptions
nothrow

Definition at line 1622 of file muParserBase.cpp.

1623  {
1624  m_InfixOprtDef.clear();
1625  ReInit();
1626  }
void mu::ParserBase::ClearOprt ( )

Clear all user defined binary operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1611 of file muParserBase.cpp.

1612  {
1613  m_OprtDef.clear();
1614  ReInit();
1615  }
void mu::ParserBase::ClearPostfixOprt ( )

Clear all user defined postfix operators.

Postcondition
Resets the parser to string parsing mode.
Exceptions
nothrow

Definition at line 1600 of file muParserBase.cpp.

1601  {
1602  m_PostOprtDef.clear();
1603  ReInit();
1604  }
void mu::ParserBase::ClearVar ( )

Clear all user defined variables.

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

Definition at line 1548 of file muParserBase.cpp.

1549  {
1550  m_VarDef.clear();
1551  ReInit();
1552  }
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 614 of file muParserBase.cpp.

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

615  {
616  if (a_sName.length() > MaxLenIdentifier)
618 
619  CheckName(a_sName, ValidNameChars());
620  m_ConstDef[a_sName] = a_fVal;
621  ReInit();
622  }
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.
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
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 134 of file muParserBase.h.

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

135  {
136  AddCallback(a_strName, ParserCallback(a_pFun, a_bAllowOpt), m_FunDef, ValidNameChars());
137  }
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 528 of file muParserBase.cpp.

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

529  {
530  if (a_sName.length() > MaxLenIdentifier)
532 
533  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, cmOPRT_INFIX), m_InfixOprtDef, ValidInfixOprtChars());
534  }
code for infix operators
Definition: muParserDef.h:176
const char_type * ValidInfixOprtChars() const
Virtual function that defines the characters allowed in infix operator definitions.
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.
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
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 547 of file muParserBase.cpp.

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

548  {
549  if (a_sName.length() > MaxLenIdentifier)
551 
552  // Check for conflicts with built in operator names
553  for (int i = 0; m_bBuiltInOp && i < cmENDIF; ++i)
554  {
555  if (a_sName == string_type(c_DefaultOprt[i]))
556  {
557  Error(ecBUILTIN_OVERLOAD, -1, a_sName);
558  }
559  }
560 
561  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, a_iPrec, a_eAssociativity), m_OprtDef, ValidOprtChars());
562  }
Trying to overload builtin operator.
Definition: muParserDef.h:254
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:186
const char_type * ValidOprtChars() const
Virtual function that defines the characters allowed in operator definitions.
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.
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 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 498 of file muParserBase.cpp.

499  {
500  if (a_sName.length() > MaxLenIdentifier)
502 
503  AddCallback(a_sName, ParserCallback(a_pFun, a_bAllowOpt, prPOSTFIX, cmOPRT_POSTFIX), m_PostOprtDef, ValidOprtChars());
504  }
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.
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.
Thrown when an identifier with more then 255 characters is used.
Definition: muParserDef.h:272
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 569 of file muParserBase.cpp.

570  {
571  // Test if a constant with that names already exists
572  if (m_StrVarDef.find(a_strName) != m_StrVarDef.end())
574 
575  CheckName(a_strName, ValidNameChars());
576 
577  m_vStringVarBuf.push_back(a_strVal); // Store variable string in internal buffer
578  m_StrVarDef[a_strName] = m_vStringVarBuf.size() - 1; // bind buffer index to variable name
579 
580  ReInit();
581  }
Name conflict.
Definition: muParserDef.h:258
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.
const char_type * ValidNameChars() const
Virtual function that defines the characters allowed in name identifiers.
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 590 of file muParserBase.cpp.

591  {
592  if (a_pVar == 0)
594 
595  if (a_sName.length() > MaxLenIdentifier)
597 
598  // Test if a constant with that names already exists
599  if (m_ConstDef.find(a_sName) != m_ConstDef.end())
601 
602  CheckName(a_sName, ValidNameChars());
603  m_VarDef[a_sName] = a_pVar;
604  ReInit();
605  }
Name conflict.
Definition: muParserDef.h:258
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.
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 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 1662 of file muParserBase.cpp.

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

1663  {
1664  m_bBuiltInOp = a_bIsOn;
1665  ReInit();
1666  }
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 1646 of file muParserBase.cpp.

1647  {
1648  ParserBase::g_DbgDumpCmdCode = bDumpCmd;
1649  ParserBase::g_DbgDumpStack = bDumpStack;
1650  }
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 1633 of file muParserBase.cpp.

1634  {
1635  m_vRPN.EnableOptimizer(a_bIsOn);
1636  ReInit();
1637  }
void mu::ParserBase::Error ( EErrorCodes  a_iErrc,
int  a_iPos = (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 1537 of file muParserBase.cpp.

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

1538  {
1539  throw exception_type(a_iErrc, a_sTok, m_pTokenReader->GetExpr(), a_iPos);
1540  }
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:98
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 1790 of file muParserBase.cpp.

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

1791  {
1792  return (this->*m_pParseFormula)();
1793  }
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 1803 of file muParserBase.cpp.

1804  {
1805  if (m_vRPN.GetSize() > 0)
1806  {
1807  ParseCmdCode();
1808  }
1809  else
1810  {
1811  ParseString();
1812  }
1813 
1814  nStackSize = m_nFinalResultIdx;
1815 
1816  // (for historic reasons the stack starts at position 1)
1817  return &m_vStackBuffer[1];
1818  }
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 736 of file muParserBase.cpp.

737  {
738  return m_FunDef;
739  }
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 1827 of file muParserBase.cpp.

1828  {
1829  return m_nFinalResultIdx;
1830  }
const char_type ** mu::ParserBase::GetOprtDef ( ) const

Get the default symbols used for the built in operators.

See also
c_DefaultOprt

Definition at line 432 of file muParserBase.cpp.

433  {
434  return (const char_type**)(&c_DefaultOprt[0]);
435  }
static const char_type * c_DefaultOprt[]
Identifiers for built in binary operators.
Definition: muParserBase.h:186
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 274 of file muParserBase.cpp.

275  {
277 
278  ss << ParserVersion;
279 
280  if (eInfo == pviFULL)
281  {
282  ss << _T(" (") << ParserVersionDate;
283  ss << std::dec << _T("; ") << sizeof(void*) * 8 << _T("BIT");
284 
285 #ifdef _DEBUG
286  ss << _T("; DEBUG");
287 #else
288  ss << _T("; RELEASE");
289 #endif
290 
291 #ifdef _UNICODE
292  ss << _T("; UNICODE");
293 #else
294 #ifdef _MBCS
295  ss << _T("; MBCS");
296 #else
297  ss << _T("; ASCII");
298 #endif
299 #endif
300 
301 #ifdef MUP_USE_OPENMP
302  ss << _T("; OPENMP");
303 #endif
304 
305  ss << _T(")");
306  }
307 
308  return ss.str();
309  }
#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 1673 of file muParserBase.cpp.

1674  {
1675  return m_bBuiltInOp;
1676  }
void mu::ParserBase::Init ( )
protected

Initialize user defined functions.

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

Definition at line 511 of file muParserBase.cpp.

512  {
513  InitCharSets();
514  InitFun();
515  InitConst();
516  InitOprt();
517  }
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 149 of file muParserBase.cpp.

150  {
151  Assign(a_Parser);
152  return *this;
153  }
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 1560 of file muParserBase.cpp.

1561  {
1562  varmap_type::iterator item = m_VarDef.find(a_strVarName);
1563  if (item != m_VarDef.end())
1564  {
1565  m_VarDef.erase(item);
1566  ReInit();
1567  }
1568  }
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 228 of file muParserBase.cpp.

229  {
230  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>('.'));
231  SetArgSep(',');
232  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:187
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 1690 of file muParserBase.cpp.

Referenced by ResetLocale().

1691  {
1692  m_pTokenReader->SetArgSep(cArgSep);
1693  }
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 202 of file muParserBase.cpp.

203  {
204  char_type cThousandsSep = std::use_facet< change_dec_sep<char_type> >(s_locale).thousands_sep();
205  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
206  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:187
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 409 of file muParserBase.cpp.

410  {
411  if (std::all_of(a_sExpr.begin(), a_sExpr.end(), [](char c) { return !std::isgraph(c); }))
412  {
414  }
415 
416  // Check locale compatibility
417  if (m_pTokenReader->GetArgSep() == std::use_facet<numpunct<char_type> >(s_locale).decimal_point())
418  Error(ecLOCALE);
419 
420  // Check maximum allowed expression length. An arbitrary value small enough so i can debug expressions sent to me
421  if (a_sExpr.length() >= MaxLenExpression)
422  Error(ecEXPRESSION_TOO_LONG, 0, a_sExpr);
423 
424  m_pTokenReader->SetFormula(a_sExpr + _T(" "));
425  ReInit();
426  }
#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:187
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.
Conflict with current locale.
Definition: muParserDef.h:264
The expression or identifier contains invalid non printable characters.
Definition: muParserDef.h:276
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 216 of file muParserBase.cpp.

217  {
218  char_type cDecSep = std::use_facet< change_dec_sep<char_type> >(s_locale).decimal_point();
219  s_locale = std::locale(std::locale("C"), new change_dec_sep<char_type>(cDecSep, cThousandsSep));
220  }
static std::locale s_locale
The locale used by the parser.
Definition: muParserBase.h:187
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 328 of file muParserBase.cpp.

329  {
330  m_pTokenReader->SetVarCreator(a_pFactory, pUserData);
331  }
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 488 of file muParserBase.cpp.

Referenced by DefineInfixOprt().

489  {
490  MUP_ASSERT(m_sInfixOprtChars.size());
491  return m_sInfixOprtChars.c_str();
492  }
#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 468 of file muParserBase.cpp.

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

469  {
470  MUP_ASSERT(m_sNameChars.size());
471  return m_sNameChars.c_str();
472  }
#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 478 of file muParserBase.cpp.

Referenced by DefineOprt(), and DefinePostfixOprt().

479  {
480  MUP_ASSERT(m_sOprtChars.size());
481  return m_sOprtChars.c_str();
482  }
#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 186 of file muParserBase.h.

Referenced by DefineOprt(), and GetOprtDef().


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