summaryrefslogtreecommitdiff
path: root/chapter-2/2.2-regular-expressions.scm
blob: fa92120adeb4821133a465e3cbd02854df27c170 (plain)
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
154
;;; Copyright © 2021 Gerald Sussman and Chris Hanson
;;; Copyright © 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(use-modules (ice-9 popen)
             (srfi srfi-1))


;; 2.2.2 Implementation of the translator

(define-syntax-rule (assert cond)
  (unless cond
    (error "assertion failed" 'cond)))

;; I am excluding the write-bourne-shell-grep-command and
;; bourne-shell-command-string procedures in the book in favor of
;; using Guile's pipe API. open-pipe* does not open a shell but runs
;; grep directly, eliminating the need to do shell quoting.  The book
;; asks "Are we having fun yet?" to which I say "yes, but I draw the
;; line at shell quoting."
(define (grep regexp string)
  (let ((pipe (open-pipe* OPEN_WRITE "grep" "-e" regexp)))
    (display string pipe)
    (newline pipe)
    (zero? (status:exit-val (close-pipe pipe)))))

(define (grep-test regexp . specs)
  (for-each (lambda (spec)
              (assert
               (if (and (pair? spec) (eq? (car spec) 'not))
                   (not (grep regexp (list-ref spec 1)))
                   (grep regexp spec))))
            specs))

(define (r:dot) ".")
(define (r:bol) "^")
(define (r:eol) "$")

(define (r:seq . exprs)
  (string-append "\\(" (apply string-append exprs) "\\)"))

(define chars-needing-quoting
  '(#\. #\[ #\\ #\^ #\$ #\*))

(define (r:quote string)
  (r:seq
   (list->string
    (append-map (lambda (char)
                  (if (memv char chars-needing-quoting)
                      (list #\\ char)
                      (list char)))
                (string->list string)))))

(grep-test (r:seq (r:quote "a") (r:dot) (r:quote "c"))
           "abc")

(define (r:alt . exprs)
  (if (pair? exprs)
      (apply r:seq
             (cons (car exprs)
                   (append-map (lambda (expr)
                                 (list "\\|" expr))
                               (cdr exprs))))
      (r:seq)))

(grep-test (r:alt (r:quote "foo") (r:quote "bar") (r:quote "baz"))
           "foo" "bar" "baz" '(not "frob"))

(define (r:repeat min max expr)
  (apply r:seq
         (append (make-list min expr)
                 (cond
                  ((not max)
                   (list expr "*"))
                  ((= max min)
                   '())
                  (else
                   (make-list (- max min) (r:alt expr "")))))))

(grep-test (r:repeat 3 5 (r:alt (r:quote "cat") (r:quote "dog")))
           "catdogcat"
           "catcatdogdog"
           "dogdogcatdogdog"
           '(not "catdogfrog"))

(define (bracket string procedure)
  (list->string
   (append '(#\[)
           (procedure (string->list string))
           '(#\]))))

(define chars-needing-quoting-in-brackets
  '(#\] #\^ #\-))

(define (quote-bracketed-contents members)
  (define (optional char)
    (if (memv char members)
        (list char)
        '()))
  (append (optional #\])
          (remove (lambda (c)
                    (memv c chars-needing-quoting-in-brackets))
                  members)
          (optional #\^)
          (optional #\-)))

(define (r:char-from string)
  (case (string-length string)
    ((0) (r:seq))
    ((1) (r:quote string))
    (else
     (bracket string
              (lambda (members)
                (if (lset= eq? '(#\- #\^) members)
                    '(#\- #\^)
                    (quote-bracketed-contents members)))))))

(define (r:char-not-from string)
  (bracket string
           (lambda (members)
             (cons #\^ (quote-bracketed-contents members)))))


;; Exercise 2.6: Adding * and + to regular expressions

(define (r:* expr)
  (r:repeat 0 #f expr))

(define (r:+ expr)
  (r:repeat 1 #f expr))

(grep-test (r:* (r:quote "foo"))
           "" "foo" "bar")
(grep-test (r:seq (r:quote "foo") (r:* (r:quote "bar")))
           "foo" "foobar" '(not ""))
(grep-test (r:+ (r:quote "foo"))
           '(not "") "foo" "foofoo")
(grep-test (r:seq (r:quote "foo") (r:+ (r:quote "bar")))
           '(not "foo") "foobar" "foobarbar")

;; Exercise 2.7: A bug, one bad joke, two tweaks, and a revelation