erl_prettypr(3)
π erl_prettypr β Erlang Module Definition (the erl_prettypr(3) page, bestie edition) π
okay so hi. this is erl_prettypr, and yes it's an Erlang module, and yes it's the one that makes your ugly abstract syntax trees look β¨prettyβ¨. it's literally called "pretty printing" and honestly? it ate.
πΈ NAME πΈ
erl_prettypr β pretty printing of abstract Erlang syntax trees. that's it, that's the whole job, and she does it flawlessly. π
π DESCRIPTION π
so bestie, this module is basically a front end to the pretty-printing library module prettypr, and its whole thing is text formatting of abstract syntax trees β the ones defined by the module erl_syntax. so like, erl_syntax builds the tree, and erl_prettypr dresses it up for the group chat. iconic division of labour, no cap. π¦
π§ DATA TYPES π§
clause_t()β it's one of these, exactly these:
case_expr | fun_expr | if_expr | maybe_expr | receive_expr |
try_expr |
{function, prettypr:document()} |
spec.
context()β defined as#ctxt{}. the vibes container, essentially.hook()β defined as:
none | fun((syntaxTree(), _, _) -> prettypr:document()).
syntaxTree()β defined aserl_syntax:syntaxTree(), and like, that's an abstract syntax tree. see theerl_syntaxmodule for details, babe. π«Ά
π₯ FUNCTIONS π₯
best(syntaxTree(), [term()]) -> empty | prettypr:document().
Creates a fixed "best" abstract layout for a syntax tree. π
So this is basically layout/2's sibling, except that here, the final layout has already been selected with respect to the given options. and if no such layout could be produced? you get the atom empty. rip. π for info on the options, go look at the format/2 function.
See also: best/1, format/2, layout/2, prettypr:best/3.
format(syntaxTree(), [term()]) -> string().
Prettyprint-formats an abstract Erlang syntax tree as text. this is the one you'll actually call, fr fr. β¨
for example, if you have a .beam file that has been compiled with debug_info, the following should print the source code for the module (as it looks in the debug info representation):
{ok,{_,[{abstract_code,{_,AC}}]}} =
beam_lib:chunks("myfile.beam",[abstract_code]),
io:put_chars(erl_prettypr:format(erl_syntax:form_list(AC)))
Available options, and bestie I need you to read all of these, no skimming:
{hook, none | hook()}β unless the value isnone, the given function is called for each node whose list of annotations is not empty. the default value isnone.{paper, integer()}β specifies the preferred maximum number of characters on any line, including indentation. the default value is80.{ribbon, integer()}β specifies the preferred maximum number of characters on any line, not counting indentation. the default value is65.{user, term()}β user-specific data for use in hook functions. the default value isundefined.{encoding, epp:source_encoding()}β specifies the encoding of the generated file.
so a hook function (see the hook() type) gets passed the current syntax tree node, the context, and a continuation. and you can poke at / rearrange that context with functions like get_ctxt_user/1 and set_ctxt_user/2. the hook must return a "document" data structure (see layout/2 and best/2) β and you can build that thing partly or entirely by applying the continuation function. π
for example, here's a trivial hook:
fun (Node, Ctxt, Cont) -> Cont(Node, Ctxt) end
which yields the same result as if no hook was given at all. basically the "i have nothing to add" hook. π
the following one, however:
fun (Node, Ctxt, Cont) ->
Doc = Cont(Node, Ctxt),
prettypr:beside(prettypr:text("<b>"),
prettypr:beside(Doc,
prettypr:text("</b>")))
end
will place the text of any annotated node (regardless of the annotation data) between HTML "boldface begin" and "boldface end" tags. slay, honestly. π
See also: erl_syntax, best/2, format/1, get_ctxt_user/1, layout/2, set_ctxt_user/2.
get_ctxt_hook(context()) -> hook().
Returns the hook function field of the prettyprinter context. the little peak behind the curtain. π
See also: set_ctxt_hook/2.
get_ctxt_linewidth(context()) -> integer().
Returns the line width field of the prettyprinter context.
See also: set_ctxt_linewidth/2.
get_ctxt_paperwidth(context()) -> integer().
Returns the paper width field of the prettyprinter context.
See also: set_ctxt_paperwidth/2.
get_ctxt_precedence(context()) -> integer().
Returns the operator precedence field of the prettyprinter context.
See also: set_ctxt_precedence/2.
get_ctxt_user(context()) -> term().
Returns the user data field of the prettyprinter context.
See also: set_ctxt_user/2.
layout(syntaxTree(), [term()]) -> prettypr:document().
Creates an abstract document layout for a syntax tree. πΈ
the result represents a set of possible layouts (see module prettypr). for info on the options, check format/2 β BUT, heads up bestie, note that the paper and ribbon options are ignored by this function. so don't come for me when they do nothing here. π
this function is a low-level interface to the pretty printer, returning a flexible representation of possible layouts, independent of the paper width that eventually gets used for formatting. you can include it as part of another document and/or further process it directly with the functions in the prettypr module, or use it in a hook function (see format/2 for details).
See also: prettypr, format/2, layout/1.
set_ctxt_hook(context(), hook()) -> context().
Updates the hook function field of the prettyprinter context.
See also: get_ctxt_hook/1.
set_ctxt_linewidth(context(), integer()) -> context().
Updates the line width field of the prettyprinter context.
NOTE: changing this value (and passing the resulting context to a continuation function) does not affect the normal formatting, but it may affect user-defined behaviour in hook functions. so like, don't be shocked when nothing visibly changes. π
See also: get_ctxt_linewidth/1.
set_ctxt_paperwidth(context(), integer()) -> context().
Updates the paper width field of the prettyprinter context.
NOTE: changing this value (and passing the resulting context to a continuation function) does not affect the normal formatting, but it may affect user-defined behaviour in hook functions. same energy as above, bestie. π
See also: get_ctxt_paperwidth/1.
set_ctxt_precedence(context(), integer()) -> context().
Updates the operator precedence field of the prettyprinter context.
see the //stdlib/erl_parse module for operator precedences. iykyk β
See also: //stdlib/erl_parse, get_ctxt_precedence/1.
set_ctxt_user(context(), term()) -> context().
Updates the user data field of the prettyprinter context.
See also: get_ctxt_user/1.
and that's the whole page, bestie, you're so welcome. this came to you from Ericsson AB, it's the syntax_tools 4.1 era of the docs, and it's the erl_prettypr(3) page β go forth and pretty-print something gorgeous. πβ¨ππ¦πΈπ
π«Άππ
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms