Книга: Practical Common Lisp
The Pretty Printer Backend
The Pretty Printer Backend
You can start by defining a class with two slots—one to hold an instance of indenting-printer
and one to hold the tab width—the number of spaces you want to increase the indentation for each level of nesting of HTML elements.
(defclass html-pretty-printer ()
((printer :accessor printer :initarg :printer)
(tab-width :accessor tab-width :initarg :tab-width :initform 2)))
Now you can implement methods specialized on html-pretty-printer
on the eight generic functions that make up the backend interface.
The FOO processors use the raw-string
function to emit strings that don't need character escaping, either because you actually want to emit normally reserved characters or because all reserved characters have already been escaped. Usually raw-string
is invoked with strings that don't contain newlines, so the default behavior is to use emit/no-newlines
unless the caller specifies a non-NIL newlines-p
argument.
(defmethod raw-string ((pp html-pretty-printer) string &optional newlines-p)
(if newlines-p
(emit (printer pp) string)
(emit/no-newlines (printer pp) string)))
The functions newline
, freshline
, indent
, unindent
, and toggle-indenting
implement fairly straightforward manipulations of the underlying indenting-printer
. The only wrinkle is that the HTML pretty printer generates pretty output only when the dynamic variable *pretty*
is true. When it's NIL
, you should generate compact HTML with no unnecessary whitespace. So, these methods, with the exception of newline
, all check *pretty*
before doing anything:[315]
(defmethod newline ((pp html-pretty-printer))
(emit-newline (printer pp)))
(defmethod freshline ((pp html-pretty-printer))
(when *pretty* (emit-freshline (printer pp))))
(defmethod indent ((pp html-pretty-printer))
(when *pretty*
(incf (indentation (printer pp)) (tab-width pp))))
(defmethod unindent ((pp html-pretty-printer))
(when *pretty*
(decf (indentation (printer pp)) (tab-width pp))))
(defmethod toggle-indenting ((pp html-pretty-printer))
(when *pretty*
(with-slots (indenting-p) (printer pp)
(setf indenting-p (not indenting-p)))))
Finally, the functions embed-value
and embed-code
are used only by the FOO compiler—embed-value
is used to generate code that'll emit the value of a Common Lisp expression, while embed-code
is used to embed a bit of code to be run and its result discarded. In the interpreter, you can't meaningfully evaluate embedded Lisp code, so the methods on these functions always signal an error.
(defmethod embed-value ((pp html-pretty-printer) value)
(error "Can't embed values when interpreting. Value: ~s" value))
(defmethod embed-code ((pp html-pretty-printer) code)
(error "Can't embed code when interpreting. Code: ~s" code))
Using Conditions to Have Your Cake and Eat It Too |
An alternate approach would be to use
when However, if First define some error classes that you can signal when
Now you can implement
Now you can do something like this:
and you'll get dropped into the debugger with this message:
If you invoke the
Then, as a convenience, you can provide restart functions—functions that invoke the
Now you can use
Finally, you can define a macro to provide a nicer syntax for binding handlers for the two kinds of errors.
With this macro defined, you can write this:
|
- 30. Practical: An HTML Generation Library, the Interpreter
- Indenting Printer
- 4.4.4 The Dispatcher
- About the author
- Chapter 7. The state machine
- Appendix E. Other resources and links
- Example NAT machine in theory
- The final stage of our NAT machine
- Compiling the user-land applications
- The conntrack entries
- Untracked connections and the raw table
- Basics of the iptables command