-
Notifications
You must be signed in to change notification settings - Fork 0
/
irregex-utils.scm
154 lines (145 loc) · 5.29 KB
/
irregex-utils.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
;;;; irregex-utils.scm
;;
;; Copyright (c) 2010 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
(define rx-special-chars
"\\|[](){}.*+?^$#")
(define (string-scan-char str c . o)
(let ((end (string-length str)))
(let scan ((i (if (pair? o) (car o) 0)))
(cond ((= i end) #f)
((eqv? c (string-ref str i)) i)
(else (scan (+ i 1)))))))
(define (irregex-quote str)
(list->string
(let loop ((ls (string->list str)) (res '()))
(if (null? ls)
(reverse res)
(let ((c (car ls)))
(if (string-scan-char rx-special-chars c)
(loop (cdr ls) (cons c (cons #\\ res)))
(loop (cdr ls) (cons c res))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (irregex-opt ls)
(define (make-alt ls)
(cond ((null? (cdr ls)) (car ls))
((every char? ls) (list (list->string ls)))
(else (cons 'or ls))))
(define (make-seq ls)
(cond ((null? (cdr ls)) (car ls))
((every (lambda (x) (or (string? x) (char? x))) ls)
(apply string-append (map (lambda (x) (if (char? x) (string x) x)) ls)))
(else (cons 'seq ls))))
(cond
((null? ls) "")
((null? (cdr ls)) (car ls))
(else
(let ((chars (make-vector 256 '())))
(let lp1 ((ls ls) (empty? #f))
(if (null? ls)
(let lp2 ((i 0) (res '()))
(if (= i 256)
(let ((res (make-alt (reverse res))))
(if empty? `(? ,res) res))
(let ((c (integer->char i))
(opts (vector-ref chars i)))
(lp2 (+ i 1)
(cond
((null? opts) res)
((equal? opts '("")) `(,c ,@res))
(else `(,(make-seq (list c (irregex-opt opts)))
,@res)))))))
(let* ((str (car ls))
(len (string-length str)))
(if (zero? len)
(lp1 (cdr ls) #t)
(let ((i (char->integer (string-ref str 0))))
(vector-set!
chars
i
(cons (substring str 1 len) (vector-ref chars i)))
(lp1 (cdr ls) empty?))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (cset->string ls)
(let ((out (open-output-string)))
(let lp ((ls ls))
(cond
((pair? ls)
(cond
((pair? (car ls))
(display (irregex-quote (string (caar ls))) out)
(write-char #\- out)
(display (irregex-quote (string (cdar ls))) out))
(else (display (irregex-quote (string (car ls))) out)))
(lp (cdr ls)))))
(get-output-string out)))
(define (sre->string obj)
(let ((out (open-output-string)))
(let lp ((x obj))
(cond
((pair? x)
(case (car x)
((: seq)
(cond
((and (pair? (cdr x)) (pair? (cddr x)) (not (eq? x obj)))
(display "(?:" out) (for-each lp (cdr x)) (display ")" out))
(else (for-each lp (cdr x)))))
((submatch)
(display "(" out) (for-each lp (cdr x)) (display ")" out))
((submatch-named)
(display "(?<" out) (display (cadr x) out) (display ">" out)
(for-each lp (cddr x)) (display ")" out))
((or)
(display "(?:" out)
(lp (cadr x))
(for-each (lambda (x) (display "|" out) (lp x)) (cddr x))
(display ")" out))
((* + ? *? ??)
(cond
((pair? (cddr x))
(display "(?:" out) (for-each lp (cdr x)) (display ")" out))
(else (lp (cadr x))))
(display (car x) out))
((not)
(cond
((and (pair? (cadr x)) (eq? 'cset (caadr x)))
(display "[^" out)
(display (cset->string (cdadr x)) out)
(display "]" out))
(else (error "can't represent general 'not' in strings" x))))
((cset)
(display "[" out)
(display (cset->string (cdr x)) out)
(display "]" out))
((- & / ~)
(cond
((or (eqv? #\~ (car x))
(and (eq? '- (car x)) (pair? (cdr x)) (eq? 'any (cadr x))))
(display "[^" out)
(display (cset->string (if (eqv? #\~ (car x)) (cdr x) (cddr x))) out)
(display "]" out))
(else
(lp `(cset ,@(sre->cset x))))))
((w/case w/nocase)
(display "(?" out)
(if (eq? (car x) 'w/case) (display "-" out))
(display ":" out)
(for-each lp (cdr x))
(display ")" out))
(else
(if (string? (car x))
(lp `(cset ,@(string->list (car x))))
(error "unknown sre operator" x)))))
((symbol? x)
(case x
((bos bol) (display "^" out))
((eos eol) (display "$" out))
((any nonl) (display "." out))
(else (error "unknown sre symbol" x))))
((string? x)
(display (irregex-quote x) out))
((char? x)
(display (irregex-quote (string x)) out))
(else
(error "unknown sre pattern" x))))
(get-output-string out)))