2

我正在使用的--compile-errors功能,menhir我对它非常满意。我也ocamlbuild用来管理我的项目的编译。由于该项目非常基础,到目前为止,构建基础设施仍然微不足道。

我在项目的根目录有一个_tags文件和一个简单的文件。Makefile我还没有myocamlbuild.ml文件。该_tags文件仅包含一行:

<src/*>: package(ppx_deriving.std)

Makefile 的相关部分是

all: src/ParsingErrors.ml
    ocamlbuild $(OPTIONS) src/Main.native

src/ParsingErrors.ml: src/Handcrafted.messages src/Parser.mly
    menhir --compile-errors src/Handcrafted.messages src/Parser.mly > src/ParsingErrors.ml

OPTIONS = -j 4 -use-menhir -use-ocamlfind -yaccflag --table -pkgs menhirLib,str,unix

OCamlbuild 和 Menhir 通常集成得很好,但--compile-errors似乎是一个相当新的功能。我的设置并不理想,因为我不喜欢src/ParsingErrors.ml在我的源目录而不是构建目录中有一个自动生成的文件。向 OCamlbuild 解释我希望 Menhir 构建此错误消息文件的最佳方式是什么?更改文件的命名约定(例如,更改为src/Parser.messagesand src/Parser.ml)不会让我感到困扰,如果它可以简化事情的话。

注意:虽然我myocamlbuild.ml在其他项目中有文件,但我从在线资源中复制了它们。我发现它们很难破译,而且我真的不明白如何写这些东西。

4

2 回答 2

4

寻找此问题答案的最佳位置是在 Menhir 的发行版中,因为 Menhir 在其自己的编译过程中使用“menhir --compile-errors”和 ocamlbuild。

要查找的文件src/myocamlbuild.ml位于源 tarball中。

这是一个相关的摘录:

(* This rule generates an .ml file [target] from an .mly file [grammar] and a
   .messages file [messages]. *)

(* If the name of a witness file is passed, it is made an additional
   dependency. This triggers a separate rule (see below) which performs a
   completeness check, that is, which checks that the .messages file lists
   every possible syntax error. *)

let compile_errors grammar messages (witness : string list) target =
  rule
    "menhir/compile_errors"
    ~prod:target
    ~deps:([ grammar; messages ] @ witness)
    (fun env _ ->
      let grammar = env grammar in
      let tags = tags_of_pathname grammar ++ "ocaml" ++ "menhir" in
      Cmd(S[
        !Options.ocamlyacc; (* menhir *)
        T tags;
        P grammar;
        A "--compile-errors"; P (env messages);
        Sh ">"; Px (env target);
      ]))

(* A generic version of the above rule, with uniform naming. *)

let generic_compile_errors (check_completeness : bool) =
  compile_errors
    (* sources: *)
    "%.mly" "%Messages.messages"
    (* if present, this dependency forces a completeness check: *)
    (if check_completeness then [ "%Messages.witness" ] else [])
    (* target: *)
    "%Messages.ml"

我很乐意与 ocamlbuild 维护者讨论 Menhir 的 myocamlbuild.ml 的某些部分是否可以移入 ocamlbuild 的标准规则集。(嗨,加布里埃尔!)

于 2016-11-16T09:41:40.210 回答
2

当前的 ocamlbuild 版本确实不支持该功能。要将其添加到您的myocamlbuild.ml插件中,您必须定义一个新的构建规则。如果你对里面的东西感到害怕myocamlbuild.ml,你可能会对新的ocamlbuild 手册感兴趣,特别是它关于插件的部分

我刚刚在 ocamlbuild 存储库上打开了一个问题报告 #121来跟踪这个缺失的功能。您是否有兴趣充分了解 ocamlbuild 以为此编写适当的规则并将其贡献给下一个 ocamlbuild 版本?如果是,请随时访问 github 问题并开始询问有关如何实现它的问题,我很乐意提供帮助。如果没有,我会在有时间的时候尝试自己实现它。

同时,请注意,为特定文件定义一次性规则比定义通用规则要容易得多。如果您准备好对您使用的源文件进行硬编码,您可能会拥有一些看起来与 Makefile 规则非常相似的东西。我今天没有时间写和检查这条规则,但如果其他人在这期间没有这样做,我明天会尝试回来。

于 2016-11-15T18:20:25.753 回答