[plt-scheme] Reader too greedy

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Feb 12 10:29:21 EST 2006

At Sun, 12 Feb 2006 11:35:06 +0200, Lauri Alanko wrote:
> The reader is a bit greedy: when it encounters a special character that
> has its own readtable procedure, that character is consumed from the
> input before calling the procedure. Also, when the reader encounters an
> invalid input character, that character is consumed before raising an
> exception.
> Both of these are a bit annoying, if I need to use that consumed
> character as part of normal input. All ports have at least a
> one-character peek buffer, so IMHO it should be used in situations like
> these: first peek, and only consume if you're going to use it.

Peeking first is more expensive. Depending on the type of port, it can
be substantially more expensive. (I refer to the peek that works, not
getc followed by ungetc...)

> There is scheme_ungetc() already. Why
> isn't it accessible from Scheme code?

It doesn't work in general. That's why the reader uses ungetc only when
it is about to re-read the character. See the note at the top of
"read.c". (Yes, I really should get rid of ungetc and restructure the

> Also, tacking characters back to the input seems pretty toilsome at the
> moment. Sure, I can create my own custom port with a one-character unget
> buffer, but this seems redundant.

Since scheme_ungetc() doesn't work, it's not redundant.

Implementing `input-port->rewindable-input-port' is not easy, but it
can be done once and for all, and it would be much less work for me
than changing the reader to always peek.

Of course, since that function can be written in Scheme, it would be
nice if someone else wrote it...


Posted on the users mailing list.