Debian Codegraph @

%3a115/regexp-impl.scm

raw symbols SCIP
1 ;; regexp.scm -- simple non-bactracking NFA implementation
2 ;; Copyright (c) 2013-2016 Alex Shinn. All rights reserved.
3 ;; BSD-style license: http://synthcode.com/license.txt
4
5 ;;; An rx represents a start state and meta-info such as the number
6 ;;; and names of submatches.
7 (define-record-type Rx
8 (make-rx start-state num-matches num-save-indexes non-greedy-indexes
9 match-rules match-names sre)
10 regexp?
11 (start-state rx-start-state rx-start-state-set!)
12 (num-matches rx-num-matches rx-num-matches-set!)
13 (num-save-indexes rx-num-save-indexes rx-num-save-indexes-set!)
14 (non-greedy-indexes rx-non-greedy-indexes rx-non-greedy-indexes-set!)
15 (match-rules rx-rules rx-rules-set!)
16 (match-names rx-names rx-names-set!)
17 (sre regexp->sre))
18
19 ;; Syntactic sugar.
20 (define-syntax rx
21 (syntax-rules ()
22 ((rx sre ...)
23 (regexp `(: sre ...)))))
24
25 ;;; A state is a single nfa state with transition rules.
26 (define-record-type State
27 (%make-state accept? chars match match-rule next1 next2 id)
28 state?
29 ;; A boolean indicating if this is an accepting state.
30 (accept? state-accept? state-accept?-set!)
31 ;; A char or char-set indicating when we can transition.
32 ;; Alternately, #f indicates an epsilon transition, while a
33 ;; procedure of the form (lambda (ch i matches) ...) is a predicate
34 ;; which should return #t if the char matches.
35 (chars state-chars state-chars-set!)
36 ;; A single integer indicating the match position to record.
37 (match state-match state-match-set!)
38 ;; The rule for merging ambiguous matches. Can be any of: left,
39 ;; right, (list i j). Posix semantics are equivalent to using left
40 ;; for the beginning of a submatch and right for the end. List is
41 ;; used to capture a list of submatch data in the current match.
42 (match-rule state-match-rule state-match-rule-set!)
43 ;; The destination if the char match succeeds.
44 (next1 state-next1 state-next1-set!)
45 ;; An optional additional transition used for forking to two states.
46 (next2 state-next2 state-next2-set!)
47 ;; A unique (per regexp) id for debugging.
48 (id state-id))
49
50 (define (make-state accept? chars match match-rule next1 next2 id)
51 (if (and next1 (not (state? next1)))
52 (error "expected a state" next1))
53 (if (and next2 (not (state? next2)))
54 (error "expected a state" next2))
55 (%make-state accept? chars match match-rule next1 next2 id))
56
57 (define ~none 0)
58 (define ~ci? 1)
59 (define ~ascii? 2)
60 (define ~nocapture? 4)
61
62 (define (flag-set? flags i) (= i (bitwise-and flags i)))
63 (define (flag-join a b) (if b (bitwise-ior a b) a))
64 (define (flag-clear a b) (bitwise-and a (bitwise-not b)))
65
66 (define (char-set-ci cset)
67 (char-set-fold
68 (lambda (ch res)
69 (char-set-adjoin! (char-set-adjoin! res (char-upcase ch))
70 (char-downcase ch)))
71 (char-set)
72 cset))
73
74 (define (make-char-state ch flags next id)
75 (if (flag-set? flags ~ci?)
76 (let ((cset (cond ((char? ch) (char-set-ci (char-set ch)))
77 ((char-set? ch) (char-set-ci ch))
78 (else ch))))
79 (make-state #f cset #f #f next #f id))
80 (make-state #f ch #f #f next #f id)))
81 (define (make-fork-state next1 next2 id)
82 (make-state #f #f #f #f next1 next2 id))
83 (define (make-epsilon-state next id)
84 (make-fork-state next #f id))
85 (define (make-accept-state id)
86 (make-state #t #f #f #f #f #f id))
87
88 ;; A record holding the current match data - essentially a wrapper
89 ;; around a vector, plus a reference to the RX for meta-info.
90 (define-record-type Regexp-Match
91 (%make-regexp-match matches rx string)
92 regexp-match?
93 (matches regexp-match-matches regexp-match-matches-set!)
94 (rx regexp-match-rx)
95 (string regexp-match-string))
96
97 (define (regexp-match-rules md)
98 (rx-rules (regexp-match-rx md)))
99 (define (regexp-match-names md)
100 (rx-names (regexp-match-rx md)))
101 (define (make-regexp-match len rx str)
102 (%make-regexp-match (make-vector len #f) rx str))
103 (define (make-regexp-match-for-rx rx str)
104 (make-regexp-match (rx-num-save-indexes rx) rx str))
105 (define (regexp-match-count md)
106 (- (quotient (vector-length (regexp-match-matches md)) 2) 1))
107
108 (define (regexp-match-name-offset md name)
109 (let lp ((ls (regexp-match-names md)) (first #f))
110 (cond
111 ((null? ls) (or first (error "unknown match name" md name)))
112 ((eq? name (caar ls))
113 (if (regexp-match-submatch-start+end md (cdar ls))
114 (cdar ls)
115 (lp (cdr ls) (or first (cdar ls)))))
116 (else (lp (cdr ls) first)))))
117
118 (define (regexp-match-ref md n)
119 (vector-ref (regexp-match-matches md)
120 (if (integer? n)
121 n
122 (regexp-match-name-offset md n))))
123
124 (define (regexp-match-set! md n val)
125 (vector-set! (regexp-match-matches md) n val))
126
127 (define (copy-regexp-match md)
128 (let* ((src (regexp-match-matches md))
129 (len (vector-length src))
130 (dst (make-vector len #f)))
131 (do ((i 0 (+ i 1)))
132 ((= i len)
133 (%make-regexp-match dst (regexp-match-rx md) (regexp-match-string md)))
134 (vector-set! dst i (vector-ref src i)))))
135
136 ;;> Returns the matching result for the given named or indexed
137 ;;> submatch \var{n}, possibly as a list for a submatch-list, or
138 ;;> \scheme{#f} if not matched.
139
140 (define (regexp-match-submatch/list md n)
141 (let ((n (if (integer? n) n (regexp-match-name-offset md n))))
142 (cond
143 ((>= n (vector-length (regexp-match-rules md)))
144 #f)
145 (else
146 (let ((rule (vector-ref (regexp-match-rules md) n)))
147 (cond
148 ((pair? rule)
149 (let ((start (regexp-match-ref md (car rule)))
150 (end (regexp-match-ref md (cdr rule)))
151 (str (regexp-match-string md)))
152 (and start end (substring-cursor str start end))))
153 (else
154 (let ((res (regexp-match-ref md rule)))
155 (if (pair? res)
156 (reverse res)
157 res)))))))))
158
159 ;;> Returns the matching substring for the given named or indexed
160 ;;> submatch \var{n}, or \scheme{#f} if not matched.
161
162 (define (regexp-match-submatch md n)
163 (let ((res (regexp-match-submatch/list md n)))
164 (if (pair? res) (car res) res)))
165
166 (define (regexp-match-submatch-start+end md n)
167 (let ((n (if (integer? n) n (regexp-match-name-offset md n))))
168 (and (< n (vector-length (regexp-match-rules md)))
169 (let ((rule (vector-ref (regexp-match-rules md) n)))
170 (if (pair? rule)
171 (let ((start (regexp-match-ref md (car rule)))
172 (end (regexp-match-ref md (cdr rule)))
173 (str (regexp-match-string md)))
174 (and start end
175 (cons (string-cursor->index str start)
176 (string-cursor->index str end))))
177 #f)))))
178
179 ;;> Returns the start index for the given named or indexed submatch
180 ;;> \var{n}, or \scheme{#f} if not matched.
181
182 (define (regexp-match-submatch-start md n)
183 (cond ((regexp-match-submatch-start+end md n) => car) (else #f)))
184
185 ;;> Returns the end index for the given named or indexed submatch
186 ;;> \var{n}, or \scheme{#f} if not matched.
187
188 (define (regexp-match-submatch-end md n)
189 (cond ((regexp-match-submatch-start+end md n) => cdr) (else #f)))
190
191 (define (regexp-match-convert recurse? md str)
192 (cond
193 ((vector? md)
194 (let lp ((i 0) (res '()))
195 (cond
196 ((>= i (vector-length md))
197 (reverse res))
198 ((string-cursor? (vector-ref md i))
199 (lp (+ i 2)
200 (cons (substring-cursor str
201 (vector-ref md i)
202 (vector-ref md (+ i 1)))
203 res)))
204 (else
205 (lp (+ i 1)
206 (cons (regexp-match-convert recurse? (vector-ref md i) str)
207 res))))))
208 ((list? md)
209 (if recurse?
210 (map (lambda (x) (regexp-match-convert recurse? x str)) (reverse md))
211 (regexp-match-convert recurse? (car md) str)))
212 ((and (pair? md) (string-cursor? (car md)) (string-cursor? (cdr md)))
213 (substring-cursor str (car md) (cdr md)))
214 ((regexp-match? md)
215 (regexp-match-convert
216 recurse? (regexp-match-matches md) (regexp-match-string md)))
217 (else
218 md)))
219
220 ;;> Convert an regexp-match result to a list of submatches, beginning
221 ;;> with the full match, using \scheme{#f} for unmatched submatches.
222
223 (define (regexp-match->list md)
224 (regexp-match-convert #f md #f))
225
226 ;;> Convert an regexp-match result to a forest of submatches, beginning
227 ;;> with the full match, using \scheme{#f} for unmatched submatches.
228
229 (define (regexp-match->sexp md)
230 (regexp-match-convert #t md #f))
231
232 ;; Collect results from a list match.
233 (define (match-collect md spec)
234 (define (match-extract md n)
235 (let* ((vec (regexp-match-matches md))
236 (rules (regexp-match-rules md))
237 (n-rule (vector-ref rules n))
238 (rule (vector-ref rules n-rule)))
239 (if (pair? rule)
240 (let ((start (regexp-match-ref md (car rule)))
241 (end (regexp-match-ref md (cdr rule))))
242 (and start end (cons start end)))
243 (regexp-match-ref md rule))))
244 (let ((end (cadr spec))
245 (vec (regexp-match-matches md)))
246 (let lp ((i (+ 1 (car spec)))
247 (ls '()))
248 (if (>= i end)
249 (reverse ls)
250 (lp (+ i 1) (cons (match-extract md i) ls))))))
251
252 ;; A searcher represents a single rx state and match information.
253 (define-record-type Searcher
254 (make-searcher state matches)
255 searcher?
256 (state searcher-state searcher-state-set!)
257 (matches searcher-matches searcher-matches-set!))
258
259 ;; Merge two regexp-matches, preferring the leftmost-longest of their
260 ;; matches, or shortest for non-greedy matches.
261 (define (regexp-match>=? m1 m2)
262 (let ((non-greedy-indexes (rx-non-greedy-indexes (regexp-match-rx m1)))
263 (end (- (vector-length (regexp-match-matches m1)) 1)))
264 (let lp ((i 0))
265 (cond
266 ((>= i end)
267 #t)
268 ((and (eqv? (regexp-match-ref m1 i)
269 (regexp-match-ref m2 i))
270 (eqv? (regexp-match-ref m1 (+ i 1))
271 (regexp-match-ref m2 (+ i 1))))
272 (lp (+ i 2)))
273 (else
274 (not
275 (and (string-cursor? (regexp-match-ref m2 i))
276 (or (not (string-cursor? (regexp-match-ref m1 i)))
277 (string-cursor<? (regexp-match-ref m2 i)
278 (regexp-match-ref m1 i))
279 ;; sanity check for incompletely advanced epsilons
280 (and (string-cursor? (regexp-match-ref m1 (+ i 1)))
281 (string-cursor<? (regexp-match-ref m1 (+ i 1))
282 (regexp-match-ref m1 i)))
283 ((if (memq (+ i 1) non-greedy-indexes) not values)
284 (and
285 (string-cursor=? (regexp-match-ref m2 i)
286 (regexp-match-ref m1 i))
287 (or (not (string-cursor? (regexp-match-ref m2 (+ i 1))))
288 (and (string-cursor? (regexp-match-ref m1 (+ i 1)))
289 (string-cursor>?
290 (regexp-match-ref m2 (+ i 1))
291 (regexp-match-ref m1 (+ i 1)))))))))))))))
292
293 (define (regexp-match-max m1 m2)
294 (if (regexp-match>=? m1 m2) m1 m2))
295
296 ;; Merge match data from sr2 into sr1, preferring the leftmost-longest
297 ;; match in the event of a conflict.
298 (define (searcher-merge! sr1 sr2)
299 (let ((m (regexp-match-max (searcher-matches sr1) (searcher-matches sr2))))
300 (if (not (eq? m (searcher-matches sr1)))
301 (searcher-matches-set! sr1 (copy-regexp-match m)))))
302
303 (define (searcher-max sr1 sr2)
304 (if (or (not (searcher? sr2))
305 (regexp-match>=? (searcher-matches sr1) (searcher-matches sr2)))
306 sr1
307 sr2))
308
309 (define (searcher-start-match sr)
310 (regexp-match-ref (searcher-matches sr) 0))
311
312 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 ;; A posse is a group of searchers.
314
315 (define (make-posse . o)
316 (make-hash-table eq?))
317
318 (define posse? hash-table?)
319 (define (posse-empty? posse) (zero? (hash-table-size posse)))
320
321 (define (posse-ref posse sr)
322 (hash-table-ref/default posse (searcher-state sr) #f))
323 (define (posse-add! posse sr)
324 (hash-table-set! posse (searcher-state sr) sr))
325 (define (posse-clear! posse)
326 (hash-table-walk posse (lambda (key val) (hash-table-delete! posse key))))
327 (define (posse-for-each proc posse)
328 (hash-table-walk posse (lambda (key val) (proc val))))
329 (define (posse-every pred posse)
330 (hash-table-fold posse (lambda (key val acc) (and acc (pred val))) #t))
331
332 (define (posse->list posse)
333 (hash-table-values posse))
334 (define (list->posse ls)
335 (let ((searchers (make-posse)))
336 (for-each (lambda (sr) (posse-add! searchers sr)) ls)
337 searchers))
338 (define (posse . args)
339 (list->posse args))
340
341 (define (make-start-searcher rx str)
342 (make-searcher (rx-start-state rx) (make-regexp-match-for-rx rx str)))
343
344 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
345 ;; Execution
346
347 ;; A transition which doesn't advance the index.
348
349 (define (epsilon-state? st)
350 (or (not (state-chars st))
351 (procedure? (state-chars st))))
352
353 ;; Match the state against a char and index.
354
355 (define (state-matches? st str i ch start end matches)
356 (let ((matcher (state-chars st)))
357 (cond
358 ((char? matcher)
359 (eqv? matcher ch))
360 ((char-set? matcher)
361 (char-set-contains? matcher ch))
362 ((pair? matcher)
363 (and (char<=? (car matcher) ch) (char<=? ch (cdr matcher))))
364 ((procedure? matcher)
365 (matcher str i ch start end matches))
366 ((not matcher))
367 (else
368 (error "unknown state matcher" (state-chars st))))))
369
370 ;; Advance epsilons together - if the State is newly added to the
371 ;; group and is an epsilon state, recursively add the transition.
372
373 (define (posse-advance! new seen accept sr str i start end)
374 (let advance! ((sr sr))
375 (let ((st (searcher-state sr)))
376 ;; Update match data.
377 (cond
378 ((state-match st)
379 (let* ((index (state-match st))
380 (matches (searcher-matches sr))
381 (before (copy-regexp-match matches)))
382 (cond
383 ((pair? index)
384 ;; Submatch list, accumulate and push.
385 (let* ((prev (regexp-match-ref matches (car index)))
386 (new (cons (match-collect matches (cdr index))
387 (if (pair? prev) prev '()))))
388 (regexp-match-set! matches (car index) new)))
389 ((not (and (eq? 'non-greedy-left (state-match-rule st))
390 (regexp-match-ref matches index)
391 (string-cursor>=? (regexp-match-ref matches index)
392 (regexp-match-ref matches (- index 1)))))
393 (regexp-match-set! matches index i))))))
394 ;; Follow transitions.
395 (cond
396 ((state-accept? st)
397 (set-cdr! accept (searcher-max sr (cdr accept))))
398 ((posse-ref seen sr)
399 => (lambda (sr-prev) (searcher-merge! sr-prev sr)))
400 ((epsilon-state? st)
401 (let ((ch (and (string-cursor<? i end) (string-cursor-ref str i))))
402 ;; Epsilon transition. If there is a procedure matcher,
403 ;; it's a guarded epsilon and needs to be checked.
404 (cond
405 ((state-matches? st str i ch start end (searcher-matches sr))
406 (posse-add! seen sr)
407 (let* ((next1 (state-next1 st))
408 (next2 (state-next2 st))
409 (matches
410 (and next2 (searcher-matches sr))))
411 (cond
412 (next1
413 (searcher-state-set! sr next1)
414 (advance! (make-searcher next1 (copy-regexp-match (searcher-matches sr))))))
415 (cond
416 (next2
417 (let ((sr2 (make-searcher next2 (copy-regexp-match matches))))
418 (advance! sr2)))))))))
419 ;; Non-special, non-epsilon searcher, add to posse.
420 ((posse-ref new sr)
421 ;; Merge regexp-match for existing searcher.
422 => (lambda (sr-prev) (searcher-merge! sr-prev sr)))
423 (else
424 ;; Add new searcher.
425 (posse-add! new sr))))))
426
427 ;; Run so long as there is more to match.
428
429 (define (regexp-run-offsets search? rx str start end)
430 (let ((rx (regexp rx))
431 (epsilons (posse))
432 (accept (list #f)))
433 (let lp ((i start)
434 (searchers1 (posse))
435 (searchers2 (posse)))
436 ;; Advance initial epsilons once from the first index, or every
437 ;; time when searching.
438 (cond
439 ((or search? (string-cursor=? i start))
440 (posse-advance! searchers1 epsilons accept (make-start-searcher rx str)
441 str i start end)
442 (posse-clear! epsilons)))
443 (cond
444 ((or (string-cursor>=? i end)
445 (and search?
446 (searcher? (cdr accept))
447 (let ((accept-start (searcher-start-match (cdr accept))))
448 (posse-every
449 (lambda (searcher)
450 (string-cursor>? (searcher-start-match searcher)
451 accept-start))
452 searchers1)))
453 (and (not search?)
454 (posse-empty? searchers1)))
455 ;; Terminate when the string is done or there are no more
456 ;; searchers. If we terminate prematurely and are not
457 ;; searching, return false.
458 (and (searcher? (cdr accept))
459 (let ((matches (searcher-matches (cdr accept))))
460 (and (or search? (string-cursor>=? (regexp-match-ref matches 1)
461 end))
462 (searcher-matches (cdr accept))))))
463 (else
464 ;; Otherwise advance normally.
465 (let ((ch (string-cursor-ref str i))
466 (i2 (string-cursor-next str i)))
467 (posse-for-each ;; NOTE: non-deterministic from hash order
468 (lambda (sr)
469 (cond
470 ((state-matches? (searcher-state sr) str i ch
471 start end (searcher-matches sr))
472 (searcher-state-set! sr (state-next1 (searcher-state sr)))
473 ;; Epsilons are considered at the next position.
474 (posse-advance! searchers2 epsilons accept sr str i2 start end)
475 (posse-clear! epsilons))))
476 searchers1)
477 (posse-clear! searchers1)
478 (lp i2 searchers2 searchers1)))))))
479
480 ;; Wrapper to determine start and end offsets.
481
482 (define (regexp-run search? rx str . o)
483 (let ((start (string-start-arg str o))
484 (end (string-end-arg str (if (pair? o) (cdr o) o))))
485 (regexp-run-offsets search? rx str start end)))
486
487 ;;> Match the given regexp or SRE against the entire string and return
488 ;;> the match data on success. Returns \scheme{#f} on failure.
489
490 (define (regexp-matches rx str . o)
491 (apply regexp-run #f rx str o))
492
493 ;;> Match the given regexp or SRE against the entire string and return
494 ;;> the \scheme{#t} on success. Returns \scheme{#f} on failure.
495
496 (define (regexp-matches? rx str . o)
497 (and (apply regexp-matches rx str o) #t))
498
499 ;;> Search for the given regexp or SRE within string and return
500 ;;> the match data on success. Returns \scheme{#f} on failure.
501
502 (define (regexp-search rx str . o)
503 (apply regexp-run #t rx str o))
504
505 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
506 ;; Compiling
507
508 (define (parse-flags ls)
509 (define (symbol->flag s)
510 (case s ((i ci case-insensitive) ~ci?) (else ~none)))
511 (let lp ((ls ls) (res ~none))
512 (if (not (pair? ls))
513 res
514 (lp (cdr ls) (flag-join res (symbol->flag (car ls)))))))
515
516 (define char-set:nonl
517 (char-set-difference char-set:full (char-set #\newline)))
518 (define char-set:control (ucs-range->char-set 0 32))
519 (define char-set:word-constituent
520 (char-set-union char-set:letter char-set:digit (char-set #\_)))
521 (define %char-set:word-constituent
522 (char-set-union %char-set:letter %char-set:digit (char-set #\_)))
523 (define (char-word-constituent? ch)
524 (char-set-contains? char-set:word-constituent ch))
525 (define get-char-set:cased
526 (let ((char-set:cased #f))
527 (lambda ()
528 (if (not char-set:cased)
529 (set! char-set:cased
530 (char-set-union char-set:upper-case
531 char-set:lower-case
532 char-set:title-case)))
533 char-set:cased)))
534
535 (define (match/bos str i ch start end matches)
536 (string-cursor=? i start))
537 (define (match/eos str i ch start end matches)
538 (string-cursor>=? i end))
539 (define (match/bol str i ch start end matches)
540 (or (string-cursor=? i start)
541 (eqv? #\newline (string-cursor-ref str (string-cursor-prev str i)))))
542 (define (match/eol str i ch start end matches)
543 (or (string-cursor>=? i end)
544 (eqv? #\newline (string-cursor-ref str i))))
545 (define (match/bow str i ch start end matches)
546 (and (string-cursor<? i end)
547 (or (string-cursor=? i start)
548 (not (char-word-constituent?
549 (string-cursor-ref str (string-cursor-prev str i)))))
550 (char-word-constituent? ch)))
551 (define (match/eow str i ch start end matches)
552 (and (or (string-cursor>=? i end)
553 (not (char-word-constituent? ch)))
554 (string-cursor>? i start)
555 (char-word-constituent?
556 (string-cursor-ref str (string-cursor-prev str i)))))
557 (define (match/nwb str i ch start end matches)
558 (and (not (match/bow str i ch start end matches))
559 (not (match/eow str i ch start end matches))))
560 (define (match/bog str i ch start end matches)
561 (and (string-cursor<? i end)
562 (or (string-cursor=? i start)
563 (match/eog str (string-cursor-prev str i) ch start end matches))))
564 (define (match/eog str i ch start end matches)
565 (and (string-cursor>? i start)
566 (or (string-cursor>=? i end)
567 (let ((m (regexp-search re:grapheme str
568 (string-cursor->index str i)
569 (string-cursor->index str end))))
570 (and m (string-cursor<=? (regexp-match-submatch-end m 0) i))))))
571
572 (define (lookup-char-set name flags)
573 (cond
574 ((flag-set? flags ~ascii?)
575 (case name
576 ((any) char-set:full)
577 ((nonl) char-set:nonl)
578 ((lower-case lower)
579 (if (flag-set? flags ~ci?) %char-set:letter %char-set:lower-case))
580 ((upper-case upper)
581 (if (flag-set? flags ~ci?) %char-set:letter %char-set:upper-case))
582 ((title-case title)
583 (if (flag-set? flags ~ci?) %char-set:letter (char-set)))
584 ((alphabetic alpha) %char-set:letter)
585 ((numeric num digit) %char-set:digit)
586 ((alphanumeric alphanum alnum) %char-set:letter+digit)
587 ((punctuation punct) %char-set:punctuation)
588 ((symbol) %char-set:symbol)
589 ((graphic graph) %char-set:graphic)
590 ((word-constituent) %char-set:word-constituent)
591 ((whitespace white space) %char-set:whitespace)
592 ((printing print) %char-set:printing)
593 ((control cntrl) %char-set:iso-control)
594 ((hex-digit xdigit hex) char-set:hex-digit)
595 ((ascii) char-set:ascii)
596 (else #f)))
597 (else
598 (case name
599 ((any) char-set:full)
600 ((nonl) char-set:nonl)
601 ((lower-case lower)
602 (if (flag-set? flags ~ci?) (get-char-set:cased) char-set:lower-case))
603 ((upper-case upper)
604 (if (flag-set? flags ~ci?) (get-char-set:cased) char-set:upper-case))
605 ((title-case title)
606 (if (flag-set? flags ~ci?) (get-char-set:cased) char-set:title-case))
607 ((alphabetic alpha) char-set:letter)
608 ((numeric num digit) char-set:digit)
609 ((alphanumeric alphanum alnum) char-set:letter+digit)
610 ((punctuation punct) char-set:punctuation)
611 ((symbol) char-set:symbol)
612 ((graphic graph) char-set:graphic)
613 ((word-constituent) char-set:word-constituent)
614 ((whitespace white space) char-set:whitespace)
615 ((printing print) char-set:printing)
616 ((control cntrl) char-set:control)
617 ((hex-digit xdigit hex) char-set:hex-digit)
618 ((ascii) char-set:ascii)
619 (else #f)))))
620
621 (define (sre-flatten-ranges orig-ls)
622 (let lp ((ls orig-ls) (res '()))
623 (cond
624 ((null? ls)
625 (reverse res))
626 ((string? (car ls))
627 (lp (append (string->list (car ls)) (cdr ls)) res))
628 ((null? (cdr ls))
629 (error "unbalanced cset / range" orig-ls))
630 ((string? (cadr ls))
631 (lp (cons (car ls) (append (string->list (cadr ls)) (cddr ls))) res))
632 (else
633 (lp (cddr ls) (cons (cons (car ls) (cadr ls)) res))))))
634
635 (define (every pred ls)
636 (or (null? ls) (and (pred (car ls)) (every pred (cdr ls)))))
637
638 (define (char-set-sre? sre)
639 (or (char? sre)
640 (and (string? sre) (= 1 (string-length sre)))
641 (lookup-char-set sre ~none)
642 (and (pair? sre)
643 (or (string? (car sre))
644 (memq (car sre)
645 '(char-set / char-range & and ~ complement - difference))
646 (and (memq (car sre)
647 '(\x7C;
648 or w/case w/nocase w/unicode w/ascii))
649 (every char-set-sre? (cdr sre)))))))
650
651 (define (non-greedy-sre? sre)
652 (and (pair? sre)
653 (or (memq (car sre) '(?? *? **? non-greedy-optional
654 non-greedy-zero-or-more non-greedy-repeated))
655 (and (memq (car sre) '(: seq w/case w/nocase w/unicode w/ascii))
656 (non-greedy-sre? (car (reverse sre))))
657 (and (eq? (car sre) 'or)
658 (any non-greedy-sre? (cdr sre))))))
659
660 (define (valid-sre? x)
661 (guard (exn (else #f)) (regexp x) #t))
662
663 (define (sre->char-set sre . o)
664 (let ((flags (if (pair? o) (car o) ~none)))
665 (define (->cs sre) (sre->char-set sre flags))
666 (define (maybe-ci sre)
667 (if (flag-set? flags ~ci?) (char-set-ci sre) sre))
668 (cond
669 ((lookup-char-set sre flags))
670 ((char-set? sre) (maybe-ci sre))
671 ((char? sre) (maybe-ci (char-set sre)))
672 ((string? sre)
673 (if (= 1 (string-length sre))
674 (maybe-ci (string->char-set sre))
675 (error "only single char strings can be char-sets")))
676 ((pair? sre)
677 (if (string? (car sre))
678 (maybe-ci (string->char-set (car sre)))
679 (case (car sre)
680 ((char-set) (maybe-ci (string->char-set (cadr sre))))
681 ((/ char-range)
682 (->cs
683 `(or ,@(map (lambda (x)
684 (ucs-range->char-set
685 (char->integer (car x))
686 (+ 1 (char->integer (cdr x)))))
687 (sre-flatten-ranges (cdr sre))))))
688 ((& and) (apply char-set-intersection (map ->cs (cdr sre))))
689 ((\x7C;
690 or) (apply char-set-union (map ->cs (cdr sre))))
691 ((~ complement) (char-set-complement (->cs `(or ,@(cdr sre)))))
692 ((- difference) (char-set-difference (->cs (cadr sre))
693 (->cs `(or ,@(cddr sre)))))
694 ((w/case) (sre->char-set (cadr sre) (flag-clear flags ~ci?)))
695 ((w/nocase) (sre->char-set (cadr sre) (flag-join flags ~ci?)))
696 ((w/ascii) (sre->char-set (cadr sre) (flag-join flags ~ascii?)))
697 ((w/unicode) (sre->char-set (cadr sre) (flag-clear flags ~ascii?)))
698 (else (error "invalid sre char-set" sre)))))
699 (else (error "invalid sre char-set" sre)))))
700
701 (define (char-set->sre cset)
702 (list (char-set->string cset)))
703
704 (define (strip-submatches sre)
705 (if (pair? sre)
706 (case (car sre)
707 (($ submatch) (strip-submatches (cons ': (cdr sre))))
708 ((-> => submatch-named) (strip-submatches (cons ': (cddr sre))))
709 (else (cons (strip-submatches (car sre))
710 (strip-submatches (cdr sre)))))
711 sre))
712
713 (define (sre-expand-reps from to sre)
714 (let ((sre0 (strip-submatches sre)))
715 (let lp ((i 0) (res '(:)))
716 (if (= i from)
717 (cond
718 ((not to)
719 (reverse (cons `(* ,sre) res)))
720 ((= from to)
721 (reverse (cons sre (cdr res))))
722 (else
723 (let lp ((i (+ i 1)) (res res))
724 (if (>= i to)
725 (reverse (cons `(? ,sre) res))
726 (lp (+ i 1) (cons `(? ,sre0) res))))))
727 (lp (+ i 1) (cons sre0 res))))))
728
729 ;;> Compile an \var{sre} into a regexp.
730
731 (define (regexp sre . o)
732 (define current-index 2)
733 (define current-match 0)
734 (define current-id 0)
735 (define match-names '())
736 (define match-rules (list (cons 0 1)))
737 (define non-greedy-indexes '())
738 (define (next-id)
739 (let ((res current-id)) (set! current-id (+ current-id 1)) res))
740 (define (make-submatch-state sre flags next index)
741 (let* ((n3 (make-epsilon-state next (next-id)))
742 (n2 (->rx sre flags n3))
743 (n1 (make-epsilon-state n2 (next-id)))
744 (non-greedy? (non-greedy-sre? sre)))
745 (state-match-set! n1 index)
746 (state-match-rule-set! n1 'left)
747 (state-match-set! n3 (+ index 1))
748 (state-match-rule-set! n3 (if non-greedy? 'non-greedy-left 'right))
749 (if non-greedy?
750 (set! non-greedy-indexes (cons (+ index 1) non-greedy-indexes)))
751 n1))
752 (define (->rx sre flags next)
753 (cond
754 ;; The base cases chars and strings match literally.
755 ((char? sre)
756 (make-char-state sre flags next (next-id)))
757 ((char-set? sre)
758 (make-char-state sre flags next (next-id)))
759 ((string? sre)
760 (->rx (cons 'seq (string->list sre)) flags next))
761 ((and (symbol? sre) (lookup-char-set sre flags))
762 => (lambda (cset) (make-char-state cset ~none next (next-id))))
763 ((symbol? sre)
764 (case sre
765 ((epsilon) next)
766 ((bos) (make-char-state match/bos flags next (next-id)))
767 ((eos) (make-char-state match/eos flags next (next-id)))
768 ((bol) (make-char-state match/bol flags next (next-id)))
769 ((eol) (make-char-state match/eol flags next (next-id)))
770 ((bow) (make-char-state match/bow flags next (next-id)))
771 ((eow) (make-char-state match/eow flags next (next-id)))
772 ((nwb) (make-char-state match/nwb flags next (next-id)))
773 ((bog) (make-char-state match/bog flags next (next-id)))
774 ((eog) (make-char-state match/eog flags next (next-id)))
775 ((grapheme)
776 (->rx
777 `(or (: (* ,char-set:hangul-l) (+ ,char-set:hangul-v)
778 (* ,char-set:hangul-t))
779 (: (* ,char-set:hangul-l) ,char-set:hangul-v
780 (* ,char-set:hangul-v) (* ,char-set:hangul-t))
781 (: (* ,char-set:hangul-l) ,char-set:hangul-lvt
782 (* ,char-set:hangul-t))
783 (+ ,char-set:hangul-l)
784 (+ ,char-set:hangul-t)
785 (+ ,char-set:regional-indicator)
786 (: "\r\n")
787 (: (~ control ("\r\n"))
788 (+ ,char-set:extend-or-spacing-mark))
789 control)
790 flags
791 next))
792 ((word) (->rx '(word+ any) flags next))
793 (else (error "unknown sre" sre))))
794 ((pair? sre)
795 (case (car sre)
796 ((seq :)
797 ;; Sequencing. An empty sequence jumps directly to next,
798 ;; otherwise we join the first element to the sequence formed
799 ;; of the remaining elements followed by next.
800 (if (null? (cdr sre))
801 next
802 ;; Make a dummy intermediate to join the states so that
803 ;; we can generate n1 first, preserving the submatch order.
804 (let* ((n2 (make-epsilon-state #f (next-id)))
805 (n1 (->rx (cadr sre) flags n2))
806 (n3 (->rx (cons 'seq (cddr sre)) flags next)))
807 (state-next1-set! n2 n3)
808 n1)))
809 ((or \x7C;
810 )
811 ;; Alternation. An empty alternation always fails.
812 ;; Otherwise we fork between any of the alternations, each
813 ;; continuing to next.
814 (cond
815 ((null? (cdr sre))
816 #f)
817 ((char-set-sre? sre)
818 (make-char-state (sre->char-set sre) flags next (next-id)))
819 ((null? (cddr sre))
820 (->rx (cadr sre) flags next))
821 (else
822 (let* ((n1 (->rx (cadr sre) flags next))
823 (n2 (->rx (cons 'or (cddr sre)) flags next)))
824 (make-fork-state n1 n2 (next-id))))))
825 ((? optional ?? non-greedy-optional)
826 ;; Optionality. Either match the body or fork to the next
827 ;; state directly.
828 (make-fork-state (->rx (cons 'seq (cdr sre)) flags next)
829 next (next-id)))
830 ((* zero-or-more *? non-greedy-zero-or-more)
831 ;; Repetition. Introduce two fork states which can jump from
832 ;; the end of the loop to the beginning and from the
833 ;; beginning to the end (to skip the first iteration).
834 (let* ((n2 (make-fork-state next #f (next-id)))
835 (n1 (make-fork-state (->rx (cons 'seq (cdr sre)) flags n2)
836 n2 (next-id))))
837 (state-next2-set! n2 n1)
838 n1))
839 ((+ one-or-more)
840 ;; One-or-more repetition. Same as above but the first
841 ;; transition is required so the rx is simpler - we only
842 ;; need one fork from the end of the loop to the beginning.
843 (let* ((n2 (make-fork-state next #f (next-id)))
844 (n1 (->rx (cons 'seq (cdr sre)) flags n2)))
845 (state-next2-set! n2 n1)
846 n1))
847 ((= exactly)
848 ;; Exact repetition.
849 (->rx (sre-expand-reps (cadr sre) (cadr sre) (cons 'seq (cddr sre)))
850 flags next))
851 ((>= at-least)
852 ;; n-or-more repetition.
853 (->rx (sre-expand-reps (cadr sre) #f (cons 'seq (cddr sre)))
854 flags next))
855 ((** repeated **? non-greedy-repeated)
856 ;; n-to-m repetition.
857 (->rx (sre-expand-reps (cadr sre) (car (cddr sre))
858 (cons 'seq (cdr (cddr sre))))
859 flags next))
860 ((-> => submatch-named)
861 ;; Named submatches just record the name for the current
862 ;; match and rewrite as a non-named submatch.
863 (cond
864 ((flag-set? flags ~nocapture?)
865 (->rx (cons 'seq (cddr sre)) flags next))
866 (else
867 (set! match-names
868 (cons (cons (cadr sre) (+ 1 current-match)) match-names))
869 (->rx (cons 'submatch (cddr sre)) flags next))))
870 ((*-> *=> submatch-named-list)
871 (cond
872 ((flag-set? flags ~nocapture?)
873 (->rx (cons 'seq (cddr sre)) flags next))
874 (else
875 (set! match-names (cons (cons (cadr sre) current-match) match-names))
876 (->rx (cons 'submatch-list (cddr sre)) flags next))))
877 (($ submatch)
878 ;; A submatch wraps next with an epsilon transition before
879 ;; next, setting the start and end index on the result and
880 ;; wrapped next respectively.
881 (cond
882 ((flag-set? flags ~nocapture?)
883 (->rx (cons 'seq (cdr sre)) flags next))
884 (else
885 (let ((num current-match)
886 (index current-index))
887 (set! current-match (+ current-match 1))
888 (set! current-index (+ current-index 2))
889 (set! match-rules `((,index . ,(+ index 1)) ,@match-rules))
890 (make-submatch-state (cons 'seq (cdr sre)) flags next index)))))
891 ((*$ submatch-list)
892 ;; A submatch-list wraps a range of submatch results into a
893 ;; single match value.
894 (cond
895 ((flag-set? flags ~nocapture?)
896 (->rx (cons 'seq (cdr sre)) flags next))
897 (else
898 (let* ((num current-match)
899 (index current-index))
900 (set! current-match (+ current-match 1))
901 (set! current-index (+ current-index 1))
902 (set! match-rules `(,index ,@match-rules))
903 (let* ((n2 (make-epsilon-state next (next-id)))
904 (n1 (->rx (cons 'submatch (cdr sre)) flags n2)))
905 (state-match-set! n2 (list index num current-match))
906 (state-match-rule-set! n2 'list)
907 n1)))))
908 ((~ - & / complement difference and char-range char-set)
909 (make-char-state (sre->char-set sre flags) ~none next (next-id)))
910 ((word)
911 (->rx `(: bow ,@(cdr sre) eow) flags next))
912 ((word+)
913 (->rx `(word (+ ,(if (equal? '(any) (cdr sre))
914 'word-constituent
915 (char-set-intersection
916 char-set:word-constituent
917 (sre->char-set `(or ,@(cdr sre)) flags)))))
918 flags
919 next))
920 ((w/case)
921 (->rx `(: ,@(cdr sre)) (flag-clear flags ~ci?) next))
922 ((w/nocase)
923 (->rx `(: ,@(cdr sre)) (flag-join flags ~ci?) next))
924 ((w/unicode)
925 (->rx `(: ,@(cdr sre)) (flag-clear flags ~ascii?) next))
926 ((w/ascii)
927 (->rx `(: ,@(cdr sre)) (flag-join flags ~ascii?) next))
928 ((w/nocapture)
929 (->rx `(: ,@(cdr sre)) (flag-join flags ~nocapture?) next))
930 (else
931 (if (string? (car sre))
932 (make-char-state (sre->char-set sre flags) ~none next (next-id))
933 (error "unknown sre" sre)))))))
934 (let ((flags (parse-flags (and (pair? o) (car o)))))
935 (if (regexp? sre)
936 sre
937 (let ((start (make-submatch-state
938 sre flags (make-accept-state (next-id)) 0)))
939 ;; (define (state->list st)
940 ;; (let ((seen (make-hash-table eq?))
941 ;; (count 0))
942 ;; (reverse
943 ;; (let lp ((st st) (res '()))
944 ;; (cond
945 ;; ((not (state? st)) res)
946 ;; ((hash-table-ref/default seen st #f) res)
947 ;; (else
948 ;; (hash-table-set! seen st count)
949 ;; (let ((orig-count count))
950 ;; (set! count (+ count 1))
951 ;; (let* ((next1 (lp (state-next1 st) '()))
952 ;; (next2 (lp (state-next2 st) '()))
953 ;; (this (append
954 ;; (list (state-id st) ;;orig-count
955 ;; (cond
956 ;; ((epsilon-state? st)
957 ;; (if (state-chars st) '? '-))
958 ;; ((and (char-set? (state-chars st))
959 ;; (< (char-set-size (state-chars st)) 5))
960 ;; (char-set->string (state-chars st)))
961 ;; ((char? (state-chars st))
962 ;; (string (state-chars st)))
963 ;; (else '+))
964 ;; (cond
965 ;; ((state-next1 st) => state-id)
966 ;; (else #f)))
967 ;; (if (state-next2 st)
968 ;; (list (state-id (state-next2 st)))
969 ;; '())
970 ;; (if (state-match st)
971 ;; (list (list 'm (state-match st)))
972 ;; '()))))
973 ;; (append next2 next1 (cons this res))))))))))
974 ;;(for-each (lambda (x) (write x) (newline)) (state->list start))
975 (make-rx start current-match current-index non-greedy-indexes
976 (list->vector (reverse match-rules)) match-names sre)))))
977
978 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
979 ;; Utilities
980
981 ;;> The fundamental regexp matching iterator. Repeatedly searches
982 ;;> \var{str} for the regexp \var{re} so long as a match can be found.
983 ;;> On each successful match, applies \scheme{(\var{kons} \var{i}
984 ;;> \var{regexp-match} \var{str} \var{acc})} where \var{i} is the
985 ;;> index since the last match (beginning with
986 ;;> \var{start}),\var{regexp-match} is the resulting match, and
987 ;;> \var{acc} is the result of the previous \var{kons} application,
988 ;;> beginning with \var{knil}. When no more matches can be found,
989 ;;> calls \var{finish} with the same arguments, except that
990 ;;> \var{regexp-match} is \scheme{#f}.
991 ;;>
992 ;;> By default \var{finish} just returns \var{acc}.
993
994 (define (regexp-fold rx kons knil str . o)
995 (let* ((rx (regexp rx))
996 (finish (if (pair? o) (car o) (lambda (from md str acc) acc)))
997 (o (if (pair? o) (cdr o) o))
998 (start (string-start-arg str o))
999 (end (string-end-arg str (if (pair? o) (cdr o) o))))
1000 (let lp ((i start)
1001 (from start)
1002 (acc knil))
1003 (cond
1004 ((and (string-cursor<? i end) (regexp-run-offsets #t rx str i end))
1005 => (lambda (md)
1006 (let ((j (regexp-match-ref md 1)))
1007 (lp (if (and (string-cursor=? i j) (string-cursor<? j end))
1008 (string-cursor-next str j)
1009 j)
1010 j
1011 (kons (string-cursor->index str from) md str acc)))))
1012 (else
1013 (finish (string-cursor->index str from) #f str acc))))))
1014
1015 ;;> Extracts all non-empty substrings of \var{str} which match
1016 ;;> \var{re} between \var{start} and \var{end} as a list of strings.
1017
1018 (define (regexp-extract rx str . o)
1019 (apply regexp-fold
1020 rx
1021 (lambda (from md str a)
1022 (let ((s (regexp-match-submatch md 0)))
1023 (if (equal? s "") a (cons s a))))
1024 '()
1025 str
1026 (lambda (from md str a) (reverse a))
1027 o))
1028
1029 ;;> Splits \var{str} into a list of strings separated by matches of
1030 ;;> \var{re}.
1031
1032 (define (regexp-split rx str . o)
1033 ;; start and end in indices passed to regexp-fold
1034 (let ((start (if (pair? o) (car o) 0))
1035 (end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str))))
1036 (regexp-fold
1037 rx
1038 (lambda (from md str a)
1039 (let ((i (regexp-match-submatch-start md 0))
1040 (j (regexp-match-submatch-end md 0)))
1041 (if (eqv? i j)
1042 a
1043 (cons j
1044 (cons (substring str (car a) i) (cdr a))))))
1045 (cons start '())
1046 str
1047 (lambda (from md str a)
1048 (reverse (cons (substring str (car a) end) (cdr a))))
1049 start
1050 end)))
1051
1052 ;;> Partitions \var{str} into a list of non-empty strings
1053 ;;> matching \var{re}, interspersed with the unmatched portions
1054 ;;> of the string. The first and every odd element is an unmatched
1055 ;;> substring, which will be the empty string if \var{re} matches
1056 ;;> at the beginning of the string or end of the previous match. The
1057 ;;> second and every even element will be a substring matching
1058 ;;> \var{re}. If the final match ends at the end of the string,
1059 ;;> no trailing empty string will be included. Thus, in the
1060 ;;> degenerate case where \var{str} is the empty string, the
1061 ;;> result is \scheme{("")}.
1062
1063 (define (regexp-partition rx str . o)
1064 (let ((start (if (pair? o) (car o) 0))
1065 (end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str))))
1066 (define (kons from md str a)
1067 (let ((i (regexp-match-submatch-start md 0))
1068 (j (regexp-match-submatch-end md 0)))
1069 (if (eqv? i j)
1070 a
1071 (let ((left (substring str (car a) i)))
1072 (cons j
1073 (cons (regexp-match-submatch md 0)
1074 (cons left (cdr a))))))))
1075 (define (final from md str a)
1076 (if (or (< from end) (null? (cdr a)))
1077 (cons (substring str (car a) end) (cdr a))
1078 (cdr a)))
1079 (reverse (regexp-fold rx kons (cons start '()) str final start end))))
1080
1081 ;;> Returns a new string replacing the \var{count}th match of \var{re}
1082 ;;> in \var{str} the \var{subst}, where the zero-indexed \var{count}
1083 ;;> defaults to zero (i.e. the first match). If there are not
1084 ;;> \var{count} matches, returns the selected substring unmodified.
1085
1086 ;;> \var{subst} can be a string, an integer or symbol indicating the
1087 ;;> contents of a numbered or named submatch of \var{re},\scheme{'pre}
1088 ;;> for the substring to the left of the match, or \scheme{'post} for
1089 ;;> the substring to the right of the match.
1090
1091 ;;> The optional parameters \var{start} and \var{end} restrict both
1092 ;;> the matching and the substitution, to the given indices, such that
1093 ;;> the result is equivalent to omitting these parameters and
1094 ;;> replacing on \scheme{(substring str start end)}. As a convenience,
1095 ;;> a value of \scheme{#f} for \var{end} is equivalent to
1096 ;;> \scheme{(string-length str)}.
1097
1098 (define (regexp-replace rx str subst . o)
1099 (let* ((start (if (and (pair? o) (car o)) (car o) 0))
1100 (o (if (pair? o) (cdr o) '()))
1101 (end (if (and (pair? o) (car o)) (car o) (string-length str)))
1102 (o (if (pair? o) (cdr o) '()))
1103 (count (if (pair? o) (car o) 0)))
1104 (let lp ((i start) (count count))
1105 (let ((m (regexp-search rx str i end)))
1106 (cond
1107 ((not m) str)
1108 ((positive? count)
1109 (lp (regexp-match-submatch-end m 0) (- count 1)))
1110 (else
1111 (string-concatenate
1112 (cons
1113 (substring str start (regexp-match-submatch-start m 0))
1114 (append
1115 (reverse (regexp-apply-match m str subst))
1116 (list (substring str (regexp-match-submatch-end m 0) end)))))))))))
1117
1118 ;;> Equivalent to \var{regexp-replace}, but replaces all occurrences
1119 ;;> of \var{re} in \var{str}.
1120
1121 (define (regexp-replace-all rx str subst . o)
1122 (regexp-fold
1123 rx
1124 (lambda (i m str acc)
1125 (let ((m-start (regexp-match-submatch-start m 0)))
1126 (append (regexp-apply-match m str subst)
1127 (if (>= i m-start)
1128 acc
1129 (cons (substring str i m-start) acc)))))
1130 '()
1131 str
1132 (lambda (i m str acc)
1133 (let ((end (string-length str)))
1134 (string-concatenate-reverse
1135 (if (>= i end)
1136 acc
1137 (cons (substring str i end) acc)))))))
1138
1139 (define (regexp-apply-match m str ls)
1140 (let lp ((ls ls) (res '()))
1141 (cond
1142 ((null? ls)
1143 res)
1144 ((not (pair? ls))
1145 (lp (list ls) res))
1146 ((integer? (car ls))
1147 (lp (cdr ls) (cons (or (regexp-match-submatch m (car ls)) "") res)))
1148 ((procedure? (car ls))
1149 (lp (cdr ls) (cons ((car ls) m) res)))
1150 ((symbol? (car ls))
1151 (case (car ls)
1152 ((pre)
1153 (lp (cdr ls)
1154 (cons (substring str 0 (regexp-match-submatch-start m 0))
1155 res)))
1156 ((post)
1157 (lp (cdr ls)
1158 (cons (substring str
1159 (regexp-match-submatch-end m 0)
1160 (string-length str))
1161 res)))
1162 (else
1163 (cond
1164 ((assq (car ls) (regexp-match-names m))
1165 => (lambda (x) (lp (cons (cdr x) (cdr ls)) res)))
1166 (else
1167 (error "unknown match replacement" (car ls)))))))
1168 (else
1169 (lp (cdr ls) (cons (car ls) res))))))
1170
1171 (define re:grapheme (regexp 'grapheme))