cruft.lisp 15.7 KB
Newer Older
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
1 2 3 4 5 6 7
;;;;
;;;; Copyright (c) 2008 - 2009 Tobias C. Rittweiler <tcr@freebits.de>
;;;;
;;;; All rights reserved.
;;;;
;;;; See LICENSE for details.
;;;;
8 9 10

(in-package :editor-hints.named-readtables)

11 12 13 14 15 16
(defmacro define-cruft (name lambda-list &body (docstring . alternatives))
  (assert (typep docstring 'string) (docstring) "Docstring missing!")
  (assert (not (null alternatives)))
  `(progn
     (declaim (inline ,name))
     (defun ,name ,lambda-list ,docstring ,(first alternatives))))
17

18 19 20
(eval-when (:compile-toplevel :execute)
  #+sbcl (when (find-symbol "ASSERT-NOT-STANDARD-READTABLE"
                            (find-package "SB-IMPL"))
21
           (pushnew :sbcl+safe-standard-readtable *features*)))
22

23

24
;;;;; Implementation-dependent cruft
25

26
;;;; Mapping between a readtable object and its readtable-name.
27

28
(defvar *readtable-names* (make-hash-table :test 'eq))
29

30 31 32 33 34 35 36 37
(define-cruft %associate-readtable-with-name (name readtable)
  "Associate READTABLE with NAME for READTABLE-NAME to work."
  #+ :common-lisp (setf (gethash readtable *readtable-names*) name))

(define-cruft %unassociate-readtable-from-name (name readtable)
  "Remove the association between READTABLE and NAME."
  #+ :common-lisp (progn (assert (eq name (gethash readtable *readtable-names*)))
                         (remhash readtable *readtable-names*)))
38

39 40 41
(define-cruft %readtable-name (readtable)
  "Return the name associated with READTABLE."
  #+ :common-lisp (values (gethash readtable *readtable-names*)))
42

43 44 45 46 47
(define-cruft %list-all-readtable-names ()
  "Return a list of all available readtable names."
  #+ :common-lisp (list* :standard :current
                         (loop for name being each hash-value of *readtable-names*
                               collect name)))
48

49

50 51 52 53 54 55 56
;;;; Mapping between a readtable-name and the actual readtable object.

;;; On Allegro we reuse their named-readtable support so we work
;;; nicely on their infrastructure.

#-allegro
(defvar *named-readtables* (make-hash-table :test 'eq))
57 58

#+allegro
59 60
(defun readtable-name-for-allegro (symbol)
  (multiple-value-bind (kwd status)
61 62 63 64 65 66 67 68
        (if (keywordp symbol)
            (values symbol nil)
            ;; Kludge: ACL uses keywords to name readtables, we allow
            ;; arbitrary symbols.
            (intern (format nil "~A.~A"
                            (package-name (symbol-package symbol))
                            (symbol-name symbol))
                    :keyword))
69 70 71 72 73 74
    (prog1 kwd
      (assert (or (not status) (get kwd 'named-readtable-designator)))
      (setf (get kwd 'named-readtable-designator) t))))

(define-cruft %associate-name-with-readtable (name readtable)
  "Associate NAME with READTABLE for FIND-READTABLE to work."
David Lichteblau's avatar
David Lichteblau committed
75
  #+ :allegro     (setf (excl:named-readtable (readtable-name-for-allegro name)) readtable)
76 77 78 79 80
  #+ :common-lisp (setf (gethash name *named-readtables*) readtable))

(define-cruft %unassociate-name-from-readtable (name readtable)
  "Remove the association between NAME and READTABLE"
  #+ :allegro     (let ((n (readtable-name-for-allegro name)))
David Lichteblau's avatar
David Lichteblau committed
81 82
                    (assert (eq readtable (excl:named-readtable n)))
                    (setf (excl:named-readtable n) nil))
83 84 85 86 87
  #+ :common-lisp (progn (assert (eq readtable (gethash name *named-readtables*)))
                         (remhash name *named-readtables*)))

(define-cruft %find-readtable (name)
  "Return the readtable named NAME."
88
  #+ :allegro     (excl:named-readtable (readtable-name-for-allegro name) nil)
89
  #+ :common-lisp (values (gethash name *named-readtables* nil)))
90

91 92 93

;;;; Reader-macro related predicates

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
;;; CLISP creates new function objects for standard reader macros on
;;; each readtable copy.
(define-cruft function= (fn1 fn2)
  "Are reader-macro function-designators FN1 and FN2 the same?"
  #+ :clisp
  (let* ((fn1 (ensure-function fn1))
         (fn2 (ensure-function fn2))
         (n1 (system::function-name fn1))
         (n2 (system::function-name fn2)))
    (if (and (eq n1 :lambda) (eq n2 :lambda))
        (eq fn1 fn2)
        (equal n1 n2)))
  #+ :common-lisp
  (eq (ensure-function fn1) (ensure-function fn2)))

;;; CCL has a bug that prevents the portable form below from working
;;; (Ticket 601). CLISP will incorrectly fold the call to G-D-M-C away
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
111
;;; if not declared inline.
112 113 114 115 116 117 118
(define-cruft dispatch-macro-char-p (char rt)
  "Is CHAR a dispatch macro character in RT?"
  #+ :ccl
  (let ((def (cdr (nth-value 1 (ccl::%get-readtable-char char rt)))))
    (or (consp (cdr def))
        (eq (car def) #'ccl::read-dispatch)))
  #+ :common-lisp
119 120 121 122
  (handler-case (locally
                    #+clisp (declare (notinline get-dispatch-macro-character))
                  (get-dispatch-macro-character char #\x rt)
                  t)
123 124 125
    (error () nil)))

;; (defun macro-char-p (char rt)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
126 127
;;   (let ((reader-fn (%get-macro-character char rt)))
;;     (and reader-fn t)))
128 129 130 131 132

;; (defun standard-macro-char-p (char rt)
;;   (multiple-value-bind (rt-fn rt-flag) (get-macro-character char rt)
;;     (multiple-value-bind (std-fn std-flag) (get-macro-character char *standard-readtable*)
;;       (and (eq rt-fn std-fn)
133
;;         (eq rt-flag std-flag)))))
134 135 136 137

;; (defun standard-dispatch-macro-char-p (disp-char sub-char rt)
;;   (flet ((non-terminating-p (ch rt) (nth-value 1 (get-macro-character ch rt))))
;;     (and (eq (non-terminating-p disp-char rt)
138 139 140
;;           (non-terminating-p disp-char *standard-readtable*))
;;       (eq (get-dispatch-macro-character disp-char sub-char rt)
;;           (get-dispatch-macro-character disp-char sub-char *standard-readtable*)))))
141

142

143 144
;;;; Readtables Iterators

145 146 147 148 149 150
(defmacro with-readtable-iterator ((name readtable) &body body)
  (let ((it (gensym)))
    `(let ((,it (%make-readtable-iterator ,readtable)))
       (macrolet ((,name () `(funcall ,',it)))
         ,@body))))

151 152 153 154 155 156 157
#+sbcl
(defun %make-readtable-iterator (readtable)
  (let ((char-macro-array (sb-impl::character-macro-array readtable))
        (char-macro-ht    (sb-impl::character-macro-hash-table readtable))
        (dispatch-tables  (sb-impl::dispatch-tables readtable))
        (char-code 0))
    (with-hash-table-iterator (ht-iterator char-macro-ht)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
158
      (labels ((grovel-base-chars ()
159 160
                 (declare (optimize sb-c::merge-tail-calls))
                 (if (>= char-code sb-int:base-char-code-limit)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
161 162 163 164 165 166 167
                     (grovel-unicode-chars)
                     (let ((reader-fn (svref char-macro-array char-code))
                           (char      (code-char (shiftf char-code (1+ char-code)))))
                       (if reader-fn
                           (yield char reader-fn)
                           (grovel-base-chars)))))
               (grovel-unicode-chars ()
168 169
                 (multiple-value-bind (more? char reader-fn) (ht-iterator)
                   (if (not more?)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                       (values nil nil nil nil nil)
                       (yield char reader-fn))))
               (yield (char reader-fn)
                 (let ((disp-ht))
                   (cond
                     ((setq disp-ht (cdr (assoc char dispatch-tables)))
                      (let* ((disp-fn (get-macro-character char readtable))
                             (sub-char-alist))
                        (maphash (lambda (k v)
                                   (push (cons k v) sub-char-alist))
                                 disp-ht)
                        (values t char disp-fn t sub-char-alist)))
                     (t
                      (values t char reader-fn nil nil))))))
        #'grovel-base-chars))))
185 186 187 188 189 190 191 192 193 194 195 196

#+clozure
(defun %make-readtable-iterator (readtable)
  (let ((char-macro-alist (ccl::rdtab.alist readtable)))
    (lambda ()
      (if char-macro-alist
          (destructuring-bind (char . defn) (pop char-macro-alist)
            (if (consp defn)
                (values t char (car defn) t (cdr defn))
                (values t char defn nil nil)))
          (values nil nil nil nil nil)))))

197
;;; Written on ACL 8.0.
198 199 200 201
#+allegro
(defun %make-readtable-iterator (readtable)
  (declare (optimize speed))            ; for TCO
  (check-type readtable readtable)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
202
  (let* ((macro-table     (first (excl::readtable-macro-table readtable)))
203
         (dispatch-tables (excl::readtable-dispatch-tables readtable))
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
204
         (table-length    (length macro-table))
205 206 207 208
         (idx 0))
    (labels ((grovel-macro-chars ()
               (if (>= idx table-length)
                   (grovel-dispatch-chars)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
209
                   (let ((read-fn (svref macro-table idx))
210
                         (oidx idx))
211
                     (incf idx)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
212 213 214 215 216
                     (if (or (eq read-fn #'excl::read-token)
                             (eq read-fn #'excl::read-dispatch-char)
                             (eq read-fn #'excl::undefined-macro-char))
                         (grovel-macro-chars)
                         (values t (code-char oidx) read-fn nil nil)))))
217 218 219 220 221 222
             (grovel-dispatch-chars ()
               (if (null dispatch-tables)
                   (values nil nil nil nil nil)
                   (destructuring-bind (disp-char sub-char-table)
                       (first dispatch-tables)
                     (setf dispatch-tables (rest dispatch-tables))
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
                     ;;; Kludge. We can't fully clear dispatch tables
                     ;;; in %CLEAR-READTABLE.
                     (when (eq (svref macro-table (char-code disp-char))
                               #'excl::read-dispatch-char)
                       (values t
                               disp-char
                               (svref macro-table (char-code disp-char))
                               t
                               (loop for subch-fn   across sub-char-table
                                     for subch-code from 0
                                     when subch-fn
                                       collect (cons (code-char subch-code)
                                                     subch-fn))))))))
      #'grovel-macro-chars)))

238 239

#-(or sbcl clozure allegro)
240
(eval-when (:compile-toplevel)
241 242
  (let ((*print-pretty* t))
    (simple-style-warn
243
     "~&~@<  ~@;~A has not been ported to ~A. ~
244 245
       We fall back to a portable implementation of readtable iterators. ~
       This implementation has to grovel through all available characters. ~
246
       On Unicode-aware implementations this may come with some costs.~@:>"
247
     (package-name '#.*package*) (lisp-implementation-type))))
248

249
#-(or sbcl clozure allegro)
250 251 252 253 254 255 256 257 258 259 260 261 262
(defun %make-readtable-iterator (readtable)
  (check-type readtable readtable)
  (let ((char-code 0))
    #'(lambda ()
        (prog ()
           :GROVEL
           (when (< char-code char-code-limit)
             (let* ((char (code-char char-code))
                    (fn   (get-macro-character char readtable)))
               (incf char-code)
               (when (not fn) (go :GROVEL))
               (multiple-value-bind (disp? alist)
                   (handler-case ; grovel dispatch macro characters.
263 264 265 266 267 268 269 270 271 272 273 274
                       (values t
                               ;; Only grovel upper case characters to
                               ;; avoid duplicates.
                               (loop for code from 0 below char-code-limit
                                     for subchar = (let ((ch (code-char code)))
                                                     (when (or (not (alpha-char-p ch))
                                                               (upper-case-p ch))
                                                       ch))
                                     for disp-fn = (and subchar
                                                        (get-dispatch-macro-character
                                                            char subchar readtable))
                                     when disp-fn
275 276 277
                                       collect (cons subchar disp-fn)))
                     (error () nil))
                 (return (values t char fn disp? alist)))))))))
278

279 280 281
(defmacro do-readtable ((entry-designator readtable &optional result)
                        &body body)
  "Iterate through a readtable's macro characters, and dispatch macro characters."
282
  (destructuring-bind (char &optional reader-fn non-terminating-p disp? table)
283 284 285 286
      (if (symbolp entry-designator)
          (list entry-designator)
          entry-designator)
    (let ((iter (gensym "ITER+"))
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
          (more? (gensym "MORE?+"))
          (rt (gensym "READTABLE+")))
      `(let ((,rt ,readtable))
         (with-readtable-iterator (,iter ,rt)
           (loop
             (multiple-value-bind (,more?
                                   ,char
                                   ,@(when reader-fn (list reader-fn))
                                   ,@(when disp? (list disp?))
                                   ,@(when table (list table)))
                 (,iter)
               (unless ,more? (return ,result))
               (let ,(when non-terminating-p
                       ;; FIXME: N-T-P should be incorporated in iterators.
                       `((,non-terminating-p
                          (nth-value 1 (get-macro-character ,char ,rt)))))
                 ,@body))))))))
304 305 306

;;;; Misc

307 308 309 310 311 312 313 314 315
;;; This should return an implementation's actual standard readtable
;;; object only if the implementation makes the effort to guard against
;;; modification of that object. Otherwise it should better return a
;;; copy.
(define-cruft %standard-readtable ()
  "Return the standard readtable."
  #+ :sbcl+safe-standard-readtable sb-impl::*standard-readtable*
  #+ :common-lisp                  (copy-readtable nil))

Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
316
;;; On SBCL, SET-SYNTAX-FROM-CHAR does not get rid of a
317
;;; readtable's dispatch table properly.
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
318 319 320
;;; Same goes for Allegro but that does not seem to provide a
;;; setter for their readtable's dispatch tables. Hence this ugly
;;; workaround.
321
(define-cruft %clear-readtable (readtable)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
322 323 324 325 326 327 328 329
  "Make all macro characters in READTABLE be constituents."
  #+ :sbcl
  (prog1 readtable
    (do-readtable (char readtable)
      (set-syntax-from-char char #\A readtable))
    (setf (sb-impl::dispatch-tables readtable) nil))
  #+ :allegro
  (prog1 readtable
330
    (do-readtable (char readtable)
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
331 332 333 334 335 336 337 338 339 340 341 342 343
      (set-syntax-from-char char #\A readtable))
    (let ((dispatch-tables (excl::readtable-dispatch-tables readtable)))
      (setf (cdr   dispatch-tables) nil)
      (setf (caar  dispatch-tables) #\Backspace)
      (setf (cadar dispatch-tables) (fill (cadar dispatch-tables) nil))))
  #+ :common-lisp
  (do-readtable (char readtable readtable)
    (set-syntax-from-char char #\A readtable)))

;;; See Clozure Trac Ticket 601. This is supposed to be removed at
;;; some point in the future.
(define-cruft %get-dispatch-macro-character (char subchar rt)
  "Ensure ANSI behaviour for GET-DISPATCH-MACRO-CHARACTER."
344
  #+ :ccl         (ignore-errors
Tobias C. Rittweiler's avatar
Tobias C. Rittweiler committed
345 346 347 348 349 350 351 352 353 354 355 356
                    (get-dispatch-macro-character char subchar rt))
  #+ :common-lisp (get-dispatch-macro-character char subchar rt))

;;; Allegro stores READ-TOKEN as reader macro function of each
;;; constituent character.
(define-cruft %get-macro-character (char rt)
  "Ensure ANSI behaviour for GET-MACRO-CHARACTER."
  #+ :allegro     (let ((fn (get-macro-character char rt)))
                    (cond ((not fn) nil)
                          ((function= fn #'excl::read-token) nil)
                          (t fn)))
  #+ :common-lisp (get-macro-character char rt))
357 358


359 360 361 362
;;;; Specialized PRINT-OBJECT for named readtables.

;;; As per #19 in CLHS 11.1.2.1.2 defining a method for PRINT-OBJECT
;;; that specializes on READTABLE is actually forbidden. It's quite
363 364
;;; likely to work (modulo package-locks) on most implementations,
;;; though.
365 366 367 368

;;; We don't need this on Allegro CL's as we hook into their
;;; named-readtable facility, and they provide such a method already.
#-allegro
369 370 371 372 373
(without-package-lock (:common-lisp)
  (defmethod print-object :around ((rt readtable) stream)
    (let ((name (readtable-name rt)))
      (if name
          (print-unreadable-object (rt stream :type nil :identity t)
374
            (format stream "~A ~S" :named-readtable name))
375
          (call-next-method)))))