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 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
39 
40 #include <boost/variant/apply_visitor.hpp>
41 
42 #pragma GCC diagnostic pop
43 
44 #include <memory>
45 #include <string>
46 
47 namespace schnek {
48 
49 class ParserOperator;
50 class ParserToken;
51 
52 typedef std::shared_ptr<ParserToken> pParserToken;
53 
54 
56 {
57  std::string message;
58  Token atomToken;
59  ParserError(std::string message_, Token atomToken_)
60  : SchnekException(), message(message_), atomToken(atomToken_) {}
61  std::string getFilename() { return atomToken.getFilename(); }
62  int getLine() { return atomToken.getLine(); }
63 };
64 
65 
67 {
68  public:
69  enum TokenType {
70  deck, blocklist, block, statementlist, statement,
71  expression, expressionlist, value, int_type, float_type, string_type, atom, none
72  };
73 
74  ParserToken();
75  ParserToken(const Token atomTok_, ParserContext context_);
76  ParserToken(const ParserToken &tok);
77  ParserToken& operator=(const ParserToken &tok);
78 
79  TokenType getType() const;
80  std::string getString() const { return atomTok.getString(); }
81 
82  void append(ParserToken &parTok);
83 
84  // assignment functions
85  void assignInteger(ParserToken &parTok);
86  void assignFloat(ParserToken &parTok);
87  void assignString(ParserToken &parTok);
88  void assignIdentifier(ParserToken &parTok);
89 
90  template<template<class> class OpType>
91  void assignUnaryOperator(ParserToken &parTok);
92 
93  template<template<class> class OpType>
94  void assignBinaryOperator(ParserToken &parTok1, ParserToken &parTok2);
95 
96  void makeExpressionList();
97  void assignFunction(ParserToken &parTok1, ParserToken &parTok2);
98  void assignFunction(ParserToken &parTok1);
99 
100  void evaluateExpression(ParserToken &identifier, ParserToken &expression);
101 
102  void storeVariable(ParserToken &parTok);
103  void updateVariable();
104 
105  void createBlock(ParserToken &parTok);
106  void endBlock();
107 
108  private:
109  void ensureVariable(ParserToken &parTok);
110 
111  ParserContext context;
112  Token atomTok;
113  TokenType type;
114  ExpressionVariant data;
115  pVariable var;
116 
117  pParserToken chainedToken;
118 
119 };
120 
121 inline std::string toString(ParserToken::TokenType type)
122 {
123  switch (type)
124  {
125  case ParserToken::deck:
126  return "deck";
127  case ParserToken::blocklist:
128  return "blocklist";
129  case ParserToken::block:
130  return "block";
131  case ParserToken::statementlist:
132  return "statementlist";
133  case ParserToken::statement:
134  return "statement";
135  case ParserToken::expression:
136  return "expression";
137  case ParserToken::expressionlist:
138  return "expressionlist";
139  case ParserToken::value:
140  return "value";
141  case ParserToken::int_type:
142  return "int_type";
143  case ParserToken::float_type:
144  return "float_type";
145  case ParserToken::string_type:
146  return "string_type";
147  case ParserToken::atom:
148  return "atom";
149  case ParserToken::none:
150  return "none";
151  default:
152  return "[Unknown ParserToken::TokenType]";
153  }
154 }
155 
156 
160 class TypePromoter : public boost::static_visitor<void>
161 {
162  private:
163  ExpressionVariant result1;
164  ExpressionVariant result2;
165 
166  public:
167  template<class ExpressionPointer1, class ExpressionPointer2>
168  void operator()(ExpressionPointer1 e1, ExpressionPointer2 e2);
169 
170  template<class ExpressionPointer>
171  void operator()(ExpressionPointer e1, ExpressionPointer e2);
172 
173  const ExpressionVariant &getResultA() { return result1; }
174  const ExpressionVariant &getResultB() { return result2; }
175 };
176 
180 class TypePromoterAssign : public boost::static_visitor<ExpressionVariant>
181 {
182  public:
183  template<class ExpressionPointer1, class ExpressionPointer2>
184  ExpressionVariant operator()(ExpressionPointer1, ExpressionPointer2 e2);
185 
186  template<class ExpressionPointer>
187  ExpressionVariant operator()(ExpressionPointer, ExpressionPointer e2);
188 
189 };
190 
191 #include "parsertoken.t"
192 
193 } // namespace
194 
195 #endif // SCHNEK_PARSERTOKEN_HPP_
Definition: parsertoken.hpp:66
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:180
Definition: parsertoken.hpp:55
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:160
Definition: exception.hpp:33