forked from y-scope/log-surgeon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.tpp
More file actions
419 lines (403 loc) · 16.7 KB
/
Copy pathLexer.tpp
File metadata and controls
419 lines (403 loc) · 16.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
#ifndef LOG_SURGEON_LEXER_TPP
#define LOG_SURGEON_LEXER_TPP
#include <cassert>
#include <memory>
#include <stack>
#include <stdexcept>
#include <string>
#include <vector>
#include <log_surgeon/Aliases.hpp>
#include <log_surgeon/Constants.hpp>
#include <log_surgeon/finite_automata/RegexAST.hpp>
/**
* utf8 format (https://en.wikipedia.org/wiki/UTF-8)
* 1 byte: 0x0 - 0x80 : 0xxxxxxx
* 2 byte: 0x80 - 0x7FF : 110xxxxx 10xxxxxx
* 3 byte: 0x800 - 0xFFFF : 1110xxxx 10xxxxxx 10xxxxxx
* 4 byte: 0x10000 - 0x1FFFFF : 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
namespace log_surgeon {
template <typename TypedNfaState, typename TypedDfaState>
auto Lexer<TypedNfaState, TypedDfaState>::flip_states(uint32_t old_storage_size) -> void {
if (m_match_pos >= old_storage_size / 2) {
m_match_pos -= old_storage_size / 2;
} else {
m_match_pos += old_storage_size / 2;
}
// TODO when m_start_pos == old_storage_size / 2, theres two possible cases
// currently so both options are potentially wrong
if (m_start_pos > old_storage_size / 2) {
m_start_pos -= old_storage_size / 2;
} else {
m_start_pos += old_storage_size / 2;
}
if (m_last_match_pos >= old_storage_size / 2) {
m_last_match_pos -= old_storage_size / 2;
} else {
m_last_match_pos += old_storage_size / 2;
}
}
template <typename TypedNfaState, typename TypedDfaState>
auto Lexer<TypedNfaState, TypedDfaState>::scan(ParserInputBuffer& input_buffer, Token& token)
-> ErrorCode {
auto const* state = m_dfa->get_root();
if (m_asked_for_more_data) {
state = m_prev_state;
m_asked_for_more_data = false;
} else {
if (m_match) {
m_match = false;
m_last_match_pos = m_match_pos;
m_last_match_line = m_match_line;
token
= Token{m_start_pos,
m_match_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_match_line,
m_type_ids};
return ErrorCode::Success;
}
m_start_pos = input_buffer.storage().pos();
m_match_pos = input_buffer.storage().pos();
m_match_line = m_line;
m_type_ids = nullptr;
}
while (true) {
auto prev_byte_buf_pos = input_buffer.storage().pos();
auto next_char{utf8::cCharErr};
if (auto const err = input_buffer.get_next_character(next_char); ErrorCode::Success != err)
{
m_asked_for_more_data = true;
m_prev_state = state;
return err;
}
if ((m_is_delimiter[next_char] || input_buffer.log_fully_consumed() || !m_has_delimiters)
&& state->is_accepting())
{
m_match = true;
m_type_ids = &(state->get_matching_variable_ids());
m_match_pos = prev_byte_buf_pos;
m_match_line = m_line;
}
auto* next = state->next(next_char);
if (next_char == '\n') {
m_line++;
if (m_has_delimiters && !m_match) {
next = m_dfa->get_root()->next(next_char);
m_match = true;
m_type_ids = &(next->get_matching_variable_ids());
m_start_pos = prev_byte_buf_pos;
m_match_pos = input_buffer.storage().pos();
m_match_line = m_line;
}
}
if (input_buffer.log_fully_consumed() || next == nullptr) {
if (m_match) {
input_buffer.set_log_fully_consumed(false);
input_buffer.set_pos(m_match_pos);
m_line = m_match_line;
if (m_last_match_pos != m_start_pos) {
token
= Token{m_last_match_pos,
m_start_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_last_match_line,
&cTokenUncaughtStringTypes};
return ErrorCode::Success;
}
m_match = false;
m_last_match_pos = m_match_pos;
m_last_match_line = m_match_line;
token
= Token{m_start_pos,
m_match_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_match_line,
m_type_ids};
return ErrorCode::Success;
}
if (input_buffer.log_fully_consumed() && m_start_pos == input_buffer.storage().pos()) {
if (m_last_match_pos != m_start_pos) {
m_match_pos = input_buffer.storage().pos();
m_type_ids = &cTokenEndTypes;
m_match = true;
token
= Token{m_last_match_pos,
m_start_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_last_match_line,
&cTokenUncaughtStringTypes};
return ErrorCode::Success;
}
token
= Token{input_buffer.storage().pos(),
input_buffer.storage().pos(),
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_line,
&cTokenEndTypes};
return ErrorCode::Success;
}
// TODO: remove timestamp from m_is_fist_char so that m_is_delimiter
// check not needed
while (input_buffer.log_fully_consumed() == false
&& (m_is_first_char[next_char] == false || m_is_delimiter[next_char] == false))
{
prev_byte_buf_pos = input_buffer.storage().pos();
if (ErrorCode err = input_buffer.get_next_character(next_char);
ErrorCode::Success != err)
{
m_asked_for_more_data = true;
m_prev_state = state;
return err;
}
}
input_buffer.set_pos(prev_byte_buf_pos);
m_start_pos = prev_byte_buf_pos;
state = m_dfa->get_root();
continue;
}
state = next;
}
}
// TODO: this is duplicating almost all the code of scan()
template <typename TypedNfaState, typename TypedDfaState>
auto Lexer<TypedNfaState, TypedDfaState>::scan_with_wildcard(
ParserInputBuffer& input_buffer,
char wildcard,
Token& token
) -> ErrorCode {
auto const* state = m_dfa->get_root();
if (m_asked_for_more_data) {
state = m_prev_state;
m_asked_for_more_data = false;
} else {
if (m_match) {
m_match = false;
m_last_match_pos = m_match_pos;
m_last_match_line = m_match_line;
token
= Token{m_start_pos,
m_match_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_match_line,
m_type_ids};
return ErrorCode::Success;
}
m_start_pos = input_buffer.storage().pos();
m_match_pos = input_buffer.storage().pos();
m_match_line = m_line;
m_type_ids = nullptr;
}
while (true) {
auto prev_byte_buf_pos = input_buffer.storage().pos();
unsigned char next_char{utf8::cCharErr};
if (ErrorCode err = input_buffer.get_next_character(next_char); ErrorCode::Success != err) {
m_asked_for_more_data = true;
m_prev_state = state;
return err;
}
if ((m_is_delimiter[next_char] || input_buffer.log_fully_consumed() || !m_has_delimiters)
&& state->is_accepting())
{
m_match = true;
m_type_ids = &(state->get_matching_variable_ids());
m_match_pos = prev_byte_buf_pos;
m_match_line = m_line;
}
TypedDfaState const* next = state->next(next_char);
if (next_char == '\n') {
m_line++;
if (m_has_delimiters && !m_match) {
next = m_dfa->get_root()->next(next_char);
m_match = true;
m_type_ids = &(next->get_matching_variable_ids());
m_start_pos = prev_byte_buf_pos;
m_match_pos = input_buffer.storage().pos();
m_match_line = m_line;
}
}
if (input_buffer.log_fully_consumed() || next == nullptr) {
assert(input_buffer.log_fully_consumed());
if (!m_match || (m_match && m_match_pos != input_buffer.storage().pos())) {
token
= Token{m_last_match_pos,
input_buffer.storage().pos(),
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_last_match_line,
&cTokenUncaughtStringTypes};
return ErrorCode::Success;
}
if (m_match) {
// BFS (keep track of m_type_ids)
if (wildcard == '?') {
for (uint32_t byte = 0; byte < cSizeOfByte; byte++) {
auto* next_state = state->next(byte);
if (next_state->is_accepting() == false) {
token
= Token{m_last_match_pos,
input_buffer.storage().pos(),
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_last_match_line,
&cTokenUncaughtStringTypes};
return ErrorCode::Success;
}
}
} else if (wildcard == '*') {
std::stack<TypedDfaState const*> unvisited_states;
std::set<TypedDfaState const*> visited_states;
unvisited_states.push(state);
while (!unvisited_states.empty()) {
TypedDfaState const* current_state = unvisited_states.top();
if (current_state == nullptr || current_state->is_accepting() == false) {
token
= Token{m_last_match_pos,
input_buffer.storage().pos(),
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_last_match_line,
&cTokenUncaughtStringTypes};
return ErrorCode::Success;
}
unvisited_states.pop();
visited_states.insert(current_state);
for (uint32_t byte = 0; byte < cSizeOfByte; byte++) {
if (m_is_delimiter[byte]) {
continue;
}
TypedDfaState const* next_state = current_state->next(byte);
if (visited_states.find(next_state) == visited_states.end()) {
unvisited_states.push(next_state);
}
}
}
}
input_buffer.set_pos(m_match_pos);
m_line = m_match_line;
m_match = false;
m_last_match_pos = m_match_pos;
m_last_match_line = m_match_line;
token
= Token{m_start_pos,
m_match_pos,
input_buffer.storage().get_active_buffer(),
input_buffer.storage().size(),
m_match_line,
m_type_ids};
return ErrorCode::Success;
}
}
state = next;
}
}
template <typename TypedNfaState, typename TypedDfaState>
auto Lexer<TypedNfaState, TypedDfaState>::increase_buffer_capacity(ParserInputBuffer& input_buffer
) -> void {
uint32_t old_storage_size{0};
bool flipped_static_buffer{false};
input_buffer.increase_capacity(old_storage_size, flipped_static_buffer);
if (old_storage_size < input_buffer.storage().size()) {
if (flipped_static_buffer) {
flip_states(old_storage_size);
}
if (0 == m_last_match_pos) {
m_last_match_pos = old_storage_size;
m_start_pos = old_storage_size;
}
}
}
template <typename TypedNfaState, typename TypedDfaState>
void Lexer<TypedNfaState, TypedDfaState>::reset() {
m_last_match_pos = 0;
m_match = false;
m_line = 0;
m_match_pos = 0;
m_start_pos = 0;
m_match_line = 0;
m_last_match_line = 0;
m_type_ids = nullptr;
m_asked_for_more_data = false;
m_prev_state = nullptr;
}
template <typename TypedNfaState, typename TypedDfaState>
void Lexer<TypedNfaState, TypedDfaState>::prepend_start_of_file_char(ParserInputBuffer& input_buffer
) {
m_prev_state = m_dfa->get_root()->next(utf8::cCharStartOfFile);
m_asked_for_more_data = true;
m_start_pos = input_buffer.storage().pos();
m_match_pos = input_buffer.storage().pos();
m_match_line = m_line;
m_type_ids = nullptr;
}
template <typename TypedNfaState, typename TypedDfaState>
void Lexer<TypedNfaState, TypedDfaState>::add_delimiters(std::vector<uint32_t> const& delimiters) {
assert(!delimiters.empty());
m_has_delimiters = true;
for (auto& i : m_is_delimiter) {
i = false;
}
for (auto delimiter : delimiters) {
m_is_delimiter[delimiter] = true;
}
m_is_delimiter[utf8::cCharStartOfFile] = true;
}
template <typename TypedNfaState, typename TypedDfaState>
void Lexer<TypedNfaState, TypedDfaState>::add_rule(
rule_id_t const rule_id,
std::unique_ptr<finite_automata::RegexAST<TypedNfaState>> rule
) {
m_rules.emplace_back(rule_id, std::move(rule));
}
template <typename TypedNfaState, typename TypedDfaState>
auto Lexer<TypedNfaState, TypedDfaState>::get_rule(rule_id_t const rule_id
) -> finite_automata::RegexAST<TypedNfaState>* {
for (auto const& rule : m_rules) {
if (rule.get_variable_id() == rule_id) {
return rule.get_regex();
}
}
return nullptr;
}
template <typename TypedNfaState, typename TypedDfaState>
void Lexer<TypedNfaState, TypedDfaState>::generate() {
for (auto const& rule : m_rules) {
for (auto const* capture : rule.get_captures()) {
std::string const capture_name{capture->get_name()};
if (m_symbol_id.contains(capture_name)) {
throw std::invalid_argument("`m_rules` contains capture names that are not unique."
);
}
auto const capture_id{m_symbol_id.size()};
m_symbol_id.emplace(capture_name, capture_id);
m_id_symbol.emplace(capture_id, capture_name);
auto const rule_id{rule.get_variable_id()};
m_rule_id_to_capture_ids.try_emplace(rule_id);
m_rule_id_to_capture_ids.at(rule_id).push_back(capture_id);
}
}
finite_automata::Nfa<TypedNfaState> nfa{m_rules};
for (auto const& [capture, tag_id_pair] : nfa.get_capture_to_tag_id_pair()) {
std::string const capture_name{capture->get_name()};
auto const capture_id{m_symbol_id.at(capture_name)};
m_capture_id_to_tag_id_pair.emplace(capture_id, tag_id_pair);
}
// TODO: DFA ignores captures. E.g., treats "capture:user=(?<user_id>\d+)" as "capture:user=\d+"
m_dfa = std::make_unique<finite_automata::Dfa<TypedDfaState>>(std::move(nfa));
auto const* state = m_dfa->get_root();
for (uint32_t i = 0; i < cSizeOfByte; i++) {
if (state->next(i) != nullptr) {
m_is_first_char[i] = true;
} else {
m_is_first_char[i] = false;
}
}
}
} // namespace log_surgeon
#endif // LOG_SURGEON_LEXER_TPP