summary refs log tree commit diff stats
path: root/lib/token.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/token.h')
-rw-r--r--lib/token.h313
1 files changed, 313 insertions, 0 deletions
diff --git a/lib/token.h b/lib/token.h new file mode 100644 index 0000000..44d99cb --- /dev/null +++ b/lib/token.h
@@ -0,0 +1,313 @@
1#ifndef TOKEN_H_AD62C505
2#define TOKEN_H_AD62C505
3
4#include <string>
5#include <list>
6#include <sstream>
7
8namespace verbly {
9
10 class verb;
11
12 class selrestr {
13 };
14
15 class synrestr {
16 };
17
18 enum class fillin_type {
19 noun_phrase,
20 participle_phrase,
21 adjective,
22 adverb
23 };
24
25 class token {
26 public:
27 enum class type {
28 verb,
29 fillin,
30 string,
31 utterance
32 };
33
34 protected:
35 // General
36 type _type;
37
38 token(type _type);
39
40 public:
41 enum type token_type() const;
42
43 virtual bool complete() const = 0;
44 virtual std::string compile() const = 0;
45 virtual token* copy() const = 0;
46 };
47
48 class verb_token : public token {
49 public:
50 enum class inflection {
51 infinitive,
52 past_tense,
53 past_participle,
54 ing_form,
55 s_form
56 };
57
58 private:
59 // Verb
60 const verb* _verb;
61 inflection _inflection = inflection::infinitive;
62
63 public:
64 verb_token(const class verb& _verb);
65
66 const class verb& verb() const;
67
68 verb_token& inflect(inflection infl);
69
70 bool complete() const;
71
72 std::string compile() const;
73
74 token* copy() const;
75 };
76
77 class utterance_token : public token {
78 private:
79 // Utterance
80 std::list<std::unique_ptr<token>> utterance;
81
82 public:
83 typedef std::list<std::unique_ptr<token>>::iterator iterator;
84 /*class iterator {
85 private:
86 friend class utterance_token;
87
88 std::list<std::unique_ptr<token>>::iterator it;
89
90 public:
91 iterator(std::list<std::unique_ptr<token>>::iterator it) : it(it)
92 {
93
94 }
95
96 iterator& operator++()
97 {
98 ++it;
99 return *this;
100 }
101
102 iterator& operator--()
103 {
104 --it;
105 return *this;
106 }
107
108 bool operator==(const iterator& other) const
109 {
110 return it == other.it;
111 }
112
113 bool operator!=(const iterator& other) const
114 {
115 return it != other.it;
116 }
117
118 token& operator*()
119 {
120 return **it;
121 }
122
123 token& operator->()
124 {
125 return **it;
126 }
127 };*/
128
129 utterance_token(std::initializer_list<token*> tkns) : token(token::type::utterance)
130 {
131 for (auto tkn : tkns)
132 {
133 utterance.push_back(std::unique_ptr<token>(tkn));
134 }
135 }
136
137 utterance_token(const utterance_token& other) : token(token::type::utterance)
138 {
139 for (auto& tkn : other.utterance)
140 {
141 utterance.push_back(std::unique_ptr<token>(tkn->copy()));
142 }
143 }
144
145 utterance_token(utterance_token&& other) : token(token::type::utterance), utterance(std::move(other.utterance))
146 {
147
148 }
149
150 utterance_token& operator=(const utterance_token& other)
151 {
152 utterance.clear();
153
154 for (auto& tkn : other.utterance)
155 {
156 utterance.push_back(std::unique_ptr<token>(tkn->copy()));
157 }
158
159 return *this;
160 }
161
162 utterance_token& operator=(utterance_token&& other)
163 {
164 utterance = std::move(other.utterance);
165
166 return *this;
167 }
168
169 iterator begin()
170 {
171 return std::begin(utterance);
172 }
173
174 iterator end()
175 {
176 return std::end(utterance);
177 }
178
179 void erase(iterator it)
180 {
181 utterance.erase(it);
182 }
183
184 bool complete() const
185 {
186 return std::all_of(std::begin(utterance), std::end(utterance), [] (const std::unique_ptr<token>& tkn) {
187 return tkn->complete();
188 });
189 }
190
191 std::string compile() const
192 {
193 std::stringstream result;
194 for (auto& t : utterance)
195 {
196 if (t->complete())
197 {
198 result << t->compile() << " ";
199 } else {
200 return "Could not compile!";
201 }
202 }
203
204 std::string output = result.str();
205 if (output != "")
206 {
207 output.pop_back();
208 }
209
210 return output;
211 }
212
213 token* copy() const
214 {
215 return new utterance_token(*this);
216 }
217 };
218
219 class fillin_token : public token {
220 private:
221 // Fillin
222 std::string m_theme;
223 fillin_type m_fillin_type;
224
225 public:
226 fillin_token(fillin_type ft) : token(token::type::fillin), m_fillin_type(ft)
227 {
228
229 }
230
231/* void synrestrs(std::initializer_list<synrestr> ins)
232 {
233 m_synrestrs = std::set<synrestr>(ins);
234 }
235
236 std::set<synrestr>& synrestrs()
237 {
238 return m_synrestrs;
239 }
240
241 void selrestrs(std::initializer_list<selrestr> ins)
242 {
243 m_selrestrs = std::set<selrestr>(ins);
244 }
245
246 std::set<selrestr>& selrestrs()
247 {
248 return m_selrestrs;
249 }*/
250
251 fillin_token theme(std::string theme)
252 {
253 m_theme = theme;
254
255 return *this;
256 }
257
258 std::string& theme()
259 {
260 return m_theme;
261 }
262
263 fillin_type fillin_type() const
264 {
265 return m_fillin_type;
266 }
267
268 bool complete() const
269 {
270 return false;
271 }
272
273 std::string compile() const
274 {
275 return "";
276 }
277
278 token* copy() const
279 {
280 return new fillin_token(*this);
281 }
282 };
283
284 class string_token : public token {
285 private:
286 // String
287 std::string str;
288
289 public:
290 string_token(std::string str) : token(token::type::string), str(str)
291 {
292
293 }
294
295 bool complete() const
296 {
297 return true;
298 }
299
300 std::string compile() const
301 {
302 return str;
303 }
304
305 token* copy() const
306 {
307 return new string_token(*this);
308 }
309 };
310
311};
312
313#endif /* end of include guard: TOKEN_H_AD62C505 */