source: project/release/4/lexgen/trunk/lexgen.scm @ 14871

Last change on this file since 14871 was 14871, checked in by Ivan Raikov, 10 years ago

bug fixes in lexgen star

File size: 6.2 KB
Line 
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 ((a1 (lambda (streams1) (a (concatenate (list streams streams1))))))
108      (p (lambda (streams1) (if (equal? streams streams1) (a streams1)
109                                ((star p) a1 a1 streams1)))
110         a streams))))
111
112;; The rest of these are built from the previous four and are provided
113;; for convenience.
114
115;; this parser always succeeds
116(define (pass a r s) (a s))
117 
118;; Positive closure. Analogous to '+'
119
120(define (pos pat) (seq pat (star pat)))
121
122;; Optional pattern. Analogous to '?'
123
124(define (opt pat) (bar pat pass))
125
126;; Converts a binary predicate procedure to a binary procedure that
127;; returns its right argument when the predicate is true, and false
128;; otherwise.
129
130(define (try p) (lambda (x y) (let ((res (p x y))) (and res y))))
131
132;; Matches a single character
133
134(define (char c) (tok c (try char=?)))
135 
136;; Matches any of a SRFI-14 set of characters.
137
138(define (set s)
139  (let ((cs (if (char-set? s) s (list->char-set (if (string? s) (string->list s) s)))))
140    (tok cs (try char-set-contains?))))
141
142;; Range of characters. Analogous to character class '[]'
143
144(define (range a b)
145  (if (char<? b a) (range b a)
146      (set (ucs-range->char-set 
147            (char->integer a) (char->integer b)))))
148
149;; Matches a consecutive list of patterns
150
151(define (lst ps)
152  (let ((ps (reverse ps)))
153    (let loop ((ps (cdr ps)) (p1 (car ps)))
154      (cond ((null? ps) p1)
155            ((null? (cdr ps))    (seq (car ps) p1))
156            (else (loop (cdr ps) (seq (car ps) p1)))))))
157 
158;; Matches a literal string s
159
160(define (lit s)
161  (let ((f (lambda (t) (tok t (try char=?)))))
162    (lst (map f (if (string? s) (string->list s) s)))))
163
164
165;; Takes the resulting streams produced by the application of a
166;; pattern on a stream (or streams) and selects the longest match if
167;; one exists.
168
169(define (longest streams)
170  (match-let (((count stream)
171               (fold (lambda (stream max)
172                       (match (list stream max)
173                              (((eaten food) (max-count max-stream)) 
174                               (if (< max-count (length eaten))
175                                   (list (length eaten) stream) max))
176                              (else (error 'longest "invalid stream" stream))))
177                     (list 0 `(() ()))
178                     streams)))
179             (and (positive? count) stream)))
180
181
182;; This takes a pattern and a string, turns the string into a list of
183;; streams (containing one stream), applies the pattern, and returns
184;; the longest match.
185
186(define (->char-list s)
187  (if (string? s) (string->list s) s))
188
189(define (lex pat error s)
190  (let* ((stream (->char-list s))
191         (res    (longest (pat identity error `((() ,stream))))))
192    (and res (list (reverse (first res)) (second res)))))
193
194)
Note: See TracBrowser for help on using the repository browser.