chickadee » lexgen

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

seq MATCHER1 MATCHER2procedure

seq builds a matcher that matches a sequence of patterns.

bar MATCHER1 MATCHER2procedure

bar matches either of two patterns. It's analogous to patterns separated by | in traditional regular expressions.

star MATCHERprocedure

star is an implementation of the Kleene closure. It is analogous to * in traditional regular expressions.

Token procedure

tok TOKEN PROCprocedure

Procedure tok builds pattern matchers based on character comparison operations. It is intended for matching input sequences that are SRFI-127 lazy streams.

For each stream given, tok 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 stream of input elements.

char CHARprocedure

Matches a single character.

set CHAR-SETprocedure

Matches any of a SRFI-14 set of characters.

range CHAR CHARprocedure

Matches a range of characters. Analogous to character class [].

lit STRINGprocedure

Matches a literal string s.

Convenience procedures

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

try PROCprocedure

Converts a binary predicate procedure to a binary procedure that returns its right argument when the predicate is true, and false otherwise.

lst MATCHER-LISTprocedure

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

passprocedure

This matcher returns without consuming any input.

pos MATCHERprocedure

Positive closure. Analogous to +.

opt MATCHERprocedure

Optional pattern. Analogous to ?.

bind F Pprocedure

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.

Note: this combinator will signal failure if the input stream is empty.

bind* F Pprocedure

The same as bind, but will signal success if the input stream is empty.

rebind F G Pprocedure

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.

Note: this combinator will signal failure if the input stream is empty.

rebind* F G Pprocedure

The same as rebind, but will signal success if the input stream is empty.

drop Pprocedure

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

Lexer procedure

lex MATCHER ERROR STRINGprocedure

lex 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

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        (range #\0 #\9))
	 (digits       (pos digit))
	 (fraction     (seq (char #\.) digits))
	 (significand  (bar (seq digits (opt fraction)) fraction))
	 (exp          (seq (set "eE") (seq (opt (set "+-")) digits)))
	 (sign         (opt (char #\-))))
    (seq sign (seq significand (opt exp)))))
 
 (define (err s)
  (print "lexical error on stream: " s)
  (list))

 (lex numpat err "-123.45e-6")

Repository

https://github.com/iraikov/chicken-lexgen

Version History

License

Based on the SML lexer generator by Thant Tessman.

 Copyright 2009-2021 Ivan Raikov.


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

Contents »