-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwrx_comp.c
1424 lines (1178 loc) · 33.5 KB
/
wrx_comp.c
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
/*
* Copyright (c) 2007-2015 Werner Stoop
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <ctype.h>
#include <assert.h>
#include "wregex.h"
#include "wrxcfg.h"
#ifdef DEBUG_OUTPUT
# include <stdio.h> /* To be removed, along with all the printf()s */
#endif
/* States in the NFA per character in the input pattern */
#define DELTA_STATES 4
/* Internal Structures *******************************************************/
/* The compiler works by breaking the regex into smaller regexes which
* are converted into small NFAs. These NFA segments are combined as
* we move upwards through the recursion.
* This structure tracks those NFA segments' states.
*/
typedef struct {
short beg, /* The state at which this NFA segment begins */
end; /* The state at which this NFA segment ends */
/* Also, due to the nature of the parser, all states between 'beg' and
* 'end' will be part of this NFA segment and its sub-segments
*/
} nfa_segment;
/*
* Internal data used while compiling the NFA
*/
typedef struct {
wregex_t *nfa; /* The NFA being generated */
const char *pat, /* The pattern being compiled */
*p; /* The position within the pattern */
jmp_buf jb; /* Jump buffer for error handling */
nfa_segment *seg; /* Stack of NFA segments */
short seg_s, /* Size of the stack */
seg_sp; /* Index of the top of the stack */
char ci; /* case insensitive flag */
} comp_data;
#define THROW(x) longjmp(cd->jb, x)
/* Helper Functions (and macros) *********************************************/
/*
* Gets and initializes the next available state in the wregex_t
* realloc()s the NFA's states if there aren't enough available
*/
static short next_state(comp_data *cd) {
short i;
int delta;
if(cd->nfa->ns + 1 >= cd->nfa->n_states) {
/* We need more states. Guess the number needed from the
remaining characters in the input pattern */
delta = (DELTA_STATES * strlen(cd->p));
if(cd->nfa->n_states == 0x7FFF) {
/* Too many states: We use shorts to index them, and this
will result in overflow */
THROW(WRX_MANY_STATES);
}
if(cd->nfa->ns >= 0x7FFF - delta)
cd->nfa->n_states = 0x7FFF;
else
cd->nfa->n_states += delta;
cd->nfa->states = realloc(cd->nfa->states,
cd->nfa->n_states * sizeof(wrx_state));
if(!cd->nfa->states)
THROW(WRX_MEMORY);
}
i = cd->nfa->ns++;
/* Initialize the state */
cd->nfa->states[i].op = 0;
cd->nfa->states[i].data.c = '\0';
cd->nfa->states[i].s[0] = -1;
cd->nfa->states[i].s[1] = -1;
cd->nfa->states[i].data.bv = NULL;
return i;
}
/*
* Pushes a NFA segment on the comp_data's stack
*/
static void push_seg(comp_data *cd, short beg, short end) {
if(cd->seg_sp + 1 >= cd->seg_s) {
/* Resize the stack */
cd->seg_s += 10;
cd->seg = realloc(cd->seg, sizeof(nfa_segment) * cd->seg_s);
if(!cd->seg) THROW(WRX_MEMORY);
}
cd->seg[cd->seg_sp].beg = beg;
cd->seg[cd->seg_sp].end = end;
cd->seg_sp++;
}
/*
* Pops a NFA segment from the comp_data's stack
* Don't call push_seg() if you're still using the returned pointer
*/
static nfa_segment *pop_seg(comp_data *cd) {
assert(cd->seg_sp > 0);
cd->seg_sp--;
return &cd->seg[cd->seg_sp];
}
/*
* Has state s1 transition to s2
*/
static void transition(comp_data *cd , short s1, short s2) {
if(cd->nfa->states[s1].s[0] < 0) {
cd->nfa->states[s1].s[0] = s2;
} else {
/* This assertion must hold because each NFA state has at most
2 epsilon transitions */
assert(cd->nfa->states[s1].s[1] < 0);
cd->nfa->states[s1].s[1] = s2;
}
}
/*
* Function weaken() makes the '*' and '+' (and '?') operators
* "lazy"/"non-greedy" by swapping s[0] and s[1] of the appropriate state
*/
static void weaken(comp_data *cd, short s) {
short t;
assert(s >= 0 && s < cd->nfa->n_states);
t = cd->nfa->states[s].s[0];
cd->nfa->states[s].s[0] = cd->nfa->states[s].s[1];
cd->nfa->states[s].s[1] = t;
}
/*
* Creates a duplicate of a particular state j.
* It is used with the "{}" operators to convert, say, "A{3}" to "AAA"
*/
static int duplicate(comp_data *cd, short j) {
int k;
char *bv;
k = next_state(cd);
cd->nfa->states[k].op = cd->nfa->states[j].op;
cd->nfa->states[k].s[0] = cd->nfa->states[j].s[0];
cd->nfa->states[k].s[1] = cd->nfa->states[j].s[1];
if(cd->nfa->states[j].op == SET) {
bv = malloc(16);
if(!bv) THROW(WRX_MEMORY);
memcpy(bv, cd->nfa->states[j].data.bv, 16);
cd->nfa->states[k].data.bv = bv;
} else if(cd->nfa->states[j].op == REC || cd->nfa->states[j].op == STP || cd->nfa->states[j].op == BRF) {
cd->nfa->states[k].data.idx = cd->nfa->states[j].data.idx;
} else
cd->nfa->states[k].data.c = cd->nfa->states[j].data.c;
return k;
}
/* Creates a bit vector from the set of characters in s, in a similar way
* to how the parser handles character sets. It feels a bit clumsy, but
* it was the neatest way i could implement the "set" escape characters;
* see value() below.
* (It asserts input, so it is not intended for user input)
*/
static char *create_bv(char *s) {
char *bv, u, v, i;
assert(s && *s);
if((bv = malloc(16)) == NULL) return NULL;
memset(bv, 0, 16);
do {
u = s[0];
if(s[1] == '-')
{
s += 2;
assert(s != '\0');
v = s[0];
}
else
v = u;
s++;
assert((u >= START_OF_PRINT && v >= START_OF_PRINT && v >= u) ||
(u == '\t' || u == '\r' || u == '\n'));
for(i = u; i <= v; i++)
BV_SET(bv, i);
} while(s[0] != '\0');
return bv;
}
/*
* Invert the bits in a range bit-vector
*/
static void invert_bv(char *bv) {
int i;
/* Note that I leave the first 4 bytes (containing the bits of the
non-printable characters) as I found them */
for(i = 4; i < 16; i++)
bv[i] = ~bv[i];
/* These three should be handled separately, since they are lower
than START_OF_PRINT */
BV_TGL(bv, '\r');
BV_TGL(bv, '\n');
BV_TGL(bv, '\t');
}
/* The Parser ****************************************************************/
static void list(comp_data *cd);
static void element(comp_data *cd);
static void value(comp_data *cd);
static char *sets(comp_data *cd);
/*
*$ pattern ::= ['^'] [list] ['$']
*/
static void pattern(comp_data *cd) {
short b, e, bol = 0, hl = 0;
nfa_segment *m1, *m2;
if(cd->p[0] == '\0') {
#ifdef DEBUG_OUTPUT
printf("\"\"");
#endif
/* empty pattern: Match everything */
b = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = MEV;
push_seg(cd, b, b);
return;
}
if(cd->p[0] == '^') {
bol = 1;
/* Create a BOL node */
b = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = BOL;
push_seg(cd, b, b);
#ifdef DEBUG_OUTPUT
printf(" ^");
#endif
cd->p++;
if(!cd->p[0]) return;
}
if(cd->p[0] != '$') {
hl = 1;
list(cd);
}
if(bol && hl) {
/* concatenate the BOL and the list */
m2 = pop_seg(cd); /* pop NFA 2 */
m1 = pop_seg(cd); /* pop NFA 1 */
/* Attach NFA 1's end to NFA 2's beginning */
transition(cd, m1->end, m2->beg);
push_seg(cd, m1->beg, m2->end);
}
if(cd->p[0] == '$') {
#ifdef DEBUG_OUTPUT
printf(" $");
#endif
if(!bol && !hl) {
/* Special case: pattern = "$", match everything */
b = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = MEV;
push_seg(cd, b, b);
}
cd->p++;
if(cd->p[0] != '\0')
THROW(WRX_BAD_DOLLAR);
/* Create a EOL node */
b = next_state(cd);
e = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = EOL;
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
m1 = pop_seg(cd); /* pop NFA 1 */
transition(cd, m1->end, b);
push_seg(cd, m1->beg, e);
}
/* Add the REC and STP states for submatch 0,
which captures the entire matching part of the string */
m1 = pop_seg(cd); /* pop the NFA */
b = next_state(cd);
e = next_state(cd);
cd->nfa->states[b].op = REC;
cd->nfa->states[b].data.idx = 0;
cd->nfa->states[e].op = STP;
cd->nfa->states[e].data.idx = 0;
transition(cd, b, m1->beg);
transition(cd, m1->end, e);
push_seg(cd, b, e);
}
/*
*$ list ::= element ["|" list]
*/
static void list(comp_data *cd) {
short b, e, n1, n2;
nfa_segment *m;
element(cd);
if(cd->p[0] == '|') {
cd->p++;
#ifdef DEBUG_OUTPUT
printf(" |");
#endif
m = pop_seg(cd); /* pop NFA 1 */
b = m->beg;
e = m->end;
list(cd); /* Compile the second NFA */
m = pop_seg(cd); /* pop NFA 2 */
n1 = next_state(cd);
n2 = next_state(cd);
cd->nfa->states[n1].op = CHC;
cd->nfa->states[n2].op = MOV;
transition(cd, n1, b);
transition(cd, n1, m->beg);
transition(cd, e, n2);
transition(cd, m->end, n2);
push_seg(cd, n1, n2);
}
}
/*
*$ element ::= ("(" [":"] list ")" | value) [(("*"|"+"|"?")["?"])|("{" [digit+] ["," [digit+]] "}" ["?"])] [element]
*/
static void element(comp_data *cd) {
short b, e;
char bref;
nfa_segment *m;
int boc, eoc, cf; /* begin/end of counting: "{boc,eoc}" */
short i, j, k,
ofs, sub1, sub2, sub3;
sub1 = cd->nfa->ns;
if(cd->p[0] == '$') return;
if(cd->p[0] == '(') {
if(cd->p[1] == ':') {
/* parenthesis used only for grouping */
bref = -1;
cd->p += 2;
#ifdef DEBUG_OUTPUT
printf(" (:");
#endif
} else {
/* parenthesis indicates a submatch capture */
bref = cd->nfa->n_subm++;
cd->p++;
#ifdef DEBUG_OUTPUT
printf(" (");
#endif
}
list(cd);
if(cd->p[0] != ')') THROW(WRX_BRACKET);
if(bref >= 0) {
/* back reference: */
m = pop_seg(cd); /* Get the NFA within the parens */
/* Create a recording state */
b = next_state(cd);
cd->nfa->states[b].op = REC;
cd->nfa->states[b].data.idx = bref;
transition(cd, b, m->beg);
/* Create a state for stopping the recording */
e = next_state(cd);
cd->nfa->states[e].op = STP;
cd->nfa->states[e].data.idx = bref;
transition(cd, m->end, e);
push_seg(cd, b, e);
}
cd->p++;
#ifdef DEBUG_OUTPUT
printf(" )");
#endif
} else {
value(cd);
}
if(cd->p[0] == '$') return;
if(cd->p[0] && strchr("*+?", cd->p[0])) {
m = pop_seg(cd); /* Get the preceding NFA */
b = next_state(cd);
e = next_state(cd);
cd->nfa->states[b].op = CHC;
cd->nfa->states[e].op = MOV;
transition(cd, b, m->beg);
transition(cd, b, e);
/* The actual differences between the operators are very subtle */
switch(cd->p[0]) {
case '*':
transition(cd, m->end, b);
push_seg(cd, b, e);
break;
case '+':
transition(cd, m->end, b);
push_seg(cd, m->beg, e);
break;
case '?':
transition(cd, m->end, e);
push_seg(cd, b, e);
break;
}
#ifdef DEBUG_OUTPUT
printf(" %c", cd->p[0]);
#endif
cd->p++;
/* Lazy evaluation? */
if(cd->p[0] == '?') {
cd->p++;
weaken(cd, b);
#ifdef DEBUG_OUTPUT
printf(" ?");
#endif
}
} else if(cd->p[0] == '{') {
boc = 0;
eoc = 0;
cf = 0;
cd->p++;
if(isdigit(cd->p[0])) cf = 1;
while(isdigit(cd->p[0])) {
boc = boc * 10 + (cd->p[0] - '0');
cd->p++;
}
if(cd->p[0] == ',') {
cf |= 2;
cd->p++;
if(isdigit(cd->p[0])) cf |= 4;
while(isdigit(cd->p[0])) {
eoc = eoc * 10 + (cd->p[0] - '0');
cd->p++;
}
}
if(cd->p[0] != '}')
THROW(WRX_CURLYB);
cd->p++;
/* So now these values for cf:
* 0: {} - treated as '*'
* 1: {x} - exactly x
* 2: {,} - treated the same as '*'
* 3: {x,} - at least x - equivalent to {x,inf}
* 4: can't happen
* 5: can't happen
* 6: {,y} - at most y - equivalent to {0,y}
* 7: {x,y} - between x and y
*/
assert(cf != 4 && cf != 5);
/* Treat {x,x} the same as {x} */
if(cf == 7 && boc == eoc) cf = 1;
switch(cf)
{
case 0:
case 2: { /* {} or {,} - treat it as we would a '*' */
m = pop_seg(cd); /* Get the preceding NFA */
b = next_state(cd);
e = next_state(cd);
cd->nfa->states[b].op = CHC;
cd->nfa->states[e].op = MOV;
transition(cd, b, m->beg);
transition(cd, b, e);
transition(cd, m->end, b);
push_seg(cd, b, e);
#ifdef DEBUG_OUTPUT
printf(" {}");
#endif
if(cd->p[0] == '?') { /* weaken? */
cd->p++;
weaken(cd, b);
#ifdef DEBUG_OUTPUT
printf(" ?");
#endif
}
} break;
case 1: { /* {boc} */
sub2 = cd->nfa->ns;
m = pop_seg(cd);
ofs = sub2 - sub1; /* offset: Number of states that will be added */
b = m->beg + ofs; /* beginning of next NFA segment */
e = m->end; /* end of current NFA segment */
#ifdef DEBUG_OUTPUT
printf(" {%d}", boc);
#endif
/* Duplicate the states between sub1 and sub2 boc times */
for(i = 1; i < boc; i++) {
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* link the previous NFA segment to the new one */
cd->nfa->states[e].s[0] = b;
/* adjust our parameters by the offset */
b += ofs;
e += ofs;
sub1 += ofs;
sub2 += ofs;
}
/* Weaken operator has no meaning here */
if(cd->p[0] == '?') cd->p++;
push_seg(cd, m->beg, e);
} break;
case 3: { /* {boc,} - at least boc */
sub2 = cd->nfa->ns;
m = pop_seg(cd);
ofs = sub2 - sub1; /* offset: Number of states that will be added */
b = m->beg + ofs; /* beginning of next NFA segment */
e = m->end; /* end of current NFA segment */
#ifdef DEBUG_OUTPUT
printf(" {%d,}", boc);
#endif
/* Duplicate the states between sub1 and sub2 boc times */
for(i = 1; i < boc; i++) {
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* link the previous NFA segment to the new one */
cd->nfa->states[e].s[0] = b;
/* adjust our parameters by the offset */
b += ofs;
e += ofs;
sub1 += ofs;
sub2 += ofs;
}
/*
* We treat "a{3,}" the same as "aaa+"
* so at this stage the "aaa" part is set up, so now we just need
* to do the "+" part:
*/
b -= ofs;
i = next_state(cd);
j = next_state(cd);
cd->nfa->states[i].op = CHC;
cd->nfa->states[j].op = MOV;
transition(cd, i, b);
transition(cd, i, j);
transition(cd, e, i);
/* weaken? */
if(cd->p[0] == '?') {
cd->p++;
weaken(cd, i);
#ifdef DEBUG_OUTPUT
printf(" ?");
#endif
}
push_seg(cd, m->beg, j);
} break;
case 6: { /* {,eoc} - at most eoc */
/* we treat "A{,3}" as "A?A?A?" */
m = pop_seg(cd); /* Get the preceding NFA A */
/* Create the equivalent to A? */
b = next_state(cd);
e = next_state(cd);
cd->nfa->states[b].op = CHC;
cd->nfa->states[e].op = MOV;
transition(cd, b, m->beg);
transition(cd, b, e);
transition(cd, m->end, e);
#ifdef DEBUG_OUTPUT
printf(" {,%d}", eoc);
#endif
/* weaken? */
if(cd->p[0] == '?') {
/* Weaken is not really useful here because
it's like saying ?? */
cd->p++;
weaken(cd, b);
#ifdef DEBUG_OUTPUT
printf(" ?");
#endif
}
/* Good! Now create A?A?A?... */
sub2 = cd->nfa->ns;
m->beg = b;
m->end = e;
ofs = sub2 - sub1; /* offset: Number of states that will be added */
b += ofs; /* beginning of next NFA segment */
/* Duplicate the states between sub1 and sub2 boc times */
for(i = 1; i < eoc; i++) {
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* link the previous NFA segment to the new one */
cd->nfa->states[e].s[0] = b;
/* adjust our parameters by the offset */
b += ofs;
e += ofs;
sub1 += ofs;
sub2 += ofs;
}
push_seg(cd, m->beg, e);
} break;
case 7: /* {boc,eoc} - between boc and eoc (inclusive) */
{
if(boc > eoc) THROW(WRX_BAD_CURLYB);
/* I'd like to evaluate "A{2,5}" as "AAA?A?A?" */
sub2 = cd->nfa->ns;
m = pop_seg(cd);
ofs = sub2 - sub1; /* offset: Number of states that will be added */
b = m->beg + ofs; /* beginning of next NFA segment */
e = m->end; /* end of current NFA segment */
#ifdef DEBUG_OUTPUT
printf(" {%d,%d}", boc, eoc);
#endif
/* Duplicate the states between sub1 and sub2 boc times */
for(i = 1; i < boc; i++) {
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* link the previous NFA segment to the new one */
cd->nfa->states[e].s[0] = b;
/* adjust our parameters by the offset */
b += ofs;
e += ofs;
sub1 += ofs;
sub2 += ofs;
}
/* At this stage we have "AA", so now we want too start adding "A?"'s */
sub3 = cd->nfa->ns; /* Remember the current state, useful later */
/* Create a new NFA segment identical to "A" */
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* Convert our duplicate of "A" into a "A?" */
i = next_state(cd);
j = next_state(cd);
cd->nfa->states[i].op = CHC;
cd->nfa->states[j].op = MOV;
cd->nfa->states[e].s[0] = i;
transition(cd, i, b);
transition(cd, i, j);
e += ofs;
transition(cd, e, j);
if(cd->p[0] == '?') /* weaken? */
weaken(cd, i);
/* At this stage we have "AAA?" (only one "A?") */
/* Recall where our first "A?" lies */
sub1 = sub3;
sub2 = cd->nfa->ns;
ofs = sub2 - sub1;
b = i;
e = j;
/* Duplicate the "A?" states between sub1 and sub2 (eoc - boc - 1) times */
for(i = boc; i < eoc - 1; i++) {
/* Create duplicates of all the states between sub1 and sub2 */
for(j = sub1; j < sub2; j++) {
/* Create a duplicate of the current state */
k = duplicate(cd, j);
/* but alter the state transitions */
if(cd->nfa->states[k].s[0] >= 0)
cd->nfa->states[k].s[0] += ofs;
if(cd->nfa->states[k].s[1] >= 0)
cd->nfa->states[k].s[1] += ofs;
}
/* adjust our parameters by the offset */
b += ofs;
/* link the previous NFA segment to the new one */
cd->nfa->states[e].s[0] = b;
e += ofs;
sub1 += ofs;
sub2 += ofs;
}
push_seg(cd, m->beg, e);
/* weaken? (we've already taken care of it) */
if(cd->p[0] == '?') {
cd->p++;
#ifdef DEBUG_OUTPUT
printf(" ?");
#endif
}
} break;
} /* switch cf */
}
if(cd->p[0] && cd->p[0] != '|' && cd->p[0] != ')' && cd->p[0] != '$') {
m = pop_seg(cd); /* pop NFA 1 */
b = m->beg;
e = m->end;
element(cd); /* compile NFA 2 */
m = pop_seg(cd); /* pop NFA 2 */
/* Attach NFA 1's end to NFA 2's beginning */
transition(cd, e, m->beg);
push_seg(cd, b, m->end);
}
}
/*
*$ value ::= (A-Za-z0-9!"#%&',-/:;=@\\_`~\r\t\n) | '<' | '>' | "[" ["^"] sets "]" | "." | '\i' list | '\I' list | 'escape sequence'
*/
static void value(comp_data *cd) {
short b, e, inv = 0, i;
if(isalnum(cd->p[0]) || cd->p[0] == ' ') {
b = next_state(cd);
e = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = cd->ci?MCI:MTC;
cd->nfa->states[b].data.c = cd->p[0];
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
push_seg(cd, b, e);
#ifdef DEBUG_OUTPUT
printf(" '%c'", cd->p[0]);
#endif
cd->p++;
} else if(cd->p[0] == '[') {
cd->p++;
b = next_state(cd);
e = next_state(cd);
#ifdef DEBUG_OUTPUT
printf(" [");
#endif
/* Invert the set? */
if(cd->p[0] == '^') {
#ifdef DEBUG_OUTPUT
printf(" ^");
#endif
cd->p++;
inv = 1;
}
/* Initialize the states */
cd->nfa->states[b].op = SET;
/* Compile the sets */
cd->nfa->states[b].data.bv = sets(cd);
if(inv) /* invert the range */
invert_bv(cd->nfa->states[b].data.bv);
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
push_seg(cd, b, e);
if(cd->p[0] == ']')
cd->p++;
else
THROW(WRX_ANGLEB);
#ifdef DEBUG_OUTPUT
printf(" ]");
#endif
} else if(cd->p[0] == '.') {
b = next_state(cd);
e = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = SET;
cd->nfa->states[b].data.bv = malloc(16);
if(!cd->nfa->states[b].data.bv) THROW(WRX_MEMORY);
for(i = 0; i < 16; i++)
cd->nfa->states[b].data.bv[i] = (i < 4)? 0: 0xFF;
BV_SET(cd->nfa->states[b].data.bv, '\r');
BV_SET(cd->nfa->states[b].data.bv, '\n');
BV_SET(cd->nfa->states[b].data.bv, '\t');
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
push_seg(cd, b, e);
#ifdef DEBUG_OUTPUT
printf(" '%c'", cd->p[0]);
#endif
cd->p++;
} else if(cd->p[0] == '<') {
b = next_state(cd);
e = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = BOW;
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
push_seg(cd, b, e);
#ifdef DEBUG_OUTPUT
printf(" <");
#endif
cd->p++;
} else if(cd->p[0] == '>') {
b = next_state(cd);
e = next_state(cd);
/* Initialize the states */
cd->nfa->states[b].op = EOW;
transition(cd, b, e);
cd->nfa->states[e].op = MOV;
push_seg(cd, b, e);
#ifdef DEBUG_OUTPUT
printf(" >");
#endif
cd->p++;
} else if(cd->p[0] == '$') {
return;
} else if(cd->p[0] == ESC) {
/* 'escape sequence' */