/[slime]/slime/swank-source-path-parser.lisp
ViewVC logotype

Contents of /slime/swank-source-path-parser.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (hide annotations)
Sun Jan 27 10:15:52 2008 UTC (6 years, 2 months ago) by heller
Branch: MAIN
Changes since 1.17: +1 -14 lines
Fix source-path mapping for #+#.'(:and).
Patch by Luis Oliveira.

* swank-source-path-parser.lisp (make-source-recording-readtable):
don't suppress the #. reader macro.
(read-and-record-source-map): don't bind *read-eval* to nil.
(suppress-sharp-dot): unused, delete it.

* slime.el (test compile-defun): test with #+#.'(:and).
1 dbarlow 1.1 ;;;; Source-paths
2    
3     ;;; CMUCL/SBCL use a data structure called "source-path" to locate
4     ;;; subforms. The compiler assigns a source-path to each form in a
5     ;;; compilation unit. Compiler notes usually contain the source-path
6     ;;; of the error location.
7     ;;;
8     ;;; Compiled code objects don't contain source paths, only the
9     ;;; "toplevel-form-number" and the (sub-) "form-number". To get from
10     ;;; the form-number to the source-path we need the entire toplevel-form
11     ;;; (i.e. we have to read the source code). CMUCL has already some
12     ;;; utilities to do this translation, but we use some extended
13     ;;; versions, because we need more exact position info. Apparently
14     ;;; Hemlock is happy with the position of the toplevel-form; we also
15     ;;; need the position of subforms.
16     ;;;
17     ;;; We use a special readtable to get the positions of the subforms.
18     ;;; The readtable stores the start and end position for each subform in
19     ;;; hashtable for later retrieval.
20 lgorrie 1.15 ;;;
21     ;;; This code has been placed in the Public Domain. All warranties
22     ;;; are disclaimed.
23 dbarlow 1.1
24     ;;; Taken from swank-cmucl.lisp, by Helmut Eller
25    
26 heller 1.3 (in-package :swank-backend)
27 dbarlow 1.1
28 heller 1.8 ;; Some test to ensure the required conformance
29     (let ((rt (copy-readtable nil)))
30     (assert (or (not (get-macro-character #\space rt))
31     (nth-value 1 (get-macro-character #\space rt))))
32     (assert (not (get-macro-character #\\ rt))))
33    
34 dbarlow 1.1 (defun make-source-recorder (fn source-map)
35     "Return a macro character function that does the same as FN, but
36     additionally stores the result together with the stream positions
37     before and after of calling FN in the hashtable SOURCE-MAP."
38 heller 1.2 (declare (type function fn))
39 dbarlow 1.1 (lambda (stream char)
40     (let ((start (file-position stream))
41     (values (multiple-value-list (funcall fn stream char)))
42     (end (file-position stream)))
43 heller 1.2 ;;(format t "[~D ~{~A~^, ~} ~D ~D]~%" start values end (char-code char))
44 heller 1.10 (unless (null values)
45 dbarlow 1.1 (push (cons start end) (gethash (car values) source-map)))
46     (values-list values))))
47    
48     (defun make-source-recording-readtable (readtable source-map)
49     "Return a source position recording copy of READTABLE.
50     The source locations are stored in SOURCE-MAP."
51     (let* ((tab (copy-readtable readtable))
52 heller 1.4 (*readtable* tab))
53 heller 1.12 (dotimes (code 128)
54 dbarlow 1.1 (let ((char (code-char code)))
55 heller 1.8 (multiple-value-bind (fn term) (get-macro-character char tab)
56 heller 1.4 (when fn
57 dbarlow 1.1 (set-macro-character char (make-source-recorder fn source-map)
58     term tab)))))
59     tab))
60    
61     (defun read-and-record-source-map (stream)
62     "Read the next object from STREAM.
63     Return the object together with a hashtable that maps
64     subexpressions of the object to stream positions."
65 heller 1.16 (let* ((source-map (make-hash-table :test #'eq))
66     (*readtable* (make-source-recording-readtable *readtable* source-map))
67 heller 1.14 (start (file-position stream))
68     (form (read stream))
69     (end (file-position stream)))
70     ;; ensure that at least FORM is in the source-map
71 heller 1.16 (unless (gethash form source-map)
72     (push (cons start end) (gethash form source-map)))
73     (values form source-map)))
74 lgorrie 1.7
75 heller 1.11 (defun read-source-form (n stream)
76     "Read the Nth toplevel form number with source location recording.
77     Return the form and the source-map."
78 heller 1.12 (let ((*read-suppress* t))
79 heller 1.11 (dotimes (i n)
80     (read stream)))
81 heller 1.18 (let ((*read-suppress* nil))
82 heller 1.11 (read-and-record-source-map stream)))
83    
84 dbarlow 1.1 (defun source-path-stream-position (path stream)
85     "Search the source-path PATH in STREAM and return its position."
86 lgorrie 1.13 (check-source-path path)
87 dbarlow 1.1 (destructuring-bind (tlf-number . path) path
88 heller 1.11 (multiple-value-bind (form source-map) (read-source-form tlf-number stream)
89 heller 1.9 (source-path-source-position (cons 0 path) form source-map))))
90 lgorrie 1.13
91     (defun check-source-path (path)
92     (unless (and (consp path)
93     (every #'integerp path))
94     (error "The source-path ~S is not valid." path)))
95 dbarlow 1.1
96     (defun source-path-string-position (path string)
97     (with-input-from-string (s string)
98     (source-path-stream-position path s)))
99    
100     (defun source-path-file-position (path filename)
101     (with-open-file (file filename)
102     (source-path-stream-position path file)))
103    
104     (defun source-path-source-position (path form source-map)
105     "Return the start position of PATH from FORM and SOURCE-MAP. All
106     subforms along the path are considered and the start and end position
107 heller 1.11 of the deepest (i.e. smallest) possible form is returned."
108 dbarlow 1.1 ;; compute all subforms along path
109     (let ((forms (loop for n in path
110     for f = form then (nth n f)
111     collect f)))
112     ;; select the first subform present in source-map
113     (loop for form in (reverse forms)
114     for positions = (gethash form source-map)
115     until (and positions (null (cdr positions)))
116     finally (destructuring-bind ((start . end)) positions
117     (return (values (1- start) end))))))
118    

  ViewVC Help
Powered by ViewVC 1.1.5