muParser API -  1.35
muParserTokenReader.h
Go to the documentation of this file.
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2022 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
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.
18 
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.
27 */
28 
29 #ifndef MU_PARSER_TOKEN_READER_H
30 #define MU_PARSER_TOKEN_READER_H
31 
32 #include <cstdio>
33 #include <cstring>
34 #include <list>
35 #include <map>
36 #include <memory>
37 #include <stack>
38 #include <string>
39 
40 #include "muParserDef.h"
41 #include "muParserToken.h"
42 
43 /** \file
44  \brief This file contains the parser token reader definition.
45 */
46 
47 
48 namespace mu
49 {
50  // Forward declaration
51  class ParserBase;
52 
53  /** \brief Token reader for the ParserBase class. */
54  class ParserTokenReader final
55  {
56  private:
57 
59 
60  public:
61 
62  ParserTokenReader(ParserBase* a_pParent);
63  ParserTokenReader* Clone(ParserBase* a_pParent) const;
64 
65  void AddValIdent(identfun_type a_pCallback);
66  void SetVarCreator(facfun_type a_pFactory, void* pUserData);
67  void SetFormula(const string_type& a_strFormula);
68  void SetArgSep(char_type cArgSep);
69 
70  int GetPos() const;
71  const string_type& GetExpr() const;
73  char_type GetArgSep() const;
74 
75  void IgnoreUndefVar(bool bIgnore);
76  void ReInit();
77  token_type ReadNextToken();
78 
79  private:
80 
81  /** \brief Syntax codes.
82 
83  The syntax codes control the syntax check done during the first time parsing of
84  the expression string. They are flags that indicate which tokens are allowed next
85  if certain tokens are identified.
86  */
87  enum ESynCodes
88  {
89  noBO = 1 << 0, ///< to avoid i.e. "cos(7)("
90  noBC = 1 << 1, ///< to avoid i.e. "sin)" or "()"
91  noVAL = 1 << 2, ///< to avoid i.e. "tan 2" or "sin(8)3.14"
92  noVAR = 1 << 3, ///< to avoid i.e. "sin a" or "sin(8)a"
93  noARG_SEP = 1 << 4, ///< to avoid i.e. ",," or "+," ...
94  noFUN = 1 << 5, ///< to avoid i.e. "sqrt cos" or "(1)sin"
95  noOPT = 1 << 6, ///< to avoid i.e. "(+)"
96  noPOSTOP = 1 << 7, ///< to avoid i.e. "(5!!)" "sin!"
97  noINFIXOP = 1 << 8, ///< to avoid i.e. "++4" "!!4"
98  noEND = 1 << 9, ///< to avoid unexpected end of formula
99  noSTR = 1 << 10, ///< to block numeric arguments on string functions
100  noASSIGN = 1 << 11, ///< to block assignment to constant i.e. "4=7"
101  noIF = 1 << 12,
102  noELSE = 1 << 13,
103  sfSTART_OF_LINE = noOPT | noBC | noPOSTOP | noASSIGN | noIF | noELSE | noARG_SEP,
104  noANY = ~0 ///< All of he above flags set
105  };
106 
107  ParserTokenReader(const ParserTokenReader& a_Reader);
108  ParserTokenReader& operator=(const ParserTokenReader& a_Reader);
109  void Assign(const ParserTokenReader& a_Reader);
110 
111  void SetParent(ParserBase* a_pParent);
112  int ExtractToken(const char_type* a_szCharSet, string_type& a_strTok, std::size_t a_iPos) const;
113  int ExtractOperatorToken(string_type& a_sTok, std::size_t a_iPos) const;
114 
115  bool IsBuiltIn(token_type& a_Tok);
116  bool IsArgSep(token_type& a_Tok);
117  bool IsEOF(token_type& a_Tok);
118  bool IsInfixOpTok(token_type& a_Tok);
119  bool IsFunTok(token_type& a_Tok);
120  bool IsPostOpTok(token_type& a_Tok);
121  bool IsOprt(token_type& a_Tok);
122  bool IsValTok(token_type& a_Tok);
123  bool IsVarTok(token_type& a_Tok);
124  bool IsStrVarTok(token_type& a_Tok);
125  bool IsUndefVarTok(token_type& a_Tok);
126  bool IsString(token_type& a_Tok);
127  void Error(EErrorCodes a_iErrc, int a_iPos = -1, const string_type& a_sTok = string_type()) const;
128 
129  token_type& SaveBeforeReturn(const token_type& tok);
130 
131  ParserBase* m_pParser;
132  string_type m_strFormula;
133  int m_iPos;
134  int m_iSynFlags;
135  bool m_bIgnoreUndefVar;
136 
137  const funmap_type* m_pFunDef;
138  const funmap_type* m_pPostOprtDef;
139  const funmap_type* m_pInfixOprtDef;
140  const funmap_type* m_pOprtDef;
141  const valmap_type* m_pConstDef;
142  const strmap_type* m_pStrVarDef;
143 
144  varmap_type* m_pVarDef; ///< The only non const pointer to parser internals
145  facfun_type m_pFactory;
146  void* m_pFactoryData;
147  std::list<identfun_type> m_vIdentFun; ///< Value token identification function
148  varmap_type m_UsedVar;
149  value_type m_fZero; ///< Dummy value of zero, referenced by undefined variables
150 
151  std::stack<int> m_bracketStack;
152 
153  token_type m_lastTok;
154  char_type m_cArgSep; ///< The character used for separating function arguments
155  };
156 } // namespace mu
157 
158 #endif
159 
160 
void IgnoreUndefVar(bool bIgnore)
Set Flag that controls behaviour in case of undefined variables being found.
value_type *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:505
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:320
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:314
varmap_type & GetUsedVar()
Return a map containing the used variables only.
int GetPos() const
Return the current position of the token reader in the formula string.
ParserTokenReader(ParserBase *a_pParent)
Constructor.
std::map< string_type, ParserCallback > funmap_type
Container for Callback objects.
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:317
token_type ReadNextToken()
Read the next token from the string.
void ReInit()
Reset the token reader to the start of the formula.
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294
void SetFormula(const string_type &a_strFormula)
Initialize the token Reader.
Namespace for mathematical applications.
Definition: muParser.cpp:46
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:502
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
Token reader for the ParserBase class.
ParserTokenReader * Clone(ParserBase *a_pParent) const
Create instance of a ParserTokenReader identical with this and return its pointer.
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:300
This file contains the parser token definition.
EErrorCodes
Error codes.
Definition: muParserDef.h:226
const string_type & GetExpr() const
Return a reference to the formula.
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:68
This file contains standard definitions used by the parser.