/[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.9 - (show annotations)
Sat Oct 30 10:16:29 2004 UTC (9 years, 5 months ago) by heller
Branch: MAIN
CVS Tags: SLIME-1-1, MULTIBYTE-ENCODING
Changes since 1.8: +5 -5 lines
(source-path-stream-position): Bind *read-suppress* only as long as we
skip over forms.  The last toplevel form in the path is read with
*read-suppress* = nil because in newer versions of CMUCL and SBCL read
will return nil if *read-suppress* is t.
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 ;;; Taken from swank-cmucl.lisp, by Helmut Eller
22
23 (in-package :swank-backend)
24
25 ;; Some test to ensure the required conformance
26 (let ((rt (copy-readtable nil)))
27 (assert (or (not (get-macro-character #\space rt))
28 (nth-value 1 (get-macro-character #\space rt))))
29 (assert (not (get-macro-character #\\ rt))))
30
31 (defun make-source-recorder (fn source-map)
32 "Return a macro character function that does the same as FN, but
33 additionally stores the result together with the stream positions
34 before and after of calling FN in the hashtable SOURCE-MAP."
35 (declare (type function fn))
36 (lambda (stream char)
37 (let ((start (file-position stream))
38 (values (multiple-value-list (funcall fn stream char)))
39 (end (file-position stream)))
40 ;;(format t "[~D ~{~A~^, ~} ~D ~D]~%" start values end (char-code char))
41 (unless (null values)
42 (push (cons start end) (gethash (car values) source-map)))
43 (values-list values))))
44
45 (defun make-source-recording-readtable (readtable source-map)
46 "Return a source position recording copy of READTABLE.
47 The source locations are stored in SOURCE-MAP."
48 (let* ((tab (copy-readtable readtable))
49 (*readtable* tab))
50 (dotimes (code char-code-limit)
51 (let ((char (code-char code)))
52 (multiple-value-bind (fn term) (get-macro-character char tab)
53 (when fn
54 (set-macro-character char (make-source-recorder fn source-map)
55 term tab)))))
56 tab))
57
58 (defvar *source-map* nil
59 "The hashtable table used for source position recording.")
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 (let* ((*source-map* (make-hash-table :test #'eq))
66 (*readtable* (make-source-recording-readtable *readtable*
67 *source-map*)))
68 (values (read stream) *source-map*)))
69
70 (defun source-path-stream-position (path stream)
71 "Search the source-path PATH in STREAM and return its position."
72 (destructuring-bind (tlf-number . path) path
73 (let ((*read-suppress* t))
74 (dotimes (i tlf-number) (read stream)))
75 (multiple-value-bind (form source-map) (read-and-record-source-map stream)
76 (source-path-source-position (cons 0 path) form source-map))))
77
78 (defun source-path-string-position (path string)
79 (with-input-from-string (s string)
80 (source-path-stream-position path s)))
81
82 (defun source-path-file-position (path filename)
83 (with-open-file (file filename)
84 (source-path-stream-position path file)))
85
86 (defun source-path-source-position (path form source-map)
87 "Return the start position of PATH from FORM and SOURCE-MAP. All
88 subforms along the path are considered and the start and end position
89 of deepest (i.e. smallest) possible form is returned."
90 ;; compute all subforms along path
91 (let ((forms (loop for n in path
92 for f = form then (nth n f)
93 collect f)))
94 ;; select the first subform present in source-map
95 (loop for form in (reverse forms)
96 for positions = (gethash form source-map)
97 until (and positions (null (cdr positions)))
98 finally (destructuring-bind ((start . end)) positions
99 (return (values (1- start) end))))))
100

  ViewVC Help
Powered by ViewVC 1.1.5