Changeset 13929
- Timestamp:
- Jul 6, 2010, 5:02:47 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/source/lib/pprint.lisp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/lib/pprint.lisp
r12215 r13929 175 175 (conses-with-cars (make-hash-table :test #'eq) :type hash-table) 176 176 (structures (make-hash-table :test #'eq) :type (or null hash-table)) 177 (others nil :type list)) 177 (others nil :type list) 178 (commit-hook nil)) 178 179 179 180 ;The list and the hash-tables contain entries of the … … 199 200 :conses-with-cars new-conses-with-cars 200 201 :structures new-structures 201 :others (copy-list (others table))))) 202 :others (copy-list (others table)) 203 :commit-hook (commit-hook table)))) 202 204 203 205 … … 939 941 (maybe-too-large xp qleft queue linel))) 940 942 (T T)) ;(:linear :unconditional :mandatory) 941 (output-line xp qleft) 942 (setup-for-next-line xp qleft)))) 943 (output-line-and-setup-for-next xp qleft)))) 943 944 (setf (xp-qleft xp) (setq qleft (qnext qleft)))) 944 945 (when flush-out? (flush xp))))) … … 947 948 948 949 (defun flush (xp) 949 (let ((ostream (xp-out-stream xp))) 950 (let ((ostream (xp-out-stream xp)) 951 (len (xp-buffer-ptr xp))) 952 (when (commit-hook *print-pprint-dispatch*) 953 (funcall (commit-hook *print-pprint-dispatch*) xp len 0)) 950 954 (when ostream 951 (write-string (xp-buffer xp) ostream :start 0 :end (xp-buffer-ptr xp)))952 (incf (xp-buffer-offset xp) (xp-buffer-ptr xp))953 (incf (xp-charpos xp) (xp-buffer-ptr xp))955 (write-string (xp-buffer xp) ostream :start 0 :end len)) 956 (incf (xp-buffer-offset xp) len) 957 (incf (xp-charpos xp) len) 954 958 (setf (xp-buffer-ptr xp) 0))) 955 959 … … 968 972 ;This prints out a line of stuff. 969 973 970 (defun output-line (xp Qentry) 971 (flet ((find-not-char-reverse (buffer out-point) 972 (declare (type simple-base-string buffer) (type fixnum out-point)) 973 (do ((i (%i- out-point 1) (%i- i 1))) 974 ((%i< i 0) nil) 975 (when (or (neq (schar buffer i) #\Space) 976 ;; Don't match possibly-quoted space ("possibly" because the #\\ itself might be 977 ;; quoted; don't bother checking for that, no big harm leaving the space even if 978 ;; not totally necessary). 979 (and (%i< 0 i) (eq (schar buffer (%i- i 1)) #\\))) 980 (return i))))) 981 (let* ((queue (xp-queue xp)) 982 (out-point (BP<-TP xp (xpq-pos queue Qentry))) 983 (last-non-blank (find-not-char-reverse (xp-buffer xp) out-point)) 984 (end (cond ((memq (xpq-kind queue Qentry) '(:fresh :unconditional)) out-point) 985 (last-non-blank (%i+ 1 last-non-blank)) 986 (T 0))) 987 (line-limit-exit (and (xp-line-limit xp) (not (%i> (xp-line-limit xp) (xp-line-no xp)))))) 988 (when line-limit-exit 989 (setf (xp-buffer-ptr xp) end) ;truncate pending output. 990 (write-string+++ " .." xp 0 3) 991 (reverse-string-in-place (xp-suffix xp) 0 (suffix-ptr xp)) 992 (write-string+++ (xp-suffix xp) xp 0 (suffix-ptr xp)) 993 (setf (xp-qleft xp) (qnext (xp-qright xp))) 994 ;(setq *abbreviation-happened* '*print-lines*) 995 (throw 'line-limit-abbreviation-exit T)) 996 (setf (xp-line-no xp)(%i+ 1 (xp-line-no xp))) 997 (let ((bstream (xp-out-stream xp))) 998 (when bstream 999 (write-string (xp-buffer xp) bstream :start 0 :end end) 1000 (stream-write-char bstream #\newline)))))) 1001 1002 (defun setup-for-next-line (xp Qentry) 974 (defun output-line-and-setup-for-next (xp Qentry) 1003 975 (let* ((queue (xp-queue xp)) 1004 976 (out-point (BP<-TP xp (xpq-pos queue Qentry))) 1005 (prefix-end 1006 (cond ((memq (xpq-kind queue Qentry) '(:unconditional :fresh)) 1007 (non-blank-prefix-ptr xp)) 1008 (T (prefix-ptr xp)))) 1009 (change (- prefix-end out-point))) 1010 (declare (fixnum out-point prefix-end change)) 977 (unconditional-p (memq (xpq-kind queue Qentry) '(:fresh :unconditional))) 978 (end (if unconditional-p 979 out-point 980 (let ((buffer (xp-buffer xp))) 981 (declare (type simple-base-string buffer)) 982 (do ((i (%i- out-point 1) (%i- i 1))) 983 ((%i< i 0) 0) 984 (when (or (neq (schar buffer i) #\Space) 985 ;; Don't match possibly-quoted space ("possibly" because the #\\ itself might be 986 ;; quoted; don't bother checking for that, no big harm leaving the space even if 987 ;; not totally necessary). 988 (and (%i< 0 i) (eq (schar buffer (%i- i 1)) #\\))) 989 (return (%i+ i 1))))))) 990 (prefix-end 991 (if unconditional-p (non-blank-prefix-ptr xp) (prefix-ptr xp))) 992 (old-ptr (xp-buffer-ptr xp)) 993 (new-ptr (%i+ old-ptr (%i- prefix-end out-point))) 994 (line-limit-exit (and (xp-line-limit xp) (not (%i> (xp-line-limit xp) (xp-line-no xp)))))) 995 (when line-limit-exit 996 (setf (xp-buffer-ptr xp) end) ;truncate pending output. 997 (write-string+++ " .." xp 0 3) 998 (reverse-string-in-place (xp-suffix xp) 0 (suffix-ptr xp)) 999 (write-string+++ (xp-suffix xp) xp 0 (suffix-ptr xp)) 1000 (setf (xp-qleft xp) (qnext (xp-qright xp))) 1001 ;(setq *abbreviation-happened* '*print-lines*) 1002 (throw 'line-limit-abbreviation-exit T)) 1003 (setf (xp-line-no xp)(%i+ 1 (xp-line-no xp))) 1004 (when (commit-hook *print-pprint-dispatch*) 1005 (funcall (commit-hook *print-pprint-dispatch*) xp out-point prefix-end)) 1006 (let ((bstream (xp-out-stream xp))) 1007 (when bstream 1008 (write-string (xp-buffer xp) bstream :start 0 :end end) 1009 (stream-write-char bstream #\newline))) 1011 1010 (setf (xp-charpos xp) 0) 1012 (when (plusp change) ;almost never happens 1013 (xp-check-size (xp-buffer xp) (%i+ (xp-buffer-ptr xp) change) 1014 #.buffer-min-size #.buffer-entry-size)) 1011 (when (%i> new-ptr old-ptr) ;almost never happens 1012 (xp-check-size (xp-buffer xp) new-ptr #.buffer-min-size #.buffer-entry-size)) 1013 (setf (xp-buffer-ptr xp) new-ptr) 1014 (decf (xp-buffer-offset xp) (- prefix-end out-point)) 1015 1015 (let ((buffer (xp-buffer xp))) 1016 (replace buffer buffer :start1 prefix-end 1017 :start2 out-point :end2 (xp-buffer-ptr xp)) 1016 (replace buffer buffer :start1 prefix-end :start2 out-point :end2 old-ptr) 1018 1017 (replace buffer (xp-prefix xp) :end2 prefix-end) 1019 (setf (xp-buffer-ptr xp) (%i+ (xp-buffer-ptr xp) change)) 1020 (setf (xp-buffer-offset xp) (%i- (xp-buffer-offset xp) change)) 1021 (when (not (memq (xpq-kind queue Qentry) '(:unconditional :fresh))) 1018 (unless unconditional-p 1022 1019 (setf (section-start-line xp) (xp-line-no xp)))))) 1020 1021 1023 1022 1024 1023 (defun set-indentation-prefix (xp new-position) … … 1310 1309 (defmethod stream-finish-output ((xp xp-structure)) 1311 1310 (attempt-to-output xp t t)) 1311 1312 1313 ;; pretty-print FORM into STREAM, recording file positions for objects (actually conses only) in MAP. 1314 ;; if ADD-NEW is false, MAP should be pre-populated and only those objects with entries in MAP 1315 ;; will be tracked. 1316 ;; The hair here comes from the fact that the pretty printer backtracks to insert newlines. 1317 (defun pprint-recording-positions (form stream &key map (add-new t)) 1318 (when (null map) 1319 (assert add-new () ":MAP required") 1320 (setq map (make-hash-table :test #'eq))) 1321 (let* ((old-table *print-pprint-dispatch*) 1322 (rec-pending nil) 1323 (map (require-type map 'hash-table))) 1324 (flet ((rec-pprint (xp object) 1325 #+gz (assert (or (null rec-pending) 1326 (<= (caar rec-pending) (xp-buffer-ptr xp)))) 1327 (let ((real-printer (get-printer object old-table))) 1328 (when real-printer 1329 (push (list* (xp-buffer-ptr xp) t object) rec-pending) 1330 (funcall real-printer xp object) 1331 (push (list* (xp-buffer-ptr xp) nil object) rec-pending)))) 1332 (rec-commit (xp commited inserted) 1333 (loop with change = (- inserted commited) 1334 as last = nil then pending 1335 as pending = rec-pending then (cdr pending) while pending 1336 do (when (<= (caar pending) commited) ;; commit the rest. 1337 (if last 1338 (setf (cdr last) nil) 1339 (setf rec-pending nil)) 1340 (loop with start = (stream-position (xp-out-stream xp)) 1341 for (offset open-p . object) in pending 1342 as cell = (or (gethash object map) 1343 (and add-new 1344 (setf (gethash object map) (cons nil nil)))) 1345 when cell 1346 do (if open-p 1347 (setf (car cell) (+ start offset)) 1348 (setf (cdr cell) (+ start offset)))) 1349 (return nil)) 1350 do (incf (caar pending) change)))) 1351 (let* ((*print-pretty* t) 1352 (*print-circle* nil) 1353 (*print-length* nil) 1354 (*print-level* nil) 1355 (*print-lines* nil) 1356 (*print-miser-width* nil) 1357 (*read-suppress* nil) 1358 (*print-pprint-dispatch* (make-pprint-dispatch-table :commit-hook #'rec-commit))) 1359 (set-pprint-dispatch 'cons #'rec-pprint) 1360 (prin1 form stream) 1361 #+gz (assert (null rec-pending)))) 1362 map)) 1363 1312 1364 1313 1365
Note:
See TracChangeset
for help on using the changeset viewer.
