Suppose you have a parser that reads from a stream, but the stream is missing something. E.g. you have a function that reads a string from a stream, but while the function expects the next character to be a opening quote, the opening quote has already been read from the stream.
The obvious solution is unread-char
. This takes a character — the last character read from the stream — and puts it back into the stream. And this is fine for the situation where your function is being invoked as (say) a reader macro. But what if you need to call the parser by itself?
What you need in this situation is not unread-char
but a concatenated stream. The stream returned by make-concatenated-stream
returns all the elements from all of its arguments as as a single stream. You can add any prefix or suffix you need:
(defun read-delimited-string (stream)
(let ((stream
(make-concatenated-stream
(make-string-input-stream "\"")
stream)))
(parse-string stream)))