12

Modifying the reader would allow introducing new read-syntax (such as #(hash table) and '(quoted)). Many Lisps have this​​​​​ capability, but no such facility seems to exist for elisp.

Drew
  • 77,472
  • 10
  • 114
  • 243
Sean Allred
  • 6,921
  • 18
  • 86

2 Answers2

12

Turns out that the manual implies that you can't actually do reader macros.

According to Appendix C Porting Common Lisp:

Reader macros. Common Lisp includes a second type of macro that works at the level of individual characters. For example, Common Lisp implements the quote notation by a reader macro called ', whereas Emacs Lisp’s parser just treats quote as a special case. Some Lisp packages use reader macros to create special syntaxes for themselves, which the Emacs parser is incapable of reading.

Dan
  • 32,980
  • 7
  • 102
  • 169
  • I think this is about Common Lisp software which use special reader macros, rather than Elisp. The page is about porting CL code to Elisp. And yes, officially the reader is not customizable, but see my answer below... – mishoo Mar 07 '16 at 16:23
  • The other problem here is even if you've somehow introduced new reader syntax, the rest of Emacs is not aware of it, so you'll need to fix syntax.c, too... – wasamasa Mar 08 '16 at 07:23
12

Apparently it can be done, but prepare for a lot of work. (or don't, because I'm gonna tackle it some weekend ;-).

(defvar *orig-read* (symbol-function 'read))

(defun read (&optional in)
  (message "reading from %s" load-file-name)
  (funcall *orig-read* in))

(setq load-read-function (symbol-function 'read))

Now "all" that's left to do is to implement a complete Lisp reader which supports everything that Elisp does and whatever you want more. I think this could be used to provide CL-like defpackage and package internal symbols, as one example. Also, a special syntax for regexps is something I badly want to do (or more exactly, some string syntax that doesn't interpret backslashes).

Edit: here's a proof-of-concept implementation: https://github.com/mishoo/elisp-reader.el

mishoo
  • 249
  • 2
  • 5
  • Probably best not to post this as an answer until after you've actually implemented a complete Lisp reader as you suggested (which, I'm guessing, is not a trivial enterprise). – Dan Mar 07 '16 at 19:51
  • @Dan It is doable, it's just that doing it in C is so much more painful as it sucks for string processing. – wasamasa Mar 08 '16 at 07:21
  • 3
    @Dan got it working, I edited the answer to add the link to the implementation. – mishoo Mar 10 '16 at 17:05