diff options
Diffstat (limited to 'lib/token.cpp')
-rw-r--r-- | lib/token.cpp | 617 |
1 files changed, 596 insertions, 21 deletions
diff --git a/lib/token.cpp b/lib/token.cpp index facab8a..1e25acb 100644 --- a/lib/token.cpp +++ b/lib/token.cpp | |||
@@ -2,52 +2,627 @@ | |||
2 | 2 | ||
3 | namespace verbly { | 3 | namespace verbly { |
4 | 4 | ||
5 | token::token(token::type _type) : _type(_type) | 5 | token::type token::get_type() const |
6 | { | 6 | { |
7 | 7 | return _type; | |
8 | } | 8 | } |
9 | 9 | ||
10 | token::type token::token_type() const | 10 | int token::get_extra() const |
11 | { | 11 | { |
12 | return _type; | 12 | return _extra; |
13 | } | 13 | } |
14 | 14 | ||
15 | verb_token::verb_token(const verb& _verb) : token(token::type::verb), _verb(&_verb) | 15 | void token::set_extra(int _arg) |
16 | { | 16 | { |
17 | 17 | _extra = _arg; | |
18 | } | 18 | } |
19 | 19 | ||
20 | const verb& verb_token::get_verb() const | 20 | token::token(const token& other) |
21 | { | 21 | { |
22 | return *_verb; | 22 | _type = other._type; |
23 | |||
24 | switch (_type) | ||
25 | { | ||
26 | case token::type::verb: | ||
27 | { | ||
28 | new(&_verb._verb) verb(other._verb._verb); | ||
29 | _verb._infl = other._verb._infl; | ||
30 | |||
31 | break; | ||
32 | } | ||
33 | |||
34 | case token::type::noun: | ||
35 | { | ||
36 | new(&_noun._noun) noun(other._noun._noun); | ||
37 | _noun._infl = other._noun._infl; | ||
38 | |||
39 | break; | ||
40 | } | ||
41 | |||
42 | case token::type::adjective: | ||
43 | { | ||
44 | new(&_adjective._adjective) adjective(other._adjective._adjective); | ||
45 | _adjective._infl = other._adjective._infl; | ||
46 | |||
47 | break; | ||
48 | } | ||
49 | |||
50 | case token::type::adverb: | ||
51 | { | ||
52 | new(&_adverb._adverb) adverb(other._adverb._adverb); | ||
53 | _adverb._infl = other._adverb._infl; | ||
54 | |||
55 | break; | ||
56 | } | ||
57 | |||
58 | case token::type::preposition: | ||
59 | { | ||
60 | new(&_preposition._preposition) preposition(other._preposition._preposition); | ||
61 | |||
62 | break; | ||
63 | } | ||
64 | |||
65 | case token::type::fillin: | ||
66 | { | ||
67 | _fillin._type = other._fillin._type; | ||
68 | |||
69 | break; | ||
70 | } | ||
71 | |||
72 | case token::type::string: | ||
73 | { | ||
74 | new(&_string._str) std::string(other._string._str); | ||
75 | |||
76 | break; | ||
77 | } | ||
78 | |||
79 | case token::type::utterance: | ||
80 | { | ||
81 | new(&_utterance._utterance) std::list<token>(other._utterance._utterance); | ||
82 | |||
83 | break; | ||
84 | } | ||
85 | } | ||
23 | } | 86 | } |
24 | 87 | ||
25 | verb_token& verb_token::inflect(verb_token::inflection infl) | 88 | token& token::operator=(const token& other) |
26 | { | 89 | { |
27 | _inflection = infl; | 90 | this->~token(); |
91 | |||
92 | _type = other._type; | ||
93 | |||
94 | switch (_type) | ||
95 | { | ||
96 | case token::type::verb: | ||
97 | { | ||
98 | new(&_verb._verb) verb(other._verb._verb); | ||
99 | _verb._infl = other._verb._infl; | ||
100 | |||
101 | break; | ||
102 | } | ||
103 | |||
104 | case token::type::noun: | ||
105 | { | ||
106 | new(&_noun._noun) noun(other._noun._noun); | ||
107 | _noun._infl = other._noun._infl; | ||
108 | |||
109 | break; | ||
110 | } | ||
111 | |||
112 | case token::type::adjective: | ||
113 | { | ||
114 | new(&_adjective._adjective) adjective(other._adjective._adjective); | ||
115 | _adjective._infl = other._adjective._infl; | ||
116 | |||
117 | break; | ||
118 | } | ||
119 | |||
120 | case token::type::adverb: | ||
121 | { | ||
122 | new(&_adverb._adverb) adverb(other._adverb._adverb); | ||
123 | _adverb._infl = other._adverb._infl; | ||
124 | |||
125 | break; | ||
126 | } | ||
127 | |||
128 | case token::type::preposition: | ||
129 | { | ||
130 | new(&_preposition._preposition) preposition(other._preposition._preposition); | ||
131 | |||
132 | break; | ||
133 | } | ||
134 | |||
135 | case token::type::fillin: | ||
136 | { | ||
137 | _fillin._type = other._fillin._type; | ||
138 | |||
139 | break; | ||
140 | } | ||
141 | |||
142 | case token::type::string: | ||
143 | { | ||
144 | new(&_string._str) std::string(other._string._str); | ||
145 | |||
146 | break; | ||
147 | } | ||
148 | |||
149 | case token::type::utterance: | ||
150 | { | ||
151 | new(&_utterance._utterance) std::list<token>(other._utterance._utterance); | ||
152 | |||
153 | break; | ||
154 | } | ||
155 | } | ||
156 | |||
28 | return *this; | 157 | return *this; |
29 | } | 158 | } |
30 | 159 | ||
31 | bool verb_token::complete() const | 160 | token::~token() |
32 | { | 161 | { |
33 | return true; | 162 | switch (_type) |
163 | { | ||
164 | case token::type::verb: | ||
165 | { | ||
166 | _verb._verb.~verb(); | ||
167 | |||
168 | break; | ||
169 | } | ||
170 | |||
171 | case token::type::noun: | ||
172 | { | ||
173 | _noun._noun.~noun(); | ||
174 | |||
175 | break; | ||
176 | } | ||
177 | |||
178 | case token::type::adjective: | ||
179 | { | ||
180 | _adjective._adjective.~adjective(); | ||
181 | |||
182 | break; | ||
183 | } | ||
184 | |||
185 | case token::type::adverb: | ||
186 | { | ||
187 | _adverb._adverb.~adverb(); | ||
188 | |||
189 | break; | ||
190 | } | ||
191 | |||
192 | case token::type::preposition: | ||
193 | { | ||
194 | _preposition._preposition.~preposition(); | ||
195 | |||
196 | break; | ||
197 | } | ||
198 | |||
199 | case token::type::fillin: | ||
200 | { | ||
201 | // Nothing! | ||
202 | |||
203 | break; | ||
204 | } | ||
205 | |||
206 | case token::type::string: | ||
207 | { | ||
208 | using string_type = std::string; | ||
209 | _string._str.~string_type(); | ||
210 | |||
211 | break; | ||
212 | } | ||
213 | |||
214 | case token::type::utterance: | ||
215 | { | ||
216 | using list_type = std::list<token>; | ||
217 | _utterance._utterance.~list_type(); | ||
218 | |||
219 | break; | ||
220 | } | ||
221 | } | ||
34 | } | 222 | } |
35 | 223 | ||
36 | std::string verb_token::compile() const | 224 | bool token::is_complete() const |
37 | { | 225 | { |
38 | switch (_inflection) | 226 | if (_type == token::type::utterance) |
39 | { | 227 | { |
40 | case inflection::infinitive: return _verb->infinitive_form(); | 228 | return std::all_of(std::begin(_utterance._utterance), std::end(_utterance._utterance), [] (const token& tkn) { |
41 | case inflection::past_tense: return _verb->past_tense_form(); | 229 | return tkn.is_complete(); |
42 | case inflection::past_participle: return _verb->past_participle_form(); | 230 | }); |
43 | case inflection::ing_form: return _verb->ing_form(); | 231 | } else if (_type == token::type::fillin) |
44 | case inflection::s_form: return _verb->s_form(); | 232 | { |
233 | return false; | ||
234 | } else { | ||
235 | return true; | ||
45 | } | 236 | } |
46 | } | 237 | } |
47 | 238 | ||
48 | token* verb_token::copy() const | 239 | std::string token::compile() const |
240 | { | ||
241 | switch (_type) | ||
242 | { | ||
243 | case token::type::verb: | ||
244 | { | ||
245 | switch (_verb._infl) | ||
246 | { | ||
247 | case token::verb_inflection::infinitive: return _verb._verb.infinitive_form(); | ||
248 | case token::verb_inflection::past_tense: return _verb._verb.past_tense_form(); | ||
249 | case token::verb_inflection::past_participle: return _verb._verb.past_participle_form(); | ||
250 | case token::verb_inflection::ing_form: return _verb._verb.ing_form(); | ||
251 | case token::verb_inflection::s_form: return _verb._verb.s_form(); | ||
252 | } | ||
253 | } | ||
254 | |||
255 | case token::type::noun: | ||
256 | { | ||
257 | switch (_noun._infl) | ||
258 | { | ||
259 | case token::noun_inflection::singular: return _noun._noun.singular_form(); | ||
260 | case token::noun_inflection::plural: return _noun._noun.plural_form(); | ||
261 | } | ||
262 | } | ||
263 | |||
264 | case token::type::adjective: | ||
265 | { | ||
266 | switch (_adjective._infl) | ||
267 | { | ||
268 | case token::adjective_inflection::base: return _adjective._adjective.base_form(); | ||
269 | case token::adjective_inflection::comparative: return _adjective._adjective.comparative_form(); | ||
270 | case token::adjective_inflection::superlative: return _adjective._adjective.superlative_form(); | ||
271 | } | ||
272 | } | ||
273 | |||
274 | case token::type::adverb: | ||
275 | { | ||
276 | switch (_adverb._infl) | ||
277 | { | ||
278 | case token::adverb_inflection::base: return _adverb._adverb.base_form(); | ||
279 | case token::adverb_inflection::comparative: return _adverb._adverb.comparative_form(); | ||
280 | case token::adverb_inflection::superlative: return _adverb._adverb.superlative_form(); | ||
281 | } | ||
282 | } | ||
283 | |||
284 | case token::type::preposition: return _preposition._preposition.get_form(); | ||
285 | case token::type::string: return _string._str; | ||
286 | |||
287 | case token::type::fillin: | ||
288 | { | ||
289 | throw std::runtime_error("Cannot compile a fillin token."); | ||
290 | } | ||
291 | |||
292 | case token::type::utterance: | ||
293 | { | ||
294 | std::list<std::string> compiled; | ||
295 | std::transform(std::begin(_utterance._utterance), std::end(_utterance._utterance), std::back_inserter(compiled), [] (token tkn) { | ||
296 | return tkn.compile(); | ||
297 | }); | ||
298 | |||
299 | return verbly::implode(std::begin(compiled), std::end(compiled), " "); | ||
300 | } | ||
301 | } | ||
302 | } | ||
303 | |||
304 | token::token(verb _verb) : _type(type::verb) | ||
305 | { | ||
306 | new(&this->_verb._verb) verb(_verb); | ||
307 | this->_verb._infl = verb_inflection::infinitive; | ||
308 | } | ||
309 | |||
310 | token::token(verb _verb, verb_inflection _infl) : token(_verb) | ||
311 | { | ||
312 | this->_verb._infl = _infl; | ||
313 | } | ||
314 | |||
315 | token& token::operator=(verb _verb) | ||
316 | { | ||
317 | *this = token{_verb}; | ||
318 | |||
319 | return *this; | ||
320 | } | ||
321 | |||
322 | verb token::get_verb() const | ||
323 | { | ||
324 | assert(_type == type::verb); | ||
325 | |||
326 | return _verb._verb; | ||
327 | } | ||
328 | |||
329 | void token::set_verb(verb _verb) | ||
330 | { | ||
331 | assert(_type == type::verb); | ||
332 | |||
333 | this->_verb._verb = _verb; | ||
334 | } | ||
335 | |||
336 | token::verb_inflection token::get_verb_inflection() const | ||
337 | { | ||
338 | assert(_type == type::verb); | ||
339 | |||
340 | return _verb._infl; | ||
341 | } | ||
342 | |||
343 | void token::set_verb_inflection(verb_inflection _infl) | ||
344 | { | ||
345 | assert(_type == type::verb); | ||
346 | |||
347 | _verb._infl = _infl; | ||
348 | } | ||
349 | |||
350 | token::token(noun _noun) : _type(type::noun) | ||
351 | { | ||
352 | new(&this->_noun._noun) noun(_noun); | ||
353 | this->_noun._infl = noun_inflection::singular; | ||
354 | } | ||
355 | |||
356 | token::token(noun _noun, noun_inflection _infl) : token(_noun) | ||
357 | { | ||
358 | this->_noun._infl = _infl; | ||
359 | } | ||
360 | |||
361 | token& token::operator=(noun _noun) | ||
49 | { | 362 | { |
50 | return new verb_token(*this); | 363 | *this = token{_noun}; |
364 | |||
365 | return *this; | ||
366 | } | ||
367 | |||
368 | noun token::get_noun() const | ||
369 | { | ||
370 | assert(_type == type::noun); | ||
371 | |||
372 | return _noun._noun; | ||
373 | } | ||
374 | |||
375 | void token::set_noun(noun _noun) | ||
376 | { | ||
377 | assert(_type == type::noun); | ||
378 | |||
379 | this->_noun._noun = _noun; | ||
380 | } | ||
381 | |||
382 | token::noun_inflection token::get_noun_inflection() const | ||
383 | { | ||
384 | assert(_type == type::noun); | ||
385 | |||
386 | return _noun._infl; | ||
387 | } | ||
388 | |||
389 | void token::set_noun_inflection(noun_inflection _infl) | ||
390 | { | ||
391 | assert(_type == type::noun); | ||
392 | |||
393 | _noun._infl = _infl; | ||
394 | } | ||
395 | |||
396 | token::token(adjective _adjective) : _type(type::adjective) | ||
397 | { | ||
398 | new(&this->_adjective._adjective) adjective(_adjective); | ||
399 | this->_adjective._infl = adjective_inflection::base; | ||
400 | } | ||
401 | |||
402 | token::token(adjective _adjective, adjective_inflection _infl) : token(_adjective) | ||
403 | { | ||
404 | this->_adjective._infl = _infl; | ||
405 | } | ||
406 | |||
407 | token& token::operator=(adjective _adjective) | ||
408 | { | ||
409 | *this = token{_adjective}; | ||
410 | |||
411 | return *this; | ||
412 | } | ||
413 | |||
414 | adjective token::get_adjective() const | ||
415 | { | ||
416 | assert(_type == type::adjective); | ||
417 | |||
418 | return _adjective._adjective; | ||
419 | } | ||
420 | |||
421 | void token::set_adjective(adjective _adjective) | ||
422 | { | ||
423 | assert(_type == type::adjective); | ||
424 | |||
425 | this->_adjective._adjective = _adjective; | ||
426 | } | ||
427 | |||
428 | token::adjective_inflection token::get_adjective_inflection() const | ||
429 | { | ||
430 | assert(_type == type::adjective); | ||
431 | |||
432 | return _adjective._infl; | ||
433 | } | ||
434 | |||
435 | void token::set_adjective_inflection(adjective_inflection _infl) | ||
436 | { | ||
437 | assert(_type == type::adjective); | ||
438 | |||
439 | _adjective._infl = _infl; | ||
440 | } | ||
441 | |||
442 | token::token(adverb _adverb) : _type(type::adverb) | ||
443 | { | ||
444 | new(&this->_adverb._adverb) adverb(_adverb); | ||
445 | this->_adverb._infl = adverb_inflection::base; | ||
446 | } | ||
447 | |||
448 | token::token(adverb _adverb, adverb_inflection _infl) : token(_adverb) | ||
449 | { | ||
450 | this->_adverb._infl = _infl; | ||
451 | } | ||
452 | |||
453 | token& token::operator=(adverb _adverb) | ||
454 | { | ||
455 | *this = token{_adverb}; | ||
456 | |||
457 | return *this; | ||
458 | } | ||
459 | |||
460 | adverb token::get_adverb() const | ||
461 | { | ||
462 | assert(_type == type::adverb); | ||
463 | |||
464 | return _adverb._adverb; | ||
465 | } | ||
466 | |||
467 | void token::set_adverb(adverb _adverb) | ||
468 | { | ||
469 | assert(_type == type::adverb); | ||
470 | |||
471 | this->_adverb._adverb = _adverb; | ||
472 | } | ||
473 | |||
474 | token::adverb_inflection token::get_adverb_inflection() const | ||
475 | { | ||
476 | assert(_type == type::adverb); | ||
477 | |||
478 | return _adverb._infl; | ||
479 | } | ||
480 | |||
481 | void token::set_adverb_inflection(adverb_inflection _infl) | ||
482 | { | ||
483 | assert(_type == type::adverb); | ||
484 | |||
485 | _adverb._infl = _infl; | ||
486 | } | ||
487 | |||
488 | token::token(preposition _preposition) : _type(type::preposition) | ||
489 | { | ||
490 | new(&this->_preposition._preposition) preposition(_preposition); | ||
491 | } | ||
492 | |||
493 | token& token::operator=(preposition _preposition) | ||
494 | { | ||
495 | *this = token{_preposition}; | ||
496 | |||
497 | return *this; | ||
498 | } | ||
499 | |||
500 | preposition token::get_preposition() const | ||
501 | { | ||
502 | assert(_type == type::preposition); | ||
503 | |||
504 | return _preposition._preposition; | ||
505 | } | ||
506 | |||
507 | void token::set_preposition(preposition _preposition) | ||
508 | { | ||
509 | assert(_type == type::preposition); | ||
510 | |||
511 | this->_preposition._preposition = _preposition; | ||
512 | } | ||
513 | |||
514 | token::token(fillin_type _ft) : _type(type::fillin) | ||
515 | { | ||
516 | _fillin._type = _ft; | ||
517 | } | ||
518 | |||
519 | token& token::operator=(fillin_type _ft) | ||
520 | { | ||
521 | *this = token{_ft}; | ||
522 | |||
523 | return *this; | ||
524 | } | ||
525 | |||
526 | token::fillin_type token::get_fillin_type() const | ||
527 | { | ||
528 | assert(_type == type::fillin); | ||
529 | |||
530 | return _fillin._type; | ||
531 | } | ||
532 | |||
533 | void token::set_fillin_type(fillin_type _ft) | ||
534 | { | ||
535 | assert(_type == type::fillin); | ||
536 | |||
537 | _fillin._type = _ft; | ||
538 | } | ||
539 | |||
540 | token::token() : _type(type::utterance) | ||
541 | { | ||
542 | new(&_utterance._utterance) std::list<token>(); | ||
543 | } | ||
544 | |||
545 | token::token(std::initializer_list<token> _init) : _type(type::utterance) | ||
546 | { | ||
547 | new(&_utterance._utterance) std::list<token>(_init); | ||
548 | } | ||
549 | |||
550 | token::iterator token::begin() | ||
551 | { | ||
552 | assert(_type == type::utterance); | ||
553 | |||
554 | return _utterance._utterance.begin(); | ||
555 | } | ||
556 | |||
557 | token::iterator token::end() | ||
558 | { | ||
559 | assert(_type == type::utterance); | ||
560 | |||
561 | return _utterance._utterance.end(); | ||
562 | } | ||
563 | |||
564 | token& token::operator<<(token _tkn) | ||
565 | { | ||
566 | assert(_type == type::utterance); | ||
567 | |||
568 | _utterance._utterance.push_back(_tkn); | ||
569 | |||
570 | return *this; | ||
571 | } | ||
572 | |||
573 | void token::push_back(token _tkn) | ||
574 | { | ||
575 | assert(_type == type::utterance); | ||
576 | |||
577 | _utterance._utterance.push_back(_tkn); | ||
578 | } | ||
579 | |||
580 | void token::insert(iterator before, token _tkn) | ||
581 | { | ||
582 | assert(_type == type::utterance); | ||
583 | |||
584 | _utterance._utterance.insert(before, _tkn); | ||
585 | } | ||
586 | |||
587 | void token::replace(iterator torepl, token _tkn) | ||
588 | { | ||
589 | assert(_type == type::utterance); | ||
590 | |||
591 | _utterance._utterance.insert(torepl, _tkn); | ||
592 | _utterance._utterance.erase(torepl); | ||
593 | } | ||
594 | |||
595 | void token::erase(iterator toer) | ||
596 | { | ||
597 | assert(_type == type::utterance); | ||
598 | |||
599 | _utterance._utterance.erase(toer); | ||
600 | } | ||
601 | |||
602 | token::token(std::string _str) : _type(type::string) | ||
603 | { | ||
604 | new(&_string._str) std::string(_str); | ||
605 | } | ||
606 | |||
607 | token& token::operator=(std::string _str) | ||
608 | { | ||
609 | *this = token{_str}; | ||
610 | |||
611 | return *this; | ||
612 | } | ||
613 | |||
614 | std::string token::get_string() const | ||
615 | { | ||
616 | assert(_type == type::string); | ||
617 | |||
618 | return _string._str; | ||
619 | } | ||
620 | |||
621 | void token::set_string(std::string _str) | ||
622 | { | ||
623 | assert(_type == type::string); | ||
624 | |||
625 | _string._str = _str; | ||
51 | } | 626 | } |
52 | 627 | ||
53 | }; | 628 | }; |