-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsgregex.c
1329 lines (1160 loc) · 31.7 KB
/
sgregex.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define RX_NEED_DEFAULT_MEMFUNC
#define _srx_Context rxExecute
#include "sgregex.h"
#define RX_LOG( x ) /*x*/
#define RX_MAX_CAPTURES 10
#define RX_MAX_SUBEXPRS 255
#define RX_MAX_REPEATS 0xffffffff
#define RX_NULL_OFFSET 0xffffffff
#define RX_NULL_INSTROFF 0x0fffffff
#define RCF_MULTILINE 0x01 /* ^/$ matches beginning/end of line too */
#define RCF_CASELESS 0x02 /* pre-equalized case for match/range */
#define RCF_DOTALL 0x04 /* "." is compiled as "[^]" instead of "[^\r\n]" */
#define RX_OP_MATCH_DONE 0 /* end of regexp */
#define RX_OP_MATCH_CHARSET 1 /* [...] / character ranges */
#define RX_OP_MATCH_CHARSET_INV 2 /* [^...] / inverse character ranges */
#define RX_OP_MATCH_STRING 3 /* plain sequence of non-special characters */
#define RX_OP_MATCH_BACKREF 4 /* previously found capture group */
#define RX_OP_MATCH_SLSTART 5 /* string / line start */
#define RX_OP_MATCH_SLEND 6 /* string / line end */
#define RX_OP_REPEAT_GREEDY 7 /* try repeated match before proceeding */
#define RX_OP_REPEAT_LAZY 8 /* try proceeding before repeated match */
#define RX_OP_JUMP 9 /* jump to the specified instruction */
#define RX_OP_BACKTRK_JUMP 10 /* jump if backtracked */
#define RX_OP_CAPTURE_START 11 /* save starting position of capture range */
#define RX_OP_CAPTURE_END 12 /* save ending position of capture range */
typedef struct rxInstr
{
uint32_t op : 4; /* opcode */
uint32_t start : 28; /* pointer to starting instruction in range */
uint32_t from; /* beginning of character data / min. repeat count / capture ID */
uint32_t len; /* length of character data / max. repeat count */
}
rxInstr;
#define RX_STATE_BACKTRACKED 0x1
typedef struct rxState
{
uint32_t off : 28; /* offset in string */
uint32_t flags : 4;
uint32_t instr; /* instruction */
uint32_t numiters; /* current iteration count / previous capture value */
}
rxState;
typedef struct rxSubexpr
{
uint32_t start;
uint32_t section_start;
uint32_t repeat_start;
uint8_t capture_slot;
}
rxSubexp;
typedef struct rxCompiler
{
srx_MemFunc memfn;
void* memctx;
rxInstr* instrs;
size_t instrs_count;
size_t instrs_mem;
rxChar* chars;
size_t chars_count;
size_t chars_mem;
uint8_t flags;
uint8_t capture_count;
int errcode;
int errpos;
rxSubexp subexprs[ RX_MAX_SUBEXPRS ];
int subexprs_count;
}
rxCompiler;
#define RX_LAST_INSTR( c ) ((c)->instrs[ (c)->instrs_count - 1 ])
#define RX_LAST_CHAR( c ) ((c)->chars[ (c)->chars_count - 1 ])
#define RX_LAST_SUBEXPR( c ) ((c)->subexprs[ (c)->subexprs_count - 1 ])
struct rxExecute
{
srx_MemFunc memfn;
void* memctx;
/* compiled program */
rxInstr* instrs; /* instruction data (opcodes and fixed-length arguments) */
rxChar* chars; /* character data (ranges and plain sequences for opcodes) */
uint8_t flags;
uint8_t capture_count;
/* runtime data */
rxState* states;
size_t states_count;
size_t states_mem;
uint32_t* iternum;
size_t iternum_count;
size_t iternum_mem;
const rxChar* str;
uint32_t captures[ RX_MAX_CAPTURES ][2];
};
typedef struct rxExecute rxExecute;
#define RX_NUM_ITERS( e ) ((e)->iternum[ (e)->iternum_count - 1 ])
#define RX_LAST_STATE( e ) ((e)->states[ (e)->states_count - 1 ])
#define RX_STRLITBUF( x ) (x), (sizeof(x)-1)
#define rxIsDigit( v ) ((v) >= '0' && (v) <= '9')
static rxChar rxToLower( rxChar c )
{
if( c >= 'A' && c <= 'Z' )
return (rxChar)( c - 'A' + 'a' );
return c;
}
static rxChar rxSwapCase( rxChar c )
{
if( c >= 'A' && c <= 'Z' )
return (rxChar)( c - 'A' + 'a' );
else if( c >= 'a' && c <= 'z' )
return (rxChar)( c - 'a' + 'A' );
return c;
}
static int rxMemCaseEq( const rxChar* a, const rxChar* b, size_t sz )
{
size_t i;
for( i = 0; i < sz; ++i )
{
if( rxToLower( a[ i ] ) != rxToLower( b[ i ] ) )
return 0;
}
return 1;
}
static int rxMatchCharset( const rxChar* ch, const rxChar* charset, size_t cslen, int ignore_case )
{
const rxChar* cc = charset;
const rxChar* charset_end = charset + cslen;
if( ignore_case )
{
while( cc != charset_end )
{
rxChar occ = rxSwapCase( *ch );
if( *ch >= cc[0] && *ch <= cc[1] )
return 1;
if( occ >= cc[0] && occ <= cc[1] )
return 1;
cc += 2;
}
}
else
{
while( cc != charset_end )
{
if( *ch >= cc[0] && *ch <= cc[1] )
return 1;
cc += 2;
}
}
return 0;
}
void rxDumpToFile( rxInstr* instrs, rxChar* chars, FILE* fp )
{
size_t i;
rxInstr* ip = instrs;
fprintf( fp, "instructions\n{\n" );
for(;;)
{
fprintf( fp, " [%03u] ", (unsigned) ( ip - instrs ) );
switch( ip->op )
{
case RX_OP_MATCH_DONE:
fprintf( fp, "MATCH_DONE\n" );
break;
case RX_OP_MATCH_CHARSET:
case RX_OP_MATCH_CHARSET_INV:
fprintf( fp, "%s (ranges[%u]=",
ip->op == RX_OP_MATCH_CHARSET ? "MATCH_CHARSET" : "MATCH_CHARSET_INV",
(unsigned) ip->len );
for( i = ip->from; i < ip->from + ip->len; ++i )
{
rxChar ch = chars[ i ];
if( ( i - ip->from ) % 2 == 1 )
fprintf( fp, "-" );
if( ch < 32 || ch > 126 )
fprintf( fp, "[%u]", (unsigned) (rxUChar) ch );
else
fprintf( fp, "%c", ch );
}
fprintf( fp, ")\n" );
break;
case RX_OP_MATCH_STRING:
fprintf( fp, "MATCH_STRING (str[%u]=", (unsigned) ip->len );
for( i = ip->from; i < ip->from + ip->len; ++i )
{
rxChar ch = chars[ i ];
if( ch < 32 || ch > 126 )
fprintf( fp, "[%u]", (unsigned) (rxUChar) ch );
else
fprintf( fp, "%c", ch );
}
fprintf( fp, ")\n" );
break;
case RX_OP_MATCH_BACKREF:
fprintf( fp, "MATCH_BACKREF (slot=%d)\n", (int) ip->from );
break;
case RX_OP_MATCH_SLSTART:
fprintf( fp, "MATCH_SLSTART\n" );
break;
case RX_OP_MATCH_SLEND:
fprintf( fp, "MATCH_SLEND\n" );
break;
case RX_OP_REPEAT_GREEDY:
fprintf( fp, "REPEAT_GREEDY (%u-%u, jump=%u)\n", (unsigned) ip->from, (unsigned) ip->len, (unsigned) ip->start );
break;
case RX_OP_REPEAT_LAZY:
fprintf( fp, "REPEAT_LAZY (%u-%u, jump=%u)\n", (unsigned) ip->from, (unsigned) ip->len, (unsigned) ip->start );
break;
case RX_OP_JUMP:
fprintf( fp, "JUMP (to=%u)\n", (unsigned) ip->start );
break;
case RX_OP_BACKTRK_JUMP:
fprintf( fp, "BACKTRK_JUMP (to=%u)\n", (unsigned) ip->start );
break;
case RX_OP_CAPTURE_START:
fprintf( fp, "CAPTURE_START (slot=%d)\n", (int) ip->from );
break;
case RX_OP_CAPTURE_END:
fprintf( fp, "CAPTURE_END (slot=%d)\n", (int) ip->from );
break;
}
if( ip->op == RX_OP_MATCH_DONE )
break;
ip++;
}
fprintf( fp, "}\n" );
}
static void rxInitCompiler( rxCompiler* c, srx_MemFunc memfn, void* memctx )
{
c->memfn = memfn;
c->memctx = memctx;
c->instrs = NULL;
c->instrs_count = 0;
c->instrs_mem = 0;
c->chars = NULL;
c->chars_count = 0;
c->chars_mem = 0;
c->flags = 0;
c->capture_count = 0;
c->errcode = RXSUCCESS;
c->errpos = 0;
c->subexprs_count = 1;
c->subexprs[ 0 ].start = 1;
c->subexprs[ 0 ].section_start = 1;
c->subexprs[ 0 ].repeat_start = 1;
c->subexprs[ 0 ].capture_slot = 0;
}
static void rxFreeCompiler( rxCompiler* c )
{
if( c->instrs )
{
c->memfn( c->memctx, c->instrs, 0 );
c->instrs = NULL;
}
if( c->chars )
{
c->memfn( c->memctx, c->chars, 0 );
c->chars = NULL;
}
}
static void rxFixLastInstr( rxCompiler* c )
{
if( c->instrs_count >= 2 &&
RX_LAST_INSTR( c ).op == RX_OP_MATCH_STRING &&
c->instrs[ c->instrs_count - 2 ].op == RX_OP_MATCH_STRING )
{
/* already have 2 string values, about to change repeat target */
c->instrs[ c->instrs_count - 2 ].len++;
c->instrs_count--;
}
}
static void rxInstrReserveSpace( rxCompiler* c )
{
if( c->instrs_count == c->instrs_mem )
{
size_t ncnt = c->instrs_mem * 2 + 16;
rxInstr* ni = (rxInstr*) c->memfn( c->memctx, c->instrs, sizeof(*ni) * ncnt );
c->instrs = ni;
c->instrs_mem = ncnt;
}
}
#define RX_INSTR_REFS_OTHER( op ) ((op) == RX_OP_REPEAT_GREEDY || (op) == RX_OP_REPEAT_LAZY || (op) == RX_OP_JUMP || (op) == RX_OP_BACKTRK_JUMP)
static void rxInsertInstr( rxCompiler* c, uint32_t pos, uint32_t op, uint32_t start, uint32_t from, uint32_t len )
{
size_t i;
rxInstr I;
{
I.op = op & 0xf;
I.start = start & 0x0fffffff;
I.from = from;
I.len = len;
}
rxInstrReserveSpace( c );
assert( pos < c->instrs_count ); /* cannot insert at end, PUSH op needs additional work */
memmove( c->instrs + pos + 1, c->instrs + pos, sizeof(*c->instrs) * ( c->instrs_count - pos ) );
c->instrs_count++;
for( i = 0; i < c->instrs_count; ++i )
{
/* any refs to instructions after the split should be fixed */
if( c->instrs[ i ].start > pos &&
c->instrs[ i ].start != RX_NULL_INSTROFF &&
RX_INSTR_REFS_OTHER( c->instrs[ i ].op ) )
c->instrs[ i ].start++;
}
c->instrs[ pos ] = I; /* assume 'start' is pre-adjusted */
}
static void rxPushInstr( rxCompiler* c, uint32_t op, uint32_t start, uint32_t from, uint32_t len )
{
rxInstr I;
{
I.op = op & 0xf;
I.start = start & 0x0fffffff;
I.from = from;
I.len = len;
}
rxFixLastInstr( c );
rxInstrReserveSpace( c );
c->instrs[ c->instrs_count++ ] = I;
}
static void rxReserveChars( rxCompiler* c, size_t num )
{
if( c->chars_count + num > c->chars_mem )
{
size_t ncnt = c->chars_mem * 2 + num;
rxChar* nc = (rxChar*) c->memfn( c->memctx, c->chars, sizeof(*nc) * ncnt );
c->chars = nc;
c->chars_mem = ncnt;
}
}
static void rxPushChars( rxCompiler* c, const rxChar* str, size_t len )
{
rxReserveChars( c, len );
memcpy( c->chars + c->chars_count, str, sizeof(*str) * len );
c->chars_count += len;
}
static void rxPushChar( rxCompiler* c, rxChar ch )
{
rxReserveChars( c, 1 );
c->chars[ c->chars_count++ ] = ch;
}
static uint32_t rxPushCharClassData( rxCompiler* c, rxChar cch )
{
uint32_t cc = c->chars_count;
switch( cch )
{
case 'd':
rxPushChars( c, RX_STRLITBUF( "09" ) );
break;
case 'h':
rxPushChars( c, RX_STRLITBUF( "\t\t " ) );
break;
case 'v':
rxPushChars( c, RX_STRLITBUF( "\x0A\x0D" ) );
break;
case 's':
rxPushChars( c, RX_STRLITBUF( "\x09\x0D " ) );
break;
case 'w':
rxPushChars( c, RX_STRLITBUF( "azAZ09__" ) );
break;
}
return c->chars_count - cc;
}
static void rxCompile( rxCompiler* c, const rxChar* str, size_t strsize )
{
int empty = 1;
const rxChar* s = str;
const rxChar* strend = str + strsize;
#define RX_SAFE_INCR( s ) if( ++(s) == strend ){ c->errpos = (s) - str; goto reached_end_too_soon; }
RX_LOG(printf("COMPILE START (first capture)\n"));
rxPushInstr( c, RX_OP_CAPTURE_START, 0, 0, 0 );
c->capture_count++;
while( s != strend )
{
switch( *s )
{
case '[':
{
const rxChar* sc;
uint32_t op = RX_OP_MATCH_CHARSET;
uint32_t start = c->chars_count;
RX_LOG(printf("CHAR '['\n"));
RX_SAFE_INCR( s );
if( *s == '^' )
{
op = RX_OP_MATCH_CHARSET_INV;
RX_SAFE_INCR( s );
}
sc = s;
if( *s == ']' )
{
RX_SAFE_INCR( s );
rxPushChar( c, *s );
rxPushChar( c, *s );
}
while( s != strend && *s != ']' )
{
if( *s == '-' && s > sc && s + 1 != strend && s[1] != ']' )
{
if( c->chars_count - start )
{
if( (unsigned) s[1] < (unsigned) RX_LAST_CHAR( c ) )
{
c->errcode = RXERANGE;
c->errpos = s - str;
return;
}
RX_LAST_CHAR( c ) = s[1];
}
RX_SAFE_INCR( s );
}
else if( *s == '\\' )
{
uint32_t count;
RX_SAFE_INCR( s );
count = rxPushCharClassData( c, *s );
if( count == 0 )
{
rxChar chars[ 2 ];
chars[ 0 ] = *s;
chars[ 1 ] = *s;
rxPushChars( c, chars, 2 );
}
}
else
{
rxChar chars[ 2 ];
chars[ 0 ] = *s;
chars[ 1 ] = *s;
rxPushChars( c, chars, 2 );
}
RX_SAFE_INCR( s );
}
if( *s == ']' )
s++; /* incr may be unsafe as ending here is valid */
rxPushInstr( c, op, 0, start, c->chars_count - start );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
}
break;
/* already handled by starting characters as these do not support nesting */
case ']':
case '}':
RX_LOG(printf("CHAR ']' or '}' (UNEXPECTED)\n"));
c->errcode = RXEUNEXP;
c->errpos = s - str;
return;
case '^':
RX_LOG(printf("[^] START (LINE/STRING)\n"));
rxPushInstr( c, RX_OP_MATCH_SLSTART, 0, 0, 0 );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count;
empty = 0;
s++;
break;
case '$':
RX_LOG(printf("[$] END (LINE/STRING)\n"));
rxPushInstr( c, RX_OP_MATCH_SLEND, 0, 0, 0 );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count;
empty = 0;
s++;
break;
case '(':
RX_LOG(printf("[(] CAPTURE_START\n"));
if( c->subexprs_count >= RX_MAX_SUBEXPRS )
goto over_limit;
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count;
c->subexprs[ c->subexprs_count ].capture_slot = 0;
if( c->capture_count < RX_MAX_CAPTURES )
{
rxPushInstr( c, RX_OP_CAPTURE_START, 0, c->capture_count, 0 );
c->subexprs[ c->subexprs_count ].capture_slot = c->capture_count;
c->capture_count++;
}
c->subexprs[ c->subexprs_count ].start = c->instrs_count;
c->subexprs[ c->subexprs_count ].section_start = c->instrs_count;
c->subexprs[ c->subexprs_count ].repeat_start = c->instrs_count;
c->subexprs_count++;
s++;
break;
case ')':
RX_LOG(printf("[)] CAPTURE_END\n"));
if( c->subexprs_count < 2 ||
RX_LAST_SUBEXPR( c ).section_start == c->instrs_count )
goto unexpected_token;
/* remove useless MATCH_STRING before looking for ending position */
rxFixLastInstr( c );
/* fix OR jumps */
{
size_t i;
for( i = RX_LAST_SUBEXPR( c ).start; i < c->instrs_count; ++i )
{
if( c->instrs[ i ].op == RX_OP_JUMP &&
c->instrs[ i ].start == RX_NULL_INSTROFF )
c->instrs[ i ].start = c->instrs_count & 0x0fffffff;
}
}
c->subexprs_count--;
if( c->subexprs[ c->subexprs_count ].capture_slot )
rxPushInstr( c, RX_OP_CAPTURE_END, 0, c->subexprs[ c->subexprs_count ].capture_slot, 0 );
s++;
break;
case '|':
RX_LOG(printf("[|] OR\n"));
if( RX_LAST_SUBEXPR( c ).section_start == c->instrs_count )
goto unexpected_token;
rxPushInstr( c, RX_OP_JUMP, RX_NULL_INSTROFF, 0, 0 );
rxInsertInstr( c, RX_LAST_SUBEXPR( c ).section_start, RX_OP_BACKTRK_JUMP, c->instrs_count + 1, 0, 0 );
RX_LAST_SUBEXPR( c ).section_start = c->instrs_count;
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count;
s++;
break;
case '?':
RX_LOG(printf("[?] 0-1 REPEAT / LAZIFIER\n"));
if( c->instrs_count && RX_LAST_INSTR( c ).op == RX_OP_REPEAT_GREEDY )
{
RX_LAST_INSTR( c ).op = RX_OP_REPEAT_LAZY;
s++;
break;
}
/* pass thru */
case '+':
case '*':
case '{':
RX_LOG(printf("[*+{?] REPEATS\n"));
/* already has a repeat as last op */
if( c->instrs_count &&
( RX_LAST_INSTR( c ).op == RX_OP_REPEAT_LAZY || RX_LAST_INSTR( c ).op == RX_OP_REPEAT_GREEDY ) )
{
goto unexpected_token;
}
if( c->instrs_count == RX_LAST_SUBEXPR( c ).repeat_start )
{
goto unexpected_token;
}
/* state validated, add repeats */
{
uint32_t min = 0, max = RX_MAX_REPEATS;
if( *s == '*' ){}
else if( *s == '+' ){ min = 1; }
else if( *s == '?' ){ max = 1; }
else if( *s == '{' )
{
RX_SAFE_INCR( s );
if( !rxIsDigit( *s ) )
goto unexpected_token;
min = 0;
while( rxIsDigit( *s ) )
{
uint32_t nmin = min * 10 + (uint32_t)( *s - '0' );
if( nmin < min )
goto over_limit;
min = nmin;
RX_SAFE_INCR( s );
}
if( *s == ',' )
{
RX_SAFE_INCR( s );
if( *s == '}' )
max = RX_MAX_REPEATS;
else
{
if( !rxIsDigit( *s ) )
goto unexpected_token;
max = 0;
while( rxIsDigit( *s ) )
{
uint32_t nmax = max * 10 + (uint32_t)( *s - '0' );
if( nmax < max )
goto over_limit;
max = nmax;
RX_SAFE_INCR( s );
}
if( min > max )
{
c->errcode = RXERANGE;
c->errpos = s - str;
return;
}
}
}
else
max = min;
if( *s != '}' )
goto unexpected_token;
}
rxInsertInstr( c, RX_LAST_SUBEXPR( c ).repeat_start, RX_OP_JUMP, c->instrs_count + 1, 0, 0 );
rxPushInstr( c, RX_OP_REPEAT_GREEDY, RX_LAST_SUBEXPR( c ).repeat_start + 1, min, max );
}
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count;
s++;
break;
case '.':
RX_LOG(printf("DOT\n"));
if( c->flags & RCF_DOTALL )
rxPushInstr( c, RX_OP_MATCH_CHARSET_INV, 0, c->chars_count, 0 );
else
{
rxPushInstr( c, RX_OP_MATCH_CHARSET_INV, 0, c->chars_count, 4 );
rxPushChars( c, RX_STRLITBUF( "\n\n\r\r" ) );
}
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
s++;
break;
case '\\':
RX_LOG(printf("BACKSLASH\n"));
RX_SAFE_INCR( s );
if( *s == '.' )
{
rxPushInstr( c, RX_OP_MATCH_STRING, 0, c->chars_count, 1 );
rxPushChar( c, *s++ );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
break;
}
else if( rxIsDigit( *s ) )
{
uint32_t dig = (uint32_t)( *s++ - '0' );
if( dig == 0 || dig >= c->capture_count )
{
c->errcode = RXENOREF;
c->errpos = s - str;
return;
}
rxPushInstr( c, RX_OP_MATCH_BACKREF, 0, dig, 0 );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
break;
}
else
{
uint32_t count = rxPushCharClassData( c, rxToLower( *s ) );
if( count )
{
rxPushInstr( c, *s >= 'a' && *s <= 'z' ? RX_OP_MATCH_CHARSET : RX_OP_MATCH_CHARSET_INV, 0, c->chars_count - count, count );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
s++;
break;
}
}
/* fallback to character output */
default:
RX_LOG(printf("CHAR '%c' (string fallback)\n", *s));
rxPushInstr( c, RX_OP_MATCH_STRING, 0, c->chars_count, 1 );
rxPushChar( c, *s++ );
RX_LAST_SUBEXPR( c ).repeat_start = c->instrs_count - 1;
break;
}
if( empty &&
c->instrs_count &&
RX_LAST_INSTR( c ).op != RX_OP_CAPTURE_END &&
RX_LAST_SUBEXPR( c ).repeat_start < c->instrs_count )
empty = 0;
}
if( empty )
{
RX_LOG(printf("EXPR IS EFFECTIVELY EMPTY\n"));
c->errcode = RXEEMPTY;
c->errpos = 0;
return;
}
if( RX_LAST_SUBEXPR( c ).section_start == c->instrs_count )
{
c->errcode = RXEPART;
c->errpos = s - str;
return;
}
/* fix OR jumps */
{
size_t i;
for( i = RX_LAST_SUBEXPR( c ).start; i < c->instrs_count; ++i )
{
if( c->instrs[ i ].op == RX_OP_JUMP &&
c->instrs[ i ].start == RX_NULL_INSTROFF )
c->instrs[ i ].start = c->instrs_count & 0x0fffffff;
}
}
RX_LOG(printf("COMPILE END (last capture)\n"));
rxPushInstr( c, RX_OP_CAPTURE_END, 0, 0, 0 );
rxPushInstr( c, RX_OP_MATCH_DONE, 0, 0, 0 );
return;
reached_end_too_soon:
c->errcode = RXEPART;
return;
unexpected_token:
c->errcode = RXEUNEXP;
c->errpos = s - str;
return;
over_limit:
c->errcode = RXELIMIT;
c->errpos = s - str;
return;
}
static void rxResetCaptures( rxExecute* e )
{
int i;
for( i = 0; i < RX_MAX_CAPTURES; ++i )
{
e->captures[ i ][0] = RX_NULL_OFFSET;
e->captures[ i ][1] = RX_NULL_OFFSET;
}
}
static void rxInitExecute( rxExecute* e, srx_MemFunc memfn, void* memctx, rxInstr* instrs, rxChar* chars )
{
e->memfn = memfn;
e->memctx = memctx;
e->instrs = instrs;
e->chars = chars;
e->flags = 0;
e->capture_count = 0;
e->states = NULL;
e->states_count = 0;
e->states_mem = 0;
e->iternum = NULL;
e->iternum_count = 0;
e->iternum_mem = 0;
rxResetCaptures( e );
}
static void rxFreeExecute( rxExecute* e )
{
if( e->instrs )
{
e->memfn( e->memctx, e->instrs, 0 );
e->instrs = NULL;
}
if( e->chars )
{
e->memfn( e->memctx, e->chars, 0 );
e->chars = NULL;
}
if( e->states )
{
e->memfn( e->memctx, e->states, 0 );
e->states = NULL;
}
if( e->iternum )
{
e->memfn( e->memctx, e->iternum, 0 );
e->iternum = NULL;
}
}
static void rxPushState( rxExecute* e, uint32_t off, uint32_t instr )
{
rxState* out;
if( e->states_count == e->states_mem )
{
size_t ncnt = e->states_mem * 2 + 16;
rxState* ns = (rxState*) e->memfn( e->memctx, e->states, sizeof(*ns) * ncnt );
e->states = ns;
e->states_mem = ncnt;
}
out = &e->states[ e->states_count++ ];
out->off = off & 0x0fffffff;
out->flags = 0;
out->instr = instr;
out->numiters = 0; /* iteration count is only set from stack */
}
static void rxPushIterCnt( rxExecute* e, uint32_t it )
{
if( e->iternum_count == e->iternum_mem )
{
size_t ncnt = e->iternum_mem * 2 + 16;
uint32_t* ni = (uint32_t*) e->memfn( e->memctx, e->iternum, sizeof(*ni) * ncnt );
e->iternum = ni;
e->iternum_mem = ncnt;
}
e->iternum[ e->iternum_count++ ] = it;
}
#ifdef NDEBUG
# define RX_POP_STATE( e ) ((e)->states_count--)
# define RX_POP_ITER_CNT( e ) ((e)->iternum_count--)
#else
# define RX_POP_STATE( e ) assert((e)->states_count-- < 0xffffffff)
# define RX_POP_ITER_CNT( e ) assert((e)->iternum_count-- < 0xffffffff)
#endif
static int rxExecDo( rxExecute* e, const rxChar* str, const rxChar* soff, size_t str_size )
{
const rxInstr* instrs = e->instrs;
const rxChar* chars = e->chars;
rxPushState( e, (uint32_t)( soff - str ), 0 );
while( e->states_count )
{
int match;
rxState* s = &RX_LAST_STATE( e );
const rxInstr* op = &instrs[ s->instr ];
RX_LOG(printf("[%d]", s->instr));
switch( op->op )
{
case RX_OP_MATCH_DONE:
RX_LOG(printf("MATCH_DONE\n"));
e->states_count = 0;
return 1;
case RX_OP_MATCH_CHARSET:
case RX_OP_MATCH_CHARSET_INV:
RX_LOG(printf("MATCH_CHARSET%s at=%d size=%d: ",op->op == RX_OP_MATCH_CHARSET_INV ? "_INV" : "",s->off,op->len));
match = str_size >= (size_t) ( s->off + 1 );
if( match )
{
match = rxMatchCharset( &str[ s->off ], &chars[ op->from ], op->len, ( e->flags & RCF_CASELESS ) != 0 );
if( op->op == RX_OP_MATCH_CHARSET_INV )
match = !match;
}
RX_LOG(printf("%s\n", match ? "MATCHED" : "FAILED"));
if( match )
{
/* replace current single path state with next */
s->off++;
s->instr++;
continue;
}
else goto did_not_match;
case RX_OP_MATCH_STRING:
RX_LOG(printf("MATCH_STRING at=%d size=%d: ",s->off,op->len));
match = str_size >= s->off + op->len;
if( match )
{
if( e->flags & RCF_CASELESS )
match = rxMemCaseEq( &str[ s->off ], &chars[ op->from ], op->len );
else
match = memcmp( &str[ s->off ], &chars[ op->from ], op->len ) == 0;
}
RX_LOG(printf("%s\n", match ? "MATCHED" : "FAILED"));
if( match )
{
/* replace current single path state with next */
s->off = ( s->off + op->len ) & 0x0fffffff;
s->instr++;
continue;
}
else goto did_not_match;
case RX_OP_MATCH_BACKREF:
RX_LOG(printf("MATCH_BACKREF at=%d slot=%d: ",s->off,op->from));
match = e->captures[ op->from ][0] != RX_NULL_OFFSET
&& e->captures[ op->from ][1] != RX_NULL_OFFSET;
{
size_t len = e->captures[ op->from ][1] - e->captures[ op->from ][0];
if( match )
{
match = str_size >= s->off + len;
if( match )
{
if( e->flags & RCF_CASELESS )
match = rxMemCaseEq( &str[ s->off ], &str[ e->captures[ op->from ][0] ], len );
else
match = memcmp( &str[ s->off ], &str[ e->captures[ op->from ][0] ], len ) == 0;
}
}
RX_LOG(printf("%s\n", match ? "MATCHED" : "FAILED"));
if( match )
{
/* replace current single path state with next */
s->off = ( s->off + len ) & 0x0fffffff;
s->instr++;
continue;
}
else goto did_not_match;
}
case RX_OP_MATCH_SLSTART:
RX_LOG(printf("MATCH_SLSTART at=%d: ",s->off));
match = s->off == 0;
if( e->flags & RCF_MULTILINE && s->off < str_size && ( str[ s->off ] == '\n' || str[ s->off ] == '\r' ) )
{
if( ((size_t)( s->off + 1 )) < str_size && str[ s->off ] == '\r' && str[ s->off + 1 ] == '\n' )
s->off++;
s->off++;
match = 1;
}
RX_LOG(printf("%s\n", match ? "MATCHED" : "FAILED"));
if( match )
{
s->instr++;
continue;
}
else goto did_not_match;