Does Ast_mapper no longer work?

Hello, I have this program:

let () =
  let f _args = Ast_mapper.default_mapper in
  Ast_mapper.run_main f

It gives me this error:

$ dune exec bin/main.exe ../blah.mli out
Failure("Ast_mapper: OCaml version mismatch or malformed input")

Not sure if this is helpful - I have only one OCaml version installed:

$ ocamlopt --version
5.0.0
$ cat ../blah.mli
type x = A of int | B of string
type y = {a : int; b: string}
$ opam switch list
#  switch  compiler                   description
β†’  5.0.0   ocaml-base-compiler.5.0.0  5.0.0

Am I using it wrong?

P.S. I really don’t want to use ppxlib for this task.

Ast_mapper.run_main assumes that the input files passed as argument contain serialized ASTs, not source code, and similarly generates serialized ASTs on output. If you want to process source code files with this approach you will need to write a custom driver that parses the source code (eg using the Parse module), applies the mapper, and converts the resulting AST back to source code (eg using the Pprintast module).

Cheers,
Nicolas

1 Like