๐Common Lisp: readtable
In Common Lisp, the reader is programmable. The reader is driven by *readtable*
. When reader finds a character from the readtable, it invokes the corresponding reader macro. Reader macro is a function that receives the input stream as an argument, can read more characters and returns a structure.
Here is an example of reader macro for quote ('
):
(defun single-quote-reader (stream char)
(declare (ignore char))
(list 'quote (read stream t nil t)))
(set-macro-character #\' #'single-quote-reader)
And here is a reader macro for semicolon (comments):
(defun semicolon-reader (stream char)
(declare (ignore char))
;; First swallow the rest of the current input line.
;; End-of-file is acceptable for terminating the comment.
(do () ((char= (read-char stream nil #\Newline t) #\Newline)))
;; Return zero values.
(values))
(set-macro-character #\; #'semicolon-reader)
Most of the Common Lisp syntax (besides symbols, numbers, and whitespace) is implemented as reader macros (see a list of reader macros in CLtL2 22.1.3. Macro Characters).
Resources
22.1.1. What the Read Function Accepts for higher-level description of the reader (including how it parses whitespace, symbols, and numbers)
Backlinks
- ๐ User-programmable language parser
- ๐ Pratt parser
- ๐ ยง Programmable syntax
- ๐ Common Lisp