Source

qtility / reader.lisp

Full commit
;;; reader.lisp
;;; Copyright (c) 2011 Robert Smith

;;; Some reader macros.

(in-package #:qtility)

(defun generate-dollar-arguments (n)
  (assert (or (null n)
              (not (minusp n))))
  (case n
    ((0 nil) `(&rest ,(intern "$$")))
    ((1) `(,(intern "$") &rest ,(intern "$$")))
    (otherwise (loop
                 :for i :from 1 :to n
                 :collect (intern (format nil "$~A" i)) :into args
                 :finally (return (append args `(&rest ,(intern "$$"))))))))

(defun |#$-reader| (stream subchar n)
  (declare (ignore subchar))
  (let ((arguments (generate-dollar-arguments n)))
    `(lambda ,arguments
       (declare (ignorable ,@(remove '&rest arguments)))
       ,(read stream t nil t))))

(defun enable-lambda-quote ()
  "Enable the use of the \"lambda quote\", which is a reader macro
  designated by #[<arg count>]$<form>. "
  (set-dispatch-macro-character #\# #\$ #'|#$-reader|))