source: project/wiki/eggref/4/lexgen @ 14866

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

lexgen doc updated

File size: 5.6 KB
Line 
1[[tags: eggs]]
2[[toc:]]
3
4== lexgen
5
6=== Description
7
8{{lexgen}} is a lexer generator comprised in its core of only four
9small procedures. The programmer combines these procedures into
10regular expression pattern matchers.
11
12A pattern matcher procedure takes a list of streams, and returns a
13new list of streams advanced by every combination allowed by the
14pattern matcher function. A stream is defined as a list that contains
15a list of characters consumed by the pattern matcher, and a list of
16characters not yet consumed.
17
18Note that the number of streams returned by a pattern matcher
19typically won't match the number of streams passed in. If the pattern
20doesn't match at all, the empty list is returned.
21
22
23=== Library Procedures
24
25Every combinator procedure in this library returns a procedure that
26takes in a list of streams as an argument.
27
28==== Basic procedures
29
30<procedure>(tok TOKEN PROC) => MATCHER</procedure>
31
32Procedure {{tok}} builds a pattern matcher function that, for each
33stream given, applies a procedure to the given token {{TOKEN}} and an
34input character. If the procedure returns a true value, that value is
35prepended to the list of consumed elements, and the input character is
36removed from the list of input elements.
37
38<procedure>(seq MATCHER1 MATCHER2) => MATCHER</procedure>
39
40{{seq}} builds a matcher that matches a sequence of patterns.
41
42<procedure>(bar MATCHER1 MATCHER2) => MATCHER</procedure>
43
44{{bar}} matches either of two patterns. It's analogous to patterns
45separated by {{|}} in traditional regular expressions.
46
47<procedure>(star MATCHER) => MATCHER</procedure>
48
49{{star}} is an implementation of the Kleene closure. It is analogous
50to {{*}} in traditional regular expressions.
51
52==== Convenience procedures
53
54These procedures are built from the previous four and are provided
55for convenience.
56
57<procedure>(try PROC) => PROC</procedure>
58
59Converts a binary predicate procedure to a binary procedure that
60returns its right argument when the predicate is true, and false
61otherwise.
62
63<procedure>(char CHAR) => MATCHER</procedure>
64
65Matches a single character.
66
67<procedure>(pos MATCHER) => MATCHER</procedure>
68
69Positive closure. Analogous to {{+}}.
70
71<procedure>(opt MATCHER) => MATCHER</procedure>
72
73Optional pattern. Analogous to {{?}}.
74
75<procedure>(set CHAR-SET) => MATCHER</procedure>
76
77Matches any of a SRFI-14 set of characters.
78
79<procedure>(range CHAR CHAR) => MATCHER</procedure>
80
81Matches a range of characters. Analogous to character class {{[]}}.
82
83<procedure>(lit STRING) => MATCHER</procedure>
84
85Matches a literal string {{s}}.
86
87
88==== Lexer procedures
89
90<procedure>(longest STREAM-LIST) => STREAM</procedure>
91
92Takes the resulting streams produced by the application of a pattern
93on a stream (or streams) and selects the longest match if one
94exists. If {{STREAM-LIST}} is empty, it returns {{#F}}.
95
96
97<procedure>(lex MATCHER ERROR STRING) => CHAR-LIST</procedure>
98
99{{lex}} takes a pattern and a string, turns the string into a list of
100streams (containing one stream), applies the pattern, and returns the
101longest match. Argument {{ERROR}} is a single-argument procedure
102called when the pattern does not match anything.
103
104=== Examples
105
106  ;; A pattern to match floating point numbers.
107  ;; "-"?(([0-9]+(\\.[0-9]+)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?
108
109  (define (err s)
110    (print "lexical error on stream: " s)
111    (list))
112
113  (define numpat
114    (let* ((digit        (range #\0 #\9))
115           (digits       (pos digit))
116           (fraction     (seq `(,(char #\.) ,digits)))
117           (significand  (bar `(,(seq `(,digits ,(opt fraction))) ,fraction)))
118           (exp          (seq `(,(set "eE") ,(opt (set "+-")) ,digits)))
119           (sign         (opt (char #\-)) ))     
120     (seq `(,sign ,(seq `(,significand ,(opt exp)))))))
121
122  (print (lex numpat err "3.45e-6"))
123
124=== Requires
125
126* [[matchable]]
127
128=== Version History
129
130* 2.0 Core procedures rewritten in continuation-passing style
131* 1.5 Using (require-extension srfi-1)
132* 1.4 Ported to Chicken 4
133* 1.2 Added procedures try and tok (supersedes pred)
134* 1.0 Initial release
135
136=== License
137
138Based on the [[http://www.standarddeviance.com/projects/combinators/combinators.html|SML lexer generator by Thant Tessman]].
139
140  Copyright 2009 Ivan Raikov.
141  All rights reserved.
142 
143  Redistribution and use in source and binary forms, with or without
144  modification, are permitted provided that the following conditions are
145  met:
146 
147  Redistributions of source code must retain the above copyright
148  notice, this list of conditions and the following disclaimer.
149 
150  Redistributions in binary form must reproduce the above copyright
151  notice, this list of conditions and the following disclaimer in the
152  documentation and/or other materials provided with the distribution.
153 
154  Neither the name of the author nor the names of its contributors may
155  be used to endorse or promote products derived from this software
156  without specific prior written permission.
157 
158  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
159  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
160  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
161  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
162  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
163  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
164  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
165  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
166  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
167  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
168  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
169  OF THE POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.