Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Lisp objects from a string

I am familiar with the basic template for collecting the Lisp objects from a file, as in:

(with-open-file (stream "filename.lisp")
    (loop for object = (read stream nil 'eof)
          until (eq object 'eof)
          collect object))

But I'm not sure how to translate this into collecting the objects from a string, for example using read-from-string. Do you then have to keep track of the index where you left off in the string? Also, how do you avoid a name conflict with eof or any other legitimate Lisp object like nil or t in the input?

like image 609
davypough Avatar asked Dec 04 '25 15:12

davypough


1 Answers

You can use with-input-from-string to read from a string.

To prevent a conflict with the eof symbol, you can use an uninterned symbol or some other object that's created dynamically.

(with-input-from-string (stream "this is a (list of 3 things)")
  (loop with eof-marker = '#:eof
        for object = (read stream nil eof-marker)
        until (eq object eof-marker)
        collect object))
like image 92
Barmar Avatar answered Dec 07 '25 03:12

Barmar