Using ppx for code generation



I would like to parse some file format and generate code at compile time. One way would be to just stitch formatted string of ocaml syntax. I was wondering if i could make use of ppx for this. Say.
let _ = () [%parse_n_gen “foo.xml”];;
Which should generate several modules baz, bar and so on by parsing that file at compile time. I am not sure if there is any facility for quasiquotation. Also how would i generate new structs and sigs from this? So far, i have seen ppx rewrite the same module by adding some more expressions.

Any pointers on this would be appreciated.


The dummy let = () binding can be dropped by using [%%parse_n_gen "foo.xml"]. To avoid the source file altogether, I think this may be a reasonable solution:

open Migrate_parsetree
open Ast_406
open Parsetree

let ocaml_version = Versions.ocaml_406
module Convert_to_current = Convert (OCaml_406) (OCaml_current)

let () =
  let loc = Location.none in
  let str = [%str
    let rec fact n = if n <= 0 then 1 else n * fact (n - 1)
  ] in
  let ppf = Format.std_formatter in (* or add an -o option *)
  Pprintast.structure ppf (Convert_to_current.copy_structure str)

Link and preprocess with ocaml-migrate-parsetree and ppx_metaquot.


A primitive script would work? Please don’t use ppx just because you can, it’s a huge API surface area where no ABI is guaranteed


My experience with generating OCaml bindings from the vulkan specification file is that manipulating OCaml AST with the help of ppx_metaquot scales better beyond a certain size. Depending on your aims, it may not be necessary to use the ppx API proper: the ppx API would be useful to insert generated content inside preexisting OCaml source file; but generating standalone OCaml source files can be done directly using the compiler-libs library and printing the generated AST with Pprintast.