forked from gigamonkey/monkeylib-foo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-output.lisp
261 lines (200 loc) · 9.22 KB
/
text-output.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;;
;; Copyright (c) 2005, Gigamonkeys Consulting All rights reserved.
;;
(in-package :foo.text-output)
(defvar *pretty* t
"Controls whether output is generated with indentation and
tracking of newlines for emitting freshlines.")
(defvar *text-output* *standard-output*
"The stream to which output is sent. Can be bound to any character output stream.")
(defvar *text-pretty-printer* nil
"The current instance of text-pretty-printer wrapped around
*text-output* when *pretty* is true.")
(defparameter *default-tab-width* 4)
(defmacro with-foo-output ((stream &key (pretty *pretty*)) &body body)
`(let* ((*text-output* ,stream)
(*text-pretty-printer* nil)
(*pretty* ,pretty))
,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Text output interface -- this interface is implemented in two
;;; ways: the text-pretty-printer, which can be thought of as an
;;; interpreter for the text output "virtual machine", and the
;;; text-output-compiler which collects a set of instructions for the
;;; text output machine and then compiles them into either a program
;;; for the pretty printing virtual machine or calls to the raw Common
;;; Lisp output functions if pretty printing is turned off.
(defgeneric raw-string (processor string &optional check-for-newlines)
(:documentation "Emit a string with no escaping. When
CHECK-FOR-NEWLINES is true, string should still be scanned for
newlines in order to deal with indentation. In general
CHECK-FOR-NEWLINES should be true unless the string is known
not to contain newlines."))
(defgeneric newline (processor)
(:documentation "Unconditionally emit a newline."))
(defgeneric freshline (processor)
(:documentation "Emit a newline unless the last character emitted was a newline."))
(defgeneric indent (processor)
(:documentation "Increase the indentation level by some fixed amount."))
(defgeneric unindent (processor)
(:documentation "Decrease the indentation level by some fixed amount."))
(defgeneric toggle-indenting (processor)
(:documentation "Temporarily turn of indentation without changing the current indentation value."))
(defgeneric embed-value (processor value)
(:documentation "Emit a literal value."))
(defgeneric embed-code (processor code)
(:documentation "Run arbitrary Lisp code which will presumably emit some output."))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; text-pretty-printer
(defclass text-pretty-printer ()
((printer :accessor printer :initarg :printer)
(tab-width :accessor tab-width :initarg :tab-width :initform *default-tab-width*)))
(defmethod raw-string ((pp text-pretty-printer) string &optional newlines-p)
(if newlines-p
(emit (printer pp) string)
(emit/no-newlines (printer pp) string)))
(defmethod newline ((pp text-pretty-printer))
(emit-newline (printer pp)))
(defmethod freshline ((pp text-pretty-printer))
(if *pretty*
(emit-freshline (printer pp))
(emit-newline (printer pp))))
(defmethod indent ((pp text-pretty-printer))
(when *pretty*
(incf (indentation (printer pp)) (tab-width pp))))
(defmethod unindent ((pp text-pretty-printer))
(when *pretty*
(decf (indentation (printer pp)) (tab-width pp))))
(defmethod toggle-indenting ((pp text-pretty-printer))
(when *pretty*
(with-slots (indenting-p) (printer pp)
(setf indenting-p (not indenting-p)))))
(defmethod embed-value ((pp text-pretty-printer) value)
(error "Can't embed values when interpreting. Value: ~s" value))
(defmethod embed-code ((pp text-pretty-printer) code)
(error "Can't embed code when interpreting. Code: ~s" code))
(defun get-pretty-printer ()
(or *text-pretty-printer* (new-pretty-printer)))
(defun new-pretty-printer ()
(make-instance
'text-pretty-printer
:printer (make-instance 'indenting-printer :out *text-output*)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Compiler
(defclass text-compiler ()
((ops :accessor ops :initform (make-op-buffer))))
(defmethod raw-string ((compiler text-compiler) string &optional newlines-p)
(push-op `(:raw-string ,string ,newlines-p) (ops compiler)))
(defmethod newline ((compiler text-compiler))
(push-op '(:newline) (ops compiler)))
(defmethod freshline ((compiler text-compiler))
(push-op '(:freshline) (ops compiler)))
(defmethod indent ((compiler text-compiler))
(push-op `(:indent) (ops compiler)))
(defmethod unindent ((compiler text-compiler))
(push-op `(:unindent) (ops compiler)))
(defmethod toggle-indenting ((compiler text-compiler))
(push-op `(:toggle-indenting) (ops compiler)))
(defmethod embed-value ((compiler text-compiler) value)
(push-op `(:embed-value ,value) (ops compiler)))
(defmethod embed-code ((compiler text-compiler) code)
(push-op `(:embed-code ,code) (ops compiler)))
(defun codegen-text (ops pretty)
"Given a vector of ops, return a Common Lisp translation,
i.e. Common Lisp code that will emit the same output as the
text-pretty-printer would if it was given the same ops."
(let ((*pretty* pretty))
`(progn ,@(generate-code (optimize-static-output ops)) nil)))
(defun generate-code (ops)
(loop for op across ops collect (apply #'op->code op)))
(defun optimize-static-output (ops)
(let ((new-ops (make-op-buffer)))
(with-output-to-string (buf)
(flet ((add-op (op)
(compile-buffer buf new-ops)
(push-op op new-ops)))
(loop for op across ops do
(ecase (first op)
(:raw-string (write-sequence (second op) buf))
((:newline :embed-value :embed-code) (add-op op))
(:freshline (add-op (if *pretty* op '(:newline))))
((:indent :unindent :toggle-indenting)
(when *pretty* (add-op op)))))
(compile-buffer buf new-ops)))
new-ops))
(defun compile-buffer (buf ops)
"Compile a string possibly containing newlines into a sequence of
:raw-string and :newline ops."
(loop with str = (get-output-stream-string buf)
for start = 0 then (1+ pos)
for pos = (position #\Newline str :start start)
when (< start (length str))
do (push-op `(:raw-string ,(subseq str start pos) nil) ops)
when pos do (push-op '(:newline) ops)
while pos))
(defgeneric op->code (op &rest operands))
(defmethod op->code ((op (eql :raw-string)) &rest operands)
(destructuring-bind (string check-for-newlines) operands
(if *pretty*
`(raw-string *text-pretty-printer* ,string ,check-for-newlines)
`(write-sequence ,string *text-output*))))
(defmethod op->code ((op (eql :newline)) &rest operands)
(if *pretty*
`(newline *text-pretty-printer*)
`(write-char #\Newline *text-output*)))
(defmethod op->code ((op (eql :freshline)) &rest operands)
(if *pretty*
`(freshline *text-pretty-printer*)
(error "Bad op when not pretty-printing: ~a" op)))
(defmethod op->code ((op (eql :indent)) &rest operands)
(if *pretty*
`(indent *text-pretty-printer*)
(error "Bad op when not pretty-printing: ~a" op)))
(defmethod op->code ((op (eql :unindent)) &rest operands)
(if *pretty*
`(unindent *text-pretty-printer*)
(error "Bad op when not pretty-printing: ~a" op)))
(defmethod op->code ((op (eql :toggle-indenting)) &rest operands)
(if *pretty*
`(toggle-indenting *text-pretty-printer*)
(error "Bad op when not pretty-printing: ~a" op)))
(defmethod op->code ((op (eql :embed-value)) &rest operands)
(let ((value (car operands)))
(if *pretty*
`(raw-string *text-pretty-printer* (princ-to-string ,value) t)
`(write-sequence ,value *text-output*))))
(defmethod op->code ((op (eql :embed-code)) &rest operands)
(first operands))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; indenting-printer -- used to actually emit text to an output
;;; stream while keeping track of newlines and emitting indentation as
;;; necessary. Used by the pretty printer.
(defclass indenting-printer ()
((out :accessor out :initarg :out)
(beginning-of-line-p :accessor beginning-of-line-p :initform t)
(indentation :accessor indentation :initform 0)
(indenting-p :accessor indenting-p :initform t)))
(defun emit (ip string)
(loop for start = 0 then (1+ pos)
for pos = (position #\Newline string :start start)
do (emit/no-newlines ip string :start start :end pos)
when pos do (emit-newline ip)
while pos))
(defun emit/no-newlines (ip string &key (start 0) end)
(indent-if-necessary ip)
(write-sequence string (out ip) :start start :end end)
(unless (zerop (- (or end (length string)) start))
(setf (beginning-of-line-p ip) nil)))
(defun emit-newline (ip)
(write-char #\Newline (out ip))
(setf (beginning-of-line-p ip) t))
(defun emit-freshline (ip)
(unless (beginning-of-line-p ip) (emit-newline ip)))
(defun indent-if-necessary (ip)
(when (and (beginning-of-line-p ip) (indenting-p ip))
(loop repeat (indentation ip) do (write-char #\Space (out ip)))
(setf (beginning-of-line-p ip) nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Ops buffer -- slight abstraction used by text-compiler.
(defun make-op-buffer () (make-array 10 :adjustable t :fill-pointer 0))
(defun push-op (op ops-buffer) (vector-push-extend op ops-buffer))