You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/writing-ppxs.mld
+16-10
Original file line number
Diff line number
Diff line change
@@ -102,7 +102,7 @@ See below for examples on when the above name and context will trigger rewriting
102
102
let _ = match () with [%add_suffix "payload"] -> () (* wrong context *)
103
103
]}
104
104
105
-
{3:extender-payload The Payload Extraction}
105
+
{3 The Payload Extraction}
106
106
107
107
An extension node contains a {{!Ppxlib.Parsetree.payload}[payload]}, which will be passed to the transformation function. However, while this payload contains all information, it is not always structured the best way for the transformation function. For instance, in [[%add_suffix "payload"]], the string ["payload"] is encoded as a structure item consisting of an expression’s evaluation, a constant that is a string.
108
108
@@ -137,8 +137,7 @@ Building and inspecting AST nodes can be painful due to how
137
137
modules to ease this generation, such as {{!Ppxlib.Ast_builder}[Ast_builder]},
138
138
{!Ppxlib_metaquot}, {{!Ppxlib.Ast_pattern}[Ast_pattern]}, and {{!Ppxlib.Ast_traverse}[Ast_traverse]}, which are
139
139
explained in their own chapters: {{!"generating-code"}Generating AST nodes},
140
-
{{!"matching-code"}Destructing AST nodes} and {{!"ast-traversal"}Traversing AST
141
-
nodes}.
140
+
{{!"matching-code"}Destructing AST nodes} and {{!"ast-traversal"}Traversing AST nodes}.
142
141
143
142
In the example below, you can ignore the body of the function until reading
144
143
those chapters.
@@ -360,11 +359,17 @@ in both structures and signatures.
360
359
361
360
{2 Attribute-guided Rewriting}
362
361
363
-
There are rules that apply to AST nodes that, like derivers, apply based on their
362
+
[ppxlib] provides context-free rules that, like derivers, apply to nodes based on their
364
363
attributes but, like extenders, allow rewriting the entire AST node. These provide
365
364
lighter-weight syntax than extenders but that also means it's less obvious that they're
366
365
rewriting the syntax tree.
367
366
367
+
Before using this kind of rule, carefully consider using an extender instead. [ppxlib]
368
+
provides an opinionated syntax for preprocessors so that it's easy for users to understand
369
+
what code is being affected by the PPX. In general, these should only be used to slightly
370
+
modify the node the attribute is attached to, rather than rewrite it to something new.
371
+
The syntax of extenders highlights to users where more involved rewriting is taking place.
372
+
368
373
These are composed of:
369
374
- The name of the rewrite rule
370
375
- The list of attributes they define
@@ -379,8 +384,8 @@ A given rewrite rule can have multiple attributes that trigger it, if any of the
379
384
attributes are present on a single node then the rule is triggered and provided with the
380
385
AST node along with the payload of all the attributes registered by this rule. To declare
381
386
attributes use the {{!Ppxlib.Attribute.declare}[Attribute.declare]} function (or the other
382
-
similar functions in that module). Note that the [Context.t] must match the type of AST
383
-
nodes that the rule will apply to.
387
+
similar functions in that module). Note that the {{!Ppxlib.Attribute.Context}[Context.t]}
388
+
must match the type of AST nodes that the rule will apply to.
384
389
385
390
{@ocaml[
386
391
# let prefix_attr = Attribute.declare "example.prefix" Expression
@@ -394,7 +399,7 @@ nodes that the rule will apply to.
394
399
{3 The Expand Function}
395
400
396
401
The expand function takes the AST node (with this rule's attributes already stripped) and
397
-
the payloads of all the declared attributes (as a list of [payload option]s to allow for
402
+
the payloads of all the declared attributes (as a list of [payload option] to allow for
398
403
attributes that haven't been included).
399
404
400
405
{@ocaml[
@@ -419,9 +424,10 @@ attributes that haven't been included).
419
424
{3 Creating a rewriting rule}
420
425
421
426
Finally, we can create the rule using the appropriate
422
-
{{!Ppxlib.Extension.Context}[Ppxlib.Extension.Context]} and register it with the driver.
423
-
There's also a {{!Ppxlib.Context_free.attr_replace}[Context_free.attr_replace]} function
424
-
with a slightly simpler API if you only use a single attribute.
427
+
{{!Ppxlib.Extension.Context}[Ppxlib.Extension.Context]} and register it with the driver using
0 commit comments