I am currently working on some project that involves Ocaml’s build system Dune. However, in my work, there is some tasks that require taking a file (mainly its path) which contains some text written in a S-expression format. For example, for those familiar with defining and building rules with dune,
(rule (deps (file a) (glob *.cmi)) (action cat a))
By investigating the dune codebase, I have understood that there is a decoder (which is actually a monad) that is defined for a type 'a, so that when this decoder is a ran on a certain S-expression it returns the object of type 'a defined in this S-expression (if it is a valid one).
My question is how exactly one could use such a defined decoder on custom file (that is not a dune file that is automatically parsed by dune) in order to extract the information? I assume that one should first parse the file in order to extract the S-expression, but I don’t know if that is automatically done by the decoder.Preformatted text
Broadly speaking, the file can be parsed using the module Parser:
This will yield an Ast.t or Ast.t list (assuming you pass Single or Many for the ~mode argument). Then a decoder can be run over this value by calling the only function available with the required signature: