-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.cpp
269 lines (233 loc) · 9.72 KB
/
session.cpp
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
#include <cassert>
#include "io.hpp"
#include "session.hpp"
#include "parser.hpp"
using namespace std;
const production*session::add_production(const ::production&production) {
const ::production*internalization = &production_bank.acquire(production);
for (const production::possible_beginning&beginning : internalization->get_beginnings()) {
productions_by_beginnings.insert(beginning.parseme, internalization);
}
productions_by_results.insert(internalization->get_result(), internalization);
return internalization;
}
const production*session::remove_production(const ::production&production) {
const ::production*internalization = production_bank.lookup(production);
assert(internalization);
for (const production::possible_beginning&beginning : internalization->get_beginnings()) {
productions_by_beginnings.erase(beginning.parseme, internalization);
}
productions_by_results.erase(internalization->get_result(), internalization);
production_bank.release(*internalization);
return internalization;
}
const session::buffer_map&session::get_buffers() const {
return buffers;
}
const unordered_set<const subsentence*>&session::get_subsentences() const {
return subsentences;
}
const unordered_set<const wording*>&session::get_wordings() const {
return wordings;
}
const unordered_set<const sentence*>&session::get_sentences() const {
return sentences;
}
const unordered_set<const passage*>&session::get_passages() const {
return passages;
}
void session::add_subsentence(const ::subsentence&subsentence) {
const ::subsentence*internalization = dynamic_cast<const ::subsentence*>(add_production(subsentence));
assert(subsentences.find(internalization) == subsentences.end());
subsentences.insert(internalization);
}
void session::add_wording(const ::wording&wording) {
const ::wording*internalization = dynamic_cast<const ::wording*>(add_production(wording));
assert(wordings.find(internalization) == wordings.end());
wordings.insert(internalization);
}
void session::add_sentence(const ::sentence&sentence) {
const ::sentence*internalization = dynamic_cast<const ::sentence*>(add_production(sentence));
assert(sentences.find(internalization) == sentences.end());
sentences.insert(internalization);
}
void session::add_passage(const ::passage&passage) {
const ::passage*internalization = dynamic_cast<const ::passage*>(add_production(passage));
assert(passages.find(internalization) == passages.end());
passages.insert(internalization);
}
void session::remove_subsentence(const ::subsentence&subsentence) {
const ::subsentence*internalization = dynamic_cast<const ::subsentence*>(remove_production(subsentence));
assert(subsentences.find(internalization) != subsentences.end());
subsentences.erase(internalization);
}
void session::remove_wording(const ::wording&wording) {
const ::wording*internalization = dynamic_cast<const ::wording*>(remove_production(wording));
assert(wordings.find(internalization) != wordings.end());
wordings.erase(internalization);
}
void session::remove_sentence(const ::sentence&sentence) {
const ::sentence*internalization = dynamic_cast<const ::sentence*>(remove_production(sentence));
assert(sentences.find(internalization) != sentences.end());
sentences.erase(internalization);
}
void session::remove_passage(const ::passage&passage) {
const ::passage*internalization = dynamic_cast<const ::passage*>(remove_production(passage));
assert(passages.find(internalization) != passages.end());
passages.erase(internalization);
}
const session::production_set&session::get_productions_beginning_with(const parseme*beginning) const {
return productions_by_beginnings[beginning];
}
const session::production_set&session::get_productions_resulting_in(const parseme*result) const {
return productions_by_results[result];
}
bool session::can_begin_descendant(const production*key, const production*root, unordered_set<const production*>&visited) const {
if (key == root) {
return true;
}
if (visited.insert(root).second) {
for (const production::possible_beginning&beginning : root->get_beginnings()) {
for (const production*production : get_productions_resulting_in(beginning.parseme)) {
if (can_begin_descendant(key, production, visited)) {
return true;
}
}
}
}
return false;
}
void session::get_additional_beginnings(const production*root, unordered_set<const production*>&visited) const {
if (visited.insert(root).second) {
for (const production::possible_beginning&beginning : root->get_beginnings()) {
for (const production*production : get_productions_resulting_in(beginning.parseme)) {
get_additional_beginnings(production, visited);
}
}
}
}
void session::get_additional_beginnings_relying_on(const production*root, unordered_map<const production*, unsigned>&visited, const production&crux, const function<bool(const production*)>&exemption) const {
if (exemption(root)) {
return;
}
unordered_map<const production*, unsigned>::iterator iterator = visited.find(root);
if (iterator == visited.end()) {
visited[root] = 1;
for (const production::possible_beginning&beginning : root->get_beginnings()) {
for (const production*production : get_productions_resulting_in(beginning.parseme)) {
get_additional_beginnings_relying_on(production, visited, crux, exemption);
}
}
} else {
++iterator->second;
}
}
unordered_set<const production*>session::get_beginnings_relying_on(const production&crux, const function<bool(const production*)>&exemption) const {
unordered_map<const production*, unsigned>visited;
get_additional_beginnings_relying_on(&crux, visited, crux, exemption);
visited.erase(&crux);
unordered_set<const production*>result;
result.insert(&crux);
for (const auto&visitation : visited) {
if (visitation.second == productions_by_beginnings[visitation.first->get_result()].size()) {
result.insert(visitation.first);
}
}
return result;
}
bool session::can_begin_sentence_with(const production*key) const {
if (dynamic_cast<const passage*>(key)) {
return true;
}
unordered_set<const production*>visited;
for (const production*root : sentences) {
if (can_begin_descendant(key, root, visited)) {
return true;
}
}
return false;
}
unordered_set<const production*>session::get_sentence_beginnings() const {
unordered_set<const production*>result;
for (const production*root : passages) {
result.insert(root);
}
for (const production*root : sentences) {
get_additional_beginnings(root, result);
}
return result;
}
unordered_set<const production*>session::get_sentence_beginnings_relying_on(const production&crux) const {
return get_beginnings_relying_on(crux, [] (const production*point) {
return dynamic_cast<const sentence*>(point) || dynamic_cast<const passage*>(point);
});
}
unordered_set<const production*>session::get_result_beginnings_relying_on(const production&crux) const {
const nonterminal*exempt_result = crux.get_result();
return get_beginnings_relying_on(crux, [exempt_result] (const production*point) {
return point->get_result() == exempt_result;
});
}
unordered_set<const production*>session::get_continuing_beginnings(const potential_match&partial_match) const {
unordered_set<const production*>result;
for (const parseme*alternative : partial_match.get_continuing_alternatives()) {
for (const production*root : get_productions_resulting_in(alternative)) {
get_additional_beginnings(root, result);
}
}
return result;
}
unordered_set<const production*>session::get_continuing_beginnings(token_iterator inclusive_end_of_matches) const {
unordered_set<const production*>result;
for (const annotation_wrapper&wrapper : inclusive_end_of_matches->get_annotations(typeid(potential_match))) {
const potential_match&partial_match = dynamic_cast<const potential_match&>(static_cast<const annotation&>(wrapper));
if (!partial_match.is_complete() && partial_match.get_inclusive_end() == inclusive_end_of_matches) {
for (const parseme*alternative : partial_match.get_continuing_alternatives()) {
for (const production*root : get_productions_resulting_in(alternative)) {
get_additional_beginnings(root, result);
}
}
}
}
return result;
}
void session::discard_buffer(unsigned buffer_number) {
buffer_map::iterator iterator = buffers.find(buffer_number);
if (iterator != buffers.end()) {
delete iterator->second;
buffers.erase(iterator);
}
}
void session::introduce_buffer(unsigned buffer_number) {
buffers.insert({buffer_number, new buffer{*this, buffer_number}});
}
void session::remove_codepoints(unsigned buffer_number, unsigned beginning, unsigned end) {
buffer_map::iterator i = buffers.find(buffer_number);
assert(i != buffers.end());
i->second->remove_codepoints(beginning, end);
}
void session::add_codepoints(unsigned buffer_number, unsigned beginning, const i7_string&insertion) {
buffer_map::iterator i = buffers.find(buffer_number);
assert(i != buffers.end());
i->second->add_codepoints(beginning, insertion);
}
typename ::session*session = nullptr;
void discard_buffer(unsigned buffer_number) {
session->discard_buffer(buffer_number);
}
void introduce_buffer(unsigned buffer_number) {
session->introduce_buffer(buffer_number);
}
void remove_codepoints(unsigned buffer_number, unsigned beginning, unsigned end) {
session->remove_codepoints(buffer_number, beginning, end);
}
void add_codepoints(unsigned buffer_number, unsigned beginning, const i7_string&insertion) {
session->add_codepoints(buffer_number, beginning, insertion);
}
ostream&operator <<(ostream&out, const typename ::session&session) {
out << "BEGIN Session" << endl;
for (const auto&buffer_mapping : session.buffers) {
out << *buffer_mapping.second;
}
return out << "END Session" << endl;
}