[plt-scheme] Re: How to use a custom procedure as read?

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Sep 30 09:54:18 EDT 2008

On Sep 30, Ali wrote:
> On Sep 30, 1:12 pm, "Noel Welsh" <noelwe... at gmail.com> wrote:
> > On Tue, Sep 30, 2008 at 3:45 AM, Ali <emailalicl... at gmail.com> wrote:
> > >> > (swap-the-reader-in)
> > >> > ... code using custom reader ...
> > >> > (swap-the-reader-out)
> > >> > ... normal reader again ...
> > ...
> > > Thanks. Do I have to write the code in a module? I'd much rather be
> > > able to swap in and out of the custom reader mode as with above.
> >
> > You have to write your code in a separate module.  If you did not,
> > Scheme would have to evaluate your code as the module was loaded to
> > work out that you were swapping in a new reader.  This would break a
> > lot of things, the module system being the prime one.
> Okay, fair enough. So to take this example of a module named canvas,
> how could I modify it to specify that to use my custom-reader?
> (module canvas mzscheme
>   ... code implementing canvas using custom-reader as the reader ...
>   (provide new-canvas draw-dot! display-canvas))

1. You write a reader function, it will often use `read', so you'll
   want to call it something else, say `read*'; you also need to do
   the same for `read-syntax'.

2. You provide them as `read' and `read-syntax':
     (provide (rename-out [read* read] [read-syntax* read-syntax]))

3. You can now use
     #reader "path-to-your-module"
   and the following expression will be read using your reader.  In
   your case, the expression will be the whole module, for example:

     #reader "my-reader.ss"
     module foo scheme/base
       define blah 123

There is also a way to change the current readtable, but that's a
really bad idea, and is almost never what you want it to be.

> > Note the documentation you are reading is for v4, while you're
> > using v3.  I suggest you upgrade your installation.
> Thanks. I upgraded Mzscheme. Tried a few things, but I've still got
> no idea what code I have to run, do I need a file in a certain place
> etc.  Can't really remember the last time I was this confused, so at
> least its an experience for me!)

[FWIW, my guess is that what I wrote above is not going to help much
unless you know how to use the module system.]

> > Finally, writing a custom reader is a fairly advanced task.  What is
> > the problem you're trying to solve?  Perhaps list members can suggest
> > a simpler way of solving it.
> I'd like to try writing code using SRFI 49
> (http://srfi.schemers.org/srfi-49/srfi-49.html). With a couple of
> minor changes I can run the example implementation in guile, using
> (setf! read sugar-read). Works fine. With a few more changes,
> Mzscheme can load the reader. All I need now is the Mzscheme
> equivalent for (setf! read sugar-read), which apparently doesn't do
> anything in mzscheme.

Yes, mzscheme tends to be more well behaved in this regard.

> Sorry for not saying this explicitly before, but I was concerned the
> discussion would be side-tracked to reasons why I shouldn't be using
> SRFI 49 in the first place.

[A valid concern, since it is a bad idea.  Even worse than you think.]

> Anyway, thanks for telling me about the docs version, and making it
> clear that I need to use a module. Really hope someone can give just
> the smallest bit of example usage, just an equivalent for (set! read
> sugar-read), where sugar-read is a user defined procedure. Or point
> me to an existing example usage. Sorry if my irritation shows, I am
> actually very grateful for your time, and I know there are more
> enjoyable things one can do with it!

You can look at "collects/scribble/reader.ss" in the distribution --
that implements the scribble syntax, and can be used as:

  #reader scribble/reader <expression-in-scribble-syntax>

Note that getting a `read' function is usually just half of the work.
MzScheme uses `read-syntax' to read code, which returns syntactic
objects that contain source information.  Getting `read-syntax'
working is usually more difficult.  You can try to make it the same as
`read' just to experiment with things, but syntax errors are not going
to have source information.
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.