MADNESS  version 0.9
muParserTokenReader.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2008 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 <map>
33 #include <memory>
34 #include <stack>
35 #include <string>
36 
37 #include "muParserDef.h"
38 #include "muParserToken.h"
39 
45 namespace mu
46 {
47  // Forward declaration
48  class ParserBase;
49 
54  {
55  private:
56 
58 
59  public:
60 
61  ParserTokenReader(ParserBase *a_pParent);
62  ParserTokenReader* Clone(ParserBase *a_pParent) const;
63 
64  void AddValIdent(identfun_type a_pCallback);
66  void SetFormula(const string_type &a_strFormula);
68 
69  int GetPos() const;
70  const string_type& GetFormula() const;
71  const varmap_type& GetUsedVar() const;
72  char_type GetArgSep() const;
73 
74  void IgnoreUndefVar(bool bIgnore);
75  void ReInit();
76  token_type ReadNextToken();
77 
78  private:
79 
86  enum ESynCodes
87  {
88  noBO = 1 << 0,
89  noBC = 1 << 1,
90  noVAL = 1 << 2,
91  noVAR = 1 << 3,
92  noARG_SEP = 1 << 4,
93  noFUN = 1 << 5,
94  noOPT = 1 << 6,
95  noPOSTOP = 1 << 7,
96  noINFIXOP = 1 << 8,
97  noEND = 1 << 9,
98  noSTR = 1 << 10,
99  noASSIGN = 1 << 11,
100  noANY = ~0
101  };
102 
103  ParserTokenReader(const ParserTokenReader &a_Reader);
104  ParserTokenReader& operator=(const ParserTokenReader &a_Reader);
105  void Assign(const ParserTokenReader &a_Reader);
106 
107  void SetParent(ParserBase *a_pParent);
108  int ExtractToken(const char_type *a_szCharSet,
109  string_type &a_strTok,
110  int a_iPos) const;
111  bool IsBuiltIn(token_type &a_Tok);
112  bool IsArgSep(token_type &a_Tok);
113  bool IsEOF(token_type &a_Tok);
114  bool IsInfixOpTok(token_type &a_Tok);
115  bool IsFunTok(token_type &a_Tok);
116  bool IsPostOpTok(token_type &a_Tok);
117  bool IsOprt(token_type &a_Tok);
118  bool IsValTok(token_type &a_Tok);
119  bool IsVarTok(token_type &a_Tok);
120  bool IsStrVarTok(token_type &a_Tok);
121  bool IsUndefVarTok(token_type &a_Tok);
122  bool IsString(token_type &a_Tok);
123  void Error(EErrorCodes a_iErrc,
124  int a_iPos = -1,
125  const string_type &a_sTok = string_type() ) const;
126 
127  token_type& SaveBeforeReturn(const token_type &tok);
128 
129  ParserBase *m_pParser;
130  string_type m_strFormula;
131  int m_iPos;
132  int m_iSynFlags;
133  bool m_bIgnoreUndefVar;
134 
135  const funmap_type *m_pFunDef;
136  const funmap_type *m_pPostOprtDef;
137  const funmap_type *m_pInfixOprtDef;
138  const funmap_type *m_pOprtDef;
139  const valmap_type *m_pConstDef;
140  const strmap_type *m_pStrVarDef;
141  varmap_type *m_pVarDef;
142  facfun_type m_pFactory;
143  void *m_pFactoryData;
144  std::vector<identfun_type> m_vIdentFun;
145  varmap_type m_UsedVar;
146  value_type m_fZero;
147  int m_iBrackets;
148  token_type m_lastTok;
149  char_type m_cArgSep;
150  };
151 } // namespace mu
152 
153 #endif
154 
155 
const muChar_t cArgSep
Definition: muParserDLL.h:148
void IgnoreUndefVar(bool bIgnore)
Set Flag that contronls behaviour in case of undefined variables beeing found.
Definition: muParserTokenReader.cpp:223
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:256
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:250
void SetVarCreator(facfun_type a_pFactory, void *pUserData)
Definition: muParserTokenReader.cpp:167
int GetPos() const
Return the current position of the token reader in the formula string.
Definition: muParserTokenReader.cpp:179
muFacFun_t a_pFactory
Definition: muParserDLL.h:79
ParserTokenReader(ParserBase *a_pParent)
Constructor.
Definition: muParserTokenReader.cpp:111
std::map< string_type, ParserCallback > funmap_type
Container for Callback objects.
Definition: muParserCallback.h:93
const varmap_type & GetUsedVar() const
Return a map containing the used variables only.
Definition: muParserTokenReader.cpp:197
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:253
token_type ReadNextToken()
Read the next token from the string.
Definition: muParserTokenReader.cpp:248
void ReInit()
Reset the token reader to the start of the formula.
Definition: muParserTokenReader.cpp:237
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:222
const string_type & GetFormula() const
Return a reference to the formula.
Definition: muParserTokenReader.cpp:190
void SetFormula(const string_type &a_strFormula)
Initialize the token Reader.
Definition: muParserTokenReader.cpp:208
void SetArgSep(char_type cArgSep)
Definition: muParserTokenReader.cpp:834
Namespace for mathematical applications.
Definition: muParser.cpp:47
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:240
Token reader for the ParserBase class.
Definition: muParserTokenReader.h:53
ParserTokenReader * Clone(ParserBase *a_pParent) const
Create instance of a ParserTokenReader identical with this and return its pointer.
Definition: muParserTokenReader.cpp:146
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:228
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:291
void AddValIdent(identfun_type a_pCallback)
Definition: muParserTokenReader.cpp:161
value_type *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:294
muFacFun_t void * pUserData
Definition: muParserDLL.h:79
EErrorCodes
Error codes.
Definition: muParserError.h:46
char_type GetArgSep() const
Definition: muParserTokenReader.cpp:840
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:63
This file contains standard definitions used by the parser.