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.

Procedure 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.

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.

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:

These procedures are built from the basic procedures and are provided for convenience.

[procedure](try PROC) => PROC

Converts 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) => MATCHER

Constructs a matcher for the sequence of matchers in MATCHER-LIST.

[procedure](pass) => MATCHER

This matcher returns without consuming any input.

[procedure](pos MATCHER) => MATCHER

Positive closure. Analogous to +.

[procedure](opt MATCHER) => MATCHER

Optional pattern. Analogous to ?.

[procedure](bind F P) => MATCHER

Given 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.

[procedure](rebind F G P) => MATCHER

Given 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.

[procedure](drop P) => MATCHER

Given a rule P, returns a matcher that always returns an empty list of consumed tokens when P succeeds.

lex takes a pattern and a string, turns the string into a list of streams (containing one stream), applies the pattern, and returns the longest match. Argument ERROR is a single-argument procedure called when the pattern does not match anything.

Copyright 2009-2011 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/>.