chickadee » lexgen

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

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 <Input>procedure

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.

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

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:

Input->Token INPUT-CLASS => TOKEN-CLASSprocedure

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.

Token->CharLex TOKEN-CLASS => CHARLEX-CLASSprocedure

This procedure takes an instance of the <Token> typeclass, and returns an instance of the CharLex typeclass, which contains the following procedures:

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

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

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

Contents »