-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
1205 lines (1037 loc) · 46.7 KB
/
parser.cpp
File metadata and controls
1205 lines (1037 loc) · 46.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cassert>
#include <typeinfo>
#include "utility.hpp"
#include "parser.hpp"
#include "internalizer.hpp"
using namespace std;
static bool is_plain_i7_or_documentation(lexical_state state) {
if (!state.get_comment_depth()) {
switch (state.get_superstate()) {
case I7:
case I7_EXTENSION_DOCUMENTATION:
case I7_IN_EXTRACT:
return true;
}
}
return false;
}
static bool is_in_plain_i7_or_documentation(token_iterator iterator) {
const token_sequence&source_text = iterator.get_owner();
return is_plain_i7_or_documentation(source_text.sum_over_interval(source_text.begin(), iterator).get_lexical_effect()(INITIAL_LEXICAL_STATE));
}
// With CYK filtering, the deduction rules for matches split into the following
// cases, with the noted antecedent structures and restrictions:
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// (sentence ending) (next) (sentence/passage --> production) (token available)
// 0. If the new match will be complete, its end condition must be satisfied.
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
// (sentence ending) (next) (sentence/passage --> production) (complete match)
// 0. If the new match will be complete, its end condition must be satisfied.
// Case IIa: Beginning of a continuing match with a token.
// (end of partial match) (next) (need --> production) (token available)
// 0. If the new match will be complete, its end condition must be satisfied.
// Case IIb: Beginning of a continuing match with a match already made.
// (end of partial match) (next) (need --> production) (complete match)
// 0. If the new match will be complete, its end condition must be satisfied.
// Case IIIa: Continuation of a match with a token.
// (end of partial match) (next) (token available)
// 0. If the new match will be complete, its end condition must be satisfied.
// 1. The new match's crossing condition must be satisfied.
// Case IIIb: Continuation of a match with another match.
// (end of partial match) (next) (complete match)
// 0. If the new match will be complete, its end condition must be satisfied.
// (1. The new match's crossing condition must be satisfied, but this is enforced by the nonterminal hierarchy.)
// Case IIIc: Continuation of a match with an epsilon.
// (end of partial match)
// 0. If the new match will be complete, its end condition must be satisfied.
// Case IV: Conditions 0 and 1 are enforced by guards calling can_reach_slot_count_at(...).
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// Case IIa: Beginning of a continuing match with a token.
static void begin_matches_with_token(vector<fact*>&results, typename ::session&session, const unordered_set<const production*>&productions, ::buffer*buffer, token_iterator position) {
assert(buffer);
for (const ::production*production : productions) {
for (unsigned slot_index : production->can_begin_with(position)) {
results.push_back(new potential_match{session, buffer, *production, slot_index + 1, position});
}
}
}
// Case IIb: Beginning of a continuing match with a match already made.
static void begin_matches_with_match(vector<fact*>&results, const unordered_set<const production*>&productions, const match&beginning_match) {
assert(beginning_match.is_filled());
const nonterminal&match_result = beginning_match.get_result();
for (const ::production*production : productions) {
for (unsigned slot_index : production->can_begin_with(match_result)) {
results.push_back(new potential_match{*production, slot_index + 1, beginning_match});
}
}
}
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
static void begin_matches_with_match(vector<fact*>&results, const intersection<const production*>&productions, const match&beginning_match) {
assert(beginning_match.is_filled());
const nonterminal&match_result = beginning_match.get_result();
for (const ::production*production : productions) {
for (unsigned slot_index : production->can_begin_with(match_result)) {
results.push_back(new potential_match{*production, slot_index + 1, beginning_match});
}
}
}
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
// Case IIb: Beginning of a continuing match with a match already made.
static void begin_matches_with_match(vector<fact*>&results, const unordered_set<const production*>&productions, token_iterator position) {
for (const annotation_wrapper&beginning_wrapper : position->get_annotations(typeid(match))) {
const match&candidate_beginning = dynamic_cast<const match&>(static_cast<const annotation&>(beginning_wrapper));
if (candidate_beginning.is_filled() && candidate_beginning.get_beginning() == position) {
for (const ::production*production : productions) {
for (unsigned slot_index : production->can_begin_with(candidate_beginning.get_result())) {
results.push_back(new potential_match{*production, slot_index + 1, candidate_beginning});
}
}
}
}
}
// Case IIb: Beginning of a continuing match with a match already made.
static void begin_matches_with_match(vector<fact*>&results, typename ::session&session, const match&partial_match, token_iterator position) {
unordered_set<const production*>candidates;
for (const parseme*alternative : partial_match.get_continuing_alternatives()) {
const unordered_set<const production*>&additional_candidates = session.get_productions_resulting_in(alternative);
candidates.insert(additional_candidates.begin(), additional_candidates.end());
}
begin_matches_with_match(results, candidates, position);
}
// Case IIIa: Continuation of a match with a token.
static void continue_matches_with_token(vector<fact*>&results, token_iterator previous_position, token_iterator next_position, bool assume_next_token_is_justified = false) {
for (const annotation_wrapper&wrapper : previous_position->get_annotations(typeid(match))) {
const match&candidate_prefix = dynamic_cast<const match&>(static_cast<const annotation&>(wrapper));
if (candidate_prefix.can_continue_with(next_position, assume_next_token_is_justified)) {
results.push_back(new potential_match{candidate_prefix, next_position, assume_next_token_is_justified});
}
}
}
// Case IIIb: Continuation of a match with another match.
static void continue_matches_with_match(vector<fact*>&results, const match&candidate_prefix, token_iterator next_position, bool assume_next_token_is_justified = false) {
for (const annotation_wrapper&addendum_wrapper : next_position->get_annotations(typeid(match))) {
const match&candidate_addendum = dynamic_cast<const match&>(static_cast<const annotation&>(addendum_wrapper));
if (candidate_prefix.can_continue_with(candidate_addendum, assume_next_token_is_justified)) {
results.push_back(new potential_match{candidate_prefix, candidate_addendum, assume_next_token_is_justified});
}
}
}
// Case IIIb: Continuation of a match with another match.
static void continue_matches_with_match(vector<fact*>&results, token_iterator previous_position, const match&candidate_addendum, bool assume_next_token_is_justified = false) {
for (const annotation_wrapper&beginning_wrapper : previous_position->get_annotations(typeid(match))) {
const match&candidate_prefix = dynamic_cast<const match&>(static_cast<const annotation&>(beginning_wrapper));
if (candidate_prefix.can_continue_with(candidate_addendum, assume_next_token_is_justified)) {
results.push_back(new potential_match{candidate_prefix, candidate_addendum});
}
}
}
// Case IIIb: Continuation of a match with another match.
static void continue_matches_with_match(vector<fact*>&results, token_iterator previous_position, token_iterator next_position, bool assume_next_token_is_justified = false) {
for (const annotation_wrapper&prefix_wrapper : previous_position->get_annotations(typeid(match))) {
const match&candidate_prefix = dynamic_cast<const match&>(static_cast<const annotation&>(prefix_wrapper));
if (candidate_prefix.get_inclusive_end() == previous_position) {
continue_matches_with_match(results, candidate_prefix, next_position, assume_next_token_is_justified);
}
}
}
token_available::token_available(typename ::session&session, ::buffer*buffer, token_iterator self) :
negative_annotation_fact{session},
buffer{buffer},
self{self} {
assert(self.can_increment());
}
token_available::token_available(typename ::session&session, token_iterator self) :
negative_annotation_fact{session},
buffer(nullptr),
self{self} {
assert(self.can_increment());
}
bool token_available::is_equal_to_instance_of_like_class(const base_class&other) const {
const token_available&cast = dynamic_cast<const token_available&>(other);
return self == cast.self;
}
vector<const fact_annotatable*>token_available::get_annotatables() const {
return {&*self};
}
void token_available::justification_hook() const {
assert(buffer);
if (!operator bool()) {
buffer->add_terminal_beginning(self);
}
negative_annotation_fact::justification_hook();
}
void token_available::unjustification_hook() const {
assert(buffer);
negative_annotation_fact::unjustification_hook();
if (!operator bool()) {
buffer->remove_terminal_beginning(self);
}
}
std::vector<fact*>token_available::get_immediate_consequences() const {
assert(buffer);
typename ::session&session = dynamic_cast<typename ::session&>(context);
token_iterator previous = ::previous(self);
vector<fact*>results;
if (previous != self) {
if (!previous.can_increment() || end_of_sentence{session, previous, true}) {
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// (sentence ending) handled by the conditional just above.
// (next) handled by the call to ::previous and the check on its return value.
// (sentence/passage --> production) handled by the call to get_sentence_beginnings() below.
// (token available) is the trigger.
begin_matches_with_token(results, session, session.get_sentence_beginnings(), buffer, self);
}
if (previous.can_increment()) {
// Case IIa: Beginning of a continuing match with a token.
// (end of partial match) handled by the call to get_continuing_beginnings(...) below.
// (next) handled by the call to ::previous and the check on its return value.
// (need --> production) handled by the call to get_continuing_beginnings(...) below.
// (token available) is the trigger.
begin_matches_with_token(results, session, session.get_continuing_beginnings(previous), buffer, self);
// Case IIIa: Continuation of a match with a token.
// (end of partial match) handled by the call to continue_matches_with_token(...) below.
// (next) handled by the call to ::previous and the check on its return value.
// (token available) is the trigger.
continue_matches_with_token(results, previous, self);
}
}
return results;
}
ostream&token_available::print(ostream&out) const {
return out << "available(" << *self << ")";
}
const base_class*token_available::clone() const {
return new token_available{dynamic_cast<typename ::session&>(context), buffer, self};
}
size_t token_available::hash() const {
return reinterpret_cast<size_t>(&*self);
}
next_token::next_token(typename ::session&session, ::buffer*buffer, token_iterator self, token_iterator next) :
annotation_fact{session},
buffer{buffer},
self{self},
next{next} {
assert(self.can_increment() || next.can_increment());
assert(self != next);
}
next_token::next_token(typename ::session&session, token_iterator self, token_iterator next) :
annotation_fact{session},
buffer{nullptr},
self{self},
next{next} {
assert(self.can_increment() || next.can_increment());
assert(self != next);
}
bool next_token::is_equal_to_instance_of_like_class(const base_class&other) const {
const next_token&cast = dynamic_cast<const next_token&>(other);
return (self == cast.self) && (next == cast.next);
}
vector<const fact_annotatable*>next_token::get_annotatables() const {
if (self.can_increment()) {
if (next.can_increment()) {
return {&*self, &*next};
}
return {&*self};
}
assert(next.can_increment());
return {&*next};
}
vector<fact*>next_token::get_immediate_consequences() const {
assert(buffer);
typename ::session&session = dynamic_cast<typename ::session&>(context);
vector<fact*>results;
if (next.can_increment() && token_available{session, next}) {
if (!self.can_increment() || end_of_sentence{session, self, true}) {
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// (sentence ending) handled by the conditional just above.
// (next) is the trigger.
// (sentence/passage --> production) handled by the call to get_sentence_beginnings() below.
// (token available) handled by the outer conditional.
begin_matches_with_token(results, session, session.get_sentence_beginnings(), buffer, next);
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
// (sentence ending) handled by the conditional just above.
// (next) is the trigger.
// (sentence/passage --> production) handled by the call to get_sentence_beginnings() below.
// (complete match) handled in begin_matches_with_match(...).
begin_matches_with_match(results, session.get_sentence_beginnings(), next);
}
if (self.can_increment()) {
// Case IIa: Beginning of a continuing match with a token.
// (end of partial match) handled by the call to get_continuing_beginnings(...) below.
// (next) is the trigger.
// (need --> production) handled by the call to get_continuing_beginnings(...) below.
// (token available) handled by the outer conditional.
begin_matches_with_token(results, session, session.get_continuing_beginnings(self), buffer, next);
// Case IIb: Beginning of a continuing match with a match already made.
// (end of partial match) handled by the call to get_continuing_beginnings(...) below.
// (next) is the trigger.
// (need --> production) handled by the call to get_continuing_beginnings(...) below.
// (complete match) handled in begin_matches_with_match(...).
begin_matches_with_match(results, session.get_continuing_beginnings(self), next);
// Case IIIa: Continuation of a match with a token.
// (end of partial match) handled by the call to continue_matches_with_token(...) below.
// (next) is the trigger.
// (token available) handled by the outer conditional.
continue_matches_with_token(results, self, next, true);
// Case IIIb: Continuation of a match with another match.
// (end of partial match) handled by the call to continue_matches_with_match(...) below.
// (next) is the trigger.
// (complete match) handled by the call to continue_matches_with_match(...) below.
continue_matches_with_match(results, self, next, true);
}
}
return results;
}
ostream&next_token::print(ostream&out) const {
out << "next(";
if (self.can_increment()) {
out << *self;
} else {
out << "<>";
}
out << ", ";
if (next.can_increment()) {
out << *next;
} else {
out << "<>";
}
return out << ")";
}
token_iterator next_token::get_self() const {
return self;
}
token_iterator next_token::get_next() const {
return next;
}
const base_class*next_token::clone() const {
return new next_token{dynamic_cast<typename ::session&>(context), buffer, self, next};
}
size_t next_token::hash() const {
return self.can_increment() ? reinterpret_cast<size_t>(&*self) : reinterpret_cast<size_t>(&*next) - 1;
}
// This function is saturating, and acts as the identity if the argument is the
// end of the sequence. This is to prevent accidental pointer chasing from the
// end of a source text to its beginnning.
token_iterator previous(const token_iterator&iterator) {
if (iterator.can_increment()) {
for (const annotation_wrapper&wrapper : iterator->get_annotations(typeid(next_token))) {
const next_token&link = dynamic_cast<const next_token&>(static_cast<const annotation&>(wrapper));
if (link.get_next() == iterator) {
return link.get_self();
}
assert(link.get_self() == iterator);
}
}
return iterator;
}
// This function is saturating, and acts as the identity if the argument is the
// end of the sequence. This is to prevent accidental pointer chasing from the
// beginnning of a source text to its end.
token_iterator next(const token_iterator&iterator) {
if (iterator.can_increment()) {
for (const annotation_wrapper&wrapper : iterator->get_annotations(typeid(next_token))) {
const next_token&link = dynamic_cast<const next_token&>(static_cast<const annotation&>(wrapper));
if (link.get_self() == iterator) {
return link.get_next();
}
assert(link.get_next() == iterator);
}
}
return iterator;
}
end_of_unit::end_of_unit(typename ::session&session, token_iterator self, bool in_the_positive_sense) :
combined_annotation_fact{session, in_the_positive_sense},
self{self} {
assert(self.can_increment());
}
bool end_of_unit::is_equal_to_instance_of_like_class(const base_class&other) const {
const end_of_unit&cast = dynamic_cast<const end_of_unit&>(other);
return
(in_the_positive_sense == cast.in_the_positive_sense) &&
(self == cast.self);
}
vector<const fact_annotatable*>end_of_unit::get_annotatables() const {
return {&*self};
}
token_iterator end_of_unit::get_self() const {
return self;
}
size_t end_of_unit::hash() const {
return reinterpret_cast<size_t>(&*self);
}
end_of_sentence::end_of_sentence(typename ::session&session, ::buffer*buffer, token_iterator self, bool in_the_positive_sense) :
end_of_unit{session, self, in_the_positive_sense},
buffer{buffer} {}
end_of_sentence::end_of_sentence(typename ::session&session, token_iterator self, bool in_the_positive_sense) :
end_of_unit{session, self, in_the_positive_sense},
buffer{nullptr} {}
void end_of_sentence::justification_hook() const {
assert(buffer);
end_of_unit::justification_hook();
buffer->add_sentence_ending(self);
}
void end_of_sentence::unjustification_hook() const {
assert(buffer);
buffer->remove_sentence_ending(self);
end_of_unit::unjustification_hook();
}
std::vector<fact*>end_of_sentence::get_immediate_consequences() const {
typename ::session&session = dynamic_cast<typename ::session&>(context);
vector<fact*>results;
if (in_the_positive_sense) {
token_iterator next = ::next(self);
if ((next != self) && next.can_increment() && token_available{session, next}) {
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// (sentence ending) is the trigger.
// (next) handled by the call to ::next and the check on its return value.
// (sentence/passage --> production) handled by the call to get_sentence_beginnings() below.
// (token available) handled by the conditional just above.
begin_matches_with_token(results, session, session.get_sentence_beginnings(), buffer, next);
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
// (sentence ending) is the trigger.
// (next) handled by the call to ::next and the check on its return value.
// (sentence/passage --> production) handled by the call to get_sentence_beginnings() below.
// (complete match) handled in begin_matches_with_match(...).
begin_matches_with_match(results, session.get_sentence_beginnings(), next);
}
}
// Case IV: Conditions 0 and 1 are enforced by guards calling can_reach_slot_count_at(...).
for (const annotation_wrapper&wrapper : self->get_annotations(typeid(potential_match))) {
const potential_match&candidate_match = dynamic_cast<const potential_match&>(static_cast<const annotation&>(wrapper));
const ::production&production = candidate_match.get_production();
unsigned slots_filled = candidate_match.get_slots_filled();
if (candidate_match.get_inclusive_end() == self && production.can_reach_slot_count_at(slots_filled, self, in_the_positive_sense) && !production.can_reach_slot_count_at(slots_filled, self, !in_the_positive_sense)) {
results.push_back(new match{candidate_match});
}
}
return results;
}
ostream&end_of_sentence::print(ostream&out) const {
return out << (in_the_positive_sense ? "end_of_sentence(" : "not_end_of_sentence(") << &(*self) << " = " << *self << ")";
}
const base_class*end_of_sentence::clone() const {
return new end_of_sentence{dynamic_cast<typename ::session&>(context), buffer, self, in_the_positive_sense};
}
bool parseme::accepts(const token_iterator&iterator) const {
return false;
}
bool parseme::accepts(const parseme&other) const {
return operator ==(other);
}
bool epsilon_terminal::accepts(const token_iterator&iterator) const {
return false;
}
const base_class*epsilon_terminal::clone() const {
return new epsilon_terminal{};
}
bool digits_terminal::accepts(const token_iterator&iterator) const {
for (i7_codepoint codepoint : *iterator->get_text()) {
if (!is_i7_digit(codepoint)) {
return false;
}
}
return true;
}
const base_class*digits_terminal::clone() const {
return new digits_terminal{};
}
bool word_terminal::accepts(const token_iterator&iterator) const {
return true;
}
const base_class*word_terminal::clone() const {
return new word_terminal{};
}
bool name_word_terminal::accepts(const token_iterator&iterator) const {
switch ((*iterator->get_text())[0]) {
case ',':
case '"':
case '(':
case ')':
return false;
}
return true;
}
const base_class*name_word_terminal::clone() const {
return new name_word_terminal{};
}
token_terminal::token_terminal(const i7_string&text) :
text{&vocabulary.acquire(text)} {}
token_terminal::~token_terminal() {
vocabulary.release(*text);
}
bool token_terminal::is_equal_to_instance_of_like_class(const base_class&other) const {
const token_terminal&cast = dynamic_cast<const token_terminal&>(other);
return text == cast.text;
}
const i7_string&token_terminal::get_text() const {
return *text;
}
bool token_terminal::accepts(const token_iterator&iterator) const {
return iterator->get_text() == text;
}
const base_class*token_terminal::clone() const {
return new token_terminal{*text};
}
size_t token_terminal::hash() const {
return reinterpret_cast<size_t>(text);
}
nonterminal::nonterminal(const i7_string&kind_name, unsigned tier) :
kind_name{&vocabulary.acquire(kind_name)},
tier{tier} {}
nonterminal::nonterminal(const nonterminal©) :
kind_name{&vocabulary.acquire(*copy.kind_name)},
tier{copy.tier} {}
nonterminal::nonterminal(const nonterminal©, unsigned replacement_tier) :
kind_name{&vocabulary.acquire(*copy.kind_name)},
tier{replacement_tier} {}
nonterminal::~nonterminal() {
vocabulary.release(*kind_name);
}
bool nonterminal::is_equal_to_instance_of_like_class(const base_class&other) const {
const nonterminal&cast = dynamic_cast<const nonterminal&>(other);
return (kind_name == cast.kind_name) && (tier == cast.tier);
}
const i7_string&nonterminal::get_kind_name() const {
return *kind_name;
}
unsigned nonterminal::get_tier() const {
return tier;
}
bool nonterminal::accepts(const parseme&other) const {
const nonterminal*cast = dynamic_cast<const nonterminal*>(&other);
return cast && (kind_name == cast->kind_name) && (tier >= cast->tier);
}
const base_class*nonterminal::clone() const {
return new nonterminal{*kind_name, tier};
}
size_t nonterminal::hash() const {
return reinterpret_cast<size_t>(kind_name) + tier;
}
production::production(typename ::session&session, const nonterminal&result, bool internal) :
fact{session},
result{dynamic_cast<const nonterminal*>(&parseme_bank.acquire(result))},
epsilon_prefix_length{0},
hash_value{0},
internal{internal} {}
production::production(const production©) :
fact{dynamic_cast<typename ::session&>(copy.context)},
result{dynamic_cast<const nonterminal*>(&parseme_bank.acquire(*copy.result))},
alternatives_sequence{copy.alternatives_sequence},
epsilon_prefix_length{copy.epsilon_prefix_length},
beginnings{copy.beginnings},
hash_value{copy.hash_value},
internal{copy.internal} {
for (const vector<const parseme*>&alternatives : alternatives_sequence) {
for (const parseme*alternative : alternatives) {
const parseme*internalization = &parseme_bank.acquire(*alternative);
assert(internalization == alternative);
(void)internalization;
}
}
}
production::~production() {
parseme_bank.release(*result);
for (const vector<const parseme*>&alternatives : alternatives_sequence) {
for (const parseme*alternative : alternatives) {
parseme_bank.release(*alternative);
}
}
}
bool production::is_equal_to_instance_of_like_class(const base_class&other) const {
const production&cast = dynamic_cast<const production&>(other);
return
(hash_value == cast.hash_value) && // for early rejection
(result == cast.result) &&
(alternatives_sequence == cast.alternatives_sequence);
}
std::vector<fact*>production::get_immediate_consequences() const {
typename ::session&session = dynamic_cast<typename ::session&>(context);
bool can_begin_sentence = this->can_begin_sentence();
vector<fact*>results;
for (const auto&i : session.get_buffers()) {
::buffer*buffer = i.second;
if (can_begin_sentence) {
unordered_set<const production*>affected_sentence_beginnings = session.get_sentence_beginnings_relying_on(*this);
for (token_iterator previous : buffer->get_sentence_endings()) {
token_iterator next = ::next(previous);
if (next.can_increment() && next != previous && token_available{session, next}) {
// Case Ia: Beginning of a match at the beginning of a sentence with a token.
// (sentence ending) handled by the two surrounding loops.
// (next) handled by the call to ::next and the check on its return value.
// (sentence/passage --> production) is the trigger, with the various subcases covered by get_sentence_beginnings_relying_on(...).
// (token available) handled by the conditional just above.
begin_matches_with_token(results, session, affected_sentence_beginnings, buffer, next);
// Case Ib: Beginning of a match at the beginning of a sentence with a match already made.
// (sentence ending) handled by the two surrounding loops.
// (next) handled by the call to ::next and the check on its return value.
// (sentence/passage --> production) is the trigger, with the various subcases covered by get_sentence_beginnings_relying_on(...).
// (complete match) handled in begin_matches_with_match(...).
begin_matches_with_match(results, affected_sentence_beginnings, next);
}
}
}
unordered_set<const production*>affected_result_beginnings = session.get_result_beginnings_relying_on(*this);
for (const match*prefix : buffer->get_partial_matches_needing(result)) {
token_iterator previous = prefix->get_inclusive_end();
token_iterator next = ::next(previous);
if (next.can_increment() && next != previous && token_available{session, next}) {
// Case IIa: Beginning of a continuing match with a token.
// (end of partial match) handled by get_partial_matches_needing(...). (It only needs to be called on the topmost result.)
// (next) handled by the call to ::next and the check on its return value.
// (need --> production) is the trigger, with the various subcases covered by get_result_beginnings_relying_on(...).
// (token available) handled by the conditional just above.
begin_matches_with_token(results, session, affected_result_beginnings, buffer, next);
// Case IIb: Beginning of a continuing match with a match already made.
// (end of partial match) handled by get_partial_matches_needing(...). (It only needs to be called on the topmost result.)
// (next) handled by the call to ::next and the check on its return value.
// (need --> production) is the trigger, with the various subcases covered by get_result_beginnings_relying_on(...).
// (complete match) handled in begin_matches_with_match(...).
begin_matches_with_match(results, affected_result_beginnings, next);
}
}
}
return results;
}
void production::add_slot() {
assert(alternatives_sequence.empty() || alternatives_sequence.back().size());
alternatives_sequence.push_back({});
hash_value <<= 1;
}
void production::add_alternative_to_last_slot(const parseme&alternative) {
assert(alternatives_sequence.size());
const parseme*internalization = &parseme_bank.acquire(alternative);
alternatives_sequence.back().push_back(internalization);
if (epsilon_prefix_length + 1 >= alternatives_sequence.size()) {
if (dynamic_cast<const epsilon_terminal*>(internalization)) {
epsilon_prefix_length = alternatives_sequence.size();
} else {
beginnings.push_back({static_cast<unsigned>(alternatives_sequence.size() - 1), internalization});
}
}
hash_value += reinterpret_cast<size_t>(internalization);
}
const nonterminal*production::get_result() const {
return result;
}
unsigned production::get_slot_count() const {
return alternatives_sequence.size();
}
const std::vector<production::possible_beginning>&production::get_beginnings() const {
return beginnings;
}
const vector<const parseme*>&production::get_alternatives(unsigned slot_index) const {
return alternatives_sequence[slot_index];
}
bool production::accepts(unsigned slot_index, const ::token_iterator&iterator) const {
assert(slot_index < alternatives_sequence.size());
for (const ::parseme*alternative : alternatives_sequence[slot_index]) {
if (alternative->accepts(iterator)) {
return true;
}
}
return false;
}
bool production::accepts(unsigned slot_index, const ::parseme&parseme) const {
assert(slot_index < alternatives_sequence.size());
for (const ::parseme*alternative : alternatives_sequence[slot_index]) {
if (alternative->accepts(parseme)) {
return true;
}
}
return false;
}
std::vector<unsigned>production::can_begin_with(const ::token_iterator&iterator) const {
std::vector<unsigned>results;
for (const possible_beginning&beginning : beginnings) {
if (beginning.parseme->accepts(iterator)) {
results.push_back(beginning.epsilon_count);
}
}
return results;
}
std::vector<unsigned>production::can_begin_with(const ::parseme&parseme) const {
std::vector<unsigned>results;
for (const possible_beginning&beginning : beginnings) {
if (beginning.parseme->accepts(parseme)) {
results.push_back(beginning.epsilon_count);
}
}
return results;
}
bool production::can_reach_slot_count_at(unsigned slot_count, token_iterator position) const {
return can_reach_slot_count_at(slot_count, position, !position.can_increment() || end_of_sentence{dynamic_cast<typename ::session&>(context), position, true});
}
size_t production::hash() const {
return hash_value;
}
subsentence::subsentence(typename ::session&session, const nonterminal&result, bool internal) :
production{session, result, internal} {}
subsentence::subsentence(const subsentence©) :
production{copy} {}
bool subsentence::can_begin_sentence() const {
typename ::session&session = dynamic_cast<typename ::session&>(context);
return session.can_begin_sentence_with(this);
}
void subsentence::justification_hook() const {
dynamic_cast<typename ::session&>(context).add_subsentence(*this);
}
void subsentence::unjustification_hook() const {
dynamic_cast<typename ::session&>(context).remove_subsentence(*this);
}
ostream&subsentence::print(ostream&out) const {
return out << "subsentence(" << ASSUME_EIGHT_BIT(result->get_kind_name()) << "...)";
}
subsentence::operator bool() const {
const unordered_set<const subsentence*>&subsentences = dynamic_cast<typename ::session&>(context).get_subsentences();
return subsentences.find(dynamic_cast<const subsentence*>(production_bank.lookup(*this))) != subsentences.end();
}
bool subsentence::can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const {
return (slot_count == alternatives_sequence.size()) || !assumed_end_of_sentence_state;
}
const base_class*subsentence::clone() const {
return new subsentence{*this};
}
wording::wording(typename ::session&session, const nonterminal&result, bool internal) :
production{session, result, internal} {}
wording::wording(const wording©) :
production{copy} {}
bool wording::can_begin_sentence() const {
typename ::session&session = dynamic_cast<typename ::session&>(context);
return session.can_begin_sentence_with(this);
}
void wording::justification_hook() const {
dynamic_cast<typename ::session&>(context).add_wording(*this);
}
void wording::unjustification_hook() const {
dynamic_cast<typename ::session&>(context).remove_wording(*this);
}
ostream&wording::print(ostream&out) const {
return out << "wording(" << ASSUME_EIGHT_BIT(result->get_kind_name()) << "...)";
}
wording::operator bool() const {
const unordered_set<const wording*>&wordings = dynamic_cast<typename ::session&>(context).get_wordings();
return wordings.find(dynamic_cast<const wording*>(production_bank.lookup(*this))) != wordings.end();
}
bool wording::can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const {
if ((slot_count == 1 || slot_count == alternatives_sequence.size()) &&
!is_in_plain_i7_or_documentation(position)) {
return false;
}
return !assumed_end_of_sentence_state;
}
const base_class*wording::clone() const {
return new wording{*this};
}
sentence::sentence(typename ::session&session, const nonterminal&result, bool internal) :
production{session, result, internal} {}
sentence::sentence(const sentence©) :
production{copy} {}
bool sentence::can_begin_sentence() const {
return true;
}
void sentence::justification_hook() const {
dynamic_cast<typename ::session&>(context).add_sentence(*this);
}
void sentence::unjustification_hook() const {
dynamic_cast<typename ::session&>(context).remove_sentence(*this);
}
ostream&sentence::print(ostream&out) const {
return out << "sentence(" << ASSUME_EIGHT_BIT(result->get_kind_name()) << "...)";
}
sentence::operator bool() const {
const unordered_set<const sentence*>&sentences = dynamic_cast<typename ::session&>(context).get_sentences();
return sentences.find(dynamic_cast<const sentence*>(production_bank.lookup(*this))) != sentences.end();
}
bool sentence::can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const {
if ((slot_count == 1 || slot_count == alternatives_sequence.size()) &&
!is_in_plain_i7_or_documentation(position)) {
return false;
}
return (slot_count == alternatives_sequence.size()) == assumed_end_of_sentence_state;
}
const base_class*sentence::clone() const {
return new sentence{*this};
}
passage::passage(typename ::session&session, const nonterminal&result, bool internal) :
production{session, result, internal} {}
passage::passage(const passage©) :
production{copy} {}
bool passage::can_begin_sentence() const {
return true;
}
void passage::justification_hook() const {
dynamic_cast<typename ::session&>(context).add_passage(*this);
}
void passage::unjustification_hook() const {
dynamic_cast<typename ::session&>(context).remove_passage(*this);
}
ostream&passage::print(ostream&out) const {
return out << "passage(" << ASSUME_EIGHT_BIT(result->get_kind_name()) << "...)";
}
passage::operator bool() const {
const unordered_set<const passage*>&passages = dynamic_cast<typename ::session&>(context).get_passages();
return passages.find(dynamic_cast<const passage*>(production_bank.lookup(*this))) != passages.end();
}
bool passage::can_reach_slot_count_at(unsigned slot_count, token_iterator position, bool assumed_end_of_sentence_state) const {
if ((slot_count == 1 || slot_count == alternatives_sequence.size()) &&
!is_in_plain_i7_or_documentation(position)) {
return false;
}
if (slot_count == alternatives_sequence.size()) {
return assumed_end_of_sentence_state;
}
return true;
}
const base_class*passage::clone() const {
return new passage{*this};
}
potential_match::potential_match(typename ::session&session, ::buffer*buffer, const ::production&production, unsigned slots_filled, const token_iterator beginning, const token_iterator inclusive_end) :
annotation_fact{session},
buffer{buffer},
production{&production_bank.acquire(production)},
slots_filled{slots_filled},
beginning{beginning},
inclusive_end{inclusive_end} {
assert(production);
}
potential_match::potential_match(typename ::session&session, ::buffer*buffer, const ::production&production, unsigned slots_filled, token_iterator beginning) :
annotation_fact{session},
buffer{buffer},
production{&production_bank.acquire(production)},
slots_filled{slots_filled},
beginning{beginning},
inclusive_end{beginning} {
assert(production);
assert(production.can_begin_with(beginning).size());
}
potential_match::potential_match(const potential_match©) :
annotation_fact{copy.context},
buffer{copy.buffer},
production{&production_bank.acquire(*copy.production)},
slots_filled{copy.slots_filled},
beginning{copy.beginning},
inclusive_end{copy.inclusive_end} {}
potential_match::~potential_match() {
production_bank.release(*production);
}
potential_match::potential_match(const potential_match&prefix, token_iterator inclusive_end, bool assume_next_token_is_justified) :
annotation_fact{prefix.context},
buffer{prefix.buffer},
production{&production_bank.acquire(*prefix.production)},
slots_filled{prefix.slots_filled + 1},
beginning{prefix.beginning},
inclusive_end{inclusive_end} {
assert(beginning != inclusive_end);
assert(assume_next_token_is_justified || prefix.inclusive_end == inclusive_end || next(prefix.inclusive_end) == inclusive_end);
}
potential_match::potential_match(const ::production&production, unsigned slots_filled, const potential_match&addendum) :
annotation_fact{addendum.context},
buffer{addendum.buffer},
production{&production_bank.acquire(production)},
slots_filled{slots_filled},
beginning{addendum.beginning},
inclusive_end{addendum.inclusive_end} {
assert(production);
assert(production.can_begin_with(addendum.get_result()).size());
}
potential_match::potential_match(const potential_match&prefix, const potential_match&addendum, bool assume_next_token_is_justified) :
annotation_fact{prefix.context},
buffer{prefix.buffer},
production{&production_bank.acquire(*prefix.production)},
slots_filled{prefix.slots_filled + 1},
beginning{prefix.beginning},
inclusive_end{addendum.inclusive_end} {
assert(&prefix.context == &addendum.context);
assert(prefix.buffer == addendum.buffer);
assert(prefix.inclusive_end != addendum.beginning);
assert(assume_next_token_is_justified || next(prefix.inclusive_end) == addendum.beginning);
}
bool potential_match::is_equal_to_instance_of_like_class(const base_class&other) const {
const potential_match&cast = dynamic_cast<const potential_match&>(other);
return
(slots_filled == cast.slots_filled) &&
(beginning == cast.beginning) &&
(inclusive_end == cast.inclusive_end) &&
(production == cast.production); // last because this is most expensive