NCBI C++ ToolKit
muParserTokenReader.h
Go to the documentation of this file.

Go to the SVN repository for 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 
40 /** \file
41  \brief This file contains the parser token reader definition.
42 */
43 
44 
45 namespace mu
46 {
47  // Forward declaration
48  class ParserBase;
49 
50  /** \brief Token reader for the ParserBase class.
51 
52  */
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);
65  void SetVarCreator(facfun_type a_pFactory, void *pUserData);
66  void SetFormula(const string_type &a_strFormula);
67  void SetArgSep(char_type cArgSep);
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();
77 
78  private:
79 
80  /** \brief Syntax codes.
81 
82  The syntax codes control the syntax check done during the first time parsing of
83  the expression string. They are flags that indicate which tokens are allowed next
84  if certain tokens are identified.
85  */
86  enum ESynCodes
87  {
88  noBO = 1 << 0, ///< to avoid i.e. "cos(7)("
89  noBC = 1 << 1, ///< to avoid i.e. "sin)" or "()"
90  noVAL = 1 << 2, ///< to avoid i.e. "tan 2" or "sin(8)3.14"
91  noVAR = 1 << 3, ///< to avoid i.e. "sin a" or "sin(8)a"
92  noARG_SEP = 1 << 4, ///< to avoid i.e. ",," or "+," ...
93  noFUN = 1 << 5, ///< to avoid i.e. "sqrt cos" or "(1)sin"
94  noOPT = 1 << 6, ///< to avoid i.e. "(+)"
95  noPOSTOP = 1 << 7, ///< to avoid i.e. "(5!!)" "sin!"
96  noINFIXOP = 1 << 8, ///< to avoid i.e. "++4" "!!4"
97  noEND = 1 << 9, ///< to avoid unexpected end of formula
98  noSTR = 1 << 10, ///< to block numeric arguments on string functions
99  noASSIGN = 1 << 11, ///< to block assignement to constant i.e. "4=7"
100  noANY = ~0 ///< All of he above flags set
101  };
102 
103  ParserTokenReader(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 
128 
131  int m_iPos;
134 
141  varmap_type *m_pVarDef; ///< The only non const pointer to parser internals
144  std::vector<identfun_type> m_vIdentFun; ///< Value token identification function
146  value_type m_fZero; ///< Dummy value of zero, referenced by undefined variables
149  char_type m_cArgSep; ///< The character used for separating function arguments
150  };
151 } // namespace mu
152 
153 #endif
154 
155 
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:64
Token reader for the ParserBase class.
bool IsArgSep(token_type &a_Tok)
bool IsPostOpTok(token_type &a_Tok)
Check if a string position contains a unary post value operator.
const funmap_type * m_pPostOprtDef
const funmap_type * m_pInfixOprtDef
ParserTokenReader(ParserBase *a_pParent)
Constructor.
bool IsFunTok(token_type &a_Tok)
Check whether the token at a given position is a function token.
bool IsValTok(token_type &a_Tok)
Check whether the token at a given position is a value token.
std::vector< identfun_type > m_vIdentFun
Value token identification function.
token_type ReadNextToken()
Read the next token from the string.
const varmap_type & GetUsedVar() const
Return a map containing the used variables only.
@ noASSIGN
to block assignement to constant i.e. "4=7"
@ noEND
to avoid unexpected end of formula
@ noSTR
to block numeric arguments on string functions
@ noINFIXOP
to avoid i.e. "++4" "!!4"
@ noVAR
to avoid i.e. "sin a" or "sin(8)a"
@ noBO
to avoid i.e. "cos(7)("
@ noOPT
to avoid i.e. "(+)"
@ noANY
All of he above flags set.
@ noVAL
to avoid i.e. "tan 2" or "sin(8)3.14"
@ noPOSTOP
to avoid i.e. "(5!!)" "sin!"
@ noARG_SEP
to avoid i.e. ",," or "+," ...
@ noBC
to avoid i.e. "sin)" or "()"
@ noFUN
to avoid i.e. "sqrt cos" or "(1)sin"
void SetParent(ParserBase *a_pParent)
char_type m_cArgSep
The character used for separating function arguments.
void Assign(const ParserTokenReader &a_Reader)
Assign state of a token reader to this token reader.
bool IsOprt(token_type &a_Tok)
Check if a string position contains a binary operator.
int ExtractToken(const char_type *a_szCharSet, string_type &a_strTok, int a_iPos) const
Extract all characters that belong to a certain charset.
value_type m_fZero
Dummy value of zero, referenced by undefined variables.
void SetArgSep(char_type cArgSep)
const funmap_type * m_pFunDef
ParserTokenReader & operator=(const ParserTokenReader &a_Reader)
Assignement operator.
void Error(EErrorCodes a_iErrc, int a_iPos=-1, const string_type &a_sTok=string_type()) const
Create an error containing the parse error position.
void SetVarCreator(facfun_type a_pFactory, void *pUserData)
void IgnoreUndefVar(bool bIgnore)
Set Flag that contronls behaviour in case of undefined variables beeing found.
const funmap_type * m_pOprtDef
ParserTokenReader * Clone(ParserBase *a_pParent) const
Create instance of a ParserTokenReader identical with this and return its pointer.
bool IsVarTok(token_type &a_Tok)
Check wheter a token at a given position is a variable token.
void SetFormula(const string_type &a_strFormula)
Initialize the token Reader.
bool IsString(token_type &a_Tok)
Check wheter a token at a given position is a string.
int GetPos() const
Return the current position of the token reader in the formula string.
bool IsInfixOpTok(token_type &a_Tok)
Check if a string position contains a unary infix operator.
ParserToken< value_type, string_type > token_type
varmap_type * m_pVarDef
The only non const pointer to parser internals.
const strmap_type * m_pStrVarDef
bool IsEOF(token_type &a_Tok)
Check for End of Formula.
bool IsBuiltIn(token_type &a_Tok)
Check if a built in operator or other token can be found.
void ReInit()
Reset the token reader to the start of the formula.
token_type & SaveBeforeReturn(const token_type &tok)
void AddValIdent(identfun_type a_pCallback)
const string_type & GetFormula() const
Return a reference to the formula.
bool IsStrVarTok(token_type &a_Tok)
bool IsUndefVarTok(token_type &a_Tok)
Check wheter a token at a given position is an undefined variable.
const valmap_type * m_pConstDef
This file contains standard definitions used by the parser.
This file contains the parser token definition.
Namespace for mathematical applications.
Definition: muParser.h:41
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:297
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:259
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:246
value_type *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:300
std::map< string_type, ParserCallback > funmap_type
Container for Callback objects.
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:256
EErrorCodes
Error codes.
Definition: muParserError.h:47
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:234
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:262
Modified on Wed Apr 24 14:14:17 2024 by modify_doxy.py rev. 669887