PasteRack.org
Paste # 91585
2018-07-09 16:14:58

Fork as a new paste.

Paste viewed 305 times.


Embed:

Experimenting with getting definition locations from .zo files

  1. #lang racket
  2.  
  3. (require compiler/zo-parse)
  4. (define (definition-from-zo zo-file sym mods)
  5.   (define the-mod
  6.     (let loop ([m (compilation-top-code (with-input-from-file zo-file zo-parse))])
  7.       (if (or (equal? (list (mod-name m)) mods)
  8.               (equal? (mod-name m) mods))
  9.           m
  10.           (for/or ([v (in-list (append (mod-pre-submodules m)
  11.                                        (mod-post-submodules m)))])
  12.             (and (mod? v) (loop v))))))
  13.   (and
  14.    the-mod
  15.    (or
  16.     ;; The srcloc in mod-binding-names hashtable is preferable: 1.
  17.     ;; Most importantly, it's correct for "definer macros" -- points
  18.     ;; to usage of the macro not the macro definition itself. 2. Less
  19.     ;; importantly, it points to the identifier instead of the parent
  20.     ;; `define`. HOWEVER the hashtable only seems to be present for
  21.     ;; the file module -- not for submodules. So as a fallback, try
  22.     ;; walking body forms for `lam`s to get the srcloc.
  23.     (match (hash-ref (hash-ref (mod-binding-names the-mod) 0 (hash)) sym #f)
  24.       [(stx
  25.         (struct* stx-obj
  26.                  ([srcloc (struct* srcloc
  27.                                    ([source source]
  28.                                     [line line]
  29.                                     [column column]))])))
  30.        (list source line (sub1 column))]
  31.       [_ #f])
  32.     (for/or ([v (in-list (mod-body the-mod))])
  33.       (match v
  34.         [(def-values
  35.            _ids
  36.            (inline-variant (closure (? lam? lam) _gen-id)
  37.                            _inline))
  38.          (match (lam-name lam)
  39.            [(vector (== sym) (? path? file) line col _ ...)
  40.             (list file line col)]
  41.            [_ #f])]
  42.         [_ #f])))))

=>