Schnek
parsertoken.hpp
1 /*
2  * parsertoken.cpp
3  *
4  * Created on: 12 Jan 2011
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_PARSERTOKEN_HPP_
28 #define SCHNEK_PARSERTOKEN_HPP_
29 
30 #include "parsercontext.hpp"
31 #include "tokenlist.hpp"
32 #include "../variables/types.hpp"
33 #include "../variables/expression.hpp"
34 
35 #include "../exception.hpp"
36 
37 #include <boost/shared_ptr.hpp>
38 #include <boost/variant/apply_visitor.hpp>
39 #include <string>
40 
41 namespace schnek {
42 
43 class ParserOperator;
44 class ParserToken;
45 
46 typedef boost::shared_ptr<ParserToken> pParserToken;
47 
48 
50 {
51  std::string message;
52  Token atomToken;
53  ParserError(std::string message_, Token atomToken_)
54  : SchnekException(), message(message_), atomToken(atomToken_) {}
55  std::string getFilename() { return atomToken.getFilename(); }
56  int getLine() { return atomToken.getLine(); }
57 };
58 
59 
61 {
62  public:
63  enum TokenType {
64  deck, blocklist, block, statementlist, statement,
65  expression, expressionlist, value, int_type, float_type, string_type, atom, none
66  };
67 
68  ParserToken();
69  ParserToken(const Token atomTok_, ParserContext context_);
70  ParserToken(const ParserToken &tok);
71  ParserToken& operator=(const ParserToken &tok);
72 
73  TokenType getType() const;
74  std::string getString() const { return atomTok.getString(); }
75 
76  void append(ParserToken &parTok);
77 
78  // assignment functions
79  void assignInteger(ParserToken &parTok);
80  void assignFloat(ParserToken &parTok);
81  void assignString(ParserToken &parTok);
82  void assignIdentifier(ParserToken &parTok);
83 
84  template<template<class> class OpType>
85  void assignUnaryOperator(ParserToken &parTok);
86 
87  template<template<class> class OpType>
88  void assignBinaryOperator(ParserToken &parTok1, ParserToken &parTok2);
89 
90  void makeExpressionList();
91  void assignFunction(ParserToken &parTok1, ParserToken &parTok2);
92  void assignFunction(ParserToken &parTok1);
93 
94  void evaluateExpression(ParserToken &identifier, ParserToken &expression);
95 
96  void storeVariable(ParserToken &parTok);
97  void updateVariable();
98 
99  void createBlock(ParserToken &parTok);
100  void endBlock();
101 
102  private:
103  void ensureVariable(ParserToken &parTok);
104 
105  ParserContext context;
106  Token atomTok;
107  TokenType type;
108  ExpressionVariant data;
109  pVariable var;
110 
111  pParserToken chainedToken;
112 
113 };
114 
115 inline std::string toString(ParserToken::TokenType type)
116 {
117  switch (type)
118  {
119  case ParserToken::deck:
120  return "deck";
121  case ParserToken::blocklist:
122  return "blocklist";
123  case ParserToken::block:
124  return "block";
125  case ParserToken::statementlist:
126  return "statementlist";
127  case ParserToken::statement:
128  return "statement";
129  case ParserToken::expression:
130  return "expression";
131  case ParserToken::expressionlist:
132  return "expressionlist";
133  case ParserToken::value:
134  return "value";
135  case ParserToken::int_type:
136  return "int_type";
137  case ParserToken::float_type:
138  return "float_type";
139  case ParserToken::string_type:
140  return "string_type";
141  case ParserToken::atom:
142  return "atom";
143  case ParserToken::none:
144  return "none";
145  default:
146  return "[Unknown ParserToken::TokenType]";
147  }
148 }
149 
150 
154 class TypePromoter : public boost::static_visitor<void>
155 {
156  private:
157  ExpressionVariant result1;
158  ExpressionVariant result2;
159 
160  public:
161  template<class ExpressionPointer1, class ExpressionPointer2>
162  void operator()(ExpressionPointer1 e1, ExpressionPointer2 e2);
164  template<class ExpressionPointer>
165  void operator()(ExpressionPointer e1, ExpressionPointer e2);
166 
167  const ExpressionVariant &getResultA() { return result1; }
168  const ExpressionVariant &getResultB() { return result2; }
169 };
170 
174 class TypePromoterAssign : public boost::static_visitor<ExpressionVariant>
175 {
176  public:
177  template<class ExpressionPointer1, class ExpressionPointer2>
178  ExpressionVariant operator()(ExpressionPointer1, ExpressionPointer2 e2);
179 
180  template<class ExpressionPointer>
181  ExpressionVariant operator()(ExpressionPointer, ExpressionPointer e2);
182 
183 };
184 
185 #include "parsertoken.t"
186 
187 } // namespace
188 
189 #endif // SCHNEK_PARSERTOKEN_HPP_
Definition: parsertoken.hpp:60
std::string getFilename() const
Returns the name of the file from which the token was read.
Definition: tokenlist.hpp:70
Definition: algo.hpp:30
Definition: parsertoken.hpp:174
Definition: parsertoken.hpp:49
Definition: parsercontext.hpp:39
int getLine() const
Returns the line number where the token was encountered.
Definition: tokenlist.hpp:76
Definition: tokenlist.hpp:43
Definition: parsertoken.hpp:154
Definition: exception.hpp:33