1
2;;> A regular expression engine implementing SRFI 115 using a
3;;> non-backtracking Thompson NFA algorithm.
4
5(define-library (chibi regexp)
6  (export regexp regexp? valid-sre? rx regexp->sre char-set->sre
7          regexp-matches regexp-matches? regexp-search
8          regexp-replace regexp-replace-all
9          regexp-fold regexp-extract regexp-split regexp-partition
10          regexp-match? regexp-match-count
11          regexp-match-submatch regexp-match-submatch/list
12          regexp-match-submatch-start regexp-match-submatch-end
13          regexp-match->list regexp-match->sexp)
14  (import (srfi 69))
15  ;; Chibi's char-set library is more factored than SRFI-14.
16  (cond-expand
17   (chibi
18    (import (rename (chibi)
19                    (protect guard)
20                    (char-downcase %char-downcase)
21                    (char-upcase %char-upcase))
22            (only (scheme char) char-downcase char-upcase)
23            (srfi 9)
24            (chibi char-set)
25            (chibi char-set full)
26            (prefix (chibi char-set ascii) %))
27    (begin
28      (define char-set:title-case
29        (char-set-union
30         (ucs-range->char-set #x1F88 #x1F90)
31         (ucs-range->char-set #x1F98 #x1FA0)
32         (ucs-range->char-set #x1FA8 #x1FB0)
33         (char-set #\x01C5 #\x01C8 #\x01CB #\x01F2 #\x1FBC #\x1FCC #\x1FFC)))))
34   (else
35    (import (scheme base) (scheme char) (srfi 1) (srfi 14))
36    (begin
37      (define %char-set:letter
38        (char-set-intersection char-set:ascii char-set:letter))
39      (define %char-set:lower-case
40        (char-set-intersection char-set:ascii char-set:lower-case))
41      (define %char-set:upper-case
42        (char-set-intersection char-set:ascii char-set:upper-case))
43      (define %char-set:digit
44        (char-set-intersection char-set:ascii char-set:digit))
45      (define %char-set:letter+digit
46        (char-set-intersection char-set:ascii char-set:letter+digit))
47      (define %char-set:punctuation
48        (char-set-intersection char-set:ascii char-set:punctuation))
49      (define %char-set:symbol
50        (char-set-intersection char-set:ascii char-set:symbol))
51      (define %char-set:graphic
52        (char-set-intersection char-set:ascii char-set:graphic))
53      (define %char-set:whitespace
54        (char-set-intersection char-set:ascii char-set:whitespace))
55      (define %char-set:printing
56        (char-set-intersection char-set:ascii char-set:printing))
57      (define %char-set:iso-control
58        (char-set-intersection char-set:ascii char-set:iso-control)))))
59  (cond-expand
60   ((library (srfi 151)) (import (srfi 151)))
61   ((library (srfi 33)) (import (srfi 33)))
62   (else (import (srfi 60))))
63  (import (chibi char-set boundary))
64  ;; Use string-cursors where available.
65  (cond-expand
66   (chibi
67    (begin
68      (define (string-start-arg s o)
69        (if (pair? o) (string-index->cursor s (car o)) (string-cursor-start s)))
70      (define (string-end-arg s o)
71        (if (pair? o) (string-index->cursor s (car o)) (string-cursor-end s)))
72      (define (string-concatenate-reverse ls)
73        (string-concatenate (reverse ls)))))
74   (else
75    (begin
76      (define (string-start-arg s o)
77        (if (pair? o) (string-index->cursor s (car o)) 0))
78      (define (string-end-arg s o)
79        (if (pair? o) (string-index->cursor s (car o)) (string-length s)))
80      (define string-cursor? integer?)
81      (define string-cursor=? =)
82      (define string-cursor<? <)
83      (define string-cursor<=? <=)
84      (define string-cursor>? >)
85      (define string-cursor>=? >=)
86      (define string-cursor-ref string-ref)
87      (define (string-cursor-next s i) (+ i 1))
88      (define (string-cursor-prev s i) (- i 1))
89      (define substring-cursor substring)
90      (define (string-cursor->index str off) off)
91      (define (string-index->cursor str i) i)
92      (define (string-concatenate ls) (apply string-append ls))
93      (define (string-concatenate-reverse ls)
94        (string-concatenate (reverse ls))))))
95  (include "regexp.scm"))
96