common-lispreader-macro

read's recursive-p argument when used inside a reader macro


I have written a Common Lisp implementation of Scheme's s-expression comments (SRFI 62):

(eval-when (:compile-toplevel
            :load-toplevel
            :execute)

  (set-dispatch-macro-character #\# #\;
    (lambda (stream char n)
      (declare (ignore char))
      (when n
        (error "Infix parameter not allowed in s-expression comment."))
      (read stream)  ; Discard the s-expression.
      (values))))

Based on my reading of The RECURSIVE-P argument, it appears that my implementation is incorrect. I have to use (read stream t nil t) instead (i.e. set the recursive-p argument to t). Is my understanding correct?

I have been using the apparently incorrect implementation above, and it seems to function correctly. What would happen if I continue to use (read stream) instead of (read stream t nil t)?


Solution

  • For an example of incorrect behavior, let's consider the first of the three reasons given in the RECURSIVE-P argument documentation. First, using your original definition:

    * (+ 1 #1=2 #;(+ #1# 3))
    Reader error: Missing #1# label.
    

    Changing (read stream) into (read string t nil t) gives the correct result:

    * (+ 1 #1=2 #;(+ #1# 3))
    3