1 /** \file
2     \brief Definition of the token reader used to break the expression string up
3            into tokens.
4 
5 <pre>
6                __________                                 ____  ___
7     _____  __ _\______   \_____ _______  ______ __________\   \/  /
8    /     \|  |  \     ___/\__  \\_  __ \/  ___// __ \_  __ \     /
9   |  Y Y  \  |  /    |     / __ \|  | \/\___ \\  ___/|  | \/     \
10   |__|_|  /____/|____|    (____  /__|  /____  >\___  >__| /___/\  \
11         \/                     \/           \/     \/           \_/
12   Copyright (C) 2021 Ingo Berg, et al.
13   All rights reserved.
14 
15   Redistribution and use in source and binary forms, with or without
16   modification, are permitted provided that the following conditions are met:
17 
18    * Redistributions of source code must retain the above copyright notice,
19      this list of conditions and the following disclaimer.
20    * Redistributions in binary form must reproduce the above copyright notice,
21      this list of conditions and the following disclaimer in the documentation
22      and/or other materials provided with the distribution.
23 
24   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27   IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33   POSSIBILITY OF SUCH DAMAGE.
34   </pre>
35 */
36 
37 #ifndef MUP_TOKEN_READER_H
38 #define MUP_TOKEN_READER_H
39 
40 //--- Standard includes ----------------------------------------------------
41 #include <cstdio>
42 #include <cstring>
43 #include <map>
44 #include <stack>
45 #include <string>
46 #include <list>
47 
48 //--- muParserX framework --------------------------------------------------
49 #include "mpIToken.h"
50 #include "mpError.h"
51 #include "mpStack.h"
52 #include "mpFwdDecl.h"
53 
54 MUP_NAMESPACE_START
55 
56   /** \brief Token reader for the ParserXBase class. */
57   class TokenReader
58   {
59   friend class ParserXBase;
60 
61   public:
62 
63     typedef std::vector<ptr_tok_type> token_buf_type;
64 
65   private:
66 
67     TokenReader(const TokenReader &a_Reader);
68     TokenReader& operator=(const TokenReader &a_Reader);
69     void Assign(const TokenReader &a_Reader);
70     void DeleteValReader();
71     void SetParent(ParserXBase *a_pParent);
72 
73     int ExtractToken(const char_type *a_szCharSet, string_type &a_sTok, int a_iPos) const;
74 
75     void SkipCommentsAndWhitespaces();
76     bool IsBuiltIn(ptr_tok_type &t);
77     bool IsEOF(ptr_tok_type &t);
78     bool IsNewline(ptr_tok_type &a_Tok);
79     bool IsNewLine(ptr_tok_type &t);
80     bool IsInfixOpTok(ptr_tok_type &t);
81     bool IsFunTok(ptr_tok_type &t);
82     bool IsPostOpTok(ptr_tok_type &t);
83     bool IsOprt(ptr_tok_type &t);
84     bool IsScOprt(ptr_tok_type &a_Tok);
85     bool IsValTok(ptr_tok_type &t);
86     bool IsVarOrConstTok(ptr_tok_type &t);
87     bool IsUndefVarTok(ptr_tok_type &t);
88     bool IsComment();
89 
90     const ptr_tok_type& Store(const ptr_tok_type &t, int pos);
91 
92     ParserXBase *m_pParser;  ///< Pointer to the parser bound to this token reader
93     string_type m_sExpr;     ///< The expression beeing currently parsed
94     int  m_nPos;             ///< Current parsing position in the expression
95     int  m_nNumBra;          ///< Number of open parenthesis
96     int  m_nNumIndex;        ///< Number of open index paranethesis
97 	int  m_nNumCurly;        ///< Number of open curly brackets
98     int  m_nNumIfElse;       ///< Coubter for if-then-else levels
99     int  m_nSynFlags;        ///< Flags to controll the syntax flow
100 
101     token_buf_type m_vTokens;
102     ECmdCode m_eLastTokCode;
103 
104     mutable fun_maptype  *m_pFunDef;
105     mutable oprt_bin_maptype *m_pOprtDef;
106     mutable oprt_bin_shortcut_maptype *m_pOprtShortcutDef;
107     mutable oprt_ifx_maptype *m_pInfixOprtDef;
108     mutable oprt_pfx_maptype *m_pPostOprtDef;
109     mutable val_maptype  *m_pConstDef;
110     val_vec_type *m_pDynVarShadowValues; ///< Value items created for holding values of variables created at parser runtime
111     var_maptype  *m_pVarDef;             ///< The only non const pointer to parser internals
112 
113     readervec_type m_vValueReader;  ///< Value token identification function
114     var_maptype m_UsedVar;
115     float_type m_fZero;             ///< Dummy value of zero, referenced by undefined variables
116 
117   public:
118 
119     TokenReader(ParserXBase *a_pParent);
120    ~TokenReader();
121     TokenReader* Clone(ParserXBase *a_pParent) const;
122 
123     void AddValueReader(IValueReader *a_pReader);
124     void AddSynFlags(int flag);
125     int GetPos() const;
126     const string_type& GetExpr() const;
127     const var_maptype& GetUsedVar() const;
128     const token_buf_type& GetTokens() const;
129     void SetExpr(const string_type &a_sExpr);
130 
131     void ReInit();
132     ptr_tok_type ReadNextToken();
133   }; // class TokenReader
134 
135 MUP_NAMESPACE_END
136 
137 #endif
138 
139 
140