moonpool/dev/ocaml/Ast_mapper/index.html
2023-08-28 17:11:38 +00:00

75 lines
No EOL
34 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>Ast_mapper (ocaml.Ast_mapper)</title><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><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">ocaml</a> &#x00BB; Ast_mapper</nav><header class="odoc-preamble"><h1>Module <code><span>Ast_mapper</span></code></h1><p>The interface of a -ppx rewriter</p><p>A -ppx rewriter is a program that accepts a serialized abstract syntax tree and outputs another, possibly modified, abstract syntax tree. This module encapsulates the interface between the compiler and the -ppx rewriters, handling such details as the serialization format, forwarding of command-line flags, and storing state.</p><p><a href="#type-mapper"><code>mapper</code></a> enables AST rewriting using open recursion. A typical mapper would be based on <a href="#val-default_mapper"><code>default_mapper</code></a>, a deep identity mapper, and will fall back on it for handling the syntax it does not modify. For example:</p><pre class="language-ocaml"><code>open Asttypes
open Parsetree
open Ast_mapper
let test_mapper argv =
{ default_mapper with
expr = fun mapper expr -&gt;
match expr with
| { pexp_desc = Pexp_extension ({ txt = &quot;test&quot; }, PStr [])} -&gt;
Ast_helper.Exp.constant (Const_int 42)
| other -&gt; default_mapper.expr mapper other; }
let () =
register &quot;ppx_test&quot; test_mapper</code></pre><p>This -ppx rewriter, which replaces <code>[%test]</code> in expressions with the constant <code>42</code>, can be compiled using <code>ocamlc -o ppx_test -I +compiler-libs ocamlcommon.cma ppx_test.ml</code>.</p><p><b>Warning:</b> this module is unstable and part of <span class="xref-unresolved">compiler-libs</span>.</p></header><nav class="odoc-toc"><ul><li><a href="#a-generic-parsetree-mapper">A generic Parsetree mapper</a></li><li><a href="#apply-mappers-to-compilation-units">Apply mappers to compilation units</a></li><li><a href="#registration-api">Registration API</a></li><li><a href="#convenience-functions-to-write-mappers">Convenience functions to write mappers</a></li><li><a href="#helper-functions-to-call-external-mappers">Helper functions to call external mappers</a></li><li><a href="#cookies">Cookies</a></li></ul></nav><div class="odoc-content"><h2 id="a-generic-parsetree-mapper"><a href="#a-generic-parsetree-mapper" class="anchor"></a>A generic Parsetree mapper</h2><div class="odoc-spec"><div class="spec type anchored" id="type-mapper"><a href="#type-mapper" class="anchor"></a><code><span><span class="keyword">type</span> mapper</span><span> = </span><span>{</span></code><ol><li id="type-mapper.attribute" class="def record field anchored"><a href="#type-mapper.attribute" class="anchor"></a><code><span>attribute : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-attribute">Parsetree.attribute</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-attribute">Parsetree.attribute</a>;</span></code></li><li id="type-mapper.attributes" class="def record field anchored"><a href="#type-mapper.attributes" class="anchor"></a><code><span>attributes : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><span><a href="../Parsetree/index.html#type-attribute">Parsetree.attribute</a> list</span> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-attribute">Parsetree.attribute</a> list</span>;</span></code></li><li id="type-mapper.binding_op" class="def record field anchored"><a href="#type-mapper.binding_op" class="anchor"></a><code><span>binding_op : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-binding_op">Parsetree.binding_op</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-binding_op">Parsetree.binding_op</a>;</span></code></li><li id="type-mapper.case" class="def record field anchored"><a href="#type-mapper.case" class="anchor"></a><code><span>case : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-case">Parsetree.case</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-case">Parsetree.case</a>;</span></code></li><li id="type-mapper.cases" class="def record field anchored"><a href="#type-mapper.cases" class="anchor"></a><code><span>cases : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><span><a href="../Parsetree/index.html#type-case">Parsetree.case</a> list</span> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-case">Parsetree.case</a> list</span>;</span></code></li><li id="type-mapper.class_declaration" class="def record field anchored"><a href="#type-mapper.class_declaration" class="anchor"></a><code><span>class_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_declaration">Parsetree.class_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_declaration">Parsetree.class_declaration</a>;</span></code></li><li id="type-mapper.class_description" class="def record field anchored"><a href="#type-mapper.class_description" class="anchor"></a><code><span>class_description : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_description">Parsetree.class_description</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_description">Parsetree.class_description</a>;</span></code></li><li id="type-mapper.class_expr" class="def record field anchored"><a href="#type-mapper.class_expr" class="anchor"></a><code><span>class_expr : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-class_expr">Parsetree.class_expr</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-class_expr">Parsetree.class_expr</a>;</span></code></li><li id="type-mapper.class_field" class="def record field anchored"><a href="#type-mapper.class_field" class="anchor"></a><code><span>class_field : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-class_field">Parsetree.class_field</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-class_field">Parsetree.class_field</a>;</span></code></li><li id="type-mapper.class_signature" class="def record field anchored"><a href="#type-mapper.class_signature" class="anchor"></a><code><span>class_signature : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_signature">Parsetree.class_signature</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_signature">Parsetree.class_signature</a>;</span></code></li><li id="type-mapper.class_structure" class="def record field anchored"><a href="#type-mapper.class_structure" class="anchor"></a><code><span>class_structure : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_structure">Parsetree.class_structure</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_structure">Parsetree.class_structure</a>;</span></code></li><li id="type-mapper.class_type" class="def record field anchored"><a href="#type-mapper.class_type" class="anchor"></a><code><span>class_type : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-class_type">Parsetree.class_type</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-class_type">Parsetree.class_type</a>;</span></code></li><li id="type-mapper.class_type_declaration" class="def record field anchored"><a href="#type-mapper.class_type_declaration" class="anchor"></a><code><span>class_type_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_type_declaration">Parsetree.class_type_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_type_declaration">Parsetree.class_type_declaration</a>;</span></code></li><li id="type-mapper.class_type_field" class="def record field anchored"><a href="#type-mapper.class_type_field" class="anchor"></a><code><span>class_type_field : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-class_type_field">Parsetree.class_type_field</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-class_type_field">Parsetree.class_type_field</a>;</span></code></li><li id="type-mapper.constant" class="def record field anchored"><a href="#type-mapper.constant" class="anchor"></a><code><span>constant : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-constant">Parsetree.constant</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-constant">Parsetree.constant</a>;</span></code></li><li id="type-mapper.constructor_declaration" class="def record field anchored"><a href="#type-mapper.constructor_declaration" class="anchor"></a><code><span>constructor_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-constructor_declaration">Parsetree.constructor_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-constructor_declaration">Parsetree.constructor_declaration</a>;</span></code></li><li id="type-mapper.expr" class="def record field anchored"><a href="#type-mapper.expr" class="anchor"></a><code><span>expr : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-expression">Parsetree.expression</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-expression">Parsetree.expression</a>;</span></code></li><li id="type-mapper.extension" class="def record field anchored"><a href="#type-mapper.extension" class="anchor"></a><code><span>extension : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-extension">Parsetree.extension</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-extension">Parsetree.extension</a>;</span></code></li><li id="type-mapper.extension_constructor" class="def record field anchored"><a href="#type-mapper.extension_constructor" class="anchor"></a><code><span>extension_constructor : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-extension_constructor">Parsetree.extension_constructor</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-extension_constructor">Parsetree.extension_constructor</a>;</span></code></li><li id="type-mapper.include_declaration" class="def record field anchored"><a href="#type-mapper.include_declaration" class="anchor"></a><code><span>include_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-include_declaration">Parsetree.include_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-include_declaration">Parsetree.include_declaration</a>;</span></code></li><li id="type-mapper.include_description" class="def record field anchored"><a href="#type-mapper.include_description" class="anchor"></a><code><span>include_description : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-include_description">Parsetree.include_description</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-include_description">Parsetree.include_description</a>;</span></code></li><li id="type-mapper.label_declaration" class="def record field anchored"><a href="#type-mapper.label_declaration" class="anchor"></a><code><span>label_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-label_declaration">Parsetree.label_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-label_declaration">Parsetree.label_declaration</a>;</span></code></li><li id="type-mapper.location" class="def record field anchored"><a href="#type-mapper.location" class="anchor"></a><code><span>location : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Location/index.html#type-t">Location.t</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Location/index.html#type-t">Location.t</a>;</span></code></li><li id="type-mapper.module_binding" class="def record field anchored"><a href="#type-mapper.module_binding" class="anchor"></a><code><span>module_binding : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-module_binding">Parsetree.module_binding</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-module_binding">Parsetree.module_binding</a>;</span></code></li><li id="type-mapper.module_declaration" class="def record field anchored"><a href="#type-mapper.module_declaration" class="anchor"></a><code><span>module_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-module_declaration">Parsetree.module_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-module_declaration">Parsetree.module_declaration</a>;</span></code></li><li id="type-mapper.module_substitution" class="def record field anchored"><a href="#type-mapper.module_substitution" class="anchor"></a><code><span>module_substitution : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-module_substitution">Parsetree.module_substitution</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-module_substitution">Parsetree.module_substitution</a>;</span></code></li><li id="type-mapper.module_expr" class="def record field anchored"><a href="#type-mapper.module_expr" class="anchor"></a><code><span>module_expr : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-module_expr">Parsetree.module_expr</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-module_expr">Parsetree.module_expr</a>;</span></code></li><li id="type-mapper.module_type" class="def record field anchored"><a href="#type-mapper.module_type" class="anchor"></a><code><span>module_type : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-module_type">Parsetree.module_type</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-module_type">Parsetree.module_type</a>;</span></code></li><li id="type-mapper.module_type_declaration" class="def record field anchored"><a href="#type-mapper.module_type_declaration" class="anchor"></a><code><span>module_type_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-module_type_declaration">Parsetree.module_type_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-module_type_declaration">Parsetree.module_type_declaration</a>;</span></code></li><li id="type-mapper.open_declaration" class="def record field anchored"><a href="#type-mapper.open_declaration" class="anchor"></a><code><span>open_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-open_declaration">Parsetree.open_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-open_declaration">Parsetree.open_declaration</a>;</span></code></li><li id="type-mapper.open_description" class="def record field anchored"><a href="#type-mapper.open_description" class="anchor"></a><code><span>open_description : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-open_description">Parsetree.open_description</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-open_description">Parsetree.open_description</a>;</span></code></li><li id="type-mapper.pat" class="def record field anchored"><a href="#type-mapper.pat" class="anchor"></a><code><span>pat : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-pattern">Parsetree.pattern</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-pattern">Parsetree.pattern</a>;</span></code></li><li id="type-mapper.payload" class="def record field anchored"><a href="#type-mapper.payload" class="anchor"></a><code><span>payload : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-payload">Parsetree.payload</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-payload">Parsetree.payload</a>;</span></code></li><li id="type-mapper.signature" class="def record field anchored"><a href="#type-mapper.signature" class="anchor"></a><code><span>signature : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-signature">Parsetree.signature</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-signature">Parsetree.signature</a>;</span></code></li><li id="type-mapper.signature_item" class="def record field anchored"><a href="#type-mapper.signature_item" class="anchor"></a><code><span>signature_item : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-signature_item">Parsetree.signature_item</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-signature_item">Parsetree.signature_item</a>;</span></code></li><li id="type-mapper.structure" class="def record field anchored"><a href="#type-mapper.structure" class="anchor"></a><code><span>structure : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-structure">Parsetree.structure</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-structure">Parsetree.structure</a>;</span></code></li><li id="type-mapper.structure_item" class="def record field anchored"><a href="#type-mapper.structure_item" class="anchor"></a><code><span>structure_item : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-structure_item">Parsetree.structure_item</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-structure_item">Parsetree.structure_item</a>;</span></code></li><li id="type-mapper.typ" class="def record field anchored"><a href="#type-mapper.typ" class="anchor"></a><code><span>typ : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-core_type">Parsetree.core_type</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-core_type">Parsetree.core_type</a>;</span></code></li><li id="type-mapper.type_declaration" class="def record field anchored"><a href="#type-mapper.type_declaration" class="anchor"></a><code><span>type_declaration : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-type_declaration">Parsetree.type_declaration</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-type_declaration">Parsetree.type_declaration</a>;</span></code></li><li id="type-mapper.type_extension" class="def record field anchored"><a href="#type-mapper.type_extension" class="anchor"></a><code><span>type_extension : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-type_extension">Parsetree.type_extension</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-type_extension">Parsetree.type_extension</a>;</span></code></li><li id="type-mapper.type_exception" class="def record field anchored"><a href="#type-mapper.type_exception" class="anchor"></a><code><span>type_exception : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-type_exception">Parsetree.type_exception</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-type_exception">Parsetree.type_exception</a>;</span></code></li><li id="type-mapper.type_kind" class="def record field anchored"><a href="#type-mapper.type_kind" class="anchor"></a><code><span>type_kind : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-type_kind">Parsetree.type_kind</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-type_kind">Parsetree.type_kind</a>;</span></code></li><li id="type-mapper.value_binding" class="def record field anchored"><a href="#type-mapper.value_binding" class="anchor"></a><code><span>value_binding : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-value_binding">Parsetree.value_binding</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-value_binding">Parsetree.value_binding</a>;</span></code></li><li id="type-mapper.value_description" class="def record field anchored"><a href="#type-mapper.value_description" class="anchor"></a><code><span>value_description : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-value_description">Parsetree.value_description</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-value_description">Parsetree.value_description</a>;</span></code></li><li id="type-mapper.with_constraint" class="def record field anchored"><a href="#type-mapper.with_constraint" class="anchor"></a><code><span>with_constraint : <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-with_constraint">Parsetree.with_constraint</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-with_constraint">Parsetree.with_constraint</a>;</span></code></li></ol><code><span>}</span></code></div><div class="spec-doc"><p>A mapper record implements one &quot;method&quot; per syntactic category, using an open recursion style: each method takes as its first argument the mapper to be applied to children in the syntax tree.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-default_mapper"><a href="#val-default_mapper" class="anchor"></a><code><span><span class="keyword">val</span> default_mapper : <a href="#type-mapper">mapper</a></span></code></div><div class="spec-doc"><p>A default mapper, which implements a &quot;deep identity&quot; mapping.</p></div></div><h2 id="apply-mappers-to-compilation-units"><a href="#apply-mappers-to-compilation-units" class="anchor"></a>Apply mappers to compilation units</h2><div class="odoc-spec"><div class="spec value anchored" id="val-tool_name"><a href="#val-tool_name" class="anchor"></a><code><span><span class="keyword">val</span> tool_name : <span>unit <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p>Can be used within a ppx preprocessor to know which tool is calling it <code>&quot;ocamlc&quot;</code>, <code>&quot;ocamlopt&quot;</code>, <code>&quot;ocamldoc&quot;</code>, <code>&quot;ocamldep&quot;</code>, <code>&quot;ocaml&quot;</code>, ... Some global variables that reflect command-line options are automatically synchronized between the calling tool and the ppx preprocessor: <a href="../Clflags/index.html#val-include_dirs"><code>Clflags.include_dirs</code></a>, <a href="../Load_path/index.html"><code>Load_path</code></a>, <a href="../Clflags/index.html#val-open_modules"><code>Clflags.open_modules</code></a>, <a href="../Clflags/index.html#val-for_package"><code>Clflags.for_package</code></a>, <a href="../Clflags/index.html#val-debug"><code>Clflags.debug</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-apply"><a href="#val-apply" class="anchor"></a><code><span><span class="keyword">val</span> apply : <span>source:string <span class="arrow">&#45;&gt;</span></span> <span>target:string <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-mapper">mapper</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Apply a mapper (parametrized by the unit name) to a dumped parsetree found in the <code>source</code> file and put the result in the <code>target</code> file. The <code>structure</code> or <code>signature</code> field of the mapper is applied to the implementation or interface.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-run_main"><a href="#val-run_main" class="anchor"></a><code><span><span class="keyword">val</span> run_main : <span><span>(<span><span>string list</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-mapper">mapper</a>)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Entry point to call to implement a standalone -ppx rewriter from a mapper, parametrized by the command line arguments. The current unit name can be obtained from <a href="../Location/index.html#val-input_name"><code>Location.input_name</code></a>. This function implements proper error reporting for uncaught exceptions.</p></div></div><h2 id="registration-api"><a href="#registration-api" class="anchor"></a>Registration API</h2><div class="odoc-spec"><div class="spec value anchored" id="val-register_function"><a href="#val-register_function" class="anchor"></a><code><span><span class="keyword">val</span> register_function : <span><span>(<span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span>string list</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-mapper">mapper</a>)</span> <span class="arrow">&#45;&gt;</span></span> unit)</span> <a href="../Stdlib/index.html#type-ref">ref</a></span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-register"><a href="#val-register" class="anchor"></a><code><span><span class="keyword">val</span> register : <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><span>string list</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-mapper">mapper</a>)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Apply the <code>register_function</code>. The default behavior is to run the mapper immediately, taking arguments from the process command line. This is to support a scenario where a mapper is linked as a stand-alone executable.</p><p>It is possible to overwrite the <code>register_function</code> to define &quot;-ppx drivers&quot;, which combine several mappers in a single process. Typically, a driver starts by defining <code>register_function</code> to a custom implementation, then lets ppx rewriters (linked statically or dynamically) register themselves, and then run all or some of them. It is also possible to have -ppx drivers apply rewriters to only specific parts of an AST.</p><p>The first argument to <code>register</code> is a symbolic name to be used by the ppx driver.</p></div></div><h2 id="convenience-functions-to-write-mappers"><a href="#convenience-functions-to-write-mappers" class="anchor"></a>Convenience functions to write mappers</h2><div class="odoc-spec"><div class="spec value anchored" id="val-map_opt"><a href="#val-map_opt" class="anchor"></a><code><span><span class="keyword">val</span> map_opt : <span><span>(<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span><span><span class="type-var">'a</span> option</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> option</span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-extension_of_error"><a href="#val-extension_of_error" class="anchor"></a><code><span><span class="keyword">val</span> extension_of_error : <span><a href="../Location/index.html#type-error">Location.error</a> <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-extension">Parsetree.extension</a></span></code></div><div class="spec-doc"><p>Encode an error into an 'ocaml.error' extension node which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the error.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-attribute_of_warning"><a href="#val-attribute_of_warning" class="anchor"></a><code><span><span class="keyword">val</span> attribute_of_warning : <span><a href="../Location/index.html#type-t">Location.t</a> <span class="arrow">&#45;&gt;</span></span> <span>string <span class="arrow">&#45;&gt;</span></span> <a href="../Parsetree/index.html#type-attribute">Parsetree.attribute</a></span></code></div><div class="spec-doc"><p>Encode a warning message into an 'ocaml.ppwarning' attribute which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the warning.</p></div></div><h2 id="helper-functions-to-call-external-mappers"><a href="#helper-functions-to-call-external-mappers" class="anchor"></a>Helper functions to call external mappers</h2><div class="odoc-spec"><div class="spec value anchored" id="val-add_ppx_context_str"><a href="#val-add_ppx_context_str" class="anchor"></a><code><span><span class="keyword">val</span> add_ppx_context_str :
<span>tool_name:string <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-structure">Parsetree.structure</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-structure">Parsetree.structure</a></span></code></div><div class="spec-doc"><p>Extract information from the current environment and encode it into an attribute which is prepended to the list of structure items in order to pass the information to an external processor.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-add_ppx_context_sig"><a href="#val-add_ppx_context_sig" class="anchor"></a><code><span><span class="keyword">val</span> add_ppx_context_sig :
<span>tool_name:string <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-signature">Parsetree.signature</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-signature">Parsetree.signature</a></span></code></div><div class="spec-doc"><p>Same as <code>add_ppx_context_str</code>, but for signatures.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-drop_ppx_context_str"><a href="#val-drop_ppx_context_str" class="anchor"></a><code><span><span class="keyword">val</span> drop_ppx_context_str :
<span>restore:bool <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-structure">Parsetree.structure</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-structure">Parsetree.structure</a></span></code></div><div class="spec-doc"><p>Drop the ocaml.ppx.context attribute from a structure. If <code>restore</code> is true, also restore the associated data in the current process.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-drop_ppx_context_sig"><a href="#val-drop_ppx_context_sig" class="anchor"></a><code><span><span class="keyword">val</span> drop_ppx_context_sig :
<span>restore:bool <span class="arrow">&#45;&gt;</span></span>
<span><a href="../Parsetree/index.html#type-signature">Parsetree.signature</a> <span class="arrow">&#45;&gt;</span></span>
<a href="../Parsetree/index.html#type-signature">Parsetree.signature</a></span></code></div><div class="spec-doc"><p>Same as <code>drop_ppx_context_str</code>, but for signatures.</p></div></div><h2 id="cookies"><a href="#cookies" class="anchor"></a>Cookies</h2><p>Cookies are used to pass information from a ppx processor to a further invocation of itself, when called from the OCaml toplevel (or other tools that support cookies).</p><div class="odoc-spec"><div class="spec value anchored" id="val-set_cookie"><a href="#val-set_cookie" class="anchor"></a><code><span><span class="keyword">val</span> set_cookie : <span>string <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-expression">Parsetree.expression</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get_cookie"><a href="#val-get_cookie" class="anchor"></a><code><span><span class="keyword">val</span> get_cookie : <span>string <span class="arrow">&#45;&gt;</span></span> <span><a href="../Parsetree/index.html#type-expression">Parsetree.expression</a> option</span></span></code></div></div></div></body></html>