/[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.2 - (hide annotations)
Sat Jan 31 09:02:22 2004 UTC (10 years, 2 months ago) by heller
Branch: MAIN
CVS Tags: SLIME-0-11
Branch point for: package-split
Changes since 1.1: +2 -1 lines
Patch by Robert E. Brown.  Add type declarations to keep SBCL quiet.
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     (in-package :swank)
24    
25     (defun make-source-recorder (fn source-map)
26     "Return a macro character function that does the same as FN, but
27     additionally stores the result together with the stream positions
28     before and after of calling FN in the hashtable SOURCE-MAP."
29 heller 1.2 (declare (type function fn))
30 dbarlow 1.1 (lambda (stream char)
31     (let ((start (file-position stream))
32     (values (multiple-value-list (funcall fn stream char)))
33     (end (file-position stream)))
34 heller 1.2 ;;(format t "[~D ~{~A~^, ~} ~D ~D]~%" start values end (char-code char))
35 dbarlow 1.1 (unless (null values)
36     (push (cons start end) (gethash (car values) source-map)))
37     (values-list values))))
38    
39     (defun make-source-recording-readtable (readtable source-map)
40     "Return a source position recording copy of READTABLE.
41     The source locations are stored in SOURCE-MAP."
42     (let* ((tab (copy-readtable readtable))
43     (*readtable* tab)
44     (undefined-macro (get-macro-character #\Space tab)))
45     ;; not sure why this should be the case, but SBCL 0.8.6 returns
46     ;; #<FUNCTION "top level local call SB!IMPL::UNDEFINED-MACRO-CHAR">
47     ;; for (get-macro-character) on characters that aren't macros.
48     ;; As there's no way to detect the syntax of a character (only
49     ;; to set it from another character) we have to compare against
50     ;; this undefined-macro function to avoid turning everything into
51     ;; a macro
52     (dotimes (code char-code-limit)
53     (let ((char (code-char code)))
54     (multiple-value-bind (fn term) (get-macro-character char tab)
55     (when (and fn (not (eq fn undefined-macro)))
56     (set-macro-character char (make-source-recorder fn source-map)
57     term tab)))))
58     tab))
59    
60     (defun make-source-map ()
61     (make-hash-table :test #'eq))
62    
63     (defvar *source-map* (make-source-map)
64     "The hashtable table used for source position recording.")
65    
66     (defvar *recording-readtable-cache* '()
67     "An alist of (READTABLE . RECORDING-READTABLE) pairs.")
68    
69     (defun lookup-recording-readtable (readtable)
70     "Find a cached or create a new recording readtable for READTABLE."
71     (or (cdr (assoc readtable *recording-readtable-cache*))
72     (let ((table (make-source-recording-readtable readtable *source-map*)))
73     (push (cons readtable table) *recording-readtable-cache*)
74     table)))
75    
76     (defun read-and-record-source-map (stream)
77     "Read the next object from STREAM.
78     Return the object together with a hashtable that maps
79     subexpressions of the object to stream positions."
80     (let ((*readtable* (lookup-recording-readtable *readtable*)))
81     (clrhash *source-map*)
82     (values (read stream) *source-map*)))
83    
84     (defun source-path-stream-position (path stream)
85     "Search the source-path PATH in STREAM and return its position."
86     (destructuring-bind (tlf-number . path) path
87     (let ((*read-suppress* t))
88     (dotimes (i tlf-number) (read stream))
89     (multiple-value-bind (form source-map)
90     (read-and-record-source-map stream)
91     (source-path-source-position (cons 0 path) form source-map)))))
92    
93     (defun source-path-string-position (path string)
94     (with-input-from-string (s string)
95     (source-path-stream-position path s)))
96    
97     (defun source-path-file-position (path filename)
98     (with-open-file (file filename)
99     (source-path-stream-position path file)))
100    
101     (defun source-path-source-position (path form source-map)
102     "Return the start position of PATH from FORM and SOURCE-MAP. All
103     subforms along the path are considered and the start and end position
104     of deepest (i.e. smallest) possible form is returned."
105     ;; compute all subforms along path
106     (let ((forms (loop for n in path
107     for f = form then (nth n f)
108     collect f)))
109     ;; select the first subform present in source-map
110     (loop for form in (reverse forms)
111     for positions = (gethash form source-map)
112     until (and positions (null (cdr positions)))
113     finally (destructuring-bind ((start . end)) positions
114     (return (values (1- start) end))))))
115    

  ViewVC Help
Powered by ViewVC 1.1.5