3 _____ __ _____________ _______ ______ ___________
4 / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5 | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6 |__|_| /____/| __(____ /__| /____ >\___ >__|
8 Copyright (C) 2004 - 2020 Ingo Berg
10 Redistribution and use in source and binary forms, with or without modification, are permitted
11 provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this list of
14 conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice, this list of
16 conditions and the following disclaimer in the documentation and/or other materials provided
17 with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "muParserFixes.h"
40 \brief This file contains standard definitions used by the parser.
43 /** \brief Define the base datatype for values.
45 This datatype must be a built in value type. You can not use custom classes.
46 It should be working with all types except "int"!
48 #define MUP_BASETYPE double
50 /** \brief Activate this option in order to compile with OpenMP support.
52 OpenMP is used only in the bulk mode it may increase the performance a bit.
54 !!! DO NOT ACTIVATE THIS MACRO HERE IF YOU USE CMAKE FOR BUILDING !!!
56 use the cmake option instead!
58 //#define MUP_USE_OPENMP
61 /** \brief Definition of the basic parser string type. */
62 #define MUP_STRING_TYPE std::wstring
66 #endif // not defined _T
72 /** \brief Definition of the basic parser string type. */
73 #define MUP_STRING_TYPE std::string
76 /** \brief An assertion that does not kill the program. */
77 #define MUP_ASSERT(COND) \
80 stringstream_type ss; \
81 ss << _T("Assertion \"") _T(#COND) _T("\" failed: ") \
82 << __FILE__ << _T(" line ") \
83 << __LINE__ << _T("."); \
84 throw ParserError( ecINTERNAL_ERROR, -1, ss.str()); \
89 #pragma warning(disable : 26812)
97 /** \brief Encapsulate wcout. */
98 inline std::wostream& console()
103 /** \brief Encapsulate cin. */
104 inline std::wistream& console_in()
111 /** \brief Encapsulate cout.
113 Used for supporting UNICODE more easily.
115 inline std::ostream& console()
120 /** \brief Encapsulate cin.
122 Used for supporting UNICODE more easily.
124 inline std::istream& console_in()
131 /** \brief Bytecode values.
133 \attention The order of the operator entries must match the order in ParserBase::c_DefaultOprt!
137 // The following are codes for built in binary operators
138 // apart from built in operators the user has the opportunity to
139 // add user defined operators.
140 cmLE = 0, ///< Operator item: less or equal
141 cmGE = 1, ///< Operator item: greater or equal
142 cmNEQ = 2, ///< Operator item: not equal
143 cmEQ = 3, ///< Operator item: equals
144 cmLT = 4, ///< Operator item: less than
145 cmGT = 5, ///< Operator item: greater than
146 cmADD = 6, ///< Operator item: add
147 cmSUB = 7, ///< Operator item: subtract
148 cmMUL = 8, ///< Operator item: multiply
149 cmDIV = 9, ///< Operator item: division
150 cmPOW = 10, ///< Operator item: y to the power of ...
153 cmASSIGN = 13, ///< Operator item: Assignment operator
154 cmBO = 14, ///< Operator item: opening bracket
155 cmBC = 15, ///< Operator item: closing bracket
156 cmIF = 16, ///< For use in the ternary if-then-else operator
157 cmELSE = 17, ///< For use in the ternary if-then-else operator
158 cmENDIF = 18, ///< For use in the ternary if-then-else operator
159 cmARG_SEP = 19, ///< function argument separator
160 cmVAR = 20, ///< variable item
161 cmVAL = 21, ///< value item
163 // For optimization purposes
169 // operators and functions
170 cmFUNC = 26, ///< Code for a generic function item
171 cmFUNC_STR, ///< Code for a function with a string parameter
172 cmFUNC_BULK, ///< Special callbacks for Bulk mode with an additional parameter for the bulk index
173 cmSTRING, ///< Code for a string token
174 cmOPRT_BIN, ///< user defined binary operator
175 cmOPRT_POSTFIX, ///< code for postfix operators
176 cmOPRT_INFIX, ///< code for infix operators
177 cmEND, ///< end of formula
178 cmUNKNOWN ///< uninitialized item
181 /** \brief Types internally used by the parser.
185 tpSTR = 0, ///< String type (Function arguments and constants only, no string variables)
186 tpDBL = 1, ///< Floating point variables
187 tpVOID = 2 ///< Undefined type.
191 enum EParserVersionInfo
198 /** \brief Parser operator precedence values. */
199 enum EOprtAssociativity
207 /** \brief Parser operator precedence values. */
213 prLOGIC = 3, ///< logic operators
214 prCMP = 4, ///< comparsion operators
215 prADD_SUB = 5, ///< addition
216 prMUL_DIV = 6, ///< multiplication/division
217 prPOW = 7, ///< power operator priority (highest)
220 prINFIX = 6, ///< Signs have a higher priority than ADD_SUB, but lower than power operator
221 prPOSTFIX = 6 ///< Postfix operator priority (currently unused)
225 /** \brief Error codes. */
228 // Formula syntax errors
229 ecUNEXPECTED_OPERATOR = 0, ///< Unexpected binary operator found
230 ecUNASSIGNABLE_TOKEN = 1, ///< Token can't be identified.
231 ecUNEXPECTED_EOF = 2, ///< Unexpected end of formula. (Example: "2+sin(")
232 ecUNEXPECTED_ARG_SEP = 3, ///< An unexpected comma has been found. (Example: "1,23")
233 ecUNEXPECTED_ARG = 4, ///< An unexpected argument has been found
234 ecUNEXPECTED_VAL = 5, ///< An unexpected value token has been found
235 ecUNEXPECTED_VAR = 6, ///< An unexpected variable token has been found
236 ecUNEXPECTED_PARENS = 7, ///< Unexpected Parenthesis, opening or closing
237 ecUNEXPECTED_STR = 8, ///< A string has been found at an inapropriate position
238 ecSTRING_EXPECTED = 9, ///< A string function has been called with a different type of argument
239 ecVAL_EXPECTED = 10, ///< A numerical function has been called with a non value type of argument
240 ecMISSING_PARENS = 11, ///< Missing parens. (Example: "3*sin(3")
241 ecUNEXPECTED_FUN = 12, ///< Unexpected function found. (Example: "sin(8)cos(9)")
242 ecUNTERMINATED_STRING = 13, ///< unterminated string constant. (Example: "3*valueof("hello)")
243 ecTOO_MANY_PARAMS = 14, ///< Too many function parameters
244 ecTOO_FEW_PARAMS = 15, ///< Too few function parameters. (Example: "ite(1<2,2)")
245 ecOPRT_TYPE_CONFLICT = 16, ///< binary operators may only be applied to value items of the same type
246 ecSTR_RESULT = 17, ///< result is a string
248 // Invalid Parser input Parameters
249 ecINVALID_NAME = 18, ///< Invalid function, variable or constant name.
250 ecINVALID_BINOP_IDENT = 19, ///< Invalid binary operator identifier
251 ecINVALID_INFIX_IDENT = 20, ///< Invalid function, variable or constant name.
252 ecINVALID_POSTFIX_IDENT = 21, ///< Invalid function, variable or constant name.
254 ecBUILTIN_OVERLOAD = 22, ///< Trying to overload builtin operator
255 ecINVALID_FUN_PTR = 23, ///< Invalid callback function pointer
256 ecINVALID_VAR_PTR = 24, ///< Invalid variable pointer
257 ecEMPTY_EXPRESSION = 25, ///< The Expression is empty
258 ecNAME_CONFLICT = 26, ///< Name conflict
259 ecOPT_PRI = 27, ///< Invalid operator priority
261 ecDOMAIN_ERROR = 28, ///< catch division by zero, sqrt(-1), log(0) (currently unused)
262 ecDIV_BY_ZERO = 29, ///< Division by zero (currently unused)
263 ecGENERIC = 30, ///< Generic error
264 ecLOCALE = 31, ///< Conflict with current locale
266 ecUNEXPECTED_CONDITIONAL = 32,
267 ecMISSING_ELSE_CLAUSE = 33,
268 ecMISPLACED_COLON = 34,
270 ecUNREASONABLE_NUMBER_OF_COMPUTATIONS = 35,
272 ecIDENTIFIER_TOO_LONG = 36, ///< Thrown when an identifier with more then 255 characters is used.
274 ecEXPRESSION_TOO_LONG = 37, ///< Throw an exception if the expression has more than 10000 characters. (an arbitrary limit)
276 ecINVALID_CHARACTERS_FOUND = 38,///< The expression or identifier contains invalid non printable characters
279 ecINTERNAL_ERROR = 39, ///< Internal error of any kind.
281 // The last two are special entries
282 ecCOUNT, ///< This is no error code, It just stores just the total number of error codes
283 ecUNDEFINED = -1 ///< Undefined message, placeholder to detect unassigned error messages
286 //------------------------------------------------------------------------------
288 //------------------------------------------------------------------------------
290 /** \brief The numeric datatype used by the parser.
292 Normally this is a floating point type either single or double precision.
294 typedef MUP_BASETYPE value_type;
296 /** \brief The stringtype used by the parser.
298 Depends on whether UNICODE is used or not.
300 typedef MUP_STRING_TYPE string_type;
302 /** \brief The character type used by the parser.
304 Depends on whether UNICODE is used or not.
306 typedef string_type::value_type char_type;
308 /** \brief Typedef for easily using stringstream that respect the parser stringtype. */
309 typedef std::basic_stringstream<char_type, std::char_traits<char_type>, std::allocator<char_type> > stringstream_type;
311 // Data container types
313 /** \brief Type used for storing variables. */
314 typedef std::map<string_type, value_type*> varmap_type;
316 /** \brief Type used for storing constants. */
317 typedef std::map<string_type, value_type> valmap_type;
319 /** \brief Type for assigning a string name to an index in the internal string table. */
320 typedef std::map<string_type, std::size_t> strmap_type;
324 /** \brief Callback type used for functions without arguments. */
325 typedef value_type(*generic_fun_type)();
327 /** \brief Callback type used for functions without arguments. */
328 typedef value_type(*fun_type0)();
330 /** \brief Callback type used for functions with a single arguments. */
331 typedef value_type(*fun_type1)(value_type);
333 /** \brief Callback type used for functions with two arguments. */
334 typedef value_type(*fun_type2)(value_type, value_type);
336 /** \brief Callback type used for functions with three arguments. */
337 typedef value_type(*fun_type3)(value_type, value_type, value_type);
339 /** \brief Callback type used for functions with four arguments. */
340 typedef value_type(*fun_type4)(value_type, value_type, value_type, value_type);
342 /** \brief Callback type used for functions with five arguments. */
343 typedef value_type(*fun_type5)(value_type, value_type, value_type, value_type, value_type);
345 /** \brief Callback type used for functions with six arguments. */
346 typedef value_type(*fun_type6)(value_type, value_type, value_type, value_type, value_type, value_type);
348 /** \brief Callback type used for functions with seven arguments. */
349 typedef value_type(*fun_type7)(value_type, value_type, value_type, value_type, value_type, value_type, value_type);
351 /** \brief Callback type used for functions with eight arguments. */
352 typedef value_type(*fun_type8)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
354 /** \brief Callback type used for functions with nine arguments. */
355 typedef value_type(*fun_type9)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
357 /** \brief Callback type used for functions with ten arguments. */
358 typedef value_type(*fun_type10)(value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
360 /** \brief Callback type used for functions without arguments. */
361 typedef value_type(*bulkfun_type0)(int, int);
363 /** \brief Callback type used for functions with a single arguments. */
364 typedef value_type(*bulkfun_type1)(int, int, value_type);
366 /** \brief Callback type used for functions with two arguments. */
367 typedef value_type(*bulkfun_type2)(int, int, value_type, value_type);
369 /** \brief Callback type used for functions with three arguments. */
370 typedef value_type(*bulkfun_type3)(int, int, value_type, value_type, value_type);
372 /** \brief Callback type used for functions with four arguments. */
373 typedef value_type(*bulkfun_type4)(int, int, value_type, value_type, value_type, value_type);
375 /** \brief Callback type used for functions with five arguments. */
376 typedef value_type(*bulkfun_type5)(int, int, value_type, value_type, value_type, value_type, value_type);
378 /** \brief Callback type used for functions with six arguments. */
379 typedef value_type(*bulkfun_type6)(int, int, value_type, value_type, value_type, value_type, value_type, value_type);
381 /** \brief Callback type used for functions with seven arguments. */
382 typedef value_type(*bulkfun_type7)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
384 /** \brief Callback type used for functions with eight arguments. */
385 typedef value_type(*bulkfun_type8)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
387 /** \brief Callback type used for functions with nine arguments. */
388 typedef value_type(*bulkfun_type9)(int, int, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type, value_type);
390 /** \brief Callback type used for functions with ten arguments. */
391 typedef 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);
393 /** \brief Callback type used for functions with a variable argument list. */
394 typedef value_type(*multfun_type)(const value_type*, int);
396 /** \brief Callback type used for functions taking a string as an argument. */
397 typedef value_type(*strfun_type1)(const char_type*);
399 /** \brief Callback type used for functions taking a string and a value as arguments. */
400 typedef value_type(*strfun_type2)(const char_type*, value_type);
402 /** \brief Callback type used for functions taking a string and two values as arguments. */
403 typedef value_type(*strfun_type3)(const char_type*, value_type, value_type);
405 /** \brief Callback type used for functions taking a string and a value as arguments. */
406 typedef value_type(*strfun_type4)(const char_type*, value_type, value_type, value_type);
408 /** \brief Callback type used for functions taking a string and two values as arguments. */
409 typedef value_type(*strfun_type5)(const char_type*, value_type, value_type, value_type, value_type);
411 /** \brief Callback used for functions that identify values in a string. */
412 typedef int (*identfun_type)(const char_type* sExpr, int* nPos, value_type* fVal);
414 /** \brief Callback used for variable creation factory functions. */
415 typedef value_type* (*facfun_type)(const char_type*, void*);
417 static const int MaxLenExpression = 5000;
418 static const int MaxLenIdentifier = 100;
419 static const string_type ParserVersion = string_type(_T("2.3.2"));
420 static const string_type ParserVersionDate = string_type(_T("20200617"));
421 } // end of namespace
423 #if defined(_MSC_VER)