ocaml-trace/ppxlib/Ppxlib/Pp_ast/index.html
2025-12-12 13:56:43 +00:00

28 lines
15 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Pp_ast (ppxlib.Ppxlib.Pp_ast)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 3.1.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../../index.html">Index</a> &#x00BB; <a href="../../index.html">ppxlib</a> &#x00BB; <a href="../index.html">Ppxlib</a> &#x00BB; Pp_ast</nav><header class="odoc-preamble"><h1>Module <code><span>Ppxlib.Pp_ast</span></code></h1><p>This module implements pretty printers for the OCaml AST's version used by ppxlib.</p><p>Those pretty printers show the AST as its OCaml representation and do not pretty print the corresponding source code. For printing ASTs as source code use the <a href="../../Astlib/Pprintast/index.html"><code>Ppxlib.Pprintast</code></a> module instead.</p><p>For example, calling <code>Pp_ast.expression Format.std_formatter [%expr x + 2]</code> will print:</p><pre>Pexp_apply
( Pexp_ident (Lident &quot;+&quot;)
, [ ( Nolabel, Pexp_ident (Lident &quot;x&quot;))
; ( Nolabel, Pexp_constant (Pconst_integer ( &quot;2&quot;, None)))
]
)</pre><p>There is also a way to customise the output format with the optional printer argument. For example, calling the following:</p><pre>Pp_ast.expression
Format.std_formatter
?printer:(fun fmt repr -&gt; Yojson.Basic.pretty_print fmt (repr_to_yojson repr))
[%expr x + 2]</pre><p>Will print:</p><pre>[
{
&quot;Pstr_eval&quot;: [
{
&quot;Pexp_apply&quot;: [
{ &quot;Pexp_ident&quot;: { &quot;Lident&quot;: &quot;+&quot; } },
[
[ &quot;Nolabel&quot;, { &quot;Pexp_ident&quot;: { &quot;Lident&quot;: &quot;x&quot; } } ],
[
&quot;Nolabel&quot;,
{ &quot;Pexp_constant&quot;: { &quot;Pconst_integer&quot;: [ &quot;2&quot;, &quot;None&quot; ] } }
]
]
]
},
&quot;__attrs&quot;
]
}
]</pre><p>To keep the output easily readable, records with <code>_desc</code> fields such as <a href="../../Ppxlib_ast/Ast/index.html#type-expression"><code>Ppxlib.Ast.expression</code></a> or <a href="../../Ppxlib_ast/Ast/index.html#type-pattern"><code>Ppxlib.Ast.pattern</code></a> are not printed as such and only the value of the corresponding <code>_desc</code> field is printed instead. This prevents AST nodes metadata, such as locations or attributes, from polluting the output, keeping it relatively concise and clean. The same goes for <a href="../Location/index.html#type-loc"><code>Location.loc</code></a> values which are printed as the value of their <code>txt</code> field.</p><p><a href="../Location/index.html#type-t"><code>Location.t</code></a> and <a href="../../Ppxlib_ast/Ast/index.html#type-attributes"><code>Ppxlib.Ast.attributes</code></a> are not displayed by default even outside of the records mentioned above.</p><p>The <a href="Config/index.html"><code>Config</code></a> module below allows to override part or all of this behaviour. When configured to display locations or attributes, the entire record will be displayed, not only its <code>_desc</code> field.</p></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-repr"><a href="#type-repr" class="anchor"></a><code><span><span class="keyword">type</span> repr</span><span> = </span></code><ol><li id="type-repr.Unit" class="def variant constructor anchored"><a href="#type-repr.Unit" class="anchor"></a><code><span>| </span><span><span class="constructor">Unit</span></span></code></li><li id="type-repr.Int" class="def variant constructor anchored"><a href="#type-repr.Int" class="anchor"></a><code><span>| </span><span><span class="constructor">Int</span> <span class="keyword">of</span> int</span></code></li><li id="type-repr.String" class="def variant constructor anchored"><a href="#type-repr.String" class="anchor"></a><code><span>| </span><span><span class="constructor">String</span> <span class="keyword">of</span> string</span></code></li><li id="type-repr.Bool" class="def variant constructor anchored"><a href="#type-repr.Bool" class="anchor"></a><code><span>| </span><span><span class="constructor">Bool</span> <span class="keyword">of</span> bool</span></code></li><li id="type-repr.Char" class="def variant constructor anchored"><a href="#type-repr.Char" class="anchor"></a><code><span>| </span><span><span class="constructor">Char</span> <span class="keyword">of</span> char</span></code></li><li id="type-repr.Array" class="def variant constructor anchored"><a href="#type-repr.Array" class="anchor"></a><code><span>| </span><span><span class="constructor">Array</span> <span class="keyword">of</span> <span><a href="#type-repr">repr</a> list</span></span></code></li><li id="type-repr.Float" class="def variant constructor anchored"><a href="#type-repr.Float" class="anchor"></a><code><span>| </span><span><span class="constructor">Float</span> <span class="keyword">of</span> float</span></code></li><li id="type-repr.Int32" class="def variant constructor anchored"><a href="#type-repr.Int32" class="anchor"></a><code><span>| </span><span><span class="constructor">Int32</span> <span class="keyword">of</span> int32</span></code></li><li id="type-repr.Int64" class="def variant constructor anchored"><a href="#type-repr.Int64" class="anchor"></a><code><span>| </span><span><span class="constructor">Int64</span> <span class="keyword">of</span> int64</span></code></li><li id="type-repr.Nativeint" class="def variant constructor anchored"><a href="#type-repr.Nativeint" class="anchor"></a><code><span>| </span><span><span class="constructor">Nativeint</span> <span class="keyword">of</span> nativeint</span></code></li><li id="type-repr.Record" class="def variant constructor anchored"><a href="#type-repr.Record" class="anchor"></a><code><span>| </span><span><span class="constructor">Record</span> <span class="keyword">of</span> <span><span>(string * <a href="#type-repr">repr</a>)</span> list</span></span></code></li><li id="type-repr.Constr" class="def variant constructor anchored"><a href="#type-repr.Constr" class="anchor"></a><code><span>| </span><span><span class="constructor">Constr</span> <span class="keyword">of</span> string * <span><a href="#type-repr">repr</a> list</span></span></code></li><li id="type-repr.Tuple" class="def variant constructor anchored"><a href="#type-repr.Tuple" class="anchor"></a><code><span>| </span><span><span class="constructor">Tuple</span> <span class="keyword">of</span> <span><a href="#type-repr">repr</a> list</span></span></code></li><li id="type-repr.List" class="def variant constructor anchored"><a href="#type-repr.List" class="anchor"></a><code><span>| </span><span><span class="constructor">List</span> <span class="keyword">of</span> <span><a href="#type-repr">repr</a> list</span></span></code></li><li id="type-repr.Special" class="def variant constructor anchored"><a href="#type-repr.Special" class="anchor"></a><code><span>| </span><span><span class="constructor">Special</span> <span class="keyword">of</span> string</span></code></li></ol></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-pp"><a href="#type-pp" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a pp</span></span><span> = <span><a href="../../../ocaml/Stdlib/Format/index.html#type-formatter">Stdlib.Format.formatter</a> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Config"><a href="#module-Config" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Config/index.html">Config</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-configurable"><a href="#type-configurable" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a configurable</span></span><span> = <span><span class="optlabel">?config</span>:<a href="Config/index.html#type-t">Config.t</a> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <a href="#type-pp">pp</a></span></span></code></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-configured"><a href="#type-configured" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a configured</span></span><span> = <span><span class="type-var">'a</span> <a href="#type-pp">pp</a></span></span></code></div></div><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-S"><a href="#module-type-S" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S/index.html">S</a></span><span> = <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-Conf"><a href="#module-type-Conf" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-Conf/index.html">Conf</a></span><span> = <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-Configured"><a href="#module-type-Configured" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-Configured/index.html">Configured</a></span><span> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span><span class="keyword">type</span> <span>'a <a href="module-type-S/index.html#type-printer">printer</a></span> = <span><span class="type-var">'a</span> <a href="#type-configured">configured</a></span></span></span></code></div></div><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-Configurable"><a href="#module-type-Configurable" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-Configurable/index.html">Configurable</a></span><span> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span><span class="keyword">type</span> <span>'a <a href="module-type-S/index.html#type-printer">printer</a></span> = <span><span class="type-var">'a</span> <a href="#type-configurable">configurable</a></span></span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Make"><a href="#module-Make" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Make/index.html">Make</a></span><span> (<a href="Make/argument-1-Conf/index.html">Conf</a> : <a href="module-type-Conf/index.html">Conf</a>) : <a href="module-type-Configured/index.html">Configured</a></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-make"><a href="#val-make" class="anchor"></a><code><span><span class="keyword">val</span> make : <span><a href="Config/index.html#type-t">Config.t</a> <span class="arrow">&#45;&gt;</span></span> <span>(<span class="keyword">module</span> <a href="module-type-Configured/index.html">Configured</a>)</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Default"><a href="#module-Default" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Default/index.html">Default</a></span><span> : <a href="module-type-Configured/index.html">Configured</a></span></code></div></div><div class="odoc-include"><details open="open"><summary class="spec include"><code><span><span class="keyword">include</span> <a href="module-type-Configurable/index.html">Configurable</a></span></code></summary><div class="odoc-spec"><div class="spec type anchored" id="type-printer"><a href="#type-printer" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a printer</span></span><span> = <span><span class="type-var">'a</span> <a href="#type-configurable">configurable</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-structure"><a href="#val-structure" class="anchor"></a><code><span><span class="keyword">val</span> structure : <span><span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-structure_item">Astlib.Ast_502.Parsetree.structure_item</a> list</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-structure_item"><a href="#val-structure_item" class="anchor"></a><code><span><span class="keyword">val</span> structure_item : <span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-structure_item">Astlib.Ast_502.Parsetree.structure_item</a> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-signature"><a href="#val-signature" class="anchor"></a><code><span><span class="keyword">val</span> signature : <span><span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-signature_item">Astlib.Ast_502.Parsetree.signature_item</a> list</span> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-signature_item"><a href="#val-signature_item" class="anchor"></a><code><span><span class="keyword">val</span> signature_item : <span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-signature_item">Astlib.Ast_502.Parsetree.signature_item</a> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-expression"><a href="#val-expression" class="anchor"></a><code><span><span class="keyword">val</span> expression : <span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-expression">Astlib.Ast_502.Parsetree.expression</a> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pattern"><a href="#val-pattern" class="anchor"></a><code><span><span class="keyword">val</span> pattern : <span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-pattern">Astlib.Ast_502.Parsetree.pattern</a> <a href="#type-printer">printer</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-core_type"><a href="#val-core_type" class="anchor"></a><code><span><span class="keyword">val</span> core_type : <span><a href="../../Astlib/Ast_502/Parsetree/index.html#type-core_type">Astlib.Ast_502.Parsetree.core_type</a> <a href="#type-printer">printer</a></span></span></code></div></div></details></div></div></body></html>