NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
muParserToken.h
Go to the documentation of this file.
1/*
2 __________
3 _____ __ __\______ \_____ _______ ______ ____ _______
4 / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5 | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6 |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7 \/ \/ \/ \/
8 Copyright (C) 2004-2012 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_H
27#define MU_PARSER_TOKEN_H
28
29#include <cassert>
30#include <string>
31#include <stack>
32#include <vector>
33#include <memory>
34
35#include "muParserError.h"
36#include "muParserCallback.h"
37
42namespace mu
43{
60 template<typename TBase, typename TString>
62 {
63 private:
64
67 void* m_pTok;
68 int m_iIdx;
69 TString m_strTok;
70 TString m_strVal;
72 std::unique_ptr<ParserCallback> m_pCallback;
73
74 public:
75
76 //---------------------------------------------------------------------------
86 , m_pTok(0)
87 , m_iIdx(-1)
88 , m_strTok()
89 , m_pCallback()
90 {}
91
92 //------------------------------------------------------------------------------
101 {
102 Assign(a_Tok);
103 }
104
105 //------------------------------------------------------------------------------
113 {
114 Assign(a_Tok);
115 return *this;
116 }
117
118 //------------------------------------------------------------------------------
123 void Assign(const ParserToken& a_Tok)
124 {
125 m_iCode = a_Tok.m_iCode;
126 m_pTok = a_Tok.m_pTok;
127 m_strTok = a_Tok.m_strTok;
128 m_iIdx = a_Tok.m_iIdx;
129 m_strVal = a_Tok.m_strVal;
130 m_iType = a_Tok.m_iType;
131 m_fVal = a_Tok.m_fVal;
132 // create new callback object if a_Tok has one
133 m_pCallback.reset(a_Tok.m_pCallback.get() ? a_Tok.m_pCallback->Clone() : 0);
134 }
135
136 //------------------------------------------------------------------------------
147 ParserToken& Set(ECmdCode a_iType, const TString& a_strTok = TString())
148 {
149 // The following types cant be set this way, they have special Set functions
150 assert(a_iType != cmVAR);
151 assert(a_iType != cmVAL);
152 assert(a_iType != cmFUNC);
153
154 m_iCode = a_iType;
155 m_iType = tpVOID;
156 m_pTok = 0;
157 m_strTok = a_strTok;
158 m_iIdx = -1;
159
160 return *this;
161 }
162
163 //------------------------------------------------------------------------------
165 ParserToken& Set(const ParserCallback& a_pCallback, const TString& a_sTok)
166 {
167 assert(a_pCallback.GetAddr());
168
169 m_iCode = a_pCallback.GetCode();
170 m_iType = tpVOID;
171 m_strTok = a_sTok;
172 m_pCallback.reset(new ParserCallback(a_pCallback));
173
174 m_pTok = 0;
175 m_iIdx = -1;
176
177 return *this;
178 }
179
180 //------------------------------------------------------------------------------
186 ParserToken& SetVal(TBase a_fVal, const TString& a_strTok = TString())
187 {
188 m_iCode = cmVAL;
189 m_iType = tpDBL;
190 m_fVal = a_fVal;
191 m_strTok = a_strTok;
192 m_iIdx = -1;
193
194 m_pTok = 0;
195 m_pCallback.reset(0);
196
197 return *this;
198 }
199
200 //------------------------------------------------------------------------------
206 ParserToken& SetVar(TBase* a_pVar, const TString& a_strTok)
207 {
208 m_iCode = cmVAR;
209 m_iType = tpDBL;
210 m_strTok = a_strTok;
211 m_iIdx = -1;
212 m_pTok = (void*)a_pVar;
213 m_pCallback.reset(0);
214 return *this;
215 }
216
217 //------------------------------------------------------------------------------
223 ParserToken& SetString(const TString& a_strTok, std::size_t a_iSize)
224 {
226 m_iType = tpSTR;
227 m_strTok = a_strTok;
228 m_iIdx = static_cast<int>(a_iSize);
229
230 m_pTok = 0;
231 m_pCallback.reset(0);
232 return *this;
233 }
234
235 //------------------------------------------------------------------------------
242 void SetIdx(int a_iIdx)
243 {
244 if (m_iCode != cmSTRING || a_iIdx < 0)
246
247 m_iIdx = a_iIdx;
248 }
249
250 //------------------------------------------------------------------------------
258 int GetIdx() const
259 {
260 if (m_iIdx < 0 || m_iCode != cmSTRING )
262
263 return m_iIdx;
264 }
265
266 //------------------------------------------------------------------------------
273 {
274 if (m_pCallback.get())
275 {
276 return m_pCallback->GetCode();
277 }
278 else
279 {
280 return m_iCode;
281 }
282 }
283
284 //------------------------------------------------------------------------------
286 {
287 if (m_pCallback.get())
288 {
289 return m_pCallback->GetType();
290 }
291 else
292 {
293 return m_iType;
294 }
295 }
296
297 //------------------------------------------------------------------------------
298 int GetPri() const
299 {
300 if ( !m_pCallback.get())
302
303 if ( m_pCallback->GetCode() != cmOPRT_BIN && m_pCallback->GetCode() != cmOPRT_INFIX)
305
306 return m_pCallback->GetPri();
307 }
308
309 //------------------------------------------------------------------------------
311 {
312 if (m_pCallback.get() == nullptr || m_pCallback->GetCode() != cmOPRT_BIN)
314
315 return m_pCallback->GetAssociativity();
316 }
317
318
319 bool IsOptimizable() const
320 {
321 if (!m_pCallback)
323
324 return m_pCallback->IsOptimizable();
325 }
326 //------------------------------------------------------------------------------
342 {
343 return (m_pCallback.get()) ? (generic_fun_type)m_pCallback->GetAddr() : 0;
344 }
345
346 //------------------------------------------------------------------------------
352 TBase GetVal() const
353 {
354 switch (m_iCode)
355 {
356 case cmVAL:
357 return m_fVal;
358 case cmVAR:
359 return *((TBase*)m_pTok);
360 default:
362 }
363 }
364
365 //------------------------------------------------------------------------------
371 TBase* GetVar() const
372 {
373 if (m_iCode != cmVAR)
375
376 return (TBase*)m_pTok;
377 }
378
379 //------------------------------------------------------------------------------
384 int GetArgCount() const
385 {
386 assert(m_pCallback.get());
387
388 if (!m_pCallback->GetAddr())
390
391 return m_pCallback->GetArgc();
392 }
393
394 //------------------------------------------------------------------------------
403 const TString& GetAsString() const
404 {
405 return m_strTok;
406 }
407 };
408} // namespace mu
409
410#endif
Encapsulation of prototypes for a numerical parser function.
void * GetAddr() const
Get the callback address for the parser function.
ECmdCode GetCode() const
Return the callback code.
Error class of the parser.
Encapsulation of the data for a single formula token.
Definition: muParserToken.h:62
ParserToken()
Constructor (default).
Definition: muParserToken.h:83
ECmdCode GetCode() const
Return the token type.
ParserToken(const ParserToken &a_Tok)
Create token from another one.
int m_iIdx
An otional index to an external buffer storing the token data.
Definition: muParserToken.h:68
ParserToken & operator=(const ParserToken &a_Tok)
Assignement operator.
void Assign(const ParserToken &a_Tok)
Copy token information from argument.
std::unique_ptr< ParserCallback > m_pCallback
Definition: muParserToken.h:72
ParserToken & SetString(const TString &a_strTok, std::size_t a_iSize)
Make this token a variable token.
void * m_pTok
Stores Token pointer; not applicable for all tokens.
Definition: muParserToken.h:67
TString m_strVal
Value for string variables.
Definition: muParserToken.h:70
ParserToken & SetVar(TBase *a_pVar, const TString &a_strTok)
make this token a variable token.
bool IsOptimizable() const
TBase * GetVar() const
Get address of a variable token.
ParserToken & Set(const ParserCallback &a_pCallback, const TString &a_sTok)
Set Callback type.
int GetArgCount() const
Return the number of function arguments.
generic_fun_type GetFuncAddr() const
Return the address of the callback function assoziated with function and operator tokens.
ParserToken & SetVal(TBase a_fVal, const TString &a_strTok=TString())
Make this token a value token.
TString m_strTok
Token string.
Definition: muParserToken.h:69
ETypeCode GetType() const
int GetIdx() const
Return Index associated with the token related data.
TBase GetVal() const
void SetIdx(int a_iIdx)
Set an index associated with the token related data.
ParserToken & Set(ECmdCode a_iType, const TString &a_strTok=TString())
Assign a token type.
value_type m_fVal
the value
Definition: muParserToken.h:71
const TString & GetAsString() const
Return the token identifier.
EOprtAssociativity GetAssociativity() const
ECmdCode m_iCode
Type of the token; The token type is a constant of type ECmdCode.
Definition: muParserToken.h:65
ETypeCode m_iType
Definition: muParserToken.h:66
int GetPri() const
Definition of the parser callback class.
This file defines the error class used by the parser.
Namespace for mathematical applications.
Definition: muParser.cpp:53
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
ECmdCode
Bytecode values.
Definition: muParserDef.h:153
@ cmSTRING
Code for a string token.
Definition: muParserDef.h:192
@ cmOPRT_INFIX
code for infix operators
Definition: muParserDef.h:195
@ cmOPRT_BIN
user defined binary operator
Definition: muParserDef.h:193
@ cmVAL
value item
Definition: muParserDef.h:177
@ cmUNKNOWN
uninitialized item
Definition: muParserDef.h:197
@ cmFUNC
Code for a generic function item.
Definition: muParserDef.h:189
@ cmVAR
variable item
Definition: muParserDef.h:180
ETypeCode
Types internally used by the parser.
Definition: muParserDef.h:204
@ tpVOID
Undefined type.
Definition: muParserDef.h:207
@ tpDBL
Floating point variables.
Definition: muParserDef.h:206
@ tpSTR
String type (Function arguments and constants only, no string variables)
Definition: muParserDef.h:205
@ ecVAL_EXPECTED
A numerical function has been called with a non value type of argument.
Definition: muParserError.h:62
@ ecINTERNAL_ERROR
Internal error of any kind.
Definition: muParserError.h:94
value_type(* generic_fun_type)()
Callback type used for functions without arguments.
Definition: muParserDef.h:287
EOprtAssociativity
Parser operator precedence values.
Definition: muParserDef.h:220