Hello,
I’m glad to announce the first release of menhirformat, a command line tool that formats and indents Menhir and ocamllex source code. It uses the ocamlformat RPC to pretty-print the OCaml sections of your lexers and parsers; it also tries its best to preserve the original location of your comments (still needs work in regard to this though!).
This is a satellite project of my ongoing and considerably larger effort to add LSP support to these two OCaml dialects, menhir-lsp. The formatting functionality was originally embedded into the LSP server to handle document formatting requests, and I split it out as a standalone application after this issue.
You can try it out with opam install menhirformat. The CLI is highly inspired by ocamlformat’s and takes the file to format as argument, e.g. menhirformat calc.mly. It exposes a small configuration by which your can tweak the formatter’s behavior; read on the available options with menhirformat --help.
You may also integrate it in your dune project by setting up dialects for Menhir and ocamllex:
(dialect
(name menhir)
(implementation
(extension mly)
(format
(run menhirformat %{input-file}))))
(dialect
(name ocamllex)
(implementation
(extension mll)
(format
(run menhirformat %{input-file}))))
Then running dune build @fmt will format your project’s .mll and .mly files and display a diff.
As someone who can’t live without format-on-save in their IDE, I was really missing a formatter for Menhir and ocamllex files in the OCaml ecosystem. I hope menhirformat fills that void.
I’m interested in opinions about the quality of the output and also in suggestions for other customization points of the configuration. Please share below or through a GitHub issue!
- opam page: menhirformat 0.1.0 (latest) · OCaml Package
- Source code: menhir-lsp/prettyprint at main · dalps/menhir-lsp · GitHub
Enjoy!