/[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.17 - (show annotations)
Sun Jun 25 08:33:16 2006 UTC (7 years, 9 months ago) by heller
Branch: MAIN
CVS Tags: SLIME-2-1
Branch point for: contrib
Changes since 1.16: +4 -1 lines
(suppress-sharp-dot): Return a unique symbol to avoid nil entries at
toplevel in the source-map.
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 ;;;
21 ;;; This code has been placed in the Public Domain. All warranties
22 ;;; are disclaimed.
23
24 ;;; Taken from swank-cmucl.lisp, by Helmut Eller
25
26 (in-package :swank-backend)
27
28 ;; 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 (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 (declare (type function fn))
39 (lambda (stream char)
40 (let ((start (file-position stream))
41 (values (multiple-value-list (funcall fn stream char)))
42 (end (file-position stream)))
43 ;;(format t "[~D ~{~A~^, ~} ~D ~D]~%" start values end (char-code char))
44 (unless (null values)
45 (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 (*readtable* tab))
53 (dotimes (code 128)
54 (let ((char (code-char code)))
55 (multiple-value-bind (fn term) (get-macro-character char tab)
56 (when fn
57 (set-macro-character char (make-source-recorder fn source-map)
58 term tab)))))
59 (suppress-sharp-dot tab)
60 tab))
61
62 (defun suppress-sharp-dot (readtable)
63 (when (get-macro-character #\# readtable)
64 (let ((sharp-dot (get-dispatch-macro-character #\# #\. readtable)))
65 (set-dispatch-macro-character #\# #\. (lambda (&rest args)
66 (let ((*read-suppress* t))
67 (apply sharp-dot args))
68 (if *read-suppress*
69 (values)
70 (list (gensym "#."))))
71 readtable))))
72
73 (defun read-and-record-source-map (stream)
74 "Read the next object from STREAM.
75 Return the object together with a hashtable that maps
76 subexpressions of the object to stream positions."
77 (let* ((source-map (make-hash-table :test #'eq))
78 (*readtable* (make-source-recording-readtable *readtable* source-map))
79 (start (file-position stream))
80 (form (read stream))
81 (end (file-position stream)))
82 ;; ensure that at least FORM is in the source-map
83 (unless (gethash form source-map)
84 (push (cons start end) (gethash form source-map)))
85 (values form source-map)))
86
87 (defun read-source-form (n stream)
88 "Read the Nth toplevel form number with source location recording.
89 Return the form and the source-map."
90 (let ((*read-suppress* t))
91 (dotimes (i n)
92 (read stream)))
93 (let ((*read-suppress* nil)
94 (*read-eval* nil))
95 (read-and-record-source-map stream)))
96
97 (defun source-path-stream-position (path stream)
98 "Search the source-path PATH in STREAM and return its position."
99 (check-source-path path)
100 (destructuring-bind (tlf-number . path) path
101 (multiple-value-bind (form source-map) (read-source-form tlf-number stream)
102 (source-path-source-position (cons 0 path) form source-map))))
103
104 (defun check-source-path (path)
105 (unless (and (consp path)
106 (every #'integerp path))
107 (error "The source-path ~S is not valid." path)))
108
109 (defun source-path-string-position (path string)
110 (with-input-from-string (s string)
111 (source-path-stream-position path s)))
112
113 (defun source-path-file-position (path filename)
114 (with-open-file (file filename)
115 (source-path-stream-position path file)))
116
117 (defun source-path-source-position (path form source-map)
118 "Return the start position of PATH from FORM and SOURCE-MAP. All
119 subforms along the path are considered and the start and end position
120 of the deepest (i.e. smallest) possible form is returned."
121 ;; compute all subforms along path
122 (let ((forms (loop for n in path
123 for f = form then (nth n f)
124 collect f)))
125 ;; select the first subform present in source-map
126 (loop for form in (reverse forms)
127 for positions = (gethash form source-map)
128 until (and positions (null (cdr positions)))
129 finally (destructuring-bind ((start . end)) positions
130 (return (values (1- start) end))))))
131

  ViewVC Help
Powered by ViewVC 1.1.5