3 ;;; Position-aware stream type
5 ;;; (c) 2009 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This file is part of the Simple Object Definition system.
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ;;;--------------------------------------------------------------------------
29 ;;; Compatibility hacking.
31 ;; ECL doesn't clobber the standard CLOSE and STREAM-ELEMENT-TYPE functions
32 ;; with the Gray generic versions.
34 (eval-when (:compile-toplevel :load-toplevel :execute)
35 (setf (fdefinition 'stream-close) #'cl:close
36 (fdefinition 'stream-elt-type) #'cl:stream-element-type))
38 ;;;--------------------------------------------------------------------------
41 (defgeneric stream-pathname (stream)
43 "Returns the pathname of the file that STREAM is open on.
45 If STREAM is open on a file, then return the pathname of that file.
46 Otherwise return NIL.")
48 ;; Provide some default methods. Most streams don't have a pathname.
49 ;; File-based streams provide a pathname, but it's usually been TRUENAMEd,
50 ;; which isn't ideal. We'll hack around this later.
51 (:method ((stream stream))
53 (:method ((stream file-stream))
56 ;;;--------------------------------------------------------------------------
59 (defclass file-location ()
60 ((pathname :initarg :pathname
61 :type (or pathname null)
62 :accessor file-location-pathname)
64 :type (or fixnum null)
65 :accessor file-location-line)
66 (column :initarg :column
67 :type (or fixnum null)
68 :accessor file-location-column))
70 "A simple structure containing file location information.
72 Construct using MAKE-FILE-LOCATION; the main useful function is
73 ERROR-FILE-LOCATION."))
75 (defun make-file-location (pathname line column)
76 "Constructor for FILE-LOCATION objects.
78 Returns a FILE-LOCATION object with the given contents."
79 (make-instance 'file-location
80 :pathname (and pathname (pathname pathname))
81 :line line :column column))
83 (defgeneric file-location (thing)
85 "Convert THING into a FILE-LOCATION, if possible.")
86 (:method ((thing null)) (make-file-location nil nil nil))
87 (:method ((thing file-location)) thing)
88 (:method ((stream stream))
89 (make-file-location (stream-pathname stream) nil nil)))
91 (defmethod print-object ((object file-location) stream)
92 (maybe-print-unreadable-object (object stream :type t)
93 (with-slots (pathname line column) object
94 (format stream "~:[<unnamed>~;~:*~A~]~@[:~D~]~@[:~D~]"
95 pathname line column))))
97 (defmethod make-load-form ((object file-location) &optional environment)
98 (make-load-form-saving-slots object :environment environment))
100 ;;;--------------------------------------------------------------------------
103 ;; Base classes for proxy streams.
105 (defclass proxy-stream (fundamental-stream)
106 ((ustream :initarg :stream
108 :reader position-aware-stream-underlying-stream))
110 "Base class for proxy streams.
112 A proxy stream is one that works by passing most of its work to an
113 underlying stream. We provide some basic functionality for the later
116 (defmethod stream-close ((stream proxy-stream) &key abort)
117 (with-slots (ustream) stream
118 (close ustream :abort abort)))
120 (defmethod stream-elt-type ((stream proxy-stream))
121 (with-slots (ustream) stream
122 (stream-elt-type ustream)))
124 (defmethod stream-file-position
125 ((stream proxy-stream) &optional (position nil posp))
126 (with-slots (ustream) stream
128 (file-position ustream position)
129 (file-position ustream))))
131 (defmethod stream-pathname ((stream proxy-stream))
132 (with-slots (ustream) stream
133 (stream-pathname ustream)))
135 ;; Base class for input streams.
137 (defclass proxy-input-stream (proxy-stream fundamental-input-stream)
140 "Base class for proxy input streams."))
142 (defmethod stream-clear-input ((stream proxy-input-stream))
143 (with-slots (ustream) stream
144 (clear-input ustream)))
146 (defmethod stream-read-sequence
147 ((stream proxy-input-stream) seq &optional (start 0) end)
148 (with-slots (ustream) stream
149 (read-sequence seq ustream :start start :end end)))
151 ;; Base class for output streams.
153 (defclass proxy-output-stream (proxy-stream fundamental-output-stream)
156 "Base class for proxy output streams."))
158 (defmethod stream-clear-output ((stream proxy-output-stream))
159 (with-slots (ustream) stream
160 (clear-output ustream)))
162 (defmethod stream-finish-output ((stream proxy-output-stream))
163 (with-slots (ustream) stream
164 (finish-output ustream)))
166 (defmethod stream-force-output ((stream proxy-output-stream))
167 (with-slots (ustream) stream
168 (force-output ustream)))
170 (defmethod stream-write-sequence
171 ((stream proxy-output-stream) seq &optional (start 0) end)
172 (with-slots (ustream) stream
173 (write-sequence seq ustream :start start :end end)))
175 ;; Character input streams.
177 (defclass proxy-character-input-stream
178 (proxy-input-stream fundamental-character-input-stream)
181 "A character-input-stream which is a proxy for an existing stream.
183 This doesn't actually change the behaviour of the underlying stream very
184 much, but it's a useful base to work on when writing more interesting
187 (defmethod stream-read-char ((stream proxy-character-input-stream))
188 (with-slots (ustream) stream
189 (read-char ustream nil :eof nil)))
191 (defmethod stream-read-line ((stream proxy-character-input-stream))
192 (with-slots (ustream) stream
193 (read-line ustream nil "" nil)))
195 (defmethod stream-unread-char ((stream proxy-character-input-stream) char)
196 (with-slots (ustream) stream
197 (unread-char char ustream)))
199 ;; Character output streams.
201 (defclass proxy-character-output-stream
202 (proxy-stream fundamental-character-output-stream)
205 "A character-output-stream which is a proxy for an existing stream.
207 This doesn't actually change the behaviour of the underlying stream very
208 much, but it's a useful base to work on when writing more interesting
211 (defmethod stream-line-column ((stream proxy-character-output-stream))
214 (defmethod stream-line-length ((stream proxy-character-output-stream))
217 (defmethod stream-terpri ((stream proxy-character-output-stream))
218 (with-slots (ustream) stream
221 (defmethod stream-write-char ((stream proxy-character-output-stream) char)
222 (with-slots (ustream) stream
223 (write-char char ustream)))
225 (defmethod stream-write-string
226 ((stream proxy-character-output-stream) string &optional (start 0) end)
227 (with-slots (ustream) stream
228 (write-string string ustream :start start :end end)))
230 ;;;--------------------------------------------------------------------------
231 ;;; The position-aware stream.
235 (defclass position-aware-stream (proxy-stream)
236 ((file :initarg :file
239 :accessor position-aware-stream-file)
243 :accessor position-aware-stream-line)
244 (column :initarg :column
247 :accessor position-aware-stream-column))
249 "Character stream which keeps track of the line and column position.
251 A position-aware-stream wraps an existing character stream and tracks the
252 line and column position of the current stream position. A newline
253 character increases the line number by one and resets the column number to
254 zero; most characters advance the column number by one, but tab advances
255 to the next multiple of eight. (This is consistent with Emacs, at least.)
256 The position can be read using STREAM-LINE-AND-COLUMN.
258 This is a base class; you probably want POSITION-AWARE-INPUT-STREAM or
259 POSITION-AWARE-OUTPUT-STREAM."))
261 (defgeneric stream-line-and-column (stream)
263 "Returns the current stream position of STREAM as line/column numbers.
265 Returns two values: the line and column numbers of STREAM's input
267 (:method ((stream stream))
269 (:method ((stream position-aware-stream))
270 (with-slots (line column) stream
271 (values line column))))
273 (defmethod stream-pathname ((stream position-aware-stream))
274 "Return the pathname corresponding to a POSITION-AWARE-STREAM.
276 A POSITION-AWARE-STREAM can be given an explicit pathname, which is
277 returned in preference to the pathname of the underlying stream. This is
278 useful in two circumstances. Firstly, the pathname associated with a file
279 stream will have been subjected to TRUENAME, and may be less pleasant to
280 present back to a user. Secondly, a name can be attached to a stream
281 which doesn't actually have a file backing it."
283 (with-slots (file) stream
284 (or file (call-next-method))))
286 (defmethod file-location ((stream position-aware-stream))
287 (multiple-value-bind (line column) (stream-line-and-column stream)
288 (make-file-location (stream-pathname stream) line column)))
292 (declaim (inline update-position))
293 (defun update-position (char line column)
294 "Updates LINE and COLUMN according to the character CHAR.
296 Returns the new LINE and COLUMN numbers resulting from having read CHAR."
298 ((#\newline #\vt #\page)
299 (values (1+ line) 0))
301 (values line (logandc2 (+ column 7) 7)))
303 (values line (1+ column)))))
305 (defmacro with-position ((stream) &body body)
306 "Convenience macro for tracking the read position.
308 Within the BODY, the macro (update CHAR) is defined to update the STREAM's
309 position according to the character CHAR.
311 The position is actually cached in local variables, but will be written
312 back to the stream even in the case of non-local control transfer from the
313 BODY. What won't work well is dynamically nesting WITH-POSITION forms."
315 (let ((streamvar (gensym "STREAM"))
316 (linevar (gensym "LINE"))
317 (colvar (gensym "COLUMN"))
318 (charvar (gensym "CHAR")))
319 `(let* ((,streamvar ,stream)
320 (,linevar (position-aware-stream-line ,streamvar))
321 (,colvar (position-aware-stream-column ,streamvar)))
322 (macrolet ((update (,charvar)
323 ;; This gets a little hairy. Hold tight.
324 `(multiple-value-setq (,',linevar ,',colvar)
325 (update-position ,,charvar ,',linevar ,',colvar))))
328 (setf (position-aware-stream-line ,streamvar) ,linevar
329 (position-aware-stream-column ,streamvar) ,colvar))))))
333 (defclass position-aware-input-stream
334 (position-aware-stream proxy-character-input-stream)
337 "A character input stream which tracks the input position.
339 This is particularly useful for parsers and suchlike, which want to
340 produce accurate error-location information."))
342 (defmethod stream-unread-char ((stream position-aware-input-stream) char)
344 ;; Tweak the position so that the next time the character is read, it will
345 ;; end up here. This isn't perfect: if the character doesn't actually
346 ;; match what was really read then it might not actually be possible: for
347 ;; example, if we push back a newline while in the middle of a line, or a
348 ;; tab while not at a tab stop. In that case, we'll just lose, but
349 ;; hopefully not too badly.
350 (with-slots (line column) stream
353 ;; In the absence of better ideas, I'll set the column number to zero.
354 ;; This is almost certainly wrong, but with a little luck nobody will
355 ;; ask and it'll be all right soon.
356 ((#\newline #\vt #\page)
360 ;; Winding back a single space is sufficient. If the position is
361 ;; currently on a tab stop then it'll advance back here next time. If
362 ;; not, we're going to lose anyway.
366 ;; Anything else: just decrement the column and cross fingers.
370 ;; And actually do it. (I could have written this as a :before or :after
371 ;; method, but I think this is the right answer. All of the other methods
372 ;; have to be primary (or around) methods, so at least it's consistent.)
375 (defmethod stream-read-sequence
376 ((stream position-aware-input-stream) seq &optional (start 0) end)
377 (declare (ignore end))
378 (let ((pos (call-next-method)))
379 (with-position (stream)
380 (dosequence (ch seq :start start :end pos)
384 (defmethod stream-read-char ((stream position-aware-input-stream))
385 (let ((char (call-next-method)))
386 (with-position (stream)
390 (defmethod stream-read-line ((stream position-aware-input-stream))
391 (multiple-value-bind (line eofp) (call-next-method)
393 (with-position (stream)
394 (dotimes (i (length line))
395 (update (char line i))))
396 (with-slots (line column) stream
403 (defclass position-aware-output-stream
404 (position-aware-stream proxy-character-output-stream)
407 "A character output stream which tracks the output position.
409 This is particularly useful when generating C code: the position can be
410 used to generate `#line' directives referring to the generated code after
411 insertion of some user code."))
413 (defmethod stream-write-sequence
414 ((stream position-aware-output-stream) seq &optional (start 0) end)
415 (with-position (stream)
416 (dosequence (ch seq :start start :end end)
420 (defmethod stream-line-column ((stream position-aware-output-stream))
421 (with-slots (column) stream
424 (defmethod stream-start-line-p ((stream position-aware-output-stream))
425 (with-slots (column) stream
428 (defmethod stream-terpri ((stream position-aware-output-stream))
429 (with-slots (line column) stream
434 (defmethod stream-write-char ((stream position-aware-output-stream) char)
435 (with-position (stream)
439 (defmethod stream-write-string
440 ((stream position-aware-output-stream) string &optional (start 0) end)
441 (with-position (stream)
442 (do ((i start (1+ i))
443 (end (or end (length string))))
445 (update (char string i))))
448 ;;;----- That's all, folks --------------------------------------------------