How to write ppx package that work with multiple OCaml AST version

As the title. Is there a way I can pin the parsetree in ppxlib to an older version? Or do I need to have a C++ Macro like code to support multiple versions? Thanks.

1 Like

The library ppxlib has implemented conversions from various versions of OCaml’s ASTs.

1 Like

Thanks. Could you point me to a code example? I am not sure how it should be used.

There current OCaml AST is converted to the one current version of PPXlib is based on.

1 Like

Thanks! I hope they put something in the documentation in the future.

Ppx rewriters written with ppxlib always support all OCaml versions >= 4.02. That’s taken care of by ppxlib itself, not by the ppx rewriters. Concretely, the ppx rewriters simply use the fixed ppxlib AST version when defining their AST transformations and ppxlib does all the magic of guaranteeing cross-compiler compatibility for those rewriters. It does so via AST migrations: when compiling a project with ppx rewriters, the ppxlib driver receives an AST of the version the project is compiled with. It then migrates that version to its fixed AST version. On that version, it applies the ppx rewriters. Then, it migrates the AST back to the version it started with and passes the result back to the build system. So as an author of a ppx rewriter, you don’t need to worry about cross-compiler compatibility.

The module Ppxlib_ast.Selected_ast.Of_ocaml @Kakadu has mentioned above is meant for special use cases in which the workflow I’ve just described doesn’t work, for example because you need to do the parsing yourself or because you need to use a compiler module that doesn’t have a ppxlib layer.

Do you mean about the fact that ppxlib takes care of ppx rewriters being cross-compiler compatible? Or do you mean about the special modules Of_ocaml and To_ocaml? I think both should be covered (the first one because it’s very fundamental and the second to make sure that the few people who need those modules know where to find / how to use them), but I’m curious which one you were referring to.

Thanks @pitag ! I think my problem was not correctly setting up the required package version. After I pinned the ppxlib to the correct version, it worked!

Sorry, I might be unclear. I am referring to the ppxlib user manual.

It is very useful! But the amount of information is limited.

I think both the fact that ppxlib takes care of ppx rewriters being cross-compiler compatible (I might be wrong for this one) and the special modules Of_ocaml and To_ocaml are not mentioned in the user manual. If there is any additional documentation. Could you please point it to me? Thanks.

1 Like

In this answer of another post, you can find the most important resources ppxlib has. We’re working both on integrating some of those resources into the ppxlib manual and on adding more info to the manual. We’ll also add some brief info on special use cases which in particular will contain some information about Of_ocaml and To_ocaml. It’s a good point that we also need to add some explanations about how ppxlib works to make sure people know that ppxlib takes care of ppx rewriters being cross-compiler compatible! Thanks for bringing that up :slight_smile: