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 | (define-extension lexgen) |
---|
42 | |
---|
43 | (declare |
---|
44 | (not usual-integrations) |
---|
45 | (fixnum) |
---|
46 | (inline) |
---|
47 | (lambda-lift) |
---|
48 | (export tok seq star bar |
---|
49 | try pos opt char |
---|
50 | set range lit |
---|
51 | longest lex)) |
---|
52 | |
---|
53 | |
---|
54 | (require-extension syntax-case) |
---|
55 | (require-extension matchable) |
---|
56 | (require-extension srfi-1) |
---|
57 | (require-extension srfi-14) |
---|
58 | |
---|
59 | ;; |
---|
60 | ;; This is a lexer generator comprised in its core of four small |
---|
61 | ;; functions. The programmer assembles these functions into regular |
---|
62 | ;; expression pattern-matching functions. |
---|
63 | ;; |
---|
64 | ;; The idea is that a pattern matcher function takes a list of |
---|
65 | ;; streams, and returns a new list of streams advanced by every |
---|
66 | ;; combination allowed by the pattern matcher function. In this |
---|
67 | ;; implementation, a stream is simply a tuple containing a list of |
---|
68 | ;; elements consumed by the pattern matcher, and a list of |
---|
69 | ;; characters not yet consumed. |
---|
70 | ;; |
---|
71 | ;; Note that the number of streams returned by the function |
---|
72 | ;; typically won't match the number of streams passed in. If the |
---|
73 | ;; pattern doesn't match at all, the empty list is returned. |
---|
74 | ;; |
---|
75 | |
---|
76 | ;; 'tok' builds a pattern matcher function that applies procedure p to |
---|
77 | ;; a given token and an input character. If the procedure returns a |
---|
78 | ;; true value, that value is prepended to the list of consumed |
---|
79 | ;; elements, and the input character is removed from the list of input |
---|
80 | ;; elements. |
---|
81 | |
---|
82 | (define (tok t p) |
---|
83 | (let ((f (lambda (s) |
---|
84 | (match s ((s (h . r)) |
---|
85 | (let ((ans (p t h))) |
---|
86 | (and ans (list (cons ans s) r)))) |
---|
87 | (else #f))))) |
---|
88 | (lambda (streams) |
---|
89 | (filter-map f streams)))) |
---|
90 | |
---|
91 | |
---|
92 | ;; This matches a sequence of patterns. |
---|
93 | |
---|
94 | (define (seq pats) |
---|
95 | (lambda (streams) |
---|
96 | (fold (lambda (f s) (f s)) streams pats))) |
---|
97 | |
---|
98 | |
---|
99 | ;; This matches any of a list of patterns. It's analogous to a series |
---|
100 | ;; of patterns separated by the '|' in traditional regular |
---|
101 | ;; expressions. |
---|
102 | |
---|
103 | (define (bar pats) |
---|
104 | (lambda (streams) |
---|
105 | (concatenate (map (lambda (f) (f streams)) pats)))) |
---|
106 | |
---|
107 | ;; Kleene closure. Analogous to '*' |
---|
108 | |
---|
109 | (define (star pat) |
---|
110 | (define (f streams) |
---|
111 | (let ((res (pat streams))) |
---|
112 | (if (null? res) (list) |
---|
113 | (cons res (f res))))) |
---|
114 | (lambda (streams) |
---|
115 | (concatenate (cons streams (f streams))))) |
---|
116 | |
---|
117 | |
---|
118 | ;; The rest of these are built from the previous four and are provided |
---|
119 | ;; for convenience. |
---|
120 | |
---|
121 | ;; Positive closure. Analogous to '+' |
---|
122 | |
---|
123 | (define (pos pat) |
---|
124 | (seq (list pat (star pat)))) |
---|
125 | |
---|
126 | ;; Optional pattern. Analogous to '?' |
---|
127 | |
---|
128 | (define (opt pat) |
---|
129 | (bar (list pat identity))) |
---|
130 | |
---|
131 | ;; Converts a binary predicate procedure to a binary procedure that |
---|
132 | ;; returns its right argument when the predicate is true, and false |
---|
133 | ;; otherwise. |
---|
134 | |
---|
135 | (define (try p) (lambda (x y) (let ((res (p x y))) (and res y)))) |
---|
136 | |
---|
137 | ;; Matches a single character |
---|
138 | |
---|
139 | (define (char c) (tok c (try char=?))) |
---|
140 | |
---|
141 | ;; Matches any of a SRFI-14 set of characters. |
---|
142 | |
---|
143 | (define (set s) |
---|
144 | (let ((cs (if (char-set? s) s (list->char-set (if (string? s) (string->list s) s))))) |
---|
145 | (tok cs (try char-set-contains?)))) |
---|
146 | |
---|
147 | ;; Range of characters. Analogous to character class '[]' |
---|
148 | |
---|
149 | (define (range a b) |
---|
150 | (if (char<? b a) (range b a) |
---|
151 | (set (ucs-range->char-set |
---|
152 | (char->integer a) (char->integer b))))) |
---|
153 | |
---|
154 | ;; Matches a literal string s |
---|
155 | |
---|
156 | (define (lit s) |
---|
157 | (let ((f (lambda (t) (tok t (try char=?))))) |
---|
158 | (seq (map f (if (string? s) (string->list s) s))))) |
---|
159 | |
---|
160 | |
---|
161 | ;; Takes the resulting streams produced by the application of a |
---|
162 | ;; pattern on a stream (or streams) and selects the longest match if |
---|
163 | ;; one exists. |
---|
164 | |
---|
165 | (define (longest streams) |
---|
166 | (match-let (((count stream) |
---|
167 | (fold (lambda (stream max) |
---|
168 | (match (list stream max) |
---|
169 | (((eaten food) (max-count max-stream)) |
---|
170 | (if (< max-count (length eaten)) |
---|
171 | (list (length eaten) stream) max)) |
---|
172 | (else (error 'longest "invalid stream" stream)))) |
---|
173 | (list 0 `(() ())) |
---|
174 | streams))) |
---|
175 | (and (positive? count) stream))) |
---|
176 | |
---|
177 | |
---|
178 | ;; This takes a pattern and a string, turns the string into a list of |
---|
179 | ;; streams (containing one stream), applies the pattern, and returns |
---|
180 | ;; the longest match. |
---|
181 | |
---|
182 | (define (->char-list s) |
---|
183 | (if (string? s) (string->list s) s)) |
---|
184 | |
---|
185 | (define (lex pat s) |
---|
186 | (let* ((stream (->char-list s)) |
---|
187 | (res (longest (pat `((() ,stream)))))) |
---|
188 | (and res (list (reverse (first res)) (second res))))) |
---|