I’m clearly doing something stupid. Advice appreciated. (Trying to parse an OCaml filed with Camlp5 parser).
cat dune; echo "==="; make; echo "==="; cat conv.ml
(executable
(public_name m_rewrite)
(name conv)
(libraries
core base
compiler-libs.common
camlp5
))
===
../_build/default/m_rewrite/conv.exe
Fatal error: exception Failure("no loaded parsing module")
make: *** [Makefile:2: run] Error 2
===
let code = {code|
module MyRecord = struct
type t = {
a: int;
b: string;
c: int
}
end
module MyVariant = struct
type t =
| Ok of string * int
| Err of int * int
end
|code}
let parse_expr s = s |> Stream.of_string |> Grammar.Entry.parse Pcaml.expr ;;
let _t: MLast.expr = parse_expr code;;
Progress:
cat dune; echo "==="; make; echo "==="; cat conv.ml
(executable
(public_name m_rewrite)
(name conv)
(libraries
core base
compiler-libs.common
camlp5
))
===
../_build/default/m_rewrite/conv.exe
Fatal error: exception Ploc.Exc(_, _)
make: *** [Makefile:2: run] Error 2
===
let code = {code|
module MyRecord = struct
type t = {
a: int;
b: string;
c: int
}
end
module MyVariant = struct
type t =
| Ok of string * int
| Err of int * int
end
|code}
let g = Grammar.gcreate (Plexer.gmake ());;
let e = Grammar.Entry.create g "expression";;
let parse_expr s = Grammar.Entry.parse_all e (Stream.of_string s) ;;
let _t: MLast.expr list = parse_expr code;;
You’ll have trouble doing it with dune: Dune doesn’t really support the old “-pp” preprocessors, since everybody switched over to “-ppx” preprocessors. [the difference is that “-pp” preprocessors take the source file as input; “-ppx” preprocessors take a parsed OCaml AST.]
So let’s back up to Makefiles. You can see examples of how to compile and link in the tutorials, e.g. camlp5/tutorials/calc+pa_lexer-original at master · camlp5/camlp5 · GitHub
Now you want to do something more than write a PPX rewriter (which can assume that somebody else parsed the file). To do this, you need to link to a “parsing kit” (that defines the syntax of OCaml). There are (more than) two (but for now, let’s stick to two):
camlp5.pa_o (official syntax)
camlp5.pa_r (revised syntax)
these are findlib packages; if you added them as “-package camlp5.pa_o”, they would be used at preprocessing-time, but you want them loaded at runtime. So you want the “link” version of this kit, viz.
“-package camlp5.pa_o.link”
Solved in GitHub - zeroexcuses/learn_camlp5 (actual work by @Chet_Murthy ; typos my fault ).