-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathparse.inl
588 lines (480 loc) · 14.6 KB
/
parse.inl
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
#pragma once
// If building this file into a library, #define PCRE_LINKAGE as nothing.
// That will set external linkage.
#ifndef PCRE_LINKAGE
#define PCRE_LINKAGE inline
#endif
#include "pcre.hxx"
#include <cassert>
#include <cstdarg>
namespace pcre {
PCRE_LINKAGE std::string format(const char* pattern, ...) {
va_list args;
va_start(args, pattern);
va_list args_copy;
va_copy(args_copy, args);
int len = std::vsnprintf(nullptr, 0, pattern, args);
std::string result(len, ' ');
std::vsnprintf((char*)result.data(), len + 1, pattern, args_copy);
va_end(args_copy);
va_end(args);
return result;
}
PCRE_LINKAGE void print_ast(const node_t* node, int indent) {
for(int i = 0; i < indent; ++i)
printf(" ");
switch(node->kind) {
case node_t::kind_char:
printf("char = %c\n", node->c);
break;
case node_t::kind_range:
printf("range = %c-%c\n", node->c_min, node->c_max);
break;
case node_t::kind_any:
printf("any (.)\n");
break;
case node_t::kind_meta:
printf("[metachar]\n");
break;
case node_t::kind_boundary:
printf("word-boundary\n");
break;
case node_t::kind_cclass:
printf("[character-class]:\n");
break;
case node_t::kind_opt:
printf("opt:\n");
break;
case node_t::kind_star:
printf("star:\n");
break;
case node_t::kind_plus:
printf("plus:\n");
break;
case node_t::kind_quant:
printf("quant: {%d, %d}:\n", node->r_min, node->r_max);
break;
case node_t::kind_capture:
printf("capture (%d):\n", node->capture_index);
break;
case node_t::kind_seq:
printf("seq:\n");
break;
case node_t::kind_alt:
printf("alt:\n");
break;
}
for(const node_ptr_t& child : node->children)
print_ast(child.get(), indent + 1);
}
struct metachar_kind_name_t {
metachar_func_t func;
const char* name;
};
PCRE_LINKAGE const metachar_kind_name_t metachar_kind_names[] {
{ metachar_func_t::iscntrl, "[:cntrl:]" },
{ metachar_func_t::isprint, "[:print:]" },
{ metachar_func_t::isspace, "[:space:]" },
{ metachar_func_t::isblank, "[:blank:]" },
{ metachar_func_t::isgraph, "[:graph:]" },
{ metachar_func_t::ispunct, "[:punct:]" },
{ metachar_func_t::isalnum, "[:alnum:]" },
{ metachar_func_t::isalpha, "[:alpha:]" },
{ metachar_func_t::isupper, "[:upper:]" },
{ metachar_func_t::islower, "[:lower:]" },
{ metachar_func_t::isdigit, "[:digit:]" },
{ metachar_func_t::isxdigit, "[:xdigit:]" },
{ metachar_func_t::isword, "[:word:]" },
};
struct result_base_t {
const char* next;
node_ptr_t attr;
result_base_t() : next(nullptr) { }
};
struct result_parse_t : protected result_base_t {
result_parse_t() : success(false) { }
result_parse_t(const char* next, node_ptr_t attr) : success(true) {
this->next = next;
this->attr = std::move(attr);
}
explicit operator bool() const {
return success;
}
// Access the attr and next members through this operator. This checks
// success to confirm we're using the result correctly.
result_base_t* operator->() {
assert(success);
return this;
}
private:
bool success;
};
PCRE_LINKAGE result_parse_t make_result(const char* next, node_ptr_t node) {
return result_parse_t(next, std::move(node));
}
struct grammar_t {
// character class.
result_parse_t parse_cclass_item(const char* pattern);
result_parse_t parse_cclass_char(const char* pattern, bool expect = false);
result_parse_t parse_escape(const char* pattern);
result_parse_t parse_metachar(const char* pattern);
// Productions in highest-to-lowest precedence.
result_parse_t parse_term(const char* pattern, bool expect);
result_parse_t parse_duplication(const char* pattern, bool expect = false);
result_parse_t parse_concat(const char* pattern, bool expect);
result_parse_t parse_alternation(const char* pattern, bool expect,
bool topmost = false);
void throw_error(const char* pattern, const char* msg);
const char* begin = nullptr;
int capture_count = 0;
};
PCRE_LINKAGE result_parse_t grammar_t::parse_cclass_item(const char* pattern) {
// Support escapes here?
result_parse_t a = parse_cclass_char(pattern);
if(a) {
pattern = a->next;
if('-' == *pattern) {
result_parse_t b = parse_cclass_char(pattern + 1, true);
a->attr->kind = node_t::kind_range;
a->attr->c_max = b->attr->c;
a->next = b->next;
if(a->attr->c_min > a->attr->c_max)
throw_error(pattern, "invalid range: max must be greater than min");
}
}
return a;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_cclass_char(const char* pattern,
bool expect) {
result_parse_t result;
switch(char c = *pattern) {
case '\0':
case ']':
break;
case '\\':
result = parse_escape(pattern);
break;
default: {
auto char_ = std::make_unique<node_t>(node_t::kind_char);
char_->c = c;
result = make_result(pattern + 1, std::move(char_));
break;
}
}
return result;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_escape(const char* pattern) {
result_parse_t result;
if('\\' == *pattern++) {
char32_t c2 = 0;
switch(char c = *pattern++) {
// C escapes.
case 'a': c2 = '\a'; break;
case 'b': c2 = '\b'; break;
case 'f': c2 = '\f'; break;
case 'n': c2 = '\n'; break;
case 'r': c2 = '\r'; break;
case 't': c2 = '\t'; break;
case 'v': c2 = '\v'; break;
// regex escapes.
case '.':
case '*':
case '+':
case '?':
case '|':
case ')':
case '(':
case '[':
case ']':
case '{':
case '}':
case '/':
case '\\': c2 = c; break;
default:
throw_error(pattern - 2, "escape not recognized");
break;
}
if(c2) {
auto char_ = std::make_unique<node_t>(node_t::kind_char);
char_->c = c2;
result = make_result(pattern, std::move(char_));
}
}
return result;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_metachar(const char* pattern) {
result_parse_t result;
switch(char c = *pattern++) {
case '\\': {
char c = *pattern++;
metachar_func_t func { };
switch(c) {
case 'd':
case 'D':
func = metachar_func_t::isdigit;
break;
case 'w':
case 'W':
func = metachar_func_t::isword;
break;
case 's':
case 'S':
func = metachar_func_t::isspace;
break;
case 'b':
case 'B': {
// Word-boundary is handled specially.
auto boundary = std::make_unique<node_t>(node_t::kind_boundary);
boundary->negate = isupper(c);
return make_result(pattern, std::move(boundary));
}
}
if(metachar_func_t::none != func) {
auto node = std::make_unique<node_t>(node_t::kind_meta);
node->negate = isupper(c);
node->metachar_func = func;
result = make_result(pattern, std::move(node));
}
break;
}
case '[': {
--pattern;
int len1 = strlen(pattern);
for(auto metachar : metachar_kind_names) {
int len2 = strlen(metachar.name);
if(len2 <= len1 && !memcmp(pattern, metachar.name, len2)) {
auto node = std::make_unique<node_t>(node_t::kind_meta);
node->negate = false;
node->metachar_func = metachar.func;
result = make_result(pattern + len2, std::move(node));
break;
}
}
break;
}
default:
break;
}
return result;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_term(const char* pattern,
bool expect) {
result_parse_t result;
switch(*pattern) {
case '\0':
case '|':
case ')':
case ']':
case '}':
case '+':
case '*':
case '?':
if(expect)
throw_error(pattern, "unexpected token where term expected");
break;
case '(': {
// Go to lowest precedence inside ( ).
++pattern;
if('?' == pattern[0] && ':' == pattern[1]) {
// This is a non-capture grouping.
pattern += 2;
result = parse_alternation(pattern, true);
} else {
// This is a capture grouping.
result = parse_alternation(pattern, true);
// Put the result into a capture node.
auto capture = std::make_unique<node_t>(node_t::kind_capture);
capture->capture_index = capture_count++;
capture->children.push_back(std::move(result->attr));
result->attr = std::move(capture);
}
pattern = result->next;
if(')' != *pattern)
throw_error(pattern, "expected ')' to close group structure");
result->next = ++pattern;
break;
}
case '[': {
// Parse character class.
result = parse_metachar(pattern);
if(!result) {
++pattern;
auto cclass = std::make_unique<node_t>(node_t::kind_cclass);
cclass->negate = false;
if('^' == *pattern)
cclass->negate = true, ++pattern;
if(']' == *pattern) {
auto char_ = std::make_unique<node_t>(node_t::kind_char);
char_->c = ']', ++pattern;
cclass->children.push_back(std::move(char_));
}
while(auto item = parse_cclass_item(pattern)) {
pattern = item->next;
cclass->children.push_back(std::move(item->attr));
}
if(!cclass->children.size())
throw_error(pattern, "expected character in character class");
if(']' != *pattern)
throw_error(pattern, "expected ']' to close character class");
++pattern;
result = make_result(pattern, std::move(cclass));
}
break;
}
case '\\': {
result = parse_metachar(pattern);
if(!result) result = parse_escape(pattern);
break;
}
case '.': {
auto any = std::make_unique<node_t>(node_t::kind_any);
result = make_result(pattern + 1, std::move(any));
break;
}
default: {
auto char_ = std::make_unique<node_t>(node_t::kind_char);
char_->c = *pattern;
result = make_result(pattern + 1, std::move(char_));
break;
}
}
return result;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_duplication(const char* pattern,
bool expect) {
// Parse unary terms and ranges.
auto term = parse_term(pattern, expect);
while(term) {
pattern = term->next;
switch(char c = *pattern) {
case '*': {
auto star = std::make_unique<node_t>(node_t::kind_star);
star->children.push_back(std::move(term->attr));
term->attr = std::move(star);
term->next = ++pattern;
break;
}
case '+': {
auto plus = std::make_unique<node_t>(node_t::kind_plus);
plus->children.push_back(std::move(term->attr));
term->attr = std::move(plus);
term->next = ++pattern;
break;
}
case '?': {
auto opt = std::make_unique<node_t>(node_t::kind_opt);
opt->children.push_back(std::move(term->attr));
term->attr = std::move(opt);
term->next = ++pattern;
break;
}
case '{': {
// Parse quantifier.
++pattern;
int min_count = -1;
int max_count = -1;
if(',' != *pattern) {
// Read the min count.
int n;
if(1 != sscanf(pattern, "%d%n", &min_count, &n))
throw_error(pattern, "expected min-quantifier");
pattern += n;
}
if(',' == *pattern) {
++pattern;
// Read the max count.
int n;
if(1 != sscanf(pattern, "%d%n", &max_count, &n))
throw_error(pattern, "expected max-quantifier");
pattern += n;
} else {
max_count = min_count;
}
if('}' != *pattern)
throw_error(pattern, "expected '}' to end quantifier");
++pattern;
if(-1 == min_count && -1 == max_count)
throw_error(pattern, "expected min or max quantifier");
if(min_count > max_count)
throw_error(pattern,
"min quantifier must not be greater than max quantifier");
auto quant = std::make_unique<node_t>(node_t::kind_quant);
quant->r_min = min_count;
quant->r_max = max_count;
quant->children.push_back(std::move(term->attr));
term->attr = std::move(quant);
term->next = pattern;
break;
}
default:
return std::move(term);
}
}
return term;
}
PCRE_LINKAGE result_parse_t grammar_t::parse_concat(const char* pattern,
bool expect) {
// Match the lhs.
result_parse_t a = parse_duplication(pattern, expect);
while(a) {
// Advance past the lhs.
pattern = a->next;
if(auto b = parse_duplication(pattern, false)) {
pattern = b->next;
if(node_t::kind_seq != a->attr->kind) {
auto seq = std::make_unique<node_t>(node_t::kind_seq);
seq->children.push_back(std::move(a->attr));
a->attr = std::move(seq);
}
a->attr->children.push_back(std::move(b->attr));
a->next = pattern;
} else
break;
}
return std::move(a);
}
PCRE_LINKAGE result_parse_t grammar_t::parse_alternation(const char* pattern,
bool expect, bool topmost) {
result_parse_t a = parse_concat(pattern, true);
while(a) {
// Advance past the lhs.
pattern = a->next;
if('|' == *pattern) {
result_parse_t b = parse_concat(pattern + 1, true);
if(node_t::kind_alt != a->attr->kind) {
auto alt = std::make_unique<node_t>(node_t::kind_alt);
alt->children.push_back(std::move(a->attr));
a->attr = std::move(alt);
}
a->attr->children.push_back(std::move(b->attr));
a->next = b->next;
} else
break;
}
if(topmost) {
switch(char c = *pattern) {
case ')':
throw_error(pattern, "')' encountered outside of group");
break;
case ']':
throw_error(pattern, "']' encountered outside of character class");
break;
default:
break;
}
}
return a;
}
PCRE_LINKAGE void grammar_t::throw_error(const char* pattern, const char* msg) {
ptrdiff_t col = pattern - begin;
std::string s = format("%s\n%*s^\n%s\n", begin, col, "", msg);
throw std::runtime_error(s);
}
PCRE_LINKAGE std::pair<node_ptr_t, int> parse_regex(const char* pattern) {
grammar_t g { pattern };
result_parse_t result = g.parse_alternation(pattern, true, true);
if(*result->next)
g.throw_error(result->next, "cannot complete pattern parse");
return std::make_pair(std::move(result->attr), g.capture_count);
}
} // namespace pcre