# Match context in PPX rewriter

**URL:** <https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714>\
**Category:** Learning\
**Tags:** ppxlib\
**Created:** [April 21, 2021, 9:12am UTC](https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714 "2021-04-21T09:12:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vch9](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vch9/32/2885_2.png) [@vch9](https://discuss.ocaml.org/u/vch9)\
**Post date:** [April 21, 2021, 9:12am UTC](https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714/1 "2021-04-21T09:12:42Z")

</div>

Hello everyone,

I am currently working on a ppx rewriter, my rule is attached to structured item. I was wondering if there was a way to retrieve information on that context ?

```ocaml
let expand ~ctxt e =
  ..

let my_rule =
  Extension.V3.declare_inline "my_ppx"
  Extension.Context.structure_item
  <payload>
  expand

```

where `ctxt` in `expand` has type:

```ocaml
type Expansion_Context.Extension.t =
 { extension_point_loc : Location.t ; base : Base.t }

(* with Base.t *)
type Base.t =
    { tool_name : string ;
      code_path : Code_path.t ; 
      input_name : string }

```

In a example:

```ocaml
let add x y = x + y
[%%my_pbt <payload>]

```

My goal would be to have access to fields such as `[add x y]` or `[x + y]`. But I have no idea at this point if it’s possible ?

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [April 21, 2021, 9:46am UTC](https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714/2 "2021-04-21T09:46:16Z")

</div>

Hi @vch9.

Firstly, it’s worth noting that Ppxlib is expressive enough to handle any AST → AST transformation you can dream up (via [`Driver.register_transformation`](https://ocaml-ppx.github.io/ppxlib/ppxlib/Ppxlib/Driver/index.html#val-register_transformation) if needbe). It’s just a question of whether your use-case fits into any of the “simpler” APIs that Ppxlib provides.

If your goal is to attach metadata to a structure item, consider using attributes rather than extension points (`[@@my_pbt <payload>]` rather than `[%%my_pbt <payload>]`). At the syntax level, item extension points (with `%%`) are not “attached” to anything but sit inside the parent structure / signature as independent items. So the syntax you describe will look at the AST level more like the following:

```ocaml
(* List of four structure items: *)

let add x y = x + y

[%%my_pbt <payload>]

let sub x y = x + y

[%%my_pbt <payload>]

```

… and a tool like OCamlformat will format it as such. Item attributes (`@@`) are actually attached to specific items, and so it’s probably easier to get Ppxlib to reflect your intended interpretation using these.

Unfortunately, it seems [`Context_free.Rule`](https://ocaml-ppx.github.io/ppxlib/ppxlib/Ppxlib/Context_free/Rule/index.html) is missing the exact function you’d need, so you’ll probably want to use [`Ast_traverse.map`](https://ocaml-ppx.github.io/ppxlib/ppxlib/Ppxlib/Ast_traverse/index.html) with a hand-written method for transforming items with your attribute. You can then pass the `#structure` method to [`Driver.register_transformation`](https://ocaml-ppx.github.io/ppxlib/ppxlib/Ppxlib/Driver/index.html#val-register_transformation).

Hope this helps!

---

<div class="post-metadata">

**Author:** ![vch9](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vch9/32/2885_2.png) [@vch9](https://discuss.ocaml.org/u/vch9)\
**Post date:** [April 21, 2021, 10:19am UTC](https://discuss.ocaml.org/t/match-context-in-ppx-rewriter/7714/3 "2021-04-21T10:19:34Z")

</div>

Definitely helps !

Attributes seems more suitable for my purpose 🙂  
I’ll also look into [Ast\_traverse.map](https://ocaml-ppx.github.io/ppxlib/ppxlib/Ppxlib/Ast_traverse/index.html), thanks for the pointer !
