8000 Make context available in transformation callbacks by pitag-ha · Pull Request #202 · ocaml-ppx/ppxlib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make context available in transformation callbacks #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2021

Conversation

pitag-ha
Copy link
Member
@pitag-ha pitag-ha commented Dec 29, 2020

The later versions (V2 or V3) of the Extension and Deriving modules allow to access an expansion context when writing such rewriters. By adding a V2 to Driver, this commit also adds that possibility to whole file transformations registered through the ~impl, intf, ~preprocess_impl, ~preprocess_intf, lint_impl or lint-intf arguments of Driver.register_transformation. The expansion context chosen is the Base expansion context in order to be compatible with Ast_traverse.map_with_expansion_context.

There are other kinds of transformations that still don't offer that possibility such as other context free rules than extensions (see issue #169).

The PR also adds input name to the expansion context.

@pitag-ha pitag-ha requested a review from xclerc as a code owner December 29, 2020 23:23
@pitag-ha pitag-ha force-pushed the context-register-callbacks branch from 64e298c to 2659578 Compare December 29, 2020 23:32
@pitag-ha
Copy link
Member Author

The ocaml-ci fails aren't related to this PR.

Copy link
Collaborator
@NathanReb NathanReb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great! Thanks for working on this, I think it's a solid improvement to ppxlib's API.

I have a few remark and suggestions.

Also something I found is that the cram test is a bit overly complex. I wonder if we couldn't simply create a single standalone driver with a single transformation that simply inserts a floating attribute with the values of tool_name, input_name and the file_path component of the code_pathor even just dump those values on the standard output. It should obviously register impl and intf transformations. We could then invoke it on the different files you generate in your cram test! I think that would simplify the setup.

What do you think?

@NathanReb
Copy link
Collaborator

Can you rebase on top of master so we get rid of most of the CI failures?

@pitag-ha pitag-ha force-pushed the context-register-callbacks branch from 2659578 to bf1a8c7 Compare January 4, 2021 17:36
Before, via the `file_path` component of `code_path`, the user could
already access the `file path` through the expansion context. This
commit adds the possibility to also access the slighty different concept
`input name`.

Signed-off-by: Sonja Heinze <sonjaleaheinze@gmail.com>
@pitag-ha
Copy link
Member Author
pitag-ha commented Jan 5, 2021

I wonder if we couldn't simply create a single standalone driver with a single transformation that simply inserts a floating attribute with the values of tool_name, input_name and the file_path component of the code_pathor even just dump those values on the standard output.

That would indeed make it simpler. It would have a couple of downsides / difficulties:

  • We can't do the "empty file" test anymore, since the file needs to have at least the floating attribute
  • Having the tool name around all the time when only testing the input name and the code path might be a bit confusing
  • We'd have to think about if and how to test the intf API. Is it possible to dump values on the standard output from within an interface file?
  • If I've understood you correctly, you would directly create the standalone executable, no ppx rewriter library. Is that right? How would you do the "using map_structure (or map_signature)" test then?

But maybe the simplicity makes up for that. In any case, I do think that in that case I would only care about input_name and file_path though and not take the tool_name into account since it wouldn't actually add any test, would it? In fact, I think I would do that anyways. What do you think?

Edit (after our conversation): Sounds all very good! Thanks! I've done that with the only difference that I've still needed two different drivers (the standard one and one for the map_structure test.

@pitag-ha pitag-ha force-pushed the context-register-callbacks branch 2 times, most recently from ca9d23b to 8b58c15 Compare January 11, 2021 12:09
@pitag-ha
Copy link
Member Author

@NathanReb, in the documentation of V2.register_transformation , I've commented on the choice of signature for the V2 callbacks . Do you think I can merge this?

Copy link
Collaborator
@NathanReb NathanReb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It mostly looks good, I just have a couple questions but then it should be good to go!

Sorry for the hassle!

The later versions (V2 or V3) of the Extension and Deriving modules allow
to access an expansion context when writing such rewriters. By adding a
V2 to Driver, this commit also adds that possibility to whole file
transformations registered through the `~impl`, `intf`, `~preprocess_impl`
`~preprocess_intf`, `lint_impl` or `lint_intf` arguments of
`Driver.register_transformation`. The expansion context chosen is the
Base expansion context in order to be compatible with
`Ast_traverse.map_with_expansion_context`.

There are other kinds of transformations that still don't offer that
possibility such as other context free rules than extensions.

Signed-off-by: Sonja Heinze <sonjaleaheinze@gmail.com>
@pitag-ha pitag-ha force-pushed the context-register-callbacks branch from 8b58c15 to d45cabd Compare January 12, 2021 16:46
Copy link
Member Author

No hassle at all, thanks for your help!

Copy link
Collaborator
@NathanReb NathanReb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, let's merge!

@pitag-ha pitag-ha merged commit fca0b5c into ocaml-ppx:master Jan 12, 2021
NathanReb added a commit to NathanReb/opam-repository that referenced this pull request Jan 20, 2021
CHANGES:

- Driver (important for bucklescript): handling binary AST's, accept any
  supported version as input; preserve that version (ocaml-ppx/ppxlib#205, @pitag-ha)

- `-as-ppx`: take into account the `-loc-filename` argument (ocaml-ppx/ppxlib#197, @pitag-ha)

- Add input name to expansion context (ocaml-ppx/ppxlib#202, @pitag-ha)

- Add Driver.V2: give access to expansion context in whole file transformation
  callbacks of `register_transformation` (ocaml-ppx/ppxlib#202, @pitag-ha)

- Driver: take `-cookie` argument into account, also when the input is a
  binary AST (@pitag-ha, ocaml-ppx/ppxlib#209)

- `run_as_ppx_rewriter`: take into account the arguments
  `-loc-filename`, `apply` and `dont-apply` (ocaml-ppx/ppxlib#205, @pitag-ha)

- Location.Error: add functions `raise` and `update_loc`
  (ocaml-ppx/ppxlib#205, @pitag-ha)
NathanReb added a commit to NathanReb/opam-repository that referenced this pull request Jan 22, 2021
CHANGES:

- Fix ppxlib.traverse declaration and make it a deriver and not a rewriter
  (ocaml-ppx/ppxlib#213, @NathanReb)
- Driver (important for bucklescript): handling binary AST's, accept any
  supported version as input; preserve that version (ocaml-ppx/ppxlib#205, @pitag-ha)

- `-as-ppx`: take into account the `-loc-filename` argument (ocaml-ppx/ppxlib#197, @pitag-ha)

- Add input name to expansion context (ocaml-ppx/ppxlib#202, @pitag-ha)

- Add Driver.V2: give access to expansion context in whole file transformation
  callbacks of `register_transformation` (ocaml-ppx/ppxlib#202, @pitag-ha)

- Driver: take `-cookie` argument into account, also when the input is a
  binary AST (@pitag-ha, ocaml-ppx/ppxlib#209)

- `run_as_ppx_rewriter`: take into account the arguments
  `-loc-filename`, `apply` and `dont-apply` (ocaml-ppx/ppxlib#205, @pitag-ha)

- Location.Error: add functions `raise` and `update_loc`
  (ocaml-ppx/ppxlib#205, @pitag-ha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0