Coverage Report

Created: 2024-07-27 06:02

/src/muparser/include/muParserTokenReader.h
Line
Count
Source (jump to first uncovered line)
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 API_EXPORT_CXX ParserTokenReader final
55
  {
56
  private:
57
58
    typedef ParserToken<value_type, string_type> token_type;
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
    /** \brief Check whether a variable factory is installed. 
71
     
72
      Variable factories automatically create new variables when a unknown variable is found in an expression.
73
    */
74
    bool HasVarCreator() const
75
0
    {
76
0
      return m_pFactory != nullptr;
77
0
    }
78
79
    int GetPos() const;
80
    const string_type& GetExpr() const;
81
    varmap_type& GetUsedVar();
82
    char_type GetArgSep() const;
83
84
    void IgnoreUndefVar(bool bIgnore);
85
    void ReInit();
86
    token_type ReadNextToken();
87
88
  private:
89
90
    /** \brief Syntax codes.
91
92
      The syntax codes control the syntax check done during the first time parsing of
93
      the expression string. They are flags that indicate which tokens are allowed next
94
      if certain tokens are identified.
95
    */
96
    enum ESynCodes
97
    {
98
      noBO = 1 << 0,      ///< to avoid i.e. "cos(7)(" 
99
      noBC = 1 << 1,      ///< to avoid i.e. "sin)" or "()"
100
      noVAL = 1 << 2,     ///< to avoid i.e. "tan 2" or "sin(8)3.14"
101
      noVAR = 1 << 3,     ///< to avoid i.e. "sin a" or "sin(8)a"
102
      noARG_SEP = 1 << 4,   ///< to avoid i.e. ",," or "+," ...
103
      noFUN = 1 << 5,     ///< to avoid i.e. "sqrt cos" or "(1)sin" 
104
      noOPT = 1 << 6,     ///< to avoid i.e. "(+)"
105
      noPOSTOP = 1 << 7,    ///< to avoid i.e. "(5!!)" "sin!"
106
      noINFIXOP = 1 << 8,   ///< to avoid i.e. "++4" "!!4"
107
      noEND = 1 << 9,     ///< to avoid unexpected end of formula
108
      noSTR = 1 << 10,    ///< to block numeric arguments on string functions
109
      noASSIGN = 1 << 11,   ///< to block assignment to constant i.e. "4=7"
110
      noIF = 1 << 12,
111
      noELSE = 1 << 13,
112
      sfSTART_OF_LINE = noOPT | noBC | noPOSTOP | noASSIGN | noIF | noELSE | noARG_SEP,
113
      noANY = ~0        ///< All of he above flags set
114
    };
115
116
    ParserTokenReader(const ParserTokenReader& a_Reader);
117
    ParserTokenReader& operator=(const ParserTokenReader& a_Reader);
118
    void Assign(const ParserTokenReader& a_Reader);
119
120
    void SetParent(ParserBase* a_pParent);
121
    int ExtractToken(const char_type* a_szCharSet, string_type& a_strTok, std::size_t a_iPos) const;
122
    int ExtractOperatorToken(string_type& a_sTok, std::size_t a_iPos) const;
123
124
    bool IsBuiltIn(token_type& a_Tok);
125
    bool IsArgSep(token_type& a_Tok);
126
    bool IsEOF(token_type& a_Tok);
127
    bool IsInfixOpTok(token_type& a_Tok);
128
    bool IsFunTok(token_type& a_Tok);
129
    bool IsPostOpTok(token_type& a_Tok);
130
    bool IsOprt(token_type& a_Tok);
131
    bool IsValTok(token_type& a_Tok);
132
    bool IsVarTok(token_type& a_Tok);
133
    bool IsStrVarTok(token_type& a_Tok);
134
    bool IsUndefVarTok(token_type& a_Tok);
135
    bool IsString(token_type& a_Tok);
136
    void Error(EErrorCodes a_iErrc, int a_iPos = -1, const string_type& a_sTok = string_type()) const;
137
138
    token_type& SaveBeforeReturn(const token_type& tok);
139
140
    ParserBase* m_pParser;
141
    string_type m_strFormula;
142
    int  m_iPos;
143
    int  m_iSynFlags;
144
    bool m_bIgnoreUndefVar;
145
146
    const funmap_type* m_pFunDef;
147
    const funmap_type* m_pPostOprtDef;
148
    const funmap_type* m_pInfixOprtDef;
149
    const funmap_type* m_pOprtDef;
150
    const valmap_type* m_pConstDef;
151
    const strmap_type* m_pStrVarDef;
152
153
    varmap_type* m_pVarDef;  ///< The only non const pointer to parser internals
154
    facfun_type m_pFactory;
155
    void* m_pFactoryData;
156
    std::list<identfun_type> m_vIdentFun; ///< Value token identification function
157
    varmap_type m_UsedVar;
158
    value_type m_fZero;      ///< Dummy value of zero, referenced by undefined variables
159
    
160
    std::stack<int> m_bracketStack;
161
162
    token_type m_lastTok;
163
    char_type m_cArgSep;     ///< The character used for separating function arguments
164
  };
165
} // namespace mu
166
167
#endif
168
169