/[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.10 - (hide annotations)
Wed Dec 15 22:37:32 2004 UTC (9 years, 4 months ago) by heller
Branch: MAIN
Changes since 1.9: +4 -2 lines
(source-path-stream-position): Bind *read-suppress* to nil before
calling read-and-record-source-map.
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    
21     ;;; Taken from swank-cmucl.lisp, by Helmut Eller
22    
23 heller 1.3 (in-package :swank-backend)
24 dbarlow 1.1
25 heller 1.8 ;; 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 dbarlow 1.1 (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 heller 1.2 (declare (type function fn))
36 dbarlow 1.1 (lambda (stream char)
37     (let ((start (file-position stream))
38     (values (multiple-value-list (funcall fn stream char)))
39     (end (file-position stream)))
40 heller 1.2 ;;(format t "[~D ~{~A~^, ~} ~D ~D]~%" start values end (char-code char))
41 heller 1.10 (unless (null values)
42 dbarlow 1.1 (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 heller 1.4 (*readtable* tab))
50 dbarlow 1.1 (dotimes (code char-code-limit)
51     (let ((char (code-char code)))
52 heller 1.8 (multiple-value-bind (fn term) (get-macro-character char tab)
53 heller 1.4 (when fn
54 dbarlow 1.1 (set-macro-character char (make-source-recorder fn source-map)
55     term tab)))))
56     tab))
57    
58 lgorrie 1.7 (defvar *source-map* nil
59 dbarlow 1.1 "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 lgorrie 1.7 (let* ((*source-map* (make-hash-table :test #'eq))
66 heller 1.9 (*readtable* (make-source-recording-readtable *readtable*
67     *source-map*)))
68 dbarlow 1.1 (values (read stream) *source-map*)))
69 lgorrie 1.7
70 dbarlow 1.1 (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 heller 1.9 (dotimes (i tlf-number) (read stream)))
75 heller 1.10 (multiple-value-bind (form source-map)
76     (let ((*read-suppress* nil))
77     (read-and-record-source-map stream))
78 heller 1.9 (source-path-source-position (cons 0 path) form source-map))))
79 dbarlow 1.1
80     (defun source-path-string-position (path string)
81     (with-input-from-string (s string)
82     (source-path-stream-position path s)))
83    
84     (defun source-path-file-position (path filename)
85     (with-open-file (file filename)
86     (source-path-stream-position path file)))
87    
88     (defun source-path-source-position (path form source-map)
89     "Return the start position of PATH from FORM and SOURCE-MAP. All
90     subforms along the path are considered and the start and end position
91     of deepest (i.e. smallest) possible form is returned."
92     ;; compute all subforms along path
93     (let ((forms (loop for n in path
94     for f = form then (nth n f)
95     collect f)))
96     ;; select the first subform present in source-map
97     (loop for form in (reverse forms)
98     for positions = (gethash form source-map)
99     until (and positions (null (cdr positions)))
100     finally (destructuring-bind ((start . end)) positions
101     (return (values (1- start) end))))))
102    

  ViewVC Help
Powered by ViewVC 1.1.5