NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
muParserTokenReader.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_READER_H
27#define MU_PARSER_TOKEN_READER_H
28
29#include <cassert>
30#include <cstdio>
31#include <cstring>
32#include <list>
33#include <map>
34#include <memory>
35#include <stack>
36#include <string>
37
38#include "muParserDef.h"
39#include "muParserToken.h"
40
46namespace mu
47{
48 // Forward declaration
49 class ParserBase;
50
55 {
56 private:
57
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 int GetPos() const;
71 const string_type& GetExpr() const;
73 char_type GetArgSep() const;
74
75 void IgnoreUndefVar(bool bIgnore);
76 void ReInit();
78
79 private:
80
88 {
89 noBO = 1 << 0,
90 noBC = 1 << 1,
91 noVAL = 1 << 2,
92 noVAR = 1 << 3,
93 noARG_SEP = 1 << 4,
94 noFUN = 1 << 5,
95 noOPT = 1 << 6,
96 noPOSTOP = 1 << 7,
97 noINFIXOP = 1 << 8,
98 noEND = 1 << 9,
99 noSTR = 1 << 10,
100 noASSIGN = 1 << 11,
101 noIF = 1 << 12,
102 noELSE = 1 << 13,
104 noANY = ~0
105 };
106
107 ParserTokenReader(const ParserTokenReader &a_Reader);
109 void Assign(const ParserTokenReader &a_Reader);
110
111 void SetParent(ParserBase *a_pParent);
112 int ExtractToken(const char_type *a_szCharSet,
113 string_type &a_strTok,
114 int a_iPos) const;
115 int ExtractOperatorToken(string_type &a_sTok, int a_iPos) const;
116
117 bool IsBuiltIn(token_type &a_Tok);
118 bool IsArgSep(token_type &a_Tok);
119 bool IsEOF(token_type &a_Tok);
120 bool IsInfixOpTok(token_type &a_Tok);
121 bool IsFunTok(token_type &a_Tok);
122 bool IsPostOpTok(token_type &a_Tok);
123 bool IsOprt(token_type &a_Tok);
124 bool IsValTok(token_type &a_Tok);
125 bool IsVarTok(token_type &a_Tok);
126 bool IsStrVarTok(token_type &a_Tok);
127 bool IsUndefVarTok(token_type &a_Tok);
128 bool IsString(token_type &a_Tok);
129 void Error(EErrorCodes a_iErrc,
130 int a_iPos = -1,
131 const string_type &a_sTok = string_type() ) const;
132
134
140
150 std::list<identfun_type> m_vIdentFun;
156 };
157} // namespace mu
158
159#endif
160
161
Mathematical expressions parser (base parser engine).
Definition: muParserBase.h:69
Token reader for the ParserBase class.
bool IsArgSep(token_type &a_Tok)
bool IsPostOpTok(token_type &a_Tok)
Check if a string position contains a unary post value operator.
const funmap_type * m_pPostOprtDef
const funmap_type * m_pInfixOprtDef
std::list< identfun_type > m_vIdentFun
Value token identification function.
ParserTokenReader(ParserBase *a_pParent)
Constructor.
bool IsFunTok(token_type &a_Tok)
Check whether the token at a given position is a function token.
bool IsValTok(token_type &a_Tok)
Check whether the token at a given position is a value token.
token_type ReadNextToken()
Read the next token from the string.
@ noASSIGN
to block assignement to constant i.e. "4=7"
@ noEND
to avoid unexpected end of formula
@ noSTR
to block numeric arguments on string functions
@ noINFIXOP
to avoid i.e. "++4" "!!4"
@ noVAR
to avoid i.e. "sin a" or "sin(8)a"
@ noBO
to avoid i.e. "cos(7)("
@ noOPT
to avoid i.e. "(+)"
@ noANY
All of he above flags set.
@ noVAL
to avoid i.e. "tan 2" or "sin(8)3.14"
@ noPOSTOP
to avoid i.e. "(5!!)" "sin!"
@ noARG_SEP
to avoid i.e. ",," or "+," ...
@ noBC
to avoid i.e. "sin)" or "()"
@ noFUN
to avoid i.e. "sqrt cos" or "(1)sin"
void SetParent(ParserBase *a_pParent)
char_type m_cArgSep
The character used for separating function arguments.
void Assign(const ParserTokenReader &a_Reader)
Assign state of a token reader to this token reader.
bool IsOprt(token_type &a_Tok)
Check if a string position contains a binary operator.
int ExtractToken(const char_type *a_szCharSet, string_type &a_strTok, int a_iPos) const
Extract all characters that belong to a certain charset.
value_type m_fZero
Dummy value of zero, referenced by undefined variables.
void SetArgSep(char_type cArgSep)
const funmap_type * m_pFunDef
ParserTokenReader & operator=(const ParserTokenReader &a_Reader)
Assignement operator.
void Error(EErrorCodes a_iErrc, int a_iPos=-1, const string_type &a_sTok=string_type()) const
Create an error containing the parse error position.
void SetVarCreator(facfun_type a_pFactory, void *pUserData)
void IgnoreUndefVar(bool bIgnore)
Set Flag that contronls behaviour in case of undefined variables beeing found.
const funmap_type * m_pOprtDef
ParserTokenReader * Clone(ParserBase *a_pParent) const
Create instance of a ParserTokenReader identical with this and return its pointer.
bool IsVarTok(token_type &a_Tok)
Check wheter a token at a given position is a variable token.
void SetFormula(const string_type &a_strFormula)
Initialize the token Reader.
bool IsString(token_type &a_Tok)
Check wheter a token at a given position is a string.
int GetPos() const
Return the current position of the token reader in the formula string.
bool IsInfixOpTok(token_type &a_Tok)
Check if a string position contains a unary infix operator.
ParserToken< value_type, string_type > token_type
int ExtractOperatorToken(string_type &a_sTok, int a_iPos) const
Check Expression for the presence of a binary operator token.
varmap_type * m_pVarDef
The only non const pointer to parser internals.
const strmap_type * m_pStrVarDef
bool IsEOF(token_type &a_Tok)
Check for End of Formula.
bool IsBuiltIn(token_type &a_Tok)
Check if a built in operator or other token can be found.
void ReInit()
Reset the token reader to the start of the formula.
token_type & SaveBeforeReturn(const token_type &tok)
varmap_type & GetUsedVar()
Return a map containing the used variables only.
void AddValIdent(identfun_type a_pCallback)
bool IsStrVarTok(token_type &a_Tok)
const string_type & GetExpr() const
Return a reference to the formula.
bool IsUndefVarTok(token_type &a_Tok)
Check wheter a token at a given position is an undefined variable.
const valmap_type * m_pConstDef
This file contains standard definitions used by the parser.
This file contains the parser token definition.
Namespace for mathematical applications.
Definition: muParser.cpp:53
int(* identfun_type)(const char_type *sExpr, int *nPos, value_type *fVal)
Callback used for functions that identify values in a string.
Definition: muParserDef.h:368
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
value_type *(* facfun_type)(const char_type *, void *)
Callback used for variable creation factory functions.
Definition: muParserDef.h:371
std::map< string_type, value_type > valmap_type
Type used for storing constants.
Definition: muParserDef.h:276
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:263
std::map< string_type, ParserCallback > funmap_type
Container for Callback objects.
std::map< string_type, value_type * > varmap_type
Type used for storing variables.
Definition: muParserDef.h:273
EErrorCodes
Error codes.
Definition: muParserError.h:50
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:257
std::map< string_type, std::size_t > strmap_type
Type for assigning a string name to an index in the internal string table.
Definition: muParserDef.h:279