git.fiddlerwoaroof.com
Browse code

feat(experiments): reader for smart quotes

Ed Langley authored on 01/09/2020 21:18:40
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+(cl:in-package :cl-user)
2
+(defpackage :fwoar.smart-quote-readtable
3
+  (:use :cl)
4
+  (:export #:standard+smart-quotes
5
+           #:smart-quotes))
6
+(in-package :fwoar.smart-quote-readtable)
7
+(declaim (special _ __))
8
+
9
+(defmacro define-smart-quote-reader (name ending-quote)
10
+  `(defun ,name (s _)
11
+     (coerce (loop for c = (read-char s t nil t)
12
+                   until (char= c ,ending-quote)
13
+                   collect c)
14
+             'string)))
15
+
16
+(define-smart-quote-reader read-smart-double-quote #\”)
17
+(define-smart-quote-reader read-smart-single-quote #\’)
18
+
19
+(defun error-unmatched-closing-quote (_ __)
20
+  (error "unmatched closing smart quote"))
21
+
22
+(named-readtables:defreadtable smart-quotes
23
+  (:macro-char #\“ 'read-smart-double-quote)
24
+  (:macro-char #\” 'error-unmatched-closing-quote)
25
+  (:macro-char #\‘ 'read-smart-single-quote)
26
+  (:macro-char #\’ 'error-unmatched-closing-quote))
27
+
28
+(named-readtables:defreadtable standard+smart-quotes
29
+  (:merge :standard)
30
+  (:fuse smart-quotes))
31
+
32
+(defvar *doc-table* (make-hash-table))
33
+(defmethod documentation ((object symbol) (doc-type (eql 'readtable)))
34
+  (gethash object *doc-table*))
35
+(defmethod (setf documentation) (newdoc (object symbol) (doc-type (eql 'readtable)))
36
+  (setf (gethash object *doc-table*)
37
+        newdoc))