Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRITE unnecessarily escapes symbols containing non-terminating macro characters #476

Open
jamesrmeehan opened this issue Jan 28, 2024 · 1 comment

Comments

@jamesrmeehan
Copy link

The Common Lisp Hyperspec says, "The Lisp reader cannot accept as input everything that the Lisp printer produces, and the Lisp reader has features that are not used by the Lisp printer."

The question is whether the Lisp printer should escape characters that the Lisp reader doesn't need to be escaped. In particular, if a symbol contains a non-terminating macro character in the middle or at the end of the symbol's name, PRIN1 escapes either that character or sometimes the whole symbol, even though it doesn't need to. E.g., in an environment where *PRINT-ESCAPE* is true, *PRINT-READABLY* is true, *PRINT-CASE* is :UPCASE, and (READTABLE-CASE *READTABLE*) is :UPCASE, we have this:

? 'A#B
|A#B|
? 'MID#DLE
MID\#DLE
? 'END#
END\#

It's not that the result is wrong, but it can be ... annoying. (I discovered this when I defined ? as a non-terminating macro character, so that when the reader sees ?LEFT, it calls the reader macro function for ? but when reading MID?DLE or RIGHT?, it does not. I like to define predicates with names like VALID?, but PRIN1 prints that as VALID\?, which is ... annoying.) After all, it's legal for PRIN1 to print the symbol CAR as |CAR| or \C\A\R, but no one would expect (or tolerate) that.

I don't know how (PRIN1 symbol), i. e., (WRITE symbol :ESCAPE T), is implemented, but in addition to checking the value of *PRINT-CASE* and *PRINT-READABLY*, it must be looking at (GET-MACRO-CHARACTER character *READTABLE*) for every character in the symbol's name to see whether its syntax type is anything other than constituent. If the syntax type is non-terminating macro, and the character is not the first one in the symbol's name, then there is no need to escape it. But it does.

@xrme
Copy link
Member

xrme commented Jan 31, 2024

I think your intuition about checking for non-constituent characters is correct.

https://github.com/Clozure/ccl/blob/master/level-1/l1-io.lisp#L1062

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants