/
irregex.scm
2718 lines (2562 loc) · 105 KB
/
irregex.scm
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
;;;; irregex.scm -- IrRegular Expressions
;;
;; Copyright (c) 2005-2008 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; At this moment there was a loud ring at the bell, and I could
;; hear Mrs. Hudson, our landlady, raising her voice in a wail of
;; expostulation and dismay.
;;
;; "By heaven, Holmes," I said, half rising, "I believe that
;; they are really after us."
;;
;; "No, it's not quite so bad as that. It is the unofficial
;; force, -- the Baker Street irregulars."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; History
;;
;; 0.6.2: 2008/07/26 - minor bugfixes, allow global disabling of utf8 mode,
;; friendlier error messages in parsing, \Q..\E support
;; 0.6.1: 2008/07/21 - added utf8 mode, more utils, bugfixes
;; 0.6: 2008/05/01 - most of PCRE supported
;; 0.5: 2008/04/24 - fully portable R4RS, many PCRE features implemented
;; 0.4: 2008/04/17 - rewriting NFA to use efficient closure compilation,
;; normal strings only, but all of the spencer tests pass
;; 0.3: 2008/03/10 - adding DFA converter (normal strings only)
;; 0.2: 2005/09/27 - adding irregex-opt (like elisp's regexp-opt) utility
;; 0.1: 2005/08/18 - simple NFA interpreter over abstract chunked strings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define irregex-tag '*irregex-tag*)
(define (make-irregex dfa dfa/search dfa/extract nfa flags
submatches lengths names)
(vector irregex-tag dfa dfa/search dfa/extract nfa flags
submatches lengths names))
(define (irregex? obj)
(and (vector? obj)
(= 9 (vector-length obj))
(eq? irregex-tag (vector-ref obj 0))))
(define (irregex-dfa x) (vector-ref x 1))
(define (irregex-dfa/search x) (vector-ref x 2))
(define (irregex-dfa/extract x) (vector-ref x 3))
(define (irregex-nfa x) (vector-ref x 4))
(define (irregex-flags x) (vector-ref x 5))
(define (irregex-submatches x) (vector-ref x 6))
(define (irregex-lengths x) (vector-ref x 7))
(define (irregex-names x) (vector-ref x 8))
(define (irregex-new-matches irx)
(make-irregex-match #f (irregex-submatches irx) (irregex-names irx)))
(define (irregex-reset-matches! m)
(do ((i (- (vector-length m) 1) (- i 1)))
((<= i 3) m)
(vector-set! m i #f)))
(define irregex-match-tag '*irregex-match-tag*)
(define (irregex-match-data? obj)
(and (vector? obj)
(>= (vector-length obj) 5)
(eq? irregex-match-tag (vector-ref obj 0))))
(define (make-irregex-match str count names)
(let ((res (make-vector (+ (* 2 (+ 1 count)) 3) #f)))
(vector-set! res 0 irregex-match-tag)
(vector-set! res 1 str)
(vector-set! res 2 names)
res))
(define (irregex-match-num-submatches m)
(- (quotient (- (vector-length m) 3) 2) 1))
(define (irregex-match-string m)
(vector-ref m 1))
(define (irregex-match-names m)
(vector-ref m 2))
(define (irregex-match-string-set! m str)
(vector-set! m 1 str))
(define (irregex-match-start-index m n)
(vector-ref m (+ 3 (* n 2))))
(define (irregex-match-end-index m n)
(vector-ref m (+ 4 (* n 2))))
(define (irregex-match-start-index-set! m n start)
(vector-set! m (+ 3 (* n 2)) start))
(define (irregex-match-end-index-set! m n end)
(vector-set! m (+ 4 (* n 2)) end))
(define (irregex-match-index m opt)
(if (pair? opt)
(cond ((number? (car opt)) (car opt))
((assq (car opt) (irregex-match-names m)) => cdr)
(else (error "unknown match name" (car opt))))
0))
(define (irregex-match-valid-index? m n)
(and (< (+ 3 (* n 2)) (vector-length m))
(vector-ref m (+ 4 (* n 2)))))
(define (irregex-match-substring m . opt)
(let ((n (irregex-match-index m opt)))
(and (irregex-match-valid-index? m n)
(substring (irregex-match-string m)
(vector-ref m (+ 3 (* n 2)))
(vector-ref m (+ 4 (* n 2)))))))
(define (irregex-match-start m . opt)
(let ((n (irregex-match-index m opt)))
(and (irregex-match-valid-index? m n)
(vector-ref m (+ 3 (* n 2))))))
(define (irregex-match-end m . opt)
(irregex-match-valid-index? m (irregex-match-index m opt)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; string utilities
;;;; Unicode version (skip surrogates)
(define *all-chars*
`(/ ,(integer->char 0) ,(integer->char #xD7FF)
,(integer->char #xE000) ,(integer->char #x10FFFF)))
;;;; ASCII version, offset to not assume 0-255
;; (define *all-chars* `(/ ,(integer->char (- (char->integer #\space) 32)) ,(integer->char (+ (char->integer #\space) 223))))
;; set to #f to ignore even an explicit request for utf8 handling
(define *allow-utf8-mode?* #t)
;; (define *named-char-properties* '())
(define (string-scan-char str c . o)
(let ((end (string-length str)))
(let scan ((i (if (pair? o) (car o) 0)))
(cond ((= i end) #f)
((eqv? c (string-ref str i)) i)
(else (scan (+ i 1)))))))
(define (string-scan-char-escape str c . o)
(let ((end (string-length str)))
(let scan ((i (if (pair? o) (car o) 0)))
(cond ((= i end) #f)
((eqv? c (string-ref str i)) i)
((eqv? c #\\) (scan (+ i 2)))
(else (scan (+ i 1)))))))
(define (string-scan-pred str pred . o)
(let ((end (string-length str)))
(let scan ((i (if (pair? o) (car o) 0)))
(cond ((= i end) #f)
((pred (string-ref str i)) i)
(else (scan (+ i 1)))))))
(define (string-split-char str c)
(let ((end (string-length str)))
(let lp ((i 0) (from 0) (res '()))
(define (collect) (cons (substring str from i) res))
(cond ((>= i end) (reverse (collect)))
((eqv? c (string-ref str i)) (lp (+ i 1) (+ i 1) (collect)))
(else (lp (+ i 1) from res))))))
(define (char-alphanumeric? c)
(or (char-alphabetic? c) (char-numeric? c)))
;; SRFI-13 extracts
(define (%%string-copy! to tstart from fstart fend)
(do ((i fstart (+ i 1))
(j tstart (+ j 1)))
((>= i fend))
(string-set! to j (string-ref from i))))
(define (string-cat-reverse string-list)
(string-cat-reverse/aux
(fold (lambda (s a) (+ (string-length s) a)) 0 string-list)
string-list))
(define (string-cat-reverse/aux len string-list)
(let ((res (make-string len)))
(let lp ((i len) (ls string-list))
(if (pair? ls)
(let* ((s (car ls))
(slen (string-length s))
(i (- i slen)))
(%%string-copy! res i s 0 slen)
(lp i (cdr ls)))))
res))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; list utilities
;; like the one-arg IOTA case
(define (zero-to n)
(if (<= n 0)
'()
(let lp ((i (- n 1)) (res '()))
(if (zero? i) (cons 0 res) (lp (- i 1) (cons i res))))))
;; take the head of list FROM up to but not including TO, which must
;; be a tail of the list
(define (take-up-to from to)
(let lp ((ls from) (res '()))
(if (and (pair? ls) (not (eq? ls to)))
(lp (cdr ls) (cons (car ls) res))
(reverse res))))
;; SRFI-1 extracts (simplified 1-ary versions)
(define (find pred ls)
(cond ((find-tail pred ls) => car)
(else #f)))
(define (find-tail pred ls)
(let lp ((ls ls))
(cond ((null? ls) #f)
((pred (car ls)) ls)
(else (lp (cdr ls))))))
(define (last ls)
(if (not (pair? ls))
(error "can't take last of empty list" ls)
(let lp ((ls ls))
(if (pair? (cdr ls))
(lp (cdr ls))
(car ls)))))
(define (any pred ls)
(and (pair? ls)
(let lp ((head (car ls)) (tail (cdr ls)))
(if (null? tail)
(pred head)
(or (pred head) (lp (car tail) (cdr tail)))))))
(define (every pred ls)
(or (null? ls)
(let lp ((head (car ls)) (tail (cdr ls)))
(if (null? tail)
(pred head)
(and (pred head) (lp (car tail) (cdr tail)))))))
(define (fold kons knil ls)
(let lp ((ls ls) (res knil))
(if (null? ls)
res
(lp (cdr ls) (kons (car ls) res)))))
(define (filter pred ls)
(let lp ((ls ls) (res '()))
(if (null? ls)
(reverse res)
(lp (cdr ls) (if (pred (car ls)) (cons (car ls) res) res)))))
(define (remove pred ls)
(let lp ((ls ls) (res '()))
(if (null? ls)
(reverse res)
(lp (cdr ls) (if (pred (car ls)) res (cons (car ls) res))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; flags
(define (bit-shr n i)
(quotient n (expt 2 i)))
(define (bit-shl n i)
(* n (expt 2 i)))
(define (bit-not n) (- #xFFFF n))
(define (bit-ior a b)
(cond
((zero? a) b)
((zero? b) a)
(else
(+ (if (or (odd? a) (odd? b)) 1 0)
(* 2 (bit-ior (quotient a 2) (quotient b 2)))))))
(define (bit-and a b)
(cond
((zero? a) 0)
((zero? b) 0)
(else
(+ (if (and (odd? a) (odd? b)) 1 0)
(* 2 (bit-and (quotient a 2) (quotient b 2)))))))
(define (flag-set? flags i)
(= i (bit-and flags i)))
(define (flag-join a b)
(if b (bit-ior a b) a))
(define (flag-clear a b)
(bit-and a (bit-not b)))
(define ~none 0)
(define ~searcher? 1)
(define ~consumer? 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; parsing
(define ~save? 1)
(define ~case-insensitive? 2)
(define ~multi-line? 4)
(define ~single-line? 8)
(define ~ignore-space? 16)
(define ~utf8? 32)
(define (symbol-list->flags ls)
(let lp ((ls ls) (res ~none))
(if (not (pair? ls))
res
(lp (cdr ls)
(flag-join
res
(case (car ls)
((i ci case-insensitive) ~case-insensitive?)
((m multi-line) ~multi-line?)
((s single-line) ~single-line?)
((x ignore-space) ~ignore-space?)
((u utf8) ~utf8?)
(else #f)))))))
(define (string->sre str . o)
(let ((end (string-length str))
(flags (symbol-list->flags o)))
(let lp ((i 0) (from 0) (flags flags) (res '()) (st '()))
;; handle case sensitivity at the literal char/string level
(define (cased-char ch)
(if (and (flag-set? flags ~case-insensitive?)
(char-alphabetic? ch))
`(or ,ch ,(char-altcase ch))
ch))
(define (cased-string str)
(if (flag-set? flags ~case-insensitive?)
(sre-sequence (map cased-char (string->list str)))
str))
;; accumulate the substring from..i as literal text
(define (collect)
(if (= i from) res (cons (cased-string (substring str from i)) res)))
;; like collect but breaks off the last single character when
;; collecting literal data, as the argument to ?/*/+ etc.
(define (collect/single)
(let* ((utf8? (flag-set? flags ~utf8?))
(j (if (and utf8? (> i 1))
(utf8-backup-to-initial-char str (- i 1))
(- i 1))))
(cond
((< j from)
res)
(else
(let ((c (cased-char (if utf8?
(utf8-string-ref str j (- i j) )
(string-ref str j)))))
(cond
((= j from)
(cons c res))
(else
(cons c
(cons (cased-string (substring str from j))
res)))))))))
;; collects for use as a result, reversing and grouping OR
;; terms, and some ugly tweaking of `function-like' groups and
;; conditionals
(define (collect/terms)
(let* ((ls (collect))
(func
(and (pair? ls)
(memq (last ls)
'(atomic if look-ahead neg-look-ahead
look-behind neg-look-behind submatch-named
w/utf8 w/noutf8))))
(prefix (if (and func (eq? 'submatch-named (car func)))
(list 'submatch-named (cadr (reverse ls)))
(and func (list (car func)))))
(ls (if func
(if (eq? 'submatch-named (car func))
(reverse (cddr (reverse ls)))
(reverse (cdr (reverse ls))))
ls)))
(let lp ((ls ls) (term '()) (res '()))
(define (shift)
(cons (sre-sequence term) res))
(cond
((null? ls)
(let* ((res (sre-alternate (shift)))
(res (if (flag-set? flags ~save?)
(list 'submatch res)
res)))
(if prefix
(if (eq? 'if (car prefix))
(cond
((not (pair? res))
'epsilon)
((memq (car res)
'(look-ahead neg-look-ahead
look-behind neg-look-behind))
res)
((eq? 'seq (car res))
`(if ,(cadr res)
,(if (pair? (cdr res))
(sre-sequence (cddr res))
'epsilon)))
(else
`(if ,(cadadr res)
,(if (pair? (cdr res))
(sre-sequence (cddadr res))
'epsilon)
,(sre-alternate
(if (pair? (cdr res)) (cddr res) '())))))
`(,@prefix ,res))
res)))
((eq? 'or (car ls)) (lp (cdr ls) '() (shift)))
(else (lp (cdr ls) (cons (car ls) term) res))))))
(define (save)
(cons (cons flags (collect)) st))
;; main parsing
(if (>= i end)
(if (pair? st)
(error "unterminated parenthesis in regexp" str)
(collect/terms))
(let ((c (string-ref str i)))
(case c
((#\.)
(lp (+ i 1) (+ i 1) flags
(cons (if (flag-set? flags ~single-line?) 'any 'nonl)
(collect))
st))
((#\?)
(let ((res (collect/single)))
(if (null? res)
(error "? can't follow empty sre" str res)
(let ((x (car res)))
(lp (+ i 1)
(+ i 1)
flags
(cons
(if (pair? x)
(case (car x)
((*) `(*? ,@(cdr x)))
((+) `(**? 1 #f ,@(cdr x)))
((?) `(?? ,@(cdr x)))
((**) `(**? ,@(cdr x)))
((=) `(**? ,(cadr x) ,@(cdr x)))
((>=) `(**? ,(cadr x) #f ,@(cddr x)))
(else `(? ,x)))
`(? ,x))
(cdr res))
st)))))
((#\+ #\*)
(let* ((res (collect/single))
(x (car res))
(op (string->symbol (string c))))
(cond
((sre-repeater? x)
(error "duplicate repetition (e.g. **) in sre" str res))
((sre-empty? x)
(error "can't repeat empty sre (e.g. ()*)" str res))
(else
(lp (+ i 1) (+ i 1) flags
(cons (list op x) (cdr res))
st)))))
((#\()
(cond
((>= (+ i 1) end)
(error "unterminated parenthesis in regexp" str))
((not (eqv? #\? (string-ref str (+ i 1))))
(lp (+ i 1) (+ i 1) (flag-join flags ~save?) '() (save)))
((>= (+ i 2) end)
(error "unterminated parenthesis in regexp" str))
(else
(case (string-ref str (+ i 2))
((#\#)
(let ((j (string-scan-char str #\) (+ i 3))))
(lp (+ j i) (+ j 1) flags (collect) st)))
((#\:)
(lp (+ i 3) (+ i 3) (flag-clear flags ~save?) '() (save)))
((#\=)
(lp (+ i 3) (+ i 3) (flag-clear flags ~save?)
'(look-ahead) (save)))
((#\!)
(lp (+ i 3) (+ i 3) (flag-clear flags ~save?)
'(neg-look-ahead) (save)))
((#\<)
(cond
((>= (+ i 3) end)
(error "unterminated parenthesis in regexp" str))
(else
(case (string-ref str (+ i 3))
((#\=)
(lp (+ i 4) (+ i 4) (flag-clear flags ~save?)
'(look-behind) (save)))
((#\!)
(lp (+ i 4) (+ i 4) (flag-clear flags ~save?)
'(neg-look-behind) (save)))
(else
(let ((j (and (char-alphabetic?
(string-ref str (+ i 3)))
(string-scan-char str #\> (+ i 4)))))
(if j
(lp (+ j 1) (+ j 1) (flag-clear flags ~save?)
`(,(string->symbol (substring str (+ i 3) j))
submatch-named)
(save))
(error "invalid (?< sequence" str))))))))
((#\>)
(lp (+ i 3) (+ i 3) (flag-clear flags ~save?)
'(atomic) (save)))
;;((#\' #\P) ; named subpatterns
;; )
;;((#\R) ; recursion
;; )
((#\()
(cond
((>= (+ i 3) end)
(error "unterminated parenthesis in regexp" str))
((char-numeric? (string-ref str (+ i 3)))
(let* ((j (string-scan-char str #\) (+ i 3)))
(n (string->number (substring str (+ i 3) j))))
(if (not n)
(error "invalid conditional reference" str)
(lp (+ j 1) (+ j 1) (flag-clear flags ~save?)
`(,n if) (save)))))
((char-alphabetic? (string-ref str (+ i 3)))
(let* ((j (string-scan-char str #\) (+ i 3)))
(s (string->symbol (substring str (+ i 3) j))))
(lp (+ j 1) (+ j 1) (flag-clear flags ~save?)
`(,s if) (save))))
(else
(lp (+ i 2) (+ i 2) (flag-clear flags ~save?)
'(if) (save)))))
((#\{)
(error "unsupported Perl-style cluster" str))
(else
(let ((old-flags flags))
(let lp2 ((j (+ i 2)) (flags flags) (invert? #f))
(define (join x)
((if invert? flag-clear flag-join) flags x))
(define (new-res res)
(let ((before (flag-set? old-flags ~utf8?))
(after (flag-set? flags ~utf8?)))
(if (eq? before after)
res
(cons (if after 'w/utf8 'w/noutf8) res))))
(cond
((>= j end)
(error "incomplete cluster" str i))
(else
(case (string-ref str j)
((#\i)
(lp2 (+ j 1) (join ~case-insensitive?) invert?))
((#\m)
(lp2 (+ j 1) (join ~multi-line?) invert?))
((#\x)
(lp2 (+ j 1) (join ~ignore-space?) invert?))
((#\u)
(if *allow-utf8-mode?*
(lp2 (+ j 1) (join ~utf8?) invert?)
(lp2 (+ j 1) flags invert?)))
((#\-)
(lp2 (+ j 1) flags (not invert?)))
((#\))
(lp (+ j 1) (+ j 1) flags (new-res (collect))
st))
((#\:)
(lp (+ j 1) (+ j 1) flags (new-res '())
(cons (cons old-flags (collect)) st)))
(else
(error "unknown regex cluster modifier" str)
)))))))))))
((#\))
(if (null? st)
(error "too many )'s in regexp" str)
(lp (+ i 1)
(+ i 1)
(caar st)
(cons (collect/terms) (cdar st))
(cdr st))))
((#\[)
(apply
(lambda (sre j)
(lp (+ j 1) (+ j 1) flags (cons sre (collect)) st))
(string-parse-cset str (+ i 1) flags)))
((#\{)
(if (or (>= (+ i 1) end)
(not (or (char-numeric? (string-ref str (+ i 1)))
(eqv? #\, (string-ref str (+ i 1))))))
(lp (+ i 1) from flags res st)
(let* ((res (collect/single))
(x (car res))
(tail (cdr res))
(j (string-scan-char str #\} (+ i 1)))
(s2 (string-split-char (substring str (+ i 1) j) #\,))
(n (or (string->number (car s2)) 0))
(m (and (pair? (cdr s2)) (string->number (cadr s2)))))
(cond
((null? (cdr s2))
(lp (+ j 1) (+ j 1) flags `((= ,n ,x) ,@tail) st))
(m
(lp (+ j 1) (+ j 1) flags `((** ,n ,m ,x) ,@tail) st))
(else
(lp (+ j 1) (+ j 1) flags `((>= ,n ,x) ,@tail) st)
)))))
((#\\)
(cond
((>= (+ i 1) end)
(error "incomplete escape sequence" str))
(else
(let ((c (string-ref str (+ i 1))))
(case c
((#\d)
(lp (+ i 2) (+ i 2) flags `(numeric ,@(collect)) st))
((#\D)
(lp (+ i 2) (+ i 2) flags `((~ numeric) ,@(collect)) st))
((#\s)
(lp (+ i 2) (+ i 2) flags `(space ,@(collect)) st))
((#\S)
(lp (+ i 2) (+ i 2) flags `((~ space) ,@(collect)) st))
((#\w)
(lp (+ i 2) (+ i 2) flags
`((or alphanumeric ("_")) ,@(collect)) st))
((#\W)
(lp (+ i 2) (+ i 2) flags
`((~ (or alphanumeric ("_"))) ,@(collect)) st))
((#\b)
(lp (+ i 2) (+ i 2) flags
`((or bow eow) ,@(collect)) st))
((#\B)
(lp (+ i 2) (+ i 2) flags `(nwb ,@(collect)) st))
((#\A)
(lp (+ i 2) (+ i 2) flags `(bos ,@(collect)) st))
((#\Z)
(lp (+ i 2) (+ i 2) flags
`((? #\newline) eos ,@(collect)) st))
((#\z)
(lp (+ i 2) (+ i 2) flags `(eos ,@(collect)) st))
((#\R)
(lp (+ i 2) (+ i 2) flags `(newline ,@(collect)) st))
((#\K)
(lp (+ i 2) (+ i 2) flags `(reset ,@(collect)) st))
;; these two are from Emacs and TRE, but not PCRE
((#\<)
(lp (+ i 2) (+ i 2) flags `(bow ,@(collect)) st))
((#\>)
(lp (+ i 2) (+ i 2) flags `(eow ,@(collect)) st))
((#\x)
(apply
(lambda (ch j)
(lp (+ j 1) (+ j 1) flags `(,ch ,@(collect)) st))
(string-parse-hex-escape str (+ i 2) end)))
((#\k)
(let ((c (string-ref str (+ i 2))))
(if (not (memv c '(#\< #\{ #\')))
(error "bad \\k usage, expected \\k<...>" str)
(let* ((terminal (char-mirror c))
(j (string-scan-char str terminal (+ i 2)))
(s (and j (substring str (+ i 3) j)))
(backref
(if (flag-set? flags ~case-insensitive?)
'backref-ci
'backref)))
(if (not j)
(error "interminated named backref" str)
(lp (+ j 1) (+ j 1) flags
`((,backref ,(string->symbol s))
,@(collect))
st))))))
((#\Q) ;; \Q..\E escapes
(let ((res (collect)))
(let lp2 ((j (+ i 2)))
(cond
((>= j end)
(lp j (+ i 2) flags res st))
((eqv? #\\ (string-ref str j))
(cond
((>= (+ j 1) end)
(lp (+ j 1) (+ i 2) flags res st))
((eqv? #\E (string-ref str (+ j 1)))
(lp (+ j 2) (+ j 2) flags
(cons (substring str (+ i 2) j) res) st))
(else
(lp2 (+ j 2)))))
(else
(lp2 (+ j 1)))))))
;;((#\p) ; XXXX unicode properties
;; )
;;((#\P)
;; )
(else
(cond
((char-numeric? c)
(let* ((j (or (string-scan-pred
str
(lambda (c) (not (char-numeric? c)))
(+ i 2))
end))
(backref
(if (flag-set? flags ~case-insensitive?)
'backref-ci
'backref))
(res `((,backref ,(string->number
(substring str (+ i 1) j)))
,@(collect))))
(lp j j flags res st)))
((char-alphabetic? c)
(let ((cell (assv c posix-escape-sequences)))
(if cell
(lp (+ i 2) (+ i 2) flags
(cons (cdr cell) (collect)) st)
(error "unknown escape sequence" str c))))
(else
(lp (+ i 2) (+ i 1) flags (collect) st)))))))))
((#\|)
(lp (+ i 1) (+ i 1) flags (cons 'or (collect)) st))
((#\^)
(let ((sym (if (flag-set? flags ~multi-line?) 'bol 'bos)))
(lp (+ i 1) (+ i 1) flags (cons sym (collect)) st)))
((#\$)
(let ((sym (if (flag-set? flags ~multi-line?) 'eol 'eos)))
(lp (+ i 1) (+ i 1) flags (cons sym (collect)) st)))
((#\space)
(if (flag-set? flags ~ignore-space?)
(lp (+ i 1) (+ i 1) flags (collect) st)
(lp (+ i 1) from flags res st)))
((#\#)
(if (flag-set? flags ~ignore-space?)
(let ((j (or (string-scan-char str #\newline (+ i 1))
(- end 1))))
(lp (+ j 1) (+ j 1) flags (collect) st))
(lp (+ i 1) from flags res st)))
(else
(lp (+ i 1) from flags res st))))))))
(define posix-escape-sequences
`((#\n . #\newline)
(#\r . ,(integer->char (+ (char->integer #\newline) 3)))
(#\t . ,(integer->char (- (char->integer #\newline) 1)))
(#\a . ,(integer->char (- (char->integer #\newline) 3)))
(#\e . ,(integer->char (+ (char->integer #\newline) #x11)))
(#\f . ,(integer->char (+ (char->integer #\newline) 2)))
))
(define (char-altcase c)
(if (char-upper-case? c) (char-downcase c) (char-upcase c)))
(define (char-mirror c)
(case c ((#\<) #\>) ((#\{) #\}) ((#\() #\)) ((#\[) #\]) (else c)))
(define (string-parse-hex-escape str i end)
(cond
((>= i end)
(error "incomplete hex escape" str i))
((eqv? #\{ (string-ref str i))
(let ((j (string-scan-char-escape str #\} (+ i 1))))
(if (not j)
(error "incomplete hex brace escape" str i)
(let* ((s (substring str (+ i 1) j))
(n (string->number s 16)))
(if n
(list (integer->char n) j)
(error "bad hex brace escape" s))))))
((>= (+ i 1) end)
(error "incomplete hex escape" str i))
(else
(let* ((s (substring str i (+ i 2)))
(n (string->number s 16)))
(if n
(list (integer->char n) (+ i 2))
(error "bad hex escape" s))))))
(define (string-parse-cset str start flags)
(let ((end (string-length str))
(invert? (eqv? #\^ (string-ref str start)))
(utf8? (flag-set? flags ~utf8?)))
(define (go i chars ranges)
(if (>= i end)
(error "incomplete char set")
(let ((c (string-ref str i)))
(case c
((#\])
(if (and (null? chars) (null? ranges))
(go (+ i 1) (cons #\] chars) ranges)
(let ((ci? (flag-set? flags ~case-insensitive?))
(hi-chars (if utf8? (filter high-char? chars) '()))
(chars (if utf8? (remove high-char? chars) chars)))
(list
((lambda (res)
(if invert? (cons '~ res) (sre-alternate res)))
(append
hi-chars
(if (pair? chars)
(list
(list (list->string
((if ci?
cset-case-insensitive
(lambda (x) x))
(reverse chars)))))
'())
(if (pair? ranges)
(let ((res (if ci?
(cset-case-insensitive
(reverse ranges))
(reverse ranges))))
(list (cons '/ (alist->plist res))))
'())))
i))))
((#\-)
(cond
((or (= i start)
(and (= i (+ start 1)) (eqv? #\^ (string-ref str start)))
(eqv? #\] (string-ref str (+ i 1))))
(go (+ i 1) (cons c chars) ranges))
((null? chars)
(error "bad char-set"))
(else
(let* ((c1 (car chars))
(c2 (string-ref str (+ i 1))))
(apply
(lambda (c2 j)
(if (char<? c2 c1)
(error "inverted range in char-set" c1 c2)
(go j (cdr chars) (cons (cons c1 c2) ranges))))
(cond
((and (eqv? #\\ c2) (assv c2 posix-escape-sequences))
=> (lambda (x) (list (cdr x) (+ i 3))))
((and (eqv? #\\ c2)
(eqv? (string-ref str (+ i 2)) #\x))
(string-parse-hex-escape str (+ i 3) end))
((and utf8? (<= #x80 (char->integer c2) #xFF))
(let ((len (utf8-start-char->length c2)))
(list (utf8-string-ref str (+ i 1) len) (+ i 1 len))))
(else
(list c2 (+ i 2)))))))))
((#\[)
(let* ((inv? (eqv? #\^ (string-ref str (+ i 1))))
(i2 (if inv? (+ i 2) (+ i 1))))
(case (string-ref str i2)
((#\:)
(let ((j (string-scan-char str #\: (+ i2 1))))
(if (or (not j) (not (eqv? #\] (string-ref str (+ j 1)))))
(error "incomplete character class" str)
(let* ((cset (sre->cset
(string->symbol
(substring str (+ i2 1) j))))
(cset (if inv? (cset-complement cset) cset)))
(go (+ j 2)
(append (filter char? cset) chars)
(append (filter pair? cset) ranges))))))
((#\= #\.)
(error "collating sequences not supported" str))
(else
(go (+ i 1) (cons #\[ chars) ranges)))))
((#\\)
(let ((c (string-ref str (+ i 1))))
(case c
((#\d #\D #\s #\S #\w #\W)
(let ((cset (sre->cset (string->sre (string #\\ c)))))
(go (+ i 2)
(append (filter char? cset) chars)
(append (filter pair? cset) ranges))))
((#\x)
(apply
(lambda (ch j)
(go j (cons ch chars) ranges))
(string-parse-hex-escape str (+ i 2) end)))
(else
(let ((c (cond ((assv c posix-escape-sequences) => cdr)
(else c))))
(go (+ i 2)
(cons (string-ref str (+ i 1)) (cons c chars))
ranges))))))
(else
(if (and utf8? (<= #x80 (char->integer c) #xFF))
(let ((len (utf8-start-char->length c)))
(go (+ i len)
(cons (utf8-string-ref str i len) chars)
ranges))
(go (+ i 1) (cons c chars) ranges)))))))
(if invert?
(go (+ start 1)
(if (flag-set? flags ~multi-line?) '(#\newline) '())
'())
(go start '() '()))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; utf8 utilities
;; Here are some hairy optimizations that need to be documented
;; better. Thanks to these, we never do any utf8 processing once the
;; regexp is compiled.
;; two chars: ab..ef
;; a[b..xFF]|[b-d][x80..xFF]|e[x80..xFF]
;; three chars: abc..ghi
;; ab[c..xFF]|a[d..xFF][x80..xFF]|
;; [b..f][x80..xFF][x80..xFF]|
;; g[x80..g][x80..xFF]|gh[x80..i]
;; four chars: abcd..ghij
;; abc[d..xFF]|ab[d..xFF][x80..xFF]|a[c..xFF][x80..xFF][x80..xFF]|
;; [b..f][x80..xFF][x80..xFF][x80..xFF]|
;; g[x80..g][x80..xFF][x80..xFF]|gh[x80..h][x80..xFF]|ghi[x80..j]
(define (high-char? c) (<= #x80 (char->integer c)))
;; number of total bytes in a utf8 char given the 1st byte
(define utf8-start-char->length
(let ((table '#(
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 0x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 1x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 2x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 3x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 4x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 5x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 6x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 7x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 8x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; 9x
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; ax
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; bx
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ; cx
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ; dx
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ; ex
4 4 4 4 4 4 4 4 5 5 5 5 6 6 0 0 ; fx
)))
(lambda (c) (vector-ref table (char->integer c)))))
(define (utf8-string-ref str i len)
(define (byte n) (char->integer (string-ref str n)))
(case len
((1) ; shouldn't happen in this module
(string-ref str i))
((2)
(integer->char
(+ (bit-shl (bit-and (byte i) #b00011111) 6)
(bit-and (byte (+ i 1)) #b00111111))))
((3)
(integer->char
(+ (bit-shl (bit-and (byte i) #b00001111) 12)
(bit-shl (bit-and (byte (+ i 1)) #b00111111) 6)
(bit-and (byte (+ i 2)) #b00111111))))
((4)
(integer->char
(+ (bit-shl (bit-and (byte i) #b00000111) 18)
(bit-shl (bit-and (byte (+ i 1)) #b00111111) 12)
(bit-shl (bit-and (byte (+ i 2)) #b00111111) 6)
(bit-and (byte (+ i 3)) #b00111111))))
(else
(error "invalid utf8 length" str len i))))
(define (utf8-backup-to-initial-char str i)
(let lp ((i i))
(if (= i 0)
0
(let ((c (char->integer (string-ref str i))))
(if (or (< c #x80) (>= c #xC0))
i
(lp (- i 1)))))))
(define (utf8-lowest-digit-of-length len)
(case len
((1) 0) ((2) #xC0) ((3) #xE0) ((4) #xF0)
(else (error "invalid utf8 length" len))))
(define (utf8-highest-digit-of-length len)
(case len
((1) #x7F) ((2) #xDF) ((3) #xEF) ((4) #xF7)
(else (error "invalid utf8 length" len))))
(define (char->utf8-list c)
(let ((i (char->integer c)))
(cond
((<= i #x7F) (list i))
((<= i #x7FF)
(list (bit-ior #b11000000 (bit-shr i 6))
(bit-ior #b10000000 (bit-and i #b111111))))
((<= i #xFFFF)
(list (bit-ior #b11100000 (bit-shr i 12))
(bit-ior #b10000000 (bit-and (bit-shr i 6) #b111111))
(bit-ior #b10000000 (bit-and i #b111111))))
((<= i #x1FFFFF)
(list (bit-ior #b11110000 (bit-shr i 18))
(bit-ior #b10000000 (bit-and (bit-shr i 12) #b111111))
(bit-ior #b10000000 (bit-and (bit-shr i 6) #b111111))
(bit-ior #b10000000 (bit-and i #b111111))))
(else (error "unicode codepoint out of range:" i)))))
(define (unicode-range->utf8-pattern lo hi)
(let ((lo-ls (char->utf8-list lo))
(hi-ls (char->utf8-list hi)))
(if (not (= (length lo-ls) (length hi-ls)))
(sre-alternate (list (unicode-range-climb-digits lo-ls hi-ls)