muParser API -  1.35
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
muParserDef.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2014 Ingo Berg
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy of this
11  software and associated documentation files (the "Software"), to deal in the Software
12  without restriction, including without limitation the rights to use, copy, modify,
13  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14  permit persons to whom the Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in all copies or
17  substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #ifndef MUP_DEF_H
26 #define MUP_DEF_H
27 
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <map>
32 
33 #include "muParserFixes.h"
34 
35 /** \file
36  \brief This file contains standard definitions used by the parser.
37 */
38 
39 #define MUP_VERSION _T("2.2.5")
40 #define MUP_VERSION_DATE _T("20150427; GC")
41 
42 #define MUP_CHARS _T("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
43 
44 /** \brief If this macro is defined mathematical exceptions (div by zero) will be thrown as exceptions. */
45 //#define MUP_MATH_EXCEPTIONS
46 
47 /** \brief Define the base datatype for values.
48 
49  This datatype must be a built in value type. You can not use custom classes.
50  It should be working with all types except "int"!
51 */
52 #define MUP_BASETYPE double
53 
54 /** \brief Activate this option in order to compile with OpenMP support.
55 
56  OpenMP is used only in the bulk mode it may increase the performance a bit.
57 */
58 //#define MUP_USE_OPENMP
59 
60 #if defined(_UNICODE)
61  /** \brief Definition of the basic parser string type. */
62  #define MUP_STRING_TYPE std::wstring
63 
64  #if !defined(_T)
65  #define _T(x) L##x
66  #endif // not defined _T
67 #else
68  #ifndef _T
69  #define _T(x) x
70  #endif
71 
72  /** \brief Definition of the basic parser string type. */
73  #define MUP_STRING_TYPE std::string
74 #endif
75 
76 #if defined(_DEBUG)
77  /** \brief Debug macro to force an abortion of the programm with a certain message.
78  */
79  #define MUP_FAIL(MSG) \
80  { \
81  bool MSG=false; \
82  assert(MSG); \
83  }
84 
85  /** \brief An assertion that does not kill the program.
86 
87  This macro is neutralised in UNICODE builds. It's
88  too difficult to translate.
89  */
90  #define MUP_ASSERT(COND) \
91  if (!(COND)) \
92  { \
93  stringstream_type ss; \
94  ss << _T("Assertion \"") _T(#COND) _T("\" failed: ") \
95  << __FILE__ << _T(" line ") \
96  << __LINE__ << _T("."); \
97  throw ParserError( ss.str() ); \
98  }
99 #else
100  #define MUP_FAIL(MSG)
101  #define MUP_ASSERT(COND)
102 #endif
103 
104 
105 namespace mu
106 {
107 #if defined(_UNICODE)
108 
109  //------------------------------------------------------------------------------
110  /** \brief Encapsulate wcout. */
111  inline std::wostream& console()
112  {
113  return std::wcout;
114  }
115 
116  /** \brief Encapsulate cin. */
117  inline std::wistream& console_in()
118  {
119  return std::wcin;
120  }
121 
122 #else
123 
124  /** \brief Encapsulate cout.
125 
126  Used for supporting UNICODE more easily.
127  */
128  inline std::ostream& console()
129  {
130  return std::cout;
131  }
132 
133  /** \brief Encapsulate cin.
134 
135  Used for supporting UNICODE more easily.
136  */
137  inline std::istream& console_in()
138  {
139  return std::cin;
140  }
141 
142 #endif
143 
144  //------------------------------------------------------------------------------
145  /** \brief Bytecode values.
146 
147  \attention The order of the operator entries must match the order in ParserBase::c_DefaultOprt!
148  */
149  enum ECmdCode
150  {
151  // The following are codes for built in binary operators
152  // apart from built in operators the user has the opportunity to
153  // add user defined operators.
154  cmLE = 0, ///< Operator item: less or equal
155  cmGE = 1, ///< Operator item: greater or equal
156  cmNEQ = 2, ///< Operator item: not equal
157  cmEQ = 3, ///< Operator item: equals
158  cmLT = 4, ///< Operator item: less than
159  cmGT = 5, ///< Operator item: greater than
160  cmADD = 6, ///< Operator item: add
161  cmSUB = 7, ///< Operator item: subtract
162  cmMUL = 8, ///< Operator item: multiply
163  cmDIV = 9, ///< Operator item: division
164  cmPOW = 10, ///< Operator item: y to the power of ...
165  cmLAND = 11,
166  cmLOR = 12,
167  cmASSIGN = 13, ///< Operator item: Assignment operator
168  cmBO = 14, ///< Operator item: opening bracket
169  cmBC = 15, ///< Operator item: closing bracket
170  cmIF = 16, ///< For use in the ternary if-then-else operator
171  cmELSE = 17, ///< For use in the ternary if-then-else operator
172  cmENDIF = 18, ///< For use in the ternary if-then-else operator
173  cmARG_SEP = 19, ///< function argument separator
174  cmVAR = 20, ///< variable item
175  cmVAL = 21, ///< value item
176 
177  // For optimization purposes
178  cmVARPOW2,
179  cmVARPOW3,
180  cmVARPOW4,
181  cmVARMUL,
182  cmPOW2,
183 
184  // operators and functions
185  cmFUNC, ///< Code for a generic function item
186  cmFUNC_STR, ///< Code for a function with a string parameter
187  cmFUNC_BULK, ///< Special callbacks for Bulk mode with an additional parameter for the bulk index
188  cmSTRING, ///< Code for a string token
189  cmOPRT_BIN, ///< user defined binary operator
190  cmOPRT_POSTFIX, ///< code for postfix operators
191  cmOPRT_INFIX, ///< code for infix operators
192  cmEND, ///< end of formula
193  cmUNKNOWN ///< uninitialized item
194  };
195 
196  //------------------------------------------------------------------------------
197  /** \brief Types internally used by the parser.
198  */
200  {
201  tpSTR = 0, ///< String type (Function arguments and constants only, no string variables)
202  tpDBL = 1, ///< Floating point variables
203  tpVOID = 2 ///< Undefined type.
204  };
205 
206  //------------------------------------------------------------------------------
207  enum EParserVersionInfo
208  {
209  pviBRIEF,
210  pviFULL
211  };
212 
213  //------------------------------------------------------------------------------
214  /** \brief Parser operator precedence values. */
216  {
217  oaLEFT = 0,
218  oaRIGHT = 1,
219  oaNONE = 2
220  };
221 
222  //------------------------------------------------------------------------------
223  /** \brief Parser operator precedence values. */
225  {
226  // binary operators
227  prLOR = 1,
228  prLAND = 2,
229  prLOGIC = 3, ///< logic operators
230  prCMP = 4, ///< comparsion operators
231  prADD_SUB = 5, ///< addition
232  prMUL_DIV = 6, ///< multiplication/division
233  prPOW = 7, ///< power operator priority (highest)
234 
235  // infix operators
236  prINFIX = 6, ///< Signs have a higher priority than ADD_SUB, but lower than power operator
237  prPOSTFIX = 6 ///< Postfix operator priority (currently unused)
238  };
239 
240  //------------------------------------------------------------------------------
241  // basic types
242 
243  /** \brief The numeric datatype used by the parser.
244 
245  Normally this is a floating point type either single or double precision.
246  */
248 
249  /** \brief The stringtype used by the parser.
250 
251  Depends on wether UNICODE is used or not.
252  */
254 
255  /** \brief The character type used by the parser.
256 
257  Depends on wether UNICODE is used or not.
258  */
260 
261  /** \brief Typedef for easily using stringstream that respect the parser stringtype. */
262  typedef std::basic_stringstream<char_type,
263  std::char_traits<char_type>,
264  std::allocator<char_type> > stringstream_type;
265 
266  // Data container types
267 
268  /** \brief Type used for storing variables. */
269  typedef std::map<string_type, value_type*> varmap_type;
270 
271  /** \brief Type used for storing constants. */
272  typedef std::map<string_type, value_type> valmap_type;
273 
274  /** \brief Type for assigning a string name to an index in the internal string table. */
275  typedef std::map<string_type, std::size_t> strmap_type;
276 
277  // Parser callbacks
278 
279  /** \brief Callback type used for functions without arguments. */
281 
282  /** \brief Callback type used for functions without arguments. */
283  typedef value_type (*fun_type0)();
284 
285  /** \brief Callback type used for functions with a single arguments. */
287 
288  /** \brief Callback type used for functions with two arguments. */
290 
291  /** \brief Callback type used for functions with three arguments. */
293 
294  /** \brief Callback type used for functions with four arguments. */
296 
297  /** \brief Callback type used for functions with five arguments. */
299 
300  /** \brief Callback type used for functions with five arguments. */
302 
303  /** \brief Callback type used for functions with five arguments. */
305 
306  /** \brief Callback type used for functions with five arguments. */
308 
309  /** \brief Callback type used for functions with five arguments. */
311 
312  /** \brief Callback type used for functions with five arguments. */
314 
315  /** \brief Callback type used for functions without arguments. */
316  typedef value_type (*bulkfun_type0)(int, int);
317 
318  /** \brief Callback type used for functions with a single arguments. */
319  typedef value_type (*bulkfun_type1)(int, int, value_type);
320 
321  /** \brief Callback type used for functions with two arguments. */
323 
324  /** \brief Callback type used for functions with three arguments. */
326 
327  /** \brief Callback type used for functions with four arguments. */
329 
330  /** \brief Callback type used for functions with five arguments. */
332 
333  /** \brief Callback type used for functions with five arguments. */
335 
336  /** \brief Callback type used for functions with five arguments. */
338 
339  /** \brief Callback type used for functions with five arguments. */
341 
342  /** \brief Callback type used for functions with five arguments. */
344 
345  /** \brief Callback type used for functions with five arguments. */
347 
348  /** \brief Callback type used for functions with a variable argument list. */
349  typedef value_type (*multfun_type)(const value_type*, int);
350 
351  /** \brief Callback type used for functions taking a string as an argument. */
352  typedef value_type (*strfun_type1)(const char_type*);
353 
354  /** \brief Callback type used for functions taking a string and a value as arguments. */
355  typedef value_type (*strfun_type2)(const char_type*, value_type);
356 
357  /** \brief Callback type used for functions taking a string and two values as arguments. */
358  typedef value_type (*strfun_type3)(const char_type*, value_type, value_type);
359 
360  /** \brief Callback used for functions that identify values in a string. */
361  typedef int (*identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal);
362 
363  /** \brief Callback used for variable creation factory functions. */
364  typedef value_type* (*facfun_type)(const char_type*, void*);
365 } // end of namespace
366 
367 #endif
368 
Operator item: closing bracket.
Definition: muParserDef.h:169
code for infix operators
Definition: muParserDef.h:191
multiplication/division
Definition: muParserDef.h:232
user defined binary operator
Definition: muParserDef.h:189
value_type(* bulkfun_type6)(int, int, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:334
std::ostream & console()
Encapsulate cout.
Definition: muParserDef.h:128
#define MUP_STRING_TYPE
Definition of the basic parser string type.
Definition: muParserDef.h:73
Postfix operator priority (currently unused)
Definition: muParserDef.h:237
std::map< string_type, std::size_t > strmap_type
Type for assigning a string name to an index in the internal string table.
Definition: muParserDef.h:275
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:269
function argument separator
Definition: muParserDef.h:173
Operator item: y to the power of ...
Definition: muParserDef.h:164
value_type(* strfun_type3)(const char_type *, value_type, value_type)
Callback type used for functions taking a string and two values as arguments.
Definition: muParserDef.h:358
code for postfix operators
Definition: muParserDef.h:190
value_type(* fun_type3)(value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:292
This file contains compatibility fixes for some platforms.
Operator item: not equal.
Definition: muParserDef.h:156
value_type(* bulkfun_type2)(int, int, value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:322
For use in the ternary if-then-else operator.
Definition: muParserDef.h:170
#define MUP_BASETYPE
If this macro is defined mathematical exceptions (div by zero) will be thrown as exceptions.
Definition: muParserDef.h:52
value_type(* bulkfun_type9)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:343
value_type(* bulkfun_type0)(int, int)
Callback type used for functions without arguments.
Definition: muParserDef.h:316
value_type(* fun_type6)(value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:301
value_type(* bulkfun_type7)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:337
end of formula
Definition: muParserDef.h:192
power operator priority (highest)
Definition: muParserDef.h:233
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:264
Code for a generic function item.
Definition: muParserDef.h:185
EOprtAssociativity
Parser operator precedence values.
Definition: muParserDef.h:215
value_type(* strfun_type1)(const char_type *)
Callback type used for functions taking a string as an argument.
Definition: muParserDef.h:352
comparsion operators
Definition: muParserDef.h:230
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:272
value_type(* strfun_type2)(const char_type *, value_type)
Callback type used for functions taking a string and a value as arguments.
Definition: muParserDef.h:355
For use in the ternary if-then-else operator.
Definition: muParserDef.h:171
ETypeCode
Types internally used by the parser.
Definition: muParserDef.h:199
value_type(* bulkfun_type1)(int, int, value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:319
Operator item: subtract.
Definition: muParserDef.h:161
addition
Definition: muParserDef.h:231
For use in the ternary if-then-else operator.
Definition: muParserDef.h:172
ECmdCode
Bytecode values.
Definition: muParserDef.h:149
logic operators
Definition: muParserDef.h:229
Operator item: multiply.
Definition: muParserDef.h:162
value_type(* bulkfun_type4)(int, int, value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:328
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:247
value_type(* bulkfun_type10)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:346
String type (Function arguments and constants only, no string variables)
Definition: muParserDef.h:201
Operator item: division.
Definition: muParserDef.h:163
Operator item: add.
Definition: muParserDef.h:160
Namespace for mathematical applications.
Definition: muParser.cpp:49
Operator item: less than.
Definition: muParserDef.h:158
value item
Definition: muParserDef.h:175
Operator item: greater than.
Definition: muParserDef.h:159
Undefined type.
Definition: muParserDef.h:203
value_type(* generic_fun_type)()
Callback type used for functions without arguments.
Definition: muParserDef.h:280
Special callbacks for Bulk mode with an additional parameter for the bulk index.
Definition: muParserDef.h:187
value_type(* fun_type8)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:307
value_type(* fun_type7)(value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:304
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:259
value_type(* fun_type0)()
Callback type used for functions without arguments.
Definition: muParserDef.h:283
Code for a function with a string parameter.
Definition: muParserDef.h:186
EOprtPrecedence
Parser operator precedence values.
Definition: muParserDef.h:224
value_type(* fun_type4)(value_type, value_type, value_type, value_type)
Callback type used for functions with four arguments.
Definition: muParserDef.h:295
Operator item: equals.
Definition: muParserDef.h:157
uninitialized item
Definition: muParserDef.h:193
Operator item: Assignment operator.
Definition: muParserDef.h:167
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:253
value_type(* fun_type1)(value_type)
Callback type used for functions with a single arguments.
Definition: muParserDef.h:286
int(* identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal)
Callback used for functions that identify values in a string.
Definition: muParserDef.h:361
value_type(* bulkfun_type3)(int, int, value_type, value_type, value_type)
Callback type used for functions with three arguments.
Definition: muParserDef.h:325
Code for a string token.
Definition: muParserDef.h:188
Operator item: less or equal.
Definition: muParserDef.h:154
variable item
Definition: muParserDef.h:174
Operator item: greater or equal.
Definition: muParserDef.h:155
value_type(* bulkfun_type8)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:340
value_type(* fun_type5)(value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:298
std::istream & console_in()
Encapsulate cin.
Definition: muParserDef.h:137
Floating point variables.
Definition: muParserDef.h:202
value_type(* fun_type2)(value_type, value_type)
Callback type used for functions with two arguments.
Definition: muParserDef.h:289
value_type(* fun_type10)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:313
value_type(* bulkfun_type5)(int, int, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:331
value_type(* fun_type9)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type)
Callback type used for functions with five arguments.
Definition: muParserDef.h:310
Signs have a higher priority than ADD_SUB, but lower than power operator.
Definition: muParserDef.h:236
Operator item: opening bracket.
Definition: muParserDef.h:168
value_type(* multfun_type)(const value_type *, int)
Callback type used for functions with a variable argument list.
Definition: muParserDef.h:349