Read input into string in Lisp reader macro -
i trying make reader macro convert @this "this". have:
(defun string-reader (stream char) (declare (ignore char)) (format nil "\"~a\"" (read-line stream t nil t)) ) (set-macro-character #\@ #'string-reader )
the problem requires put newline after ever @this. i've tried (read), returns variable test, has not been set. can't hard-code number of characters after @ symbol, because don't know how many there be. there way fix this?
edit: way loop on read-char , peek-char, reading until #),#\space, or #\newline?
you can try use read
, @ returns:
(defun string-reader (stream char) (declare (ignore char)) (let ((this (let ((*readtable* (copy-readtable))) (setf (readtable-case *readtable*) :preserve) (read stream t nil t)))) (etypecase (string this) (symbol (symbol-name this))))) (set-macro-character #\@ #'string-reader)
above allow @this
, @"this"
, not @333
.
this version reads string until whitespace:
(defun read-as-string-until-whitespace (stream) (with-output-to-string (out-stream) (loop next = (peek-char nil stream t nil t) until (member next '(#\space #\newline #\tab)) (write-char (read-char stream t nil t) out-stream)))) (defun string-reader (stream char) (declare (ignore char)) (read-as-string-until-whitespace stream)) (set-macro-character #\@ #'string-reader)
example:
cl-user 21 > @this "this" cl-user 22 > @42 "42" cl-user 23 > @foobar "foobar"
Comments
Post a Comment