| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 20 |
(define-syntax rx |
| 21 |
(syntax-rules () |
| 22 |
((rx sre ...) |
| 23 |
(regexp `(: sre ...))))) |
| 24 |
|
| 25 |
|
| 26 |
(define-record-type State |
| 27 |
(%make-state accept? chars match match-rule next1 next2 id) |
| 28 |
state? |
| 29 |
|
| 30 |
(accept? state-accept? state-accept?-set!) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
(chars state-chars state-chars-set!) |
| 36 |
|
| 37 |
(match state-match state-match-set!) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
(match-rule state-match-rule state-match-rule-set!) |
| 43 |
|
| 44 |
(next1 state-next1 state-next1-set!) |
| 45 |
|
| 46 |
(next2 state-next2 state-next2-set!) |
| 47 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 160 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
(define (regexp-match-submatch-start md n) |
| 183 |
(cond ((regexp-match-submatch-start+end md n) => car) (else #f))) |
| 184 |
|
| 185 |
|
| 186 |
|
| 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 |
|
| 221 |
|
| 222 |
|
| 223 |
(define (regexp-match->list md) |
| 224 |
(regexp-match-convert #f md #f)) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
(define (regexp-match->sexp md) |
| 230 |
(regexp-match-convert #t md #f)) |
| 231 |
|
| 232 |
|
| 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 |
|
| 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 |
|
| 260 |
|
| 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 |
|
| 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 |
|
| 297 |
|
| 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 |
|
| 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 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
(define (epsilon-state? st) |
| 350 |
(or (not (state-chars st)) |
| 351 |
(procedure? (state-chars st)))) |
| 352 |
|
| 353 |
|
| 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 |
|
| 371 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 403 |
|
| 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 |
|
| 420 |
((posse-ref new sr) |
| 421 |
|
| 422 |
=> (lambda (sr-prev) (searcher-merge! sr-prev sr))) |
| 423 |
(else |
| 424 |
|
| 425 |
(posse-add! new sr)))))) |
| 426 |
|
| 427 |
|
| 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 |
|
| 437 |
|
| 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 |
|
| 456 |
|
| 457 |
|
| 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 |
|
| 465 |
(let ((ch (string-cursor-ref str i)) |
| 466 |
(i2 (string-cursor-next str i))) |
| 467 |
(posse-for-each |
| 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 |
|
| 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 |
|
| 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 |
|
| 488 |
|
| 489 |
|
| 490 |
(define (regexp-matches rx str . o) |
| 491 |
(apply regexp-run #f rx str o)) |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
(define (regexp-matches? rx str . o) |
| 497 |
(and (apply regexp-matches rx str o) #t)) |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
(define (regexp-search rx str . o) |
| 503 |
(apply regexp-run #t rx str o)) |
| 504 |
|
| 505 |
|
| 506 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 798 |
|
| 799 |
|
| 800 |
(if (null? (cdr sre)) |
| 801 |
next |
| 802 |
|
| 803 |
|
| 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 |
|
| 812 |
|
| 813 |
|
| 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 |
|
| 827 |
|
| 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 |
|
| 832 |
|
| 833 |
|
| 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 |
|
| 841 |
|
| 842 |
|
| 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 |
|
| 849 |
(->rx (sre-expand-reps (cadr sre) (cadr sre) (cons 'seq (cddr sre))) |
| 850 |
flags next)) |
| 851 |
((>= at-least) |
| 852 |
|
| 853 |
(->rx (sre-expand-reps (cadr sre) #f (cons 'seq (cddr sre))) |
| 854 |
flags next)) |
| 855 |
((** repeated **? non-greedy-repeated) |
| 856 |
|
| 857 |
(->rx (sre-expand-reps (cadr sre) (car (cddr sre)) |
| 858 |
(cons 'seq (cdr (cddr sre)))) |
| 859 |
flags next)) |
| 860 |
((-> => submatch-named) |
| 861 |
|
| 862 |
|
| 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 |
|
| 879 |
|
| 880 |
|
| 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 |
|
| 893 |
|
| 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 |
|
| 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 |
(make-rx start current-match current-index non-greedy-indexes |
| 976 |
(list->vector (reverse match-rules)) match-names sre))))) |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 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 |
|
| 1016 |
|
| 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 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
(define (regexp-split rx str . o) |
| 1033 |
|
| 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 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 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 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 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 |
|
| 1119 |
|
| 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)) |