Skip to content

Commit ff0f281

Browse files
committed
Document pretty-printing mechanism
1 parent 208b2e8 commit ff0f281

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

src/langapi/language_util.cpp

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,73 @@ static languaget* get_language(
5151

5252
std::vector<pretty_printer_factoryt> registered_pretty_printers;
5353

54+
/*******************************************************************\
55+
56+
Function: register_global_pretty_printer
57+
58+
Inputs: `new_printer`: factory method returning
59+
`unique_ptr<pretty_printert>`
60+
61+
Outputs:
62+
63+
Purpose: This registers a custom pretty-printer for use printing
64+
expressions with `from_expr` and `from_type` also defined
65+
in language_util.h. The function given should allocate
66+
an instance of the printer and return a unique_ptr that
67+
disposes of it appropriately (so if allocated with `new`,
68+
ordinary `std::unique_ptr` will do the trick).
69+
`from_expr` and `from_type` will always build a stack of
70+
printer classes of the form:
71+
L -> C1 -> C2 ... Cn -> X
72+
73+
L is the language frontend pretty-printer associated
74+
with an expression, C1 ... Cn are instances of the registered
75+
custom printers in order of registration, and X is the fallback
76+
irep-to-lisp printer `norep_pretty_printert`. The -> arrows
77+
represent `next_pretty_printer` fields used to defer printing
78+
of expressions a particular printer can't handle; all printers
79+
in the stack also get `top_pretty_printer` set to point at L,
80+
which they should use when printing subexpressions to give
81+
the whole stack a chance to provide a representation.
82+
83+
Note at present language custom printers such as `expr2javat`
84+
are subclasses of `expr2ct` rather than using this deferral
85+
mechanism; thus even when a Java expression is being printed
86+
there is still only one L in the stack, rather than
87+
expr2javat -> expr2ct -> C1 ... Cn -> X
88+
as one might expect. The language printers (in particular
89+
expr2ct) always assume they are at the top of the stack
90+
(so top_pretty_printer == this), and will need adapting if
91+
we want to e.g. place a custom printer *above* expr2ct
92+
in the future.
93+
94+
\*******************************************************************/
95+
5496
void register_global_pretty_printer(pretty_printer_factoryt new_printer)
5597
{
5698
registered_pretty_printers.push_back(new_printer);
5799
}
58100

101+
/*******************************************************************\
102+
103+
Function: get_pretty_printer_stack
104+
105+
Inputs: `ns`: namespace
106+
`language_printer`: pointer to instance of a pretty-printer
107+
that should be placed at the head of the printer stack.
108+
109+
Outputs:
110+
111+
Purpose: (See `register_global_pretty_printer` above for context)
112+
Takes a reference to language-specific pretty-printer L,
113+
instantiates custom printers C1 .. Cn if any have been
114+
registered, creates the fallback norep pretty-printer X,
115+
and sets their next_ and top_pretty_printer pointers.
116+
A vector of unique pointers is returned whose back member
117+
is the head of the stack (L, in the notation used above).
118+
119+
\*******************************************************************/
120+
59121
static std::vector<std::unique_ptr<pretty_printert>> get_pretty_printer_stack(
60122
const namespacet &ns,
61123
std::unique_ptr<pretty_printert> language_printer)
@@ -84,11 +146,17 @@ static std::vector<std::unique_ptr<pretty_printert>> get_pretty_printer_stack(
84146
85147
Function: from_expr
86148
87-
Inputs:
149+
Inputs: `ns`: global namespace
150+
`identifier`: symbol-table identifier associated with `expr`
151+
or the empty string if none
152+
`expr`: expression to print
88153
89-
Outputs:
154+
Outputs: Returns pretty-printed string equivalent of `expr`.
90155
91-
Purpose:
156+
Purpose: Pretty-prints an expression. If custom pretty-printers have
157+
been registered they will be instantiated and may take part
158+
in printing `expr`; see `register_global_pretty_printer` for
159+
details.
92160
93161
\*******************************************************************/
94162

@@ -106,11 +174,17 @@ std::string from_expr(
106174
107175
Function: from_type
108176
109-
Inputs:
177+
Inputs: `ns`: global namespace
178+
`identifier`: symbol-table identifier associated with `type`
179+
or the empty string if none
180+
`type`: type to print
110181
111-
Outputs:
182+
Outputs: Returns pretty-printed string equivalent of `type`.
112183
113-
Purpose:
184+
Purpose: Pretty-prints an type. If custom pretty-printers have
185+
been registered they will be instantiated and may take part
186+
in printing `type`; see `register_global_pretty_printer` for
187+
details.
114188
115189
\*******************************************************************/
116190

src/langapi/pretty_printer.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*******************************************************************\
22
3-
Module:
3+
Module: Generic expression / type pretty-printer
44
5-
Author: Daniel Kroening, [email protected]
5+
Author: Chris Smowton, [email protected]
66
77
\*******************************************************************/
88

@@ -18,6 +18,16 @@ Author: Daniel Kroening, [email protected]
1818

1919
#include <cassert>
2020

21+
/*
22+
* Interface to an expression and/or type pretty-printer.
23+
* The convert(...) routines should pretty-print the expressions
24+
* they know how to deal with, and defer to next_pretty_printer
25+
* for those they can't handle, and top_pretty_printer for
26+
* subexpressions (whether it understands them or not).
27+
* Per default they always defer.
28+
* See `util/language_util.h` and particularly
29+
* `register_global_pretty_printer` for detail on how these are used.
30+
*/
2131
class pretty_printert
2232
{
2333
public:

0 commit comments

Comments
 (0)