1 | ;; |
---|
2 | ;; Lexer combinator library. |
---|
3 | ;; |
---|
4 | ;; Based on the SML lexer generator by Thant Tessman. |
---|
5 | ;; |
---|
6 | ;; Ported to Chicken Scheme by Ivan Raikov. |
---|
7 | ;; Copyright 2009 Ivan Raikov. |
---|
8 | ;; |
---|
9 | ;; |
---|
10 | ;; Redistribution and use in source and binary forms, with or without |
---|
11 | ;; modification, are permitted provided that the following conditions |
---|
12 | ;; are met: |
---|
13 | ;; |
---|
14 | ;; - Redistributions of source code must retain the above copyright |
---|
15 | ;; notice, this list of conditions and the following disclaimer. |
---|
16 | ;; |
---|
17 | ;; - Redistributions in binary form must reproduce the above |
---|
18 | ;; copyright notice, this list of conditions and the following |
---|
19 | ;; disclaimer in the documentation and/or other materials provided |
---|
20 | ;; with the distribution. |
---|
21 | ;; |
---|
22 | ;; - Neither name of the copyright holders nor the names of its |
---|
23 | ;; contributors may be used to endorse or promote products derived |
---|
24 | ;; from this software without specific prior written permission. |
---|
25 | ;; |
---|
26 | ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE |
---|
27 | ;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
---|
28 | ;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
29 | ;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
30 | ;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR THE |
---|
31 | ;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
32 | ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
33 | ;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
---|
34 | ;; USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
---|
35 | ;; AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
36 | ;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
---|
37 | ;; ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
38 | ;; POSSIBILITY OF SUCH DAMAGE. |
---|
39 | ;; |
---|
40 | |
---|
41 | (module lexgen |
---|
42 | |
---|
43 | ( tok seq star bar |
---|
44 | try pass pos opt char |
---|
45 | set range lst lit |
---|
46 | longest lex ) |
---|
47 | |
---|
48 | |
---|
49 | (import scheme chicken data-structures srfi-14) |
---|
50 | (require-extension srfi-1 matchable) |
---|
51 | |
---|
52 | ;; |
---|
53 | ;; This is a lexer generator comprised in its core of four small |
---|
54 | ;; functions. The programmer assembles these functions into regular |
---|
55 | ;; expression pattern-matching functions. |
---|
56 | ;; |
---|
57 | ;; The idea is that a pattern matcher function takes a list of |
---|
58 | ;; streams, and returns a new list of streams advanced by every |
---|
59 | ;; combination allowed by the pattern matcher function. In this |
---|
60 | ;; implementation, a stream is simply a tuple containing a list of |
---|
61 | ;; elements consumed by the pattern matcher, and a list of |
---|
62 | ;; characters not yet consumed. |
---|
63 | ;; |
---|
64 | ;; Note that the number of streams returned by the function |
---|
65 | ;; typically won't match the number of streams passed in. If the |
---|
66 | ;; pattern doesn't match at all, the empty list is returned. |
---|
67 | ;; |
---|
68 | |
---|
69 | ;; 'tok' builds a pattern matcher function that applies procedure p to |
---|
70 | ;; a given token and an input character. If the procedure returns a |
---|
71 | ;; true value, that value is prepended to the list of consumed |
---|
72 | ;; elements, and the input character is removed from the list of input |
---|
73 | ;; elements. |
---|
74 | |
---|
75 | (define (tok t p) |
---|
76 | (let ((f (lambda (s) |
---|
77 | (match s ((c (h . r)) |
---|
78 | (let ((ans (p t h))) |
---|
79 | (and ans (list (cons ans c) r)))) |
---|
80 | ((c ()) s) |
---|
81 | (else #f))))) |
---|
82 | (lambda (a r streams) |
---|
83 | (let ((streams1 (filter-map f streams))) |
---|
84 | (if (null? streams1) (r streams) (a streams1)))))) |
---|
85 | |
---|
86 | |
---|
87 | ;; This matches a sequence of patterns. |
---|
88 | |
---|
89 | (define (seq p1 p2) |
---|
90 | (lambda (a r streams) |
---|
91 | (p1 (lambda (streams1) (p2 a r streams1)) r streams))) |
---|
92 | |
---|
93 | |
---|
94 | ;; This matches either one of two patterns. It's analogous to patterns |
---|
95 | ;; separated by the '|' in regular expressions. |
---|
96 | |
---|
97 | (define (bar p1 p2) |
---|
98 | (lambda (a r streams) |
---|
99 | (let ((r1 (lambda (streams1) (p2 a (lambda (streams2) (r streams1)) streams)) )) |
---|
100 | (p1 a r1 streams)))) |
---|
101 | |
---|
102 | |
---|
103 | ;; Kleene closure. Analogous to '*' |
---|
104 | |
---|
105 | (define (star p) |
---|
106 | (lambda (a r streams) |
---|
107 | (let* ((r1 (lambda (streams1) (a (concatenate (list streams streams1))))) |
---|
108 | (a2 (lambda (streams1) (p (lambda (streams2) (a (concatenate (list streams streams1 streams2)))) r1 streams1))) |
---|
109 | (a1 (lambda (streams1) (p a2 r1 streams1)))) |
---|
110 | (p a1 a streams)))) |
---|
111 | |
---|
112 | |
---|
113 | ;; The rest of these are built from the previous four and are provided |
---|
114 | ;; for convenience. |
---|
115 | |
---|
116 | ;; this parser always succeeds |
---|
117 | (define (pass a r s) |
---|
118 | (a s)) |
---|
119 | |
---|
120 | ;; Positive closure. Analogous to '+' |
---|
121 | |
---|
122 | (define (pos pat) |
---|
123 | (seq pat (star pat))) |
---|
124 | |
---|
125 | ;; Optional pattern. Analogous to '?' |
---|
126 | |
---|
127 | (define (opt pat) |
---|
128 | (bar pat pass)) |
---|
129 | |
---|
130 | ;; Converts a binary predicate procedure to a binary procedure that |
---|
131 | ;; returns its right argument when the predicate is true, and false |
---|
132 | ;; otherwise. |
---|
133 | |
---|
134 | (define (try p) (lambda (x y) (let ((res (p x y))) (and res y)))) |
---|
135 | |
---|
136 | ;; Matches a single character |
---|
137 | |
---|
138 | (define (char c) (tok c (try char=?))) |
---|
139 | |
---|
140 | ;; Matches any of a SRFI-14 set of characters. |
---|
141 | |
---|
142 | (define (set s) |
---|
143 | (let ((cs (if (char-set? s) s (list->char-set (if (string? s) (string->list s) s))))) |
---|
144 | (tok cs (try char-set-contains?)))) |
---|
145 | |
---|
146 | ;; Range of characters. Analogous to character class '[]' |
---|
147 | |
---|
148 | (define (range a b) |
---|
149 | (if (char<? b a) (range b a) |
---|
150 | (set (ucs-range->char-set |
---|
151 | (char->integer a) (char->integer b))))) |
---|
152 | |
---|
153 | ;; Matches a consecutive list of patterns |
---|
154 | |
---|
155 | (define (lst ps) |
---|
156 | (let ((ps (reverse ps))) |
---|
157 | (let loop ((ps (cdr ps)) (p1 (car ps))) |
---|
158 | (cond ((null? ps) p1) |
---|
159 | ((null? (cdr ps)) (seq (car ps) p1)) |
---|
160 | (else (loop (cdr ps) (seq (car ps) p1))))))) |
---|
161 | |
---|
162 | ;; Matches a literal string s |
---|
163 | |
---|
164 | (define (lit s) |
---|
165 | (let ((f (lambda (t) (tok t (try char=?))))) |
---|
166 | (lst (map f (if (string? s) (string->list s) s))))) |
---|
167 | |
---|
168 | |
---|
169 | ;; Takes the resulting streams produced by the application of a |
---|
170 | ;; pattern on a stream (or streams) and selects the longest match if |
---|
171 | ;; one exists. |
---|
172 | |
---|
173 | (define (longest streams) |
---|
174 | (match-let (((count stream) |
---|
175 | (fold (lambda (stream max) |
---|
176 | (match (list stream max) |
---|
177 | (((eaten food) (max-count max-stream)) |
---|
178 | (if (< max-count (length eaten)) |
---|
179 | (list (length eaten) stream) max)) |
---|
180 | (else (error 'longest "invalid stream" stream)))) |
---|
181 | (list 0 `(() ())) |
---|
182 | streams))) |
---|
183 | (and (positive? count) stream))) |
---|
184 | |
---|
185 | |
---|
186 | ;; This takes a pattern and a string, turns the string into a list of |
---|
187 | ;; streams (containing one stream), applies the pattern, and returns |
---|
188 | ;; the longest match. |
---|
189 | |
---|
190 | (define (->char-list s) |
---|
191 | (if (string? s) (string->list s) s)) |
---|
192 | |
---|
193 | (define (lex pat error s) |
---|
194 | (let* ((stream (->char-list s)) |
---|
195 | (res (longest (pat identity error `((() ,stream)))))) |
---|
196 | (and res (list (reverse (first res)) (second res))))) |
---|
197 | |
---|
198 | ) |
---|