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 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 literal string s |
---|
154 | |
---|
155 | (define (lit s) |
---|
156 | (let ((f (lambda (t) (tok t (try char=?))))) |
---|
157 | (seq (map f (if (string? s) (string->list s) s))))) |
---|
158 | |
---|
159 | |
---|
160 | ;; Takes the resulting streams produced by the application of a |
---|
161 | ;; pattern on a stream (or streams) and selects the longest match if |
---|
162 | ;; one exists. |
---|
163 | |
---|
164 | (define (longest streams) |
---|
165 | (match-let (((count stream) |
---|
166 | (fold (lambda (stream max) |
---|
167 | (match (list stream max) |
---|
168 | (((eaten food) (max-count max-stream)) |
---|
169 | (if (< max-count (length eaten)) |
---|
170 | (list (length eaten) stream) max)) |
---|
171 | (else (error 'longest "invalid stream" stream)))) |
---|
172 | (list 0 `(() ())) |
---|
173 | streams))) |
---|
174 | (and (positive? count) stream))) |
---|
175 | |
---|
176 | |
---|
177 | ;; This takes a pattern and a string, turns the string into a list of |
---|
178 | ;; streams (containing one stream), applies the pattern, and returns |
---|
179 | ;; the longest match. |
---|
180 | |
---|
181 | (define (->char-list s) |
---|
182 | (if (string? s) (string->list s) s)) |
---|
183 | |
---|
184 | (define (lex pat error s) |
---|
185 | (let* ((stream (->char-list s)) |
---|
186 | (res (longest (pat identity error `((() ,stream)))))) |
---|
187 | (and res (list (reverse (first res)) (second res))))) |
---|
188 | |
---|
189 | ) |
---|