-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
277 lines (251 loc) · 9.93 KB
/
buffer.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
270
271
272
273
274
275
276
277
#include <vector>
#include "buffer.hpp"
#include "lexical_highlights.hpp"
#include "io.hpp"
#include "parser.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 begins_with_a_digit(const i7_string*text) {
return text->size() && is_i7_digit((*text)[0]);
}
static bool ends_with_a_digit(const i7_string*text) {
return text->size() && is_i7_digit((*text)[text->size() - 1]);
}
static bool should_be_a_sentence_end(token_iterator position, token_iterator next) {
static const i7_string*FULL_STOP = &vocabulary.acquire(ENCODE("."));
static const i7_string*SEMICOLON = &vocabulary.acquire(ENCODE(";"));
static const i7_string*COLON = &vocabulary.acquire(ENCODE(":"));
if (next.can_increment() && (position->get_line_count() == 1) && next->get_line_count()) {
// Found a paragraph break.
return true;
}
const i7_string*text = position->get_text();
if (text == FULL_STOP || text == SEMICOLON || text == COLON) {
// Check that the sentence-ending punctuation is not an intra-KOV delimiter.
if (!next.can_increment() || !begins_with_a_digit(next->get_text()) || !position.can_decrement()) {
return true;
}
token_iterator previous = position;
--previous;
return !ends_with_a_digit(previous->get_text());
}
return false;
}
static token_iterator previous_by_skipping_whitespace(token_iterator position) {
token_iterator result = position;
do {
if (!result.can_decrement()) {
return result.move_to_end();
}
--result;
} while (result->is_only_whitespace());
return result;
}
static token_iterator next_by_skipping_whitespace(token_iterator position) {
token_iterator result = position;
while ((++result).can_increment() && result->is_only_whitespace());
return result;
}
void buffer::parser_rehighlight_handler(lexical_state beginning_state, token_iterator beginning, token_iterator end) {
// Step I: Make sure that negative and combined annotation facts are false.
for (token_iterator i = beginning; i != end; ++i) {
token_available available{owner, this, i};
available.surreptitiously_make_false();
assert(i->has_annotation(available));
end_of_sentence not_an_end{owner, this, i, false};
not_an_end.justify(); // Justify the negation.
assert(i->has_annotation(not_an_end));
}
// Step II: Correct observations on the edges.
if (beginning.can_decrement()) {
token_iterator previous = beginning;
--previous;
::end_of_sentence end_of_sentence{owner, this, previous, true};
if (should_be_a_sentence_end(previous, beginning)) {
if (!end_of_sentence) {
end_of_sentence.justify();
}
} else if (end_of_sentence) {
end_of_sentence.unjustify();
}
}
if (end.can_increment()) {
token_iterator next = end;
--next;
::end_of_sentence end_of_sentence{owner, this, end, true};
if (should_be_a_sentence_end(end, next)) {
if (!end_of_sentence) {
end_of_sentence.justify();
}
} else if (end_of_sentence) {
end_of_sentence.unjustify();
}
}
// Step IIIa: Make end-of-sentence observations true. (We do these first for performance reasons.)
lexical_state state = beginning_state;
for (monoid_sequence<token>::iterator i = beginning, j = i; i != end; i = j) {
++j;
// end_of_sentence
if (is_plain_i7_or_documentation(state)) {
if (should_be_a_sentence_end(i, j)) {
::end_of_sentence end_of_sentence{owner, this, i, true};
end_of_sentence.justify();
}
}
state = i->get_lexical_effect()(state);
}
// Step IIIb: Make other observations true.
state = beginning_state;
token_iterator previous = previous_by_skipping_whitespace(beginning);
bool previous_valid = (previous != beginning);
for (monoid_sequence<token>::iterator i = beginning, j = i; i != end; i = j) {
++j;
// token_available
token_available available{owner, this, i};
available.justify();
// next_token
if (!i->is_only_whitespace()) {
::next_token next_token{owner, this, previous, i};
next_token.justify();
assert(::previous(i) == previous);
previous = i;
previous_valid = true;
}
state = i->get_lexical_effect()(state);
}
if (end.can_decrement() && previous_valid) {
token_iterator inclusive_end = end;
--inclusive_end;
token_iterator next = next_by_skipping_whitespace(inclusive_end);
if (previous.can_increment() || next.can_increment()) {
::next_token next_token{owner, this, previous, next};
next_token.justify();
assert(!next.can_increment() || ::previous(next) == previous);
}
}
}
namespace {
struct highlight {
unsigned beginning_codepoint_index;
unsigned end_codepoint_index;
::highlight_code highlight_code;
};
}
static const highlight_code INVALID_HIGHLIGHT = 0xFFFFFFFF;
void buffer::rehighlight(const lexical_reference_points_from_edit&reference_points_from_edit) {
if (reference_points_from_edit.start_of_relexed_text == source_text.end()) {
return;
}
unsigned initial_codepoint_index = source_text.sum_over_interval(source_text.begin(), reference_points_from_edit.start_of_relexed_text).get_codepoint_count();
vector<highlight>new_highlights;
bool done_with_relexed_portion = false;
//
unsigned codepoint_index_before = initial_codepoint_index;
lexical_state old_lexical_state_before = reference_points_from_edit.old_post_relex_state;
lexical_state lexical_state_before = reference_points_from_edit.pre_relex_state;
unsigned highlight_codepoint_index_before = codepoint_index_before;
uint32_t highlight_before = INVALID_HIGHLIGHT;
monoid_sequence<token>::iterator i = reference_points_from_edit.start_of_relexed_text;
for (; i != source_text.end(); ++i) {
const lexer_monoid&lexical_effect = i->get_lexical_effect();
done_with_relexed_portion |= (i == reference_points_from_edit.end_of_relexed_text);
if (done_with_relexed_portion) {
if (old_lexical_state_before == lexical_state_before) {
break;
}
old_lexical_state_before = lexical_effect(old_lexical_state_before);
}
lexical_state lexical_state_after = lexical_effect(lexical_state_before);
uint32_t highlight_after = get_highlight_code(lexical_state_before, lexical_state_after);
if (highlight_before != highlight_after) {
if (highlight_before != INVALID_HIGHLIGHT) {
new_highlights.push_back({ highlight_codepoint_index_before, codepoint_index_before, highlight_before });
}
highlight_codepoint_index_before = codepoint_index_before;
}
codepoint_index_before += i->get_codepoint_count();
lexical_state_before = lexical_state_after;
highlight_before = highlight_after;
}
//
parser_rehighlight_handler(reference_points_from_edit.pre_relex_state, reference_points_from_edit.start_of_relexed_text, i);
//
if (highlight_codepoint_index_before < codepoint_index_before) {
new_highlights.push_back({ highlight_codepoint_index_before, codepoint_index_before, highlight_before });
}
remove_highlights(buffer_number, initial_codepoint_index, codepoint_index_before);
for (const ::highlight&highlight : new_highlights) {
add_highlight(buffer_number, highlight.beginning_codepoint_index, highlight.end_codepoint_index, highlight.highlight_code);
}
}
const unordered_set<token_iterator>&buffer::get_parseme_beginnings(const parseme&terminal) {
return parseme_beginnings[parseme_bank.lookup(terminal)];
}
void buffer::add_terminal_beginning(token_iterator beginning) {
parseme_beginnings.insert(&parseme_bank.acquire(token_terminal{*beginning->get_text()}), beginning);
}
void buffer::remove_terminal_beginning(token_iterator beginning) {
const parseme*key = parseme_bank.lookup(token_terminal{*beginning->get_text()});
parseme_beginnings.erase(key, beginning);
parseme_bank.release(*key);
}
void buffer::add_parseme_beginning(const ::parseme&parseme, token_iterator beginning) {
parseme_beginnings.insert(&parseme_bank.acquire(parseme), beginning);
}
void buffer::remove_parseme_beginning(const ::parseme&parseme, token_iterator beginning) {
const ::parseme*key = parseme_bank.lookup(parseme);
parseme_beginnings.erase(key, beginning);
parseme_bank.release(*key);
}
const unordered_set<token_iterator>&buffer::get_sentence_endings() const {
return sentence_endings;
}
void buffer::add_sentence_ending(token_iterator position) {
sentence_endings.insert(position);
}
void buffer::remove_sentence_ending(token_iterator position) {
sentence_endings.erase(position);
}
const unordered_set<const match*>&buffer::get_partial_matches_needing(const nonterminal*result) const {
return partial_matches_by_need[result];
}
void buffer::add_partial_match(const match*partial_match) {
for (const parseme*alternative : partial_match->get_continuing_alternatives()) {
const nonterminal*need = dynamic_cast<const nonterminal*>(alternative);
if (need) {
partial_matches_by_need.insert(need, partial_match);
}
}
}
void buffer::remove_partial_match(const match*partial_match) {
for (const parseme*alternative : partial_match->get_continuing_alternatives()) {
const nonterminal*need = dynamic_cast<const nonterminal*>(alternative);
if (need) {
partial_matches_by_need.erase(need, partial_match);
}
}
}
void buffer::remove_codepoints(unsigned beginning, unsigned end) {
rehighlight(::remove_codepoints(source_text, beginning, end));
}
void buffer::add_codepoints(unsigned beginning, const i7_string&insertion) {
rehighlight(::add_codepoints(source_text, beginning, insertion));
}
ostream&operator <<(ostream&out, const ::buffer&buffer) {
out << "BEGIN Buffer " << buffer.buffer_number << endl;
for (auto i = buffer.source_text.begin(), end = buffer.source_text.end(); i != end; ++i) {
out << "Annotations on " << *i << ":" << endl;
i->dump(out);
}
return out << "END Buffer " << buffer.buffer_number << endl;
}