Debian Codegraph @

lib/scheme/regex.scm

raw
1 ;;;
2 ;;; scheme.regex Scheme Regular Expressions (R7RS Tangerine)
3 ;;;
4 ;;; Copyright (c) 2019 Duy Nguyen <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; Originally SRFI-115
35 (define-module scheme.regex
36 (use scheme.charset)
37 (use gauche.regexp.sre)
38 (export rx regexp regexp->sre char-set->sre valid-sre? regexp?
39 regexp-matches regexp-matches? regexp-search
40 regexp-fold regexp-extract regexp-split regexp-partition
41 regexp-replace regexp-replace-all
42 regexp-match? regexp-match-count regexp-match-submatch
43 regexp-match-submatch-start regexp-match-submatch-end
44 regexp-match->list))
45 (select-module scheme.regex)
46
47 (define-syntax rx
48 (syntax-rules ()
49 [(_ sre ...) (regexp `(: sre ...))]))
50
51 (define (regexp re)
52 (if (regexp? re) re (sre->regexp re)))
53
54 (define (regexp->sre re)
55 (regexp-unparse-sre (regexp-ast re)))
56
57 (define (char-set->sre cs)
58 (list (list->string (char-set->list cs))))
59
60 (define (valid-sre? sre)
61 (guard (e [(<regexp-invalid-sre> e) #f][else (raise e)])
62 (regexp-parse-sre sre)
63 #t))
64
65 (define (regexp-matches re str :optional start end)
66 (if (regexp? re)
67 (let1 match (regexp-search re str start end)
68 (if (and match
69 (zero? (rxmatch-start match))
70 (eq? (rxmatch-end match)
71 (if (undefined? end)
72 (string-length str)
73 end)))
74 match
75 #f))
76 ;; if re is not compiled yet, insert bos to take advantage of
77 ;; BOL_ANCHORED optimization.
78 (regexp-search `(: bos ,re eos) str start end)))
79
80 (define (regexp-matches? re str :optional start end)
81 (if (regexp-matches re str start end) #t #f))
82
83 (define (regexp-search re str :optional start end)
84 (rxmatch (regexp re) str start end))
85
86 (define (regexp-fold re kons knil str :optional finish start end)
87 (cond
88 [(undefined? finish)
89 (regexp-fold re kons knil str (lambda (from match str acc)
90 acc))]
91 [(undefined? start)
92 (regexp-fold re kons knil str finish 0)]
93 [(undefined? end)
94 (regexp-fold re kons knil str finish 0 (string-length str))]
95 [(not (regexp? re))
96 (regexp-fold (regexp re) kons knil str finish 0 end)]
97 [else
98 (let loop ([start start]
99 [last-end start]
100 [acc knil])
101 (let1 match (and (< start end)
102 (regexp-search re str start end))
103 (cond
104 [(not match)
105 (finish last-end #f str acc)]
106 [(eq? start (rxmatch-end match)) ; empty match, try again
107 (loop (+ start 1) last-end acc)]
108 [else
109 (loop (rxmatch-end match)
110 (rxmatch-end match)
111 (kons last-end match str acc))])))]))
112
113 (define (regexp-extract re str . opt)
114 (apply regexp-fold re
115 (lambda (i match str acc)
116 (if (equal? (match 0) "")
117 acc
118 (cons (match 0) acc)))
119 '()
120 str
121 (lambda (i match str acc)
122 (reverse acc))
123 opt))
124
125 (define (regexp-split rx str . o)
126 ;; start and end in indices passed to regexp-fold
127 (let ((start (if (pair? o) (car o) 0))
128 (end (if (and (pair? o) (pair? (cdr o)))
129 (cadr o)
130 (string-length str))))
131 (regexp-fold rx
132 (lambda (from md str a)
133 (let ((i (regexp-match-submatch-start md 0))
134 (j (regexp-match-submatch-end md 0)))
135 (if (eqv? i j)
136 a
137 (cons j
138 (cons (substring str (car a) i)
139 (cdr a))))))
140 (cons start '())
141 str
142 (lambda (from md str a)
143 (reverse
144 (cons (substring str (car a) end)
145 (cdr a))))
146 start
147 end)))
148
149 (define (regexp-partition rx str . o)
150 (let ((start (if (pair? o) (car o) 0))
151 (end (if (and (pair? o) (pair? (cdr o)))
152 (cadr o)
153 (string-length str))))
154 (define (kons from md str a)
155 (let ((i (regexp-match-submatch-start md 0))
156 (j (regexp-match-submatch-end md 0)))
157 (if (eqv? i j)
158 a
159 (let ((left (substring str (car a) i)))
160 (cons j
161 (cons (regexp-match-submatch md 0)
162 (cons left (cdr a))))))))
163 (define (final from md str a)
164 (if (or (< from end)
165 (null? (cdr a)))
166 (cons (substring str (car a) end) (cdr a))
167 (cdr a)))
168 (reverse
169 (regexp-fold rx
170 kons
171 (cons start '())
172 str
173 final
174 start
175 end))))
176
177 ;; %regexp-replace-rec takes a substitution as either a procedure or a
178 ;; list of symbols, numbers and strings. Wrap single symbols or
179 ;; strings to a list to follow this rule
180 (define (transform-sub rx sub)
181 (define (check-pre-post sub)
182 (when (and (eq? sub 'pre) (assq 'pre (regexp-named-groups rx)))
183 (error "If 'pre is used in regexp-replace, there should not be a capture group also named 'pre"))
184 (when (and (eq? sub 'post) (assq 'post (regexp-named-groups rx)))
185 (error "If 'post is used in regexp-replace, there should not be a capture group also named 'post"))
186 sub)
187
188 (cond
189 [(string? sub) (list sub)]
190 [(number? sub) (list sub)]
191 [(symbol? sub) (list (check-pre-post sub))]
192 [(list? sub) (map check-pre-post sub)]
193 [else sub]))
194
195 (define (%substring str start end)
196 (if (or (> start 0) end)
197 (substring str start (or end (string-length str)))
198 str))
199
200 (define (regexp-replace rx str sub :optional
201 (start 0)
202 (end #f)
203 (count 0))
204 (let1 rx (regexp rx)
205 ((with-module gauche.internal %regexp-replace)
206 rx (%substring str start end)
207 (transform-sub rx sub) count 1)))
208
209 (define (regexp-replace-all rx str sub :optional
210 (start 0)
211 (end #f))
212 (let1 rx (regexp rx)
213 ((with-module gauche.internal %regexp-replace)
214 rx (%substring str start end)
215 (transform-sub rx sub) 0 #f)))
216
217 (define regexp-match? regmatch?)
218 (define (regexp-match-count match)
219 (- (rxmatch-num-matches match) 1))
220 (define regexp-match-submatch rxmatch-substring)
221 (define regexp-match-submatch-start rxmatch-start)
222 (define regexp-match-submatch-end rxmatch-end)
223 (define regexp-match->list rxmatch-substrings)