forked from okuoku/xitomatl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.sls
428 lines (388 loc) · 13 KB
/
regexp.sls
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
#!r6rs
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
;; LICENSE from the original collection this file is distributed with.
;; FIXME?: Is 'bos 2nd ret-val idea flawed for future negative-look-behind?
;; N.L.B. might have a 'bos, and it would fail at first until having moved down
;; the string, but maybe the 'bos idea doesn't work with that? But maybe the
;; N.L.B. matcher will compose correctly with the other matchers and it actually
;; will work.
;; TODO: Group capturing.
(library (xitomatl regexp)
(export
regexp-search
regexp-search/chunked
regexp-match
regexp-match/chunked
compile-SRE)
(import
(rename (rnrs)
(for-all andmap)
(exists ormap))
(only (xitomatl define)
define/AV)
(only (xitomatl predicates)
non-negative-integer?)
(only (xitomatl match)
match-lambda)
(xitomatl irregex)
(only (xitomatl irregex extras)
range-list-chunker)
(for (only (xitomatl macro-utils)
identifier-append)
expand))
(define regexp-search/chunked
(case-lambda
((sre chunker chunk)
(regexp-search/chunked sre chunker chunk ((chunker-get-start chunker) chunk)))
((sre chunker chunk idx)
(let ((m (compile-SRE sre)) (r (ref chunker)))
(let-values (((c o i) (r chunk idx)))
(let loop ((o o) (i i) (b i))
(let-values (((o^ i^) (m o i b r values)))
(if o^
(let ((im (make-irregex-match 0 '())))
;; TODO: Supporting groups will require different arguments to
;; above call to make-irregex-match.
(irregex-match-chunker-set! im chunker)
(irregex-match-start-chunk-set! im 0 o)
(irregex-match-start-index-set! im 0 i)
(irregex-match-end-chunk-set! im 0 o^)
(irregex-match-end-index-set! im 0 i^)
im)
(and (not (eq? i^ 'bos))
(let-values (((c o i) (r o i)))
(and c (loop o (+ 1 i) #F))))))))))))
(define regexp-search
(case-lambda
((sre str) (regexp-search sre str 0))
((sre str start) (regexp-search sre str start (string-length str)))
((sre str start end)
(regexp-search/chunked sre range-list-chunker (list str start end) start))))
(define (regexp-match/chunked sre chunker chunk . a)
(apply regexp-search/chunked `(: bos ,sre eos) chunker chunk a))
(define (regexp-match sre str . a)
(apply regexp-search `(: bos ,sre eos) str a))
;;----------------------------------------------------------------------------
(define/AV (ref chunker)
(define get-start (chunker-get-start chunker))
(define get-end (chunker-get-end chunker))
(define get-str (chunker-get-str chunker))
(define get-next (chunker-get-next chunker))
(lambda (chunk idx)
(let ((s (get-start chunk)) (e (get-end chunk)))
(cond ((and (<= s idx) (< idx e))
(values (string-ref (get-str chunk) idx) chunk idx))
((= idx e)
(let ((n (get-next chunk)))
(if n
(let ((ns (get-start n)))
(values (string-ref (get-str n) ns) n ns))
(values #F chunk idx))))
(else
(AV "invalid index" idx s e))))))
;;----------------------------------------------------------------------------
(define (M-char c)
;; TODO?: Use char-set of size 1 instead?
(lambda (o i b r k)
(let-values (((c2 o i) (r o i)))
(if (and c2 (char=? c2 c))
(k o (+ 1 i))
(k #F #F)))))
(define (M-str str)
(let ((l (string-length str)))
(lambda (o i b r k)
(let loop ((o o) (i i) (x 0))
(if (= x l)
(k o i)
(let-values (((c o i) (r o i)))
(if (and c (char=? c (string-ref str x)))
(loop o (+ 1 i) (+ 1 x))
(k #F #F))))))))
;; TODO: Optimize char sets to consolidate as much as possible.
(define (M-/ ranges)
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if (and c
(let loop ((ranges ranges))
(and (pair? ranges)
(or (char<=? (car ranges) c (cadr ranges))
(loop (cddr ranges))))))
(k o (+ 1 i))
(k #F #F)))))
(define (M-~ m)
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if c
(let loop ((m m))
(if (null? m)
(k o (+ 1 i))
;; NOTE: b can be ignored because all sub-matchers must be only
;; char-set matchers, and they never need b.
((car m) o i #F r
(lambda (o^ i^)
(if o^
(k #F #F)
(loop (cdr m)))))))
(k #F #F)))))
(define (M-& m)
(if (positive? (length m))
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if c
(let loop ((m m))
(if (null? m)
(k o (+ 1 i))
;; NOTE: It's okay to ignore b, as described above.
((car m) o i #F r
(lambda (o^ i^)
(if o^
(loop (cdr m))
(k #F i^))))))
(k #F #F))))
M-false))
;; TODO: Optimize compound matchers for (= 1 (length m)).
(define (M-: m)
(lambda (o i b r k)
(let loop ((m m) (o o) (i i) (b b))
(if (null? m)
(k o i)
((car m) o i b r
(lambda (o^ i^)
(if o^
(loop (cdr m) o^ i^ (and (eq? o^ o) b))
(k #F i^))))))))
(define (M-or m cs?)
;; TODO: Optimize successive chars as a char-set.
;; TODO: Only chars is a proper char-set.
(lambda (o i b r k)
(let loop ((m m) (il '()))
(define (fail-info)
(and (pair? il)
(andmap (lambda (x) (eq? 'bos x)) il)
'bos))
(if (null? m)
(k #F (fail-info))
((car m) o i b r
(lambda (o^ i^)
(if o^
(if cs?
(k o^ i^)
(let-values (((o^^ i^^) (k o^ i^)))
(if o^^
(values o^^ i^^)
(loop (cdr m) (cons i^^ il)))))
(loop (cdr m) (cons i^ il)))))))))
(define (M-** min max m)
(lambda (o i b r k)
(let loop ((o o) (i i) (b b) (n 0) (p '((#F . #F))))
(define (k/try i^)
(let back ((o o) (i i) (n n) (p p) (i^ i^))
(if (>= n min)
(let-values (((o^ i^) (k o i)))
(if o^
(values o^ i^)
(back (caar p) (cdar p) (- n 1) (cdr p) i^)))
(k #F i^))))
(if (and (or (not max) (< n max))
(not (and (eq? o (caar p)) (= i (cdar p)))))
(m o i b r
(lambda (o^ i^)
(if o^
(loop o^ i^ (and (eq? o^ o) b) (+ 1 n) (cons (cons o i) p))
(k/try i^))))
(k/try #F)))))
(define (M-**? min max m)
(lambda (o i b r k)
(let loop ((o o) (i i) (b b) (n 0) (po #F) (pi #F))
(define (try)
(if (and (or (not max) (< n max))
(not (and (eq? o po) (= i pi))))
(m o i b r
(lambda (o^ i^)
(if o^
(loop o^ i^ (and (eq? o^ o) b) (+ 1 n) o i)
(k #F #F))))
(k #F #F)))
(if (>= n min)
(let-values (((o^ i^) (k o i)))
(if o^
(values o^ i^)
(try)))
(try)))))
(define (M-look-ahead m)
(lambda (o i b r k)
(m o i b r
(lambda (o^ i^)
(if o^
(k o i)
(k #F i^))))))
(define (M-false o i b r k)
(k #F #F))
(define (M-char/pred pred)
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if (and c (pred c))
(k o (+ 1 i))
(k #F #F)))))
#;(define (M-proc proc)
(lambda (o i b r k)
(let-values (((o^ i^) (proc o i b r)))
(k o^ i^))))
;;----------------------------------------------------------------------------
(define (valid-range? min max)
(and (non-negative-integer? min)
(or (not max)
(and (non-negative-integer? max)
(<= min max)))))
(define (valid-char-ranges? x)
(or (null? x)
(and (pair? x) (pair? (cdr x))
(char? (car x)) (char? (cadr x))
(char<=? (car x) (cadr x))
(valid-char-ranges? (cddr x)))))
(define valid-char-set?
(match-lambda
((:predicate char?) #T)
(('/ c ...)
(valid-char-ranges? c))
(((:or 'or '~ '- '&) x ...)
(andmap valid-char-set? x))
(x (symbol? x)
(memq x named-char-sets))
(_ #F)))
(define/AV compile-SRE
;; TODO?: Don't use match-lambda and instead do it more manually, for efficiency.
(match-lambda
(x (symbol? x)
(or (named-M x)
(AV "unknown named SRE" x)))
(x (char? x)
(M-char x))
(x (string? x)
(M-str x))
(('/ c ...)
(valid-char-ranges? c)
(M-/ c))
(('~ cs ...)
(andmap valid-char-set? cs)
(M-~ (map compile-SRE cs)))
(('- cs ...)
(andmap valid-char-set? cs)
(if (pair? cs)
(compile-SRE `(& ,(car cs) (~ . ,(cdr cs))))
M-false))
(('& cs ...)
(andmap valid-char-set? cs)
(M-& (map compile-SRE cs)))
((': x ...)
(M-: (map compile-SRE x)))
(('or x ...)
(M-or (map compile-SRE x) (andmap valid-char-set? x)))
(('* x ...)
(compile-SRE `(** 0 #F . ,x)))
(('*? x ...)
(compile-SRE `(**? 0 #F . ,x)))
(('+ x ...)
(compile-SRE `(** 1 #F . ,x)))
#|(('+? x ...) ;; invalid symbol, what to do...?
(compile-SRE `(**? 1 #F . ,x)))|#
(('? x ...)
(compile-SRE `(** 0 1 . ,x)))
(('?? x ...)
(compile-SRE `(**? 0 1 . ,x)))
(('= n x ...)
(non-negative-integer? n)
(compile-SRE `(** ,n ,n . ,x)))
(('>= n x ...)
(non-negative-integer? n)
(compile-SRE `(** ,n #F . ,x)))
(('>=? n x ...)
(non-negative-integer? n)
(compile-SRE `(**? ,n #F . ,x)))
(('** min max x ...)
(valid-range? min max)
(M-** min max (compile-SRE `(: . ,x))))
(('**? min max x ...)
(valid-range? min max)
(M-**? min max (compile-SRE `(: . ,x))))
(('look-ahead x ...)
(M-look-ahead (compile-SRE `(: . ,x))))
(x (procedure? x)
x)
(#F
M-false)
(x
(AV "invalid SRE" x))))
;;----------------------------------------------------------------------------
(define (named-M name)
(cond ((assq name named-M-alist) => cdr)
(else #F)))
(define named-M-alist '())
(define named-char-sets '())
(define (named-M-add! name sre cs?)
(define M (compile-SRE sre))
(set! named-M-alist (cons (cons name M) named-M-alist))
(when cs?
(set! named-char-sets (cons name named-char-sets)))
M)
(define-syntax define-named-M
(lambda (stx)
(syntax-case stx ()
((kw ((SRE-name M-name) flag ... expr) . r)
(andmap identifier? (list #'SRE-name #'M-name))
(with-syntax ((cs? (and (memq ':cs (syntax->datum #'(flag ...))) #T)))
#'(begin
(define M-name (named-M-add! 'SRE-name expr cs?))
(kw . r))))
((kw (SRE-name . cr) . r)
(identifier? #'SRE-name)
(with-syntax ((M-name (identifier-append #'SRE-name "M-" #'SRE-name)))
#'(kw ((SRE-name M-name) . cr) . r)))
((_)
#'(begin)))))
(define-named-M
;; NOTE: These are pre-compiled, so they won't work for possible future
;; compile-SRE needing to recursively pass-through info. So, they should
;; all be patterns which do not involve anything which would require that.
(bos
(lambda (o i b r k)
(if (and b (= i b))
(k o i)
(k #F 'bos))))
(eos
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if c
(k #F #F)
(k o i)))))
(any :cs
(lambda (o i b r k)
(let-values (((c o i) (r o i)))
(if c
(k o (+ 1 i))
(k #F #F)))))
(newline-char :cs
'(or #\xA #\xD #\x85 #\xC #\x2028 #\x2029))
(nonl :cs
'(- any newline-char))
(alphabetic :cs
(M-char/pred char-alphabetic?))
(numeric :cs
(M-char/pred char-numeric?))
(whitespace :cs
(M-char/pred char-whitespace?))
(digit :cs
'(/ #\0 #\9))
(hex-digit :cs
'(or digit (/ #\a #\f #\A #\F))))
(define (M-gen-cat gc)
(M-char/pred (lambda (c) (eq? gc (char-general-category c)))))
(define-syntax define-gen-cat-M
(syntax-rules ()
((_ name ...)
(define-named-M
(name :cs (M-gen-cat 'name))
...))))
(define-gen-cat-M Lu Ll Lt Lm Lo Mn Mc Me Nd Nl No Pc Pd Ps Pe
Pi Pf Po Sm Sc Sk So Zs Zl Zp Cc Cf Cs Co Cn)
)