Unexpected (incorrect?) reader invocations with slime-compile-file
Hi there!
I've been trying to debug something strange that seems to be happening when I use slime-compile-file along with reader macros. The issue only seems to come up when SBCL signals a warning. I've managed to reduce my failing case to this:
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun printing-reader (stream char)
(declare (ignore char))
(print (read stream t nil t)))
(set-macro-character #\! #'printing-reader))
(defun test (a b)
!a)
If I save this into a .lisp file and then run slime-compile-file in Emacs I see the following output:
A
NIL
A
; in: DEFUN TEST
; (DEFUN TEST (A B) A)
; --> PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA
; ==>
; #'(SB-INT:NAMED-LAMBDA TEST
; (A B)
; (DECLARE (SB-C::TOP-LEVEL-FORM))
; (BLOCK TEST A))
;
; caught STYLE-WARNING:
; The variable B is defined but never used.
;
; compilation unit finished
; caught 1 STYLE-WARNING condition
The style warning is expected, but I only expected a single A to be printed. Instead, I see A, NIL, and A. This means that my reader macro has been run three times, and most troubling is that calls to read within one of them return NIL!
In this reader macro that's not a problem, but in my real failing case I rely on the result of calls to read, with my own readtable, to decide when to stop reading for my reader macro, which then leads to the entire file failing to compile (according to SLIME).
I've isolated this to SLIME, because when I run compile-file in the SBCL REPL it only prints A. Similarly, running slime-load-file prints A only once. Running slime-compile-defun on the definition of test prints A twice, but does not have the problematic NIL.
Can anyone help me figure out where I'm going wrong, and how I can resolve this?
CL-USER> (lisp-implementation-type)
"SBCL"
CL-USER> (lisp-implementation-version)
"2.1.3"
;;; slime.el --- Superior Lisp Interaction Mode for Emacs ...
;; Version: 2.26.1
Posted Apr 5, 2021, 11:43 AM UTC
It needs to read the code to report the warning (once with *read-suppress*=T). Why does it matter to you?
Ah, okay! I didn't know about
*read-suppress*, but that explains things. Thanks!I'm writing a macro that lets me write things like
which invokes an external program (in this case
/bin/echowith"Hello"and"world", and redirecting the output to*standard-output*).I need to use a custom readtable for each of the terms with
readtable-caseset to:preserveand$as an escape reader, so I also add a reader for)which returns a sentinel "end of command" object. I then repeatedlyreaduntil I get my sentinel object, at which point I stop processing. Ifreadalways returnsnilthen that doesn't work, because the sentinel object never gets returned and anything after a command reader fails.There are two ways that I can solve this, then:
I can wrap my
readcalls with a(let ((*read-suppress* nil)) ...), but that feels wrong (particularly because I don't entirely understand the consequences of that).I can use
peek-charto check for), rather than going through my readtable. This technically has a corner case if the base readtable has a reader macro defined for a whitespace character, but I guess that's not a realistic concern...Oh hey! A lisp/shell hybrid! That’s my jam! Check out https://shcl.io if you want to see my version. It’s a bit stale (I’m on a loooong break), but I haven’t abandoned it yet!
Anyway, let me see if I can give you some pointers…
I gave up on using the reader to read shell commands. It was possible for simple shell-like things, but there was too much impedance mismatch for my liking.
I think the conventional way to recognize the end is to make the closing character into a reader macro that just signals an error. The key thing is you don’t want it to be considered part of identifiers, and setting it as a macro achieves that effect. After reading each form you peek the next char to see if your closing bracket is there.
I’m not sure what your concern with space is. This works regardless of what reader macros you have set in the base table (as long as you’re okay with overwriting their macro for the close paren).
I have actually seen SHCL, and my aspirations are much more restrained than a full POSIX shell. 🙂 It's not even a library, just a small helper for a simple application I'm writing for fun. I'm definitely thinking of it as syntax sugar in Lisp rather than a shell. For instance, I don't allow substitutions within a variable (because it's really weird in Lisp code), only a shorthand for
formatthat looks like this:$(echo $("Hello ~a" x)).I hadn't considered having my reader signal an error at the end of the command sequence, rather than returning a value. That would let me use
readto find the end, without needing to override the*read-suppress*binding. What I have currently is that I define a reader macro that never gets called (because I usepeek-charand avoid calling read if it's)), which provides the identifier-termination behaviour I want.My concern around whitespace is that my use of
peek-charalso consumes any leading whitespace, which means that if the reader had a macro defined for#\tabthen I would consume that character without the reader getting a chance to do so. I don't think many people define reader macros for whitespace characters, so it's not a big deal, but it just feels a bit wrong.When I say that the closing char should signal an error, I don’t mean that you should catch the error… but I suppose you can. When you register a macro character you indicate whether it needs to appear at the start of an identifier or not to be effective. Sorry, it’s been a while since I’ve read the docs and I’m on mobile. I might not use the right words here. When the reader is reading a symbol, it checks to see if the next character is associated with a reader macro AND if it is the sort that can interrupt a symbol. Registering close paren as a macro is mostly just to make it so that close parents don’t get picked up as part of symbol reading. It’s also convenient because if you encounter a close paren in a context where you aren’t looking for it, you have a mismatched parenthesis and deserve an error anyway!
The scheme you describe with PEEK-CHAR is exactly how I’ve seen other people solve the problem. Since you’re using close paren, you don’t need to register a close-paren reader macro normally… but if you imagine doing this with square brackets or curly braces, then you probably wouldn’t be so lucky to have a pre-defined reader macro for the closing side!
For Whitespace, you’re absolutely right, but I think (again, haven’t read the details in years) the lisp reader is defined to skip whitespace in a bunch of different contexts. So, reader macros on whitespace are troubled no matter what. I might be wrong about that… but just about every interesting reader macro I’ve read uses PEEK-CHAR’s whitespace-skipping feature, and so you’ll be in good company of having weird behaviors when someone tries to do it.