3 _____ __ _____________ _______ ______ ___________
4 / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5 | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6 |__|_| /____/| __(____ /__| /____ >\___ >__|
8 Copyright (C) 2004 - 2020 Ingo Berg
10 Redistribution and use in source and binary forms, with or without modification, are permitted
11 provided that the following conditions are met:
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.
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.
29 #ifndef MU_PARSER_TOKEN_READER_H
30 #define MU_PARSER_TOKEN_READER_H
40 #include "muParserDef.h"
41 #include "muParserToken.h"
44 \brief This file contains the parser token reader definition.
50 // Forward declaration
53 /** \brief Token reader for the ParserBase class. */
54 class ParserTokenReader final
58 typedef ParserToken<value_type, string_type> token_type;
62 ParserTokenReader(ParserBase* a_pParent);
63 ParserTokenReader* Clone(ParserBase* a_pParent) const;
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);
71 const string_type& GetExpr() const;
72 varmap_type& GetUsedVar();
73 char_type GetArgSep() const;
75 void IgnoreUndefVar(bool bIgnore);
77 token_type ReadNextToken();
81 /** \brief Syntax codes.
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.
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 assignment to constant i.e. "4=7"
103 sfSTART_OF_LINE = noOPT | noBC | noPOSTOP | noASSIGN | noIF | noELSE | noARG_SEP,
104 noANY = ~0 ///< All of he above flags set
107 ParserTokenReader(const ParserTokenReader& a_Reader);
108 ParserTokenReader& operator=(const ParserTokenReader& a_Reader);
109 void Assign(const ParserTokenReader& a_Reader);
111 void SetParent(ParserBase* a_pParent);
112 int ExtractToken(const char_type* a_szCharSet, string_type& a_strTok, std::size_t a_iPos) const;
113 int ExtractOperatorToken(string_type& a_sTok, std::size_t a_iPos) const;
115 bool IsBuiltIn(token_type& a_Tok);
116 bool IsArgSep(token_type& a_Tok);
117 bool IsEOF(token_type& a_Tok);
118 bool IsInfixOpTok(token_type& a_Tok);
119 bool IsFunTok(token_type& a_Tok);
120 bool IsPostOpTok(token_type& a_Tok);
121 bool IsOprt(token_type& a_Tok);
122 bool IsValTok(token_type& a_Tok);
123 bool IsVarTok(token_type& a_Tok);
124 bool IsStrVarTok(token_type& a_Tok);
125 bool IsUndefVarTok(token_type& a_Tok);
126 bool IsString(token_type& a_Tok);
127 void Error(EErrorCodes a_iErrc, int a_iPos = -1, const string_type& a_sTok = string_type()) const;
129 token_type& SaveBeforeReturn(const token_type& tok);
131 ParserBase* m_pParser;
132 string_type m_strFormula;
135 bool m_bIgnoreUndefVar;
137 const funmap_type* m_pFunDef;
138 const funmap_type* m_pPostOprtDef;
139 const funmap_type* m_pInfixOprtDef;
140 const funmap_type* m_pOprtDef;
141 const valmap_type* m_pConstDef;
142 const strmap_type* m_pStrVarDef;
144 varmap_type* m_pVarDef; ///< The only non const pointer to parser internals
145 facfun_type m_pFactory;
146 void* m_pFactoryData;
147 std::list<identfun_type> m_vIdentFun; ///< Value token identification function
148 varmap_type m_UsedVar;
149 value_type m_fZero; ///< Dummy value of zero, referenced by undefined variables
151 std::stack<int> m_bracketStack;
153 token_type m_lastTok;
154 char_type m_cArgSep; ///< The character used for separating function arguments