[plt-scheme] Cuystomizing read

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Sun Dec 24 18:56:28 EST 2006

On Sun, 24 Dec 2006, Greg Woodhouse wrote:

> I'm trying to understand the manual section (11.2) that discusses 
> customizing the I/O handlers. I know this a kind of a pointless example, 
> but if I wanted read to consume #true and return #t (not just consume #t 
> and leave rue in the input buffer), how might this be done?

I'm not sure if this is right (I'm also just hacking this on the fly), but 
this appears to do something:

(module my-readtable mzscheme
   (provide make-my-readtable)

   ;; A small example of a readtable to allow the user to enter #true
   ;; and #false.

   (define (parse-dispatch ch port src line col pos)
       [(and (char=? #\f ch)
             (string=? "alse" (peek-string 4 0 port)))
        (read-string 4 port)
        (datum->syntax-object #f #f
                              (list src line col pos 6))]
       [(and (char=? #\t ch)
             (string=? "rue" (peek-string 3 0 port)))
        (read-string 3 port)
        (datum->syntax-object #f #t
                              (list src line col pos 5))]
       [(char=? #\f ch)
        (datum->syntax-object #f #f
                              (list src line col pos 2))]
       [(char=? #\t ch)
        (datum->syntax-object #f #t
                              (list src line col pos 2))]))

   (define (make-my-readtable)
     (make-readtable (current-readtable)
                     #\t 'dispatch-macro parse-dispatch
                     #\f 'dispatch-macro parse-dispatch)))

(require my-readtable)
(current-readtable (make-my-readtable))
(list #t #true #f #false)

I don't have much prior experience with readtables, though, so this might 
not be quite right.

Posted on the users mailing list.