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