muParser API -  1.35
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
muParserTokenReader.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2013 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 
26 #ifndef MU_PARSER_TOKEN_READER_H
27 #define MU_PARSER_TOKEN_READER_H
28 
29 #include <cassert>
30 #include <cstdio>
31 #include <cstring>
32 #include <list>
33 #include <map>
34 #include <memory>
35 #include <stack>
36 #include <string>
37 
38 #include "muParserDef.h"
39 #include "muParserToken.h"
40 
41 /** \file
42  \brief This file contains the parser token reader definition.
43 */
44 
45 
46 namespace mu
47 {
48  // Forward declaration
49  class ParserBase;
50 
51  /** \brief Token reader for the ParserBase class.
52 
53  */
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 assignement 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,
113  string_type &a_strTok,
114  int a_iPos) const;
115  int ExtractOperatorToken(string_type &a_sTok, int a_iPos) const;
116 
117  bool IsBuiltIn(token_type &a_Tok);
118  bool IsArgSep(token_type &a_Tok);
119  bool IsEOF(token_type &a_Tok);
120  bool IsInfixOpTok(token_type &a_Tok);
121  bool IsFunTok(token_type &a_Tok);
122  bool IsPostOpTok(token_type &a_Tok);
123  bool IsOprt(token_type &a_Tok);
124  bool IsValTok(token_type &a_Tok);
125  bool IsVarTok(token_type &a_Tok);
126  bool IsStrVarTok(token_type &a_Tok);
127  bool IsUndefVarTok(token_type &a_Tok);
128  bool IsString(token_type &a_Tok);
129  void Error(EErrorCodes a_iErrc,
130  int a_iPos = -1,
131  const string_type &a_sTok = string_type() ) const;
132 
133  token_type& SaveBeforeReturn(const token_type &tok);
134 
135  ParserBase *m_pParser;
136  string_type m_strFormula;
137  int m_iPos;
138  int m_iSynFlags;
139  bool m_bIgnoreUndefVar;
140 
141  const funmap_type *m_pFunDef;
142  const funmap_type *m_pPostOprtDef;
143  const funmap_type *m_pInfixOprtDef;
144  const funmap_type *m_pOprtDef;
145  const valmap_type *m_pConstDef;
146  const strmap_type *m_pStrVarDef;
147  varmap_type *m_pVarDef; ///< The only non const pointer to parser internals
148  facfun_type m_pFactory;
149  void *m_pFactoryData;
150  std::list<identfun_type> m_vIdentFun; ///< Value token identification function
151  varmap_type m_UsedVar;
152  value_type m_fZero; ///< Dummy value of zero, referenced by undefined variables
153  int m_iBrackets;
154  token_type m_lastTok;
155  char_type m_cArgSep; ///< The character used for separating function arguments
156  };
157 } // namespace mu
158 
159 #endif
160 
161 
void IgnoreUndefVar(bool bIgnore)
Set Flag that controls behaviour in case of undefined variables being found.
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
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:272
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:247
void SetFormula(const string_type &a_strFormula)
Initialize the token Reader.
Namespace for mathematical applications.
Definition: muParser.cpp:49
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:259
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:253
This file contains the parser token definition.
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 *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:364
EErrorCodes
Error codes.
Definition: muParserError.h:46
const string_type & GetExpr() const
Return a reference to the formula.
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:62
This file contains standard definitions used by the parser.