Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
lexgen
Description
lexgen is a lexer generator comprised in its core of only five small procedures that can be combined to form pattern matchers.
A pattern matcher procedure takes an input stream, and returns a new stream advanced by the pattern.
A stream is defined as a list that contains a list of characters consumed by the pattern matcher, and a list of characters not yet consumed. E.g., the list
((#\a) (#\b #\c #\d #\e))
represents a stream that contains the consumed character a, and the unconsumed characters b c d e.
A pattern matcher has the form of a procedure that takes a success continuation, which is invoked when the pattern matches and the stream is advanced, an error continuation, which is invoked when the pattern does not match, and an input stream.
Library Procedures
Every combinator procedure in this library returns a procedure that takes in a success continuation, error continuation and input stream as arguments.
Basic procedures
[procedure] (seq MATCHER1 MATCHER2) => MATCHERseq builds a matcher that matches a sequence of patterns.
[procedure] (bar MATCHER1 MATCHER2) => MATCHERbar matches either of two patterns. It's analogous to patterns separated by | in traditional regular expressions.
[procedure] (star MATCHER) => MATCHERstar is an implementation of the Kleene closure. It is analogous to * in traditional regular expressions.
Token procedure
[procedure] (tok <Input>) => (LAMBDA TOKEN PROC) => MATCHERProcedure tok builds pattern matchers based on character comparison operations. It is intended for matching input sequences of arbitrary kinds, e.g. character lists, strings, or other kinds of sequences. To achieve abstraction over the input sequence kind, tok is parameterised on a type class named <Input>. Please see libraries typeclass and input-classes for information on the type class interface.
As an example, the code below creates an input class for character lists and defines a version of tok specialized for character lists.
(require-extension typeclass input-classes) (define char-list-<Input> (make-<Input> null? car cdr)) (define char-list-tok (tok <char-list-<Input>))
Once applied to an input class, tok builds a pattern matcher that, for each stream given, applies a procedure to the given token TOKEN and an input character. If the procedure returns a true value, that value is prepended to the list of consumed elements, and the input character is removed from the list of input elements.
<CharLex> type class and related procedures
This library provides several procedures for character matching based on the tok procedure. These procedures are enumerated as the fields of another typeclas, <CharLex>, which inherits from the <Token> typeclass:
(define-class <CharLex> (<Token> T) char set range lit)
The <Token> typeclass inherits from the <Input> typeclass and contains only the tok field:
(define-class <Token> (<Input> input) tok)
This library provides convenience functions to create instances of CharLex based on different input typeclasses:
[procedure] (Input->Token INPUT-CLASS => TOKEN-CLASS)This procedure takes an instance of the <Input> typeclass, created by the make-<Instance> constructor shown above, and returns an instance of the <Token> typeclass, which in turn contains an instance of tok specialized for the given input class.
[procedure] (Token->CharLex TOKEN-CLASS => CHARLEX-CLASS)This procedure takes an instance of the <Token> typeclass, and returns an instance of the CharLex typeclass, which contains the following procedures:
[procedure] (char CHAR) => MATCHERMatches a single character.
[procedure] (set CHAR-SET) => MATCHERMatches any of a SRFI-14 set of characters.
[procedure] (range CHAR CHAR) => MATCHERMatches a range of characters. Analogous to character class [].
[procedure] (lit STRING) => MATCHERMatches a literal string s.
Convenience procedures
These procedures are built from the basic procedures and are provided for convenience.
[procedure] (try PROC) => PROCConverts a binary predicate procedure to a binary procedure that returns its right argument when the predicate is true, and false otherwise.
[procedure] (lst MATCHER-LIST) => MATCHERConstructs a matcher for the sequence of matchers in MATCHER-LIST.
[procedure] (pass) => MATCHERThis matcher returns without consuming any input.
[procedure] (pos MATCHER) => MATCHERPositive closure. Analogous to +.
[procedure] (opt MATCHER) => MATCHEROptional pattern. Analogous to ?.
[procedure] (bind F P) => MATCHERGiven a rule P and function F, returns a matcher that first applies P to the input stream, then applies F to the returned list of consumed tokens, and returns the result and the remainder of the input stream.
Note: this combinator will signal failure if the input stream is empty.
[procedure] (bind* F P) => MATCHERThe same as bind, but will signal success if the input stream is empty.
[procedure] (rebind F G P) => MATCHERGiven a rule P and procedures F and G, returns a matcher that first applies F to the input stream, then applies P to the resulting stream, then applies G to the resulting list of consumed elements and returns the result along with the remainder of the input stream.
Note: this combinator will signal failure if the input stream is empty.
[procedure] (rebind* F G P) => MATCHERThe same as rebind, but will signal success if the input stream is empty.
[procedure] (drop P) => MATCHERGiven a rule P, returns a matcher that always returns an empty list of consumed tokens when P succeeds.
Lexer procedure
[procedure] (lex MATCHER ERROR STRING) => CHAR-LISTlex takes a pattern and a string, turns the string into a list of streams (containing one stream), applies the pattern, and returns the first possible match. Argument ERROR is a single-argument procedure called when the pattern does not match anything.
Examples
Creating a lexer specialized for lists of characters
(require-extension typeclass input-classes lexgen srfi-1 srfi-14 test) ;; The following definitions create matchers {{char}} {{range}} ;; {{set}} {{lit}} specialized for lists of characters. (define char-list-<Input> (make-<Input> null? car cdr)) ;; Creates an instance of the <Token> typeclass that is named char-list-<Token> (define char-list-<Token> (Input->Token char-list-<Input>)) ;; Creates an instance of the <CharLex> typeclass that is named char-list-<CharLex> (define char-list-<CharLex> (Token->CharLex char-list-<Token>)) ;; The following declaration imports the fields of the typeclass ;; instances defined above, and prefixes each identifier with ;; char-list/. For example, if the <Token> typeclass defines a field ;; named range, the import-instance declaration below will create an ;; identifier named char-list/range. (import-instance (<Token> char-list-<Token> char-list/) (<CharLex> char-list-<CharLex> char-list/))
A pattern to match floating point numbers
;; A pattern to match floating point numbers. ;; "-"?(([0-9]+(\\.[0-9]+)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)? (define numpat (let* ((digit (char-list/range #\0 #\9)) (digits (pos digit)) (fraction (seq (char-list/char #\.) digits)) (significand (bar (seq digits (opt fraction)) fraction)) (exp (seq (char-list/set "eE") (seq (opt (char-list/set "+-")) digits))) (sign (opt (char-list/char #\-)))) (seq sign (seq significand (opt exp))))) (define (err s) (print "lexical error on stream: " s) (list)) (lex numpat err "-123.45e-6")
Tokens with position information
(define-record-type postok (make-postok pos token) postok? (pos postok-pos ) (token postok-token ) ) (define pos? pair?) (define pos-row car) (define pos-col cdr) (define make-pos cons) (define-record-printer (postok x out) (fprintf out "#<token ~A: ~A>" (postok-pos x) (postok-token x))) (define (getpos p) (let ((f (lambda (in) (and (pair? in) (postok-pos (car in))))) (g (lambda (i s) (list (make-postok i (car s)))))) (rebind f g p))) (define pos-<Input> (let ((pos-tail (lambda (strm) (cond ((or (null? strm) (null? (cdr strm))) '()) (else (let* ((curtok (car strm)) (pos0 (postok-pos curtok)) (pos1 (let ((row0 (pos-row pos0)) (col0 (pos-col pos0))) (case (cadr strm) ((#\newline) (make-pos (+ 1 row0) 1)) ((#\return) (make-pos row0 1)) (else (make-pos row0 (+ 1 col0)))))) (res (cons (make-postok pos1 (cadr strm)) (cddr strm)))) res))))) (pos-null? null?) (pos-head (compose postok-token car))) (make-<Input> pos-null? pos-head pos-tail))) (define pos-<Token> (Input->Token pos-<Input>)) (define pos-<CharLex> (Token->CharLex pos-<Token>)) (import-instance (<Token> pos-<Token> pos/) (<CharLex> pos-<CharLex> pos/)) (define (make-pos-stream strm) (let ((begpos (make-pos 1 1))) `(() ,(cons (make-postok begpos (car strm)) (cdr strm))))) (define pos-numpat-stream (make-pos-stream (string->list "-123.45e-6"))) (define pbnumpat (let* ((digit (pos/range #\0 #\9)) (digits (star digit)) (fraction (seq (pos/char #\.) digits)) (significand (bar (seq digits (opt fraction)) fraction)) (exp (seq (pos/set "eE") (seq (opt (pos/set "+-")) digits))) (sign (opt (pos/char #\-)) ) (pat (seq (getpos (bind make-sign sign)) (seq (getpos (bind make-significand significand)) (getpos (bind make-exp (opt exp))))))) pat)) (define (pos-num-parser s) (car (lex pbnumpat err s)))
Requires
Version History
- 7.1 Bug fix in bind* [thanks to Peter Bex]
- 7.0 Added bind* and rebind* variants of bind and rebind [thanks to Peter Bex]
- 6.1-6.2 Corrected behavior of the tok combinator so that the failure continuation is invoked upon end-of-input [thanks to Chris Salch]
- 6.0 Using utf8 for char operations
- 5.2 Ensure test script returns proper exit status
- 5.0-5.1 Added error continuation to the matcher interface and eliminated multiple stream matching
- 4.0 Implemented typeclass interface for abstracting over input sequences
- 3.8 Added procedure star* (greedy Kleene closure matching)
- 3.6 Added procedure redo [thanks to Christian Kellermann]
- 3.5 Bug fixes in bind [reported by Peter Bex]
- 3.3 Bug fixes in stream comparison
- 3.2 Improved input stream comparison procedures
- 3.1 Added rebind combinator and stream-unfold procedure
- 3.0 Added an extension mechanism for input streams of different types (to be elaborated and documented in subsequent versions).
- 2.6 Added bind and drop combinators
- 2.5 The seq combinator checks whether the first parser in the sequence has failed
- 2.4 Added (require-library srfi-1); using lset<= instead of equal? in star
- 2.3 Bug fix in procedure range; added procedure cps-table
- 2.2 Bug fix in procedure star
- 2.1 Added procedure lst
- 2.0 Core procedures rewritten in continuation-passing style
- 1.5 Using (require-extension srfi-1)
- 1.4 Ported to Chicken 4
- 1.2 Added procedures try and tok (supersedes pred)
- 1.0 Initial release
License
Based on the SML lexer generator by Thant Tessman.
Copyright 2009-2014 Ivan Raikov and the Okinawa Institute of Science and Technology. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. A full copy of the GPL license can be found at <http://www.gnu.org/licenses/>.