MathTypeLibrary(libmath++)  0.0.3
reader.h
1 // Math Type Library
3 // $Id: reader.h,v 1.6 2002/04/20 06:35:05 cparpart Exp $
4 // (This file contains the reader (parser) specific interface)
5 //
6 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public License
19 // along with this library; see the file COPYING.LIB. If not, write to
20 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
23 #ifndef libmath_reader_h
24 #define libmath_reader_h
25 
26 #include <math++/error.h>
27 
28 #include <string>
29 
30 namespace math {
31 
32 template<class> class TNode;
33 
38 class EReadError : public EMath {
39 public:
40  EReadError(const std::string& AReason) : EMath(AReason) {}
41 };
42 
48 template<class T>
49 class TReader {
50 private:
52  enum TToken {
53  /* TAKE CARE : THE VALUES ARE HARD CODED */
54  tkInvalid, tkEnd,
55  tkNumber = 1000, tkSymbol,
56  tkUnEqu, tkLess, tkGreater, tkLessEqu, tkGreaterEqu,
57  tkVeryLess, tkVeryGreat,
58  tkEqu = '=', tkComma = ',',
59  tkPlus = '+', tkMinus = '-', tkMul = '*', tkDiv = '/', tkPow = '^',
60  tkRndOpen = '(', tkRndClose = ')', tkBrOpen = '[', tkBrClose = ']',
61  tkAngOpen = '<', tkAngClose = '>', tkSetOpen = '{', tkSetClose = '}'
62  };
63 
64  std::string FExprStr; // holds the current parsed expression
65  std::string::size_type FPos;// holds the current read index
66 
67  TToken FToken; // holds current parsed token id
68  T FNumber; // holds last read number
69  std::string FSymbol; // holds last read symbol
70 
71 private:
72  TReader(const std::string& AInput);
73 
75  TNode<T> *equation(bool get);
76  // parses an expression (really any expression, even sub equations)
77  TNode<T> *expr(bool get);
79  TNode<T> *simpleExpr(bool get);
81  TNode<T> *term(bool get);
83  TNode<T> *factor(bool get);
85  TNode<T> *prim(bool get);
86 
88  TNode<T> *createSymbol();
90  TNode<T> *param();
91 
93  bool eof() const;
94 
96  TToken nextToken();
98  bool readOperator();
100  bool readNumber();
102  bool readSymbol();
103 
105  static std::string tok2str(TToken AToken);
107  void consume(TToken AToken);
108 
109 public:
114  static TNode<T> *parse(const std::string& AInput, bool AEquation = false);
115 };
116 
117 } // namespace math
118 
119 #include <math++/reader.tcc>
120 
121 #endif
static TNode< T > * parse(const std::string &AInput, bool AEquation=false)