| 1 |
;;; |
| 2 |
;;; librx.scm - builtin regular-expression procedures |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[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 |
(select-module gauche) |
| 35 |
(use util.match) |
| 36 |
|
| 37 |
(inline-stub |
| 38 |
(.include "gauche/priv/configP.h" |
| 39 |
"gauche/priv/regexpP.h")) |
| 40 |
|
| 41 |
(define-cproc regexp? (obj) ::<boolean> :constant SCM_REGEXPP) |
| 42 |
(define-cproc regmatch? (obj) ::<boolean> SCM_REGMATCHP) |
| 43 |
|
| 44 |
(define-cproc string->regexp (str::<string> :key (case-fold #f) (multi-line #f)) |
| 45 |
(return |
| 46 |
(Scm_RegComp str |
| 47 |
(logior (?: (SCM_BOOL_VALUE case-fold) SCM_REGEXP_CASE_FOLD 0) |
| 48 |
(?: (SCM_BOOL_VALUE multi-line) SCM_REGEXP_MULTI_LINE 0))))) |
| 49 |
(define-cproc regexp-ast (regexp::<regexp>) (return (-> regexp ast))) |
| 50 |
(define-cproc regexp-case-fold? (regexp::<regexp>) ::<boolean> |
| 51 |
(return (logand (-> regexp flags) SCM_REGEXP_CASE_FOLD))) |
| 52 |
|
| 53 |
(define-cproc regexp-parse (str::<string> :key (case-fold #f) (multi-line #f)) |
| 54 |
(return |
| 55 |
(Scm_RegComp str |
| 56 |
(logior (?: (SCM_BOOL_VALUE case-fold) SCM_REGEXP_CASE_FOLD 0) |
| 57 |
(?: (SCM_BOOL_VALUE multi-line) SCM_REGEXP_MULTI_LINE 0) |
| 58 |
SCM_REGEXP_PARSE_ONLY)))) |
| 59 |
(define-cproc regexp-compile (ast :key (multi-line #f)) |
| 60 |
(return (Scm_RegCompFromAST2 ast |
| 61 |
(?: (SCM_BOOL_VALUE multi-line) |
| 62 |
SCM_REGEXP_MULTI_LINE |
| 63 |
0)))) |
| 64 |
(define-cproc regexp-optimize (ast) Scm_RegOptimizeAST) |
| 65 |
|
| 66 |
(define-cproc regexp-num-groups (regexp::<regexp>) ::<int> |
| 67 |
(return (-> regexp numGroups))) |
| 68 |
(define-cproc regexp-named-groups (regexp::<regexp>) |
| 69 |
(return (-> regexp grpNames))) |
| 70 |
|
| 71 |
(define-cproc rxmatch (regexp str::<string> :optional start end) |
| 72 |
(let* ([rx::ScmRegexp* NULL]) |
| 73 |
(cond [(SCM_STRINGP regexp) (set! rx (SCM_REGEXP (Scm_RegComp |
| 74 |
(SCM_STRING regexp) 0)))] |
| 75 |
[(SCM_REGEXPP regexp) (set! rx (SCM_REGEXP regexp))] |
| 76 |
[else (SCM_TYPE_ERROR regexp "regexp")]) |
| 77 |
(return (Scm_RegExec rx str start end)))) |
| 78 |
|
| 79 |
(inline-stub |
| 80 |
(define-cise-stmt rxmatchop |
| 81 |
[(_ (exp ...)) (template exp)] |
| 82 |
[(_ fn) (template `(,fn (SCM_REGMATCH match) obj))] |
| 83 |
:where |
| 84 |
(define (template result) |
| 85 |
`(cond [(SCM_FALSEP match) (return '#f)] |
| 86 |
[(SCM_REGMATCHP match) (return ,result)] |
| 87 |
[else (SCM_TYPE_ERROR match "regmatch object or #f") |
| 88 |
(return SCM_UNDEFINED)]))) |
| 89 |
) |
| 90 |
|
| 91 |
(define-cproc rxmatch-substring (match :optional (obj 0)) |
| 92 |
(rxmatchop Scm_RegMatchSubstr)) |
| 93 |
(define-cproc rxmatch-start (match :optional (obj 0)) |
| 94 |
(rxmatchop Scm_RegMatchStart)) |
| 95 |
(define-cproc rxmatch-end (match :optional (obj 0)) |
| 96 |
(rxmatchop Scm_RegMatchEnd)) |
| 97 |
(define-cproc rxmatch-before (match :optional (obj 0)) |
| 98 |
(rxmatchop Scm_RegMatchBefore)) |
| 99 |
(define-cproc rxmatch-after (match :optional (obj 0)) |
| 100 |
(rxmatchop Scm_RegMatchAfter)) |
| 101 |
(define-cproc rxmatch-num-matches (match) |
| 102 |
(if (SCM_FALSEP match) |
| 103 |
(return (SCM_MAKE_INT 0)) |
| 104 |
(rxmatchop (SCM_MAKE_INT (-> (SCM_REGMATCH match) numMatches))))) |
| 105 |
(define-cproc rxmatch-named-groups (match) |
| 106 |
(if (SCM_FALSEP match) |
| 107 |
(return SCM_NIL) |
| 108 |
(rxmatchop (-> (SCM_REGMATCH match) grpNames)))) |
| 109 |
|
| 110 |
(select-module gauche.internal) |
| 111 |
(define-cproc %regexp-dump (rx::<regexp>) ::<void> Scm_RegDump) |
| 112 |
(define-cproc %regmatch-dump (rm::<regmatch>) ::<void> Scm_RegMatchDump) |
| 113 |
|
| 114 |
(define-cproc %regexp-pattern (regexp::<regexp>) |
| 115 |
(setter (regexp::<regexp> pat::<string>) ::<void> |
| 116 |
(set! (-> regexp pattern) (SCM_OBJ pat))) |
| 117 |
(return (-> regexp pattern))) |
| 118 |
(define-cproc %regexp-laset (regexp::<regexp>) ; for testing |
| 119 |
(return (-> regexp laset))) |
| 120 |
|
| 121 |
(select-module gauche.internal) |
| 122 |
;; aux routine for regexp-replace[-all] |
| 123 |
;; "abc\\1de\\3" => '("abc" 1 "de" 3) |
| 124 |
(define (%regexp-parse-subpattern sub) |
| 125 |
(cond |
| 126 |
[(string? sub) |
| 127 |
(let loop ((sub sub) (r '())) |
| 128 |
(cond [(rxmatch #/\\(?:(\d+)|k<([^>]+)>|(.))/ sub) |
| 129 |
=> (^m |
| 130 |
(define (loop2 elem) |
| 131 |
(loop (rxmatch-after m) |
| 132 |
(list* elem (rxmatch-before m) r))) |
| 133 |
(cond [(rxmatch-substring m 1) |
| 134 |
=> (^d (loop2 (string->number d)))] |
| 135 |
[(rxmatch-substring m 2) |
| 136 |
=> (^s (loop2 (string->symbol s)))] |
| 137 |
[else (loop2 (rxmatch-substring m 3))]))] |
| 138 |
[else (reverse (cons sub r))]))] |
| 139 |
[(procedure? sub) sub] |
| 140 |
[else (error "string, procedure or list required, but got" sub)])) |
| 141 |
|
| 142 |
;; Skip the first subskip matches, then start replacing only up to |
| 143 |
;; subcount times (or infinite if subcount is #f). |
| 144 |
(define (%regexp-replace-rec rx string subpat subskip subcount out) |
| 145 |
(define (next-string match) |
| 146 |
(let1 rest (rxmatch-after match) |
| 147 |
(and (not (equal? rest "")) |
| 148 |
(if (= (rxmatch-start match) (rxmatch-end match)) |
| 149 |
(begin (display (string-ref rest 0) out) |
| 150 |
(string-copy rest 1)) |
| 151 |
rest)))) |
| 152 |
(if (and subcount (zero? subcount)) |
| 153 |
(display string out) |
| 154 |
(let1 match (rxmatch rx string) |
| 155 |
(cond |
| 156 |
[(not match) |
| 157 |
(display string out)] |
| 158 |
[(> subskip 0) |
| 159 |
(display (rxmatch-before match) out) |
| 160 |
(display (rxmatch-substring match) out) |
| 161 |
(and-let1 next (next-string match) |
| 162 |
(%regexp-replace-rec rx next subpat (- subskip 1) subcount out))] |
| 163 |
[else |
| 164 |
(display (rxmatch-before match) out) |
| 165 |
(if (procedure? subpat) |
| 166 |
(display (subpat match) out) |
| 167 |
(dolist [pat subpat] |
| 168 |
(display (cond |
| 169 |
[(eq? pat 'pre) (rxmatch-before match)] |
| 170 |
[(eq? pat 'post) (rxmatch-after match)] |
| 171 |
[(or (number? pat) (symbol? pat)) |
| 172 |
(rxmatch-substring match pat)] |
| 173 |
[else pat]) |
| 174 |
out))) |
| 175 |
(and-let1 next (next-string match) |
| 176 |
(%regexp-replace-rec rx next subpat subskip |
| 177 |
(and subcount (- subcount 1)) |
| 178 |
out))])))) |
| 179 |
|
| 180 |
(define (%regexp-replace rx string subpat subskip subcount) |
| 181 |
(call-with-output-string |
| 182 |
(cut %regexp-replace-rec rx string subpat subskip subcount <>))) |
| 183 |
|
| 184 |
(define-in-module gauche (regexp-replace rx string sub) |
| 185 |
(%regexp-replace rx string |
| 186 |
(%regexp-parse-subpattern sub) 0 1)) |
| 187 |
|
| 188 |
(define-in-module gauche (regexp-replace-all rx string sub) |
| 189 |
(%regexp-replace rx string |
| 190 |
(%regexp-parse-subpattern sub) 0 #f)) |
| 191 |
|
| 192 |
(define (regexp-replace-driver name func-1) |
| 193 |
(^[string rx sub . more] |
| 194 |
(cond [(null? more) (func-1 rx string sub)] |
| 195 |
[else |
| 196 |
(unless (even? (length more)) |
| 197 |
(errorf "~a: regexp and substitution don't pair up" name)) |
| 198 |
(let loop ([s (func-1 rx string sub)] |
| 199 |
[args more]) |
| 200 |
(if (null? args) |
| 201 |
s |
| 202 |
(loop (func-1 (car args) s (cadr args)) |
| 203 |
(cddr args))))]))) |
| 204 |
|
| 205 |
(define-in-module gauche regexp-replace* |
| 206 |
(regexp-replace-driver 'regexp-replace* regexp-replace)) |
| 207 |
|
| 208 |
(define-in-module gauche regexp-replace-all* |
| 209 |
(regexp-replace-driver 'regexp-replace-all* regexp-replace-all)) |
| 210 |
|
| 211 |
;; Contributed from Alex Shinn; modified a bit by shiro |
| 212 |
(define-in-module gauche (regexp-quote str) |
| 213 |
(with-string-io str |
| 214 |
(^[] (let loop ((c (read-char))) |
| 215 |
(unless (eof-object? c) |
| 216 |
(when (char-set-contains? #[\\|\[\](){}.*+?^$] c) (write-char #\\)) |
| 217 |
(write-char c) |
| 218 |
(loop (read-char))))))) |
| 219 |
|
| 220 |
(define-in-module gauche (regexp->string rx) |
| 221 |
(or (%regexp-pattern rx) |
| 222 |
(rlet1 s (regexp-unparse (regexp-ast rx)) |
| 223 |
(set! (%regexp-pattern rx) s)))) |
| 224 |
|
| 225 |
(define-in-module gauche (rxmatch->string rx str . sel) |
| 226 |
(cond [(null? sel) (rxmatch-substring (rxmatch rx str))] |
| 227 |
[(eq? (car sel) 'after) |
| 228 |
(apply rxmatch-after (rxmatch rx str) (cdr sel))] |
| 229 |
[(eq? (car sel) 'before) |
| 230 |
(apply rxmatch-before (rxmatch rx str) (cdr sel))] |
| 231 |
[else (rxmatch-substring (rxmatch rx str) (car sel))])) |