您可以尝试使用read
,然后查看它返回的内容:
(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 this
(string this)
(symbol (symbol-name this)))))
(set-macro-character #\@ #'string-reader)
以上将允许@This
and @"This"
,但不允许@333
。
这个版本只读取一个字符串,直到空格:
(defun read-as-string-until-whitespace (stream)
(with-output-to-string (out-stream)
(loop for next = (peek-char nil stream t nil t)
until (member next '(#\space #\newline #\tab))
do (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)
例子:
CL-USER 21 > @this
"this"
CL-USER 22 > @42
"42"
CL-USER 23 > @FooBar
"FooBar"