如何在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语法分析器中获取行号。

最新更新