簡體   English   中英

如何覆蓋 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 成員的行號?

模塊函數不是方法。 更准確地說,沒有后期綁定的主題,並且永遠不能被覆蓋。 您正在鏈接的代碼隱藏了原始引擎 function。

此外,Yojson 正在計算錯誤消息的行號,但它們從未集成到解析的 json 中。 如果不重寫解析器,就無法從 Yojson 解析器中獲取行號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM