muParserToken.h 14.1 KB
Newer Older
1
/*
2
3
                 __________
    _____   __ __\______   \_____  _______  ______  ____ _______
4
5
   /     \ |  |  \|     ___/\__  \ \_  __ \/  ___/_/ __ \\_  __ \
  |  Y Y  \|  |  /|    |     / __ \_|  | \/\___ \ \  ___/ |  | \/
6
7
  |__|_|  /|____/ |____|    (____  /|__|  /____  > \___  >|__|
        \/                       \/            \/      \/
8
9
  Copyright (C) 2004-2008 Ingo Berg

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
13
  without restriction, including without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14
15
  permit persons to whom the Software is furnished to do so, subject to the following conditions:

16
  The above copyright notice and this permission notice shall be included in all copies or
17
18
19
  substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20
21
22
23
  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
*/

#ifndef MU_PARSER_TOKEN_H
#define MU_PARSER_TOKEN_H

#include <cassert>
#include <string>
#include <stack>
#include <vector>
#include <memory>

#include "muParserError.h"
#include "muParserCallback.h"

/** \file
    \brief This file contains the parser token definition.
*/

namespace mu
{
44
  /** \brief Encapsulation of the data for a single formula token.
45
46
47
48
49
50
51
52
53
54
55
56
57

    Formula token implementation. Part of the Math Parser Package.
    Formula tokens can be either one of the following:
    <ul>
      <li>value</li>
      <li>variable</li>
      <li>function with numerical arguments</li>
      <li>functions with a string as argument</li>
      <li>prefix operators</li>
      <li>infix operators</li>
	    <li>binary operator</li>
    </ul>

58
   \author (C) 2004 Ingo Berg
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  */
  template<typename TBase, typename TString>
  class ParserToken
  {
  public:

      /** \brief Additional token flags. */
      enum ETokFlags
	    {
	      flVOLATILE = 1    ///< Mark a token that depends on a variable or a function that is not conservative
	    };

  private:

      ECmdCode  m_iCode;  ///< Type of the token; The token type is a constant of type #ECmdCode.
      ETypeCode m_iType;
      void  *m_pTok;      ///< Stores Token pointer; not applicable for all tokens
  	  int  m_iFlags;      ///< Additional flags for the token.
      int  m_iIdx;        ///< An otional index to an external buffer storing the token data
      TString m_strTok;   ///< Token string
      TString m_strVal;   ///< Value for string variables
      value_type m_fVal;
81
#if AMDIS_HAS_CXX11
82
83
84
85
86
87
88
89
90
      std::unique_ptr<ParserCallback> m_pCallback;
#else
      std::auto_ptr<ParserCallback> m_pCallback;
#endif

  public:

      //---------------------------------------------------------------------------
      /** \brief Constructor (default).
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
          Sets token to an neutral state of type cmUNKNOWN.
          \throw nothrow
          \sa ECmdCode
      */
      ParserToken()
        :m_iCode(cmUNKNOWN)
        ,m_iType(tpVOID)
        ,m_pTok(0)
        ,m_iFlags(0)
        ,m_iIdx(-1)
        ,m_strTok()
        ,m_pCallback()
      {}

      //------------------------------------------------------------------------------
      /** \brief Create token from another one.
108

109
110
111
112
113
114
115
116
117
          Implemented by calling Assign(...)
          \throw nothrow
          \post m_iType==cmUNKNOWN
          \sa #Assign
      */
      ParserToken(const ParserToken &a_Tok)
      {
        Assign(a_Tok);
      }
118

119
      //------------------------------------------------------------------------------
120
121
      /** \brief Assignement operator.

122
123
124
125
126
127
128
129
130
131
132
133
          Copy token state from another token and return this.
          Implemented by calling Assign(...).
          \throw nothrow
      */
      ParserToken& operator=(const ParserToken &a_Tok)
      {
        Assign(a_Tok);
        return *this;
      }

      //------------------------------------------------------------------------------
      /** \brief Copy token information from argument.
134

135
136
137
138
139
140
141
142
143
144
145
146
          \throw nothrow
      */
      void Assign(const ParserToken &a_Tok)
      {
        m_iCode = a_Tok.m_iCode;
        m_pTok = a_Tok.m_pTok;
        m_iFlags = a_Tok.m_iFlags;
        m_strTok = a_Tok.m_strTok;
        m_iIdx = a_Tok.m_iIdx;
        m_strVal = a_Tok.m_strVal;
        m_iType = a_Tok.m_iType;
        m_fVal = a_Tok.m_fVal;
147
        // create new callback object if a_Tok has one
148
149
150
151
        m_pCallback.reset(a_Tok.m_pCallback.get() ? a_Tok.m_pCallback->Clone() : 0);
      }

      //------------------------------------------------------------------------------
152
      /** \brief Add additional flags to the token.
153
154

          Flags are currently used to mark volatile (non optimizeable) functions.
155
          \sa m_iFlags, ETokFlags
156
157
158
159
160
161
162
      */
      void AddFlags(int a_iFlags)
      {
        m_iFlags |= a_iFlags;
      }

      //------------------------------------------------------------------------------
163
      /** \brief Check if a certain flag ist set.
164
165
166
167
168
169
170
171

          \throw nothrow
      */
      bool IsFlagSet(int a_iFlags) const
      {
        #if defined(_MSC_VER)
          #pragma warning( disable : 4800 )
        #endif
172

173
174
175
176
177
178
179
180
        return (bool)(m_iFlags & a_iFlags);

        #if defined(_MSC_VER)
          #pragma warning( default : 4800 ) // int: Variable set to boolean value (may degrade performance)
        #endif
      }

      //------------------------------------------------------------------------------
181
      /** \brief Assign a token type.
182

183
        Token may not be of type value, variable or function. Those have seperate set functions.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

        \pre [assert] a_iType!=cmVAR
        \pre [assert] a_iType!=cmVAL
        \pre [assert] a_iType!=cmFUNC
        \post m_fVal = 0
        \post m_pTok = 0
      */
      ParserToken& Set(ECmdCode a_iType, const TString &a_strTok=TString())
      {
        // The following types cant be set this way, they have special Set functions
        assert(a_iType!=cmVAR);
        assert(a_iType!=cmVAL);
        assert(a_iType!=cmFUNC);

        m_iCode = a_iType;
        m_iType = tpVOID;
        m_pTok = 0;
        m_iFlags = 0;
        m_strTok = a_strTok;
        m_iIdx = -1;

        return *this;
      }

      //------------------------------------------------------------------------------
      /** \brief Set Callback type. */
      ParserToken& Set(const ParserCallback &a_pCallback, const TString &a_sTok)
      {
        assert(a_pCallback.GetAddr());

        m_iCode = a_pCallback.GetCode();
        m_iType = tpVOID;
        m_strTok = a_sTok;
        m_pCallback.reset(new ParserCallback(a_pCallback));

        m_pTok = 0;
        m_iFlags = 0;
        m_iIdx = -1;
222

223
224
225
226
227
228
229
        if (!m_pCallback->IsOptimizable())
          AddFlags(flVOLATILE);

        return *this;
      }

      //------------------------------------------------------------------------------
230
231
      /** \brief Make this token a value token.

232
233
234
235
236
237
238
239
240
241
242
          Member variables not necessary for value tokens will be invalidated.
          \throw nothrow
      */
      ParserToken& SetVal(TBase a_fVal, const TString &a_strTok=TString())
      {
        m_iCode = cmVAL;
        m_iType = tpDBL;
        m_fVal = a_fVal;
        m_iFlags = 0;
        m_strTok = a_strTok;
        m_iIdx = -1;
243

244
245
246
247
248
249
250
        m_pTok = 0;
        m_pCallback.reset(0);

        return *this;
      }

      //------------------------------------------------------------------------------
251
252
      /** \brief make this token a variable token.

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
          Member variables not necessary for variable tokens will be invalidated.
          \throw nothrow
      */
      ParserToken& SetVar(TBase *a_pVar, const TString &a_strTok)
      {
        m_iCode = cmVAR;
        m_iType = tpDBL;
        m_iFlags = 0;
        m_strTok = a_strTok;
        m_iIdx = -1;
        m_pTok = (void*)a_pVar;
        m_pCallback.reset(0);

        AddFlags(ParserToken::flVOLATILE);
        return *this;
      }

      //------------------------------------------------------------------------------
271
272
      /** \brief Make this token a variable token.

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
          Member variables not necessary for variable tokens will be invalidated.
          \throw nothrow
      */
      ParserToken& SetString(const TString &a_strTok, std::size_t a_iSize)
      {
        m_iCode = cmSTRING;
        m_iType = tpSTR;
        m_iFlags = 0;
        m_strTok = a_strTok;
        m_iIdx = static_cast<int>(a_iSize);

        m_pTok = 0;
        m_pCallback.reset(0);

        AddFlags(ParserToken::flVOLATILE);
        return *this;
      }

      //------------------------------------------------------------------------------
292
293
      /** \brief Set an index associated with the token related data.

294
295
296
297
298
299
300
301
          In cmSTRFUNC - This is the index to a string table in the main parser.
          \param a_iIdx The index the string function result will take in the bytecode parser.
          \throw exception_type if #a_iIdx<0 or #m_iType!=cmSTRING
      */
      void SetIdx(int a_iIdx)
      {
        if (m_iCode!=cmSTRING || a_iIdx<0)
	        throw ParserError(ecINTERNAL_ERROR);
302

303
304
305
306
        m_iIdx = a_iIdx;
      }

      //------------------------------------------------------------------------------
307
308
      /** \brief Return Index associated with the token related data.

309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
          In cmSTRFUNC - This is the index to a string table in the main parser.

          \throw exception_type if #m_iIdx<0 or #m_iType!=cmSTRING
          \return The index the result will take in the Bytecode calculatin array (#m_iIdx).
      */
      int GetIdx() const
      {
        if (m_iIdx<0 || m_iCode!=cmSTRING )
          throw ParserError(ecINTERNAL_ERROR);

        return m_iIdx;
      }

      //------------------------------------------------------------------------------
      /** \brief Return the token type.
324

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
          \return #m_iType
          \throw nothrow
      */
      ECmdCode GetCode() const
      {
        if (m_pCallback.get())
        {
          return m_pCallback->GetCode();
        }
        else
        {
          return m_iCode;
        }
      }

      //------------------------------------------------------------------------------
      ETypeCode GetType() const
      {
        if (m_pCallback.get())
        {
          return m_pCallback->GetType();
        }
        else
        {
          return m_iType;
        }
      }
352

353
354
355
356
357
      //------------------------------------------------------------------------------
      int GetPri() const
      {
        if ( !m_pCallback.get())
	        throw ParserError(ecINTERNAL_ERROR);
358

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
        if ( m_pCallback->GetCode()!=cmOPRT_BIN && m_pCallback->GetCode()!=cmOPRT_INFIX)
	        throw ParserError(ecINTERNAL_ERROR);

        return m_pCallback->GetPri();
      }

      //------------------------------------------------------------------------------
      EOprtAssociativity GetAssociativity() const
      {
        if (m_pCallback.get()==NULL || m_pCallback->GetCode()!=cmOPRT_BIN)
	        throw ParserError(ecINTERNAL_ERROR);

        return m_pCallback->GetAssociativity();
      }

      //------------------------------------------------------------------------------
      /** \brief Return the address of the callback function assoziated with
                 function and operator tokens.

          \return The pointer stored in #m_pTok.
          \throw exception_type if token type is non of:
                 <ul>
                   <li>cmFUNC</li>
                   <li>cmSTRFUNC</li>
                   <li>cmPOSTOP</li>
                   <li>cmINFIXOP</li>
                   <li>cmOPRT_BIN</li>
                 </ul>
          \sa ECmdCode
      */
      void* GetFuncAddr() const
      {
        return (m_pCallback.get()) ? m_pCallback->GetAddr() : 0;
      }

      //------------------------------------------------------------------------------
      /** \biref Get value of the token.
396

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
          Only applicable to variable and value tokens.
          \throw exception_type if token is no value/variable token.
      */
      TBase GetVal() const
      {
        switch (m_iCode)
        {
          case cmVAL:  return m_fVal;
          case cmVAR:  return *((TBase*)m_pTok);
          default:     throw ParserError(ecVAL_EXPECTED);
        }
      }

      //------------------------------------------------------------------------------
      /** \brief Get address of a variable token.

        Valid only if m_iType==CmdVar.
        \throw exception_type if token is no variable token.
      */
      TBase* GetVar() const
      {
        if (m_iCode!=cmVAR)
	        throw ParserError(ecINTERNAL_ERROR);

        return (TBase*)m_pTok;
      }

      //------------------------------------------------------------------------------
425
      /** \brief Return the number of function arguments.
426
427
428
429
430
431
432
433
434
435
436
437
438
439

        Valid only if m_iType==CmdFUNC.
      */
      int GetArgCount() const
      {
        assert(m_pCallback.get());

        if (!m_pCallback->GetAddr())
	        throw ParserError(ecINTERNAL_ERROR);

        return m_pCallback->GetArgc();
      }

      //------------------------------------------------------------------------------
440
441
      /** \brief Return the token identifier.

442
443
444
445
446
447
448
449
450
451
452
453
454
455
          If #m_iType is cmSTRING the token identifier is the value of the string argument
          for a string function.
          \return #m_strTok
          \throw nothrow
          \sa m_strTok
      */
      const TString& GetAsString() const
      {
        return m_strTok;
      }
  };
} // namespace mu

#endif