如何覆盖 Ocaml 中的模块方法

How to override a module method in Ocaml

假设我有一个模块 A,它覆盖了模块 B 中的一些方法。 我希望模块 A 通过覆盖 B 或以其他方式防止模块 A 中的此类覆盖。

我需要这个来从 YoJSON 获取行号。我尝试使用其他解析器,但它们的功能不如 YoJSON。

缓冲区中的行号被 YoJSON 中的以下代码阻止 read.ml

  module Lexing =
    (*
      We override Lexing.engine in order to avoid creating a new position
      record each time a rule is matched.
      This reduces total parsing time by about 31%.
    *)
  struct
    include Lexing

    external c_engine : lex_tables -> int -> lexbuf -> int = "caml_lex_engine"

    let engine tbl state buf =
      let result = c_engine tbl state buf in
      (*
      if result >= 0 then begin
        buf.lex_start_p <- buf.lex_curr_p;
        buf.lex_curr_p <- {buf.lex_curr_p
                           with pos_cnum = buf.lex_abs_pos + buf.lex_curr_pos};
      end;
      *)
      result
  end

  open Printf
  open Lexing

来源:https://github.com/ocaml-community/yojson/blob/master/lib/read.mll

有什么办法可以扭转这种行为?会再次覆盖 Lexer,然后覆盖 YoJSON 中使用 Lexer 的所有方法吗?

或者有没有办法用 YoJSON 或其他解析器以其他方式获取 json 成员的行号?

模块函数不是方法。更准确地说,没有后期绑定的主题,并且永远不会被覆盖。您链接的代码隐藏了原始引擎功能。

此外,Yojson 正在计算错误消息的行号,但它们从未集成到已解析的 json 中。如果不重写解析器,就无法从 Yojson 解析器获取行号。