How can I use dune and ppxlib to generate library code?

I am working on a port of ungrammar to ocaml. It is a DSL for specifying concrete syntax trees. Ungrammar is just a parser that produces “grammars” which are lists of nodes and tokens, whatever those are.

The original setup is like this:

Running cargo xtask codegen first parses a rust grammar and generates file based on the grammar in the working directory. This generated rust code defines an API for working with rust syntax, and this API is used throughout the rest of the codebase.

So far, I have a working ungrammar parser and would now like to use ppxlib to generate some ocaml. After struggling a bit with writing a preprocessor, I am now wondering how I can best use ppxlib to create a program that generates an ocaml file. I just want to generate some code from an ocaml value, I don’t really care for transforming ocaml ASTs through extension nodes.

Ideally I would like a dune rule which generates the code in my working tree, not somewhere deep in the _build folder. This dune rule would specify the dependency on the specific grammar to be parsed.

Related: Using ppx for code generation - #5 by jyy

You’re probably looking for ppxlib’s Ast_builder APIs to build an ast; the documentation is too sparse but it gets the job done. You want generated code to be part of your codebase as well, so you could write an executable that generates the ast and prints it with ppxlib’s Pprintast to stdout or a file, then add a dune rule for your library that builds and promotes the output to your working tree:

(rule
 (deps cool_lang.ungram)
 (mode promote)
 (action
  (with-stdout-to cool_lang.ml
   (run ../generator/generator.exe cool_lang.ungram))))

You would only implement this as a preprocessor if you wanted to write grammars inline that expand to generated code, for example a quoted-string extension node {%ungrammar|...|} that expands into a parser function or grammar module.

1 Like

I tried doing something similar earlier this year - I tried to capture my own understanding of Ast_builder here if you find it helpful

3 Likes