类型 Sedlexing.lexbuf 与类型 Lexing.lexbuf 不兼容
Type Sedlexing.lexbuf is not compatible with type Lexing.lexbuf
我想通过 sedlex+menhir 将 menhir 的增量 API 和错误处理添加到我的项目中;我试图在我的代码中采用 attempt2
和 fail
of this sample。这是示例的 attempt2
:
let attempt2 filename text =
(* Allocate and initialize a lexing buffer. *)
let lexbuf = L.init filename (Lexing.from_string text) in
(* Wrap the lexer and lexbuf together into a supplier, that is, a
function of type [unit -> token * position * position]. *)
let supplier = I.lexer_lexbuf_to_supplier Lexer.token lexbuf in
(* Equip the supplier with a two-place buffer that records the positions
of the last two tokens. This is useful when a syntax error occurs, as
these are the token just before and just after the error. *)
let buffer, supplier = E.wrap_supplier supplier in
(* Fetch the parser's initial checkpoint. *)
let checkpoint = UnitActionsParser.Incremental.main lexbuf.lex_curr_p in
(* Run the parser. *)
(* We do not handle [Lexer.Error] because we know that we will not
encounter a lexical error during this second parsing run. *)
I.loop_handle succeed (fail text buffer) supplier checkpoint
在我的代码中,我试过:
let lexbuf : Lexing.lexbuf = MenhirLib.LexerUtil.init "filename" (Lexing.from_string s) in
let supplier = UnitActionsParser_e.MenhirInterpreter.lexer_lexbuf_to_supplier Sedlexer_e.token_ao lexbuf in
但是编译报错:
260 | let supplier = UnitActionsParser_e.MenhirInterpreter.lexer_lexbuf_to_supplier Sedlexer_e.token_ao lexbuf in
^^^^^^^^^^^^^^^^^^^
Error: This expression has type Sedlexing.lexbuf -> Parser_e.token
but an expression was expected of type
Lexing.lexbuf -> UnitActionsParser_e.MenhirInterpreter.token
Type Sedlexing.lexbuf is not compatible with type Lexing.lexbuf
make: *** [expression/e.cmo] Error 2
有人能帮忙吗?
(* link 在 GitHub *)
lexer_lexbuf_to_supplier的代码如下:
let lexer_lexbuf_to_supplier
(lexer : Lexing.lexbuf -> token)
(lexbuf : Lexing.lexbuf)
: supplier =
fun () ->
let token = lexer lexbuf in
let startp = lexbuf.Lexing.lex_start_p
and endp = lexbuf.Lexing.lex_curr_p in
token, startp, endp
所以我做了一个lexer_lexbuf_to_supplier_sedlex
:
let lexer_lexbuf_to_supplier_sedlex
(lexer: Sedlexing.lexbuf -> token)
(lexbuf : Sedlexing.lexbuf)
: supplier =
fun () ->
let token = lexer lexbuf in
let startp, endp = Sedlexing.lexing_positions lexbuf in
token, startp, endp
目前有效。
我想通过 sedlex+menhir 将 menhir 的增量 API 和错误处理添加到我的项目中;我试图在我的代码中采用 attempt2
和 fail
of this sample。这是示例的 attempt2
:
let attempt2 filename text =
(* Allocate and initialize a lexing buffer. *)
let lexbuf = L.init filename (Lexing.from_string text) in
(* Wrap the lexer and lexbuf together into a supplier, that is, a
function of type [unit -> token * position * position]. *)
let supplier = I.lexer_lexbuf_to_supplier Lexer.token lexbuf in
(* Equip the supplier with a two-place buffer that records the positions
of the last two tokens. This is useful when a syntax error occurs, as
these are the token just before and just after the error. *)
let buffer, supplier = E.wrap_supplier supplier in
(* Fetch the parser's initial checkpoint. *)
let checkpoint = UnitActionsParser.Incremental.main lexbuf.lex_curr_p in
(* Run the parser. *)
(* We do not handle [Lexer.Error] because we know that we will not
encounter a lexical error during this second parsing run. *)
I.loop_handle succeed (fail text buffer) supplier checkpoint
在我的代码中,我试过:
let lexbuf : Lexing.lexbuf = MenhirLib.LexerUtil.init "filename" (Lexing.from_string s) in
let supplier = UnitActionsParser_e.MenhirInterpreter.lexer_lexbuf_to_supplier Sedlexer_e.token_ao lexbuf in
但是编译报错:
260 | let supplier = UnitActionsParser_e.MenhirInterpreter.lexer_lexbuf_to_supplier Sedlexer_e.token_ao lexbuf in
^^^^^^^^^^^^^^^^^^^
Error: This expression has type Sedlexing.lexbuf -> Parser_e.token
but an expression was expected of type
Lexing.lexbuf -> UnitActionsParser_e.MenhirInterpreter.token
Type Sedlexing.lexbuf is not compatible with type Lexing.lexbuf
make: *** [expression/e.cmo] Error 2
有人能帮忙吗?
(* link 在 GitHub *)
lexer_lexbuf_to_supplier的代码如下:
let lexer_lexbuf_to_supplier
(lexer : Lexing.lexbuf -> token)
(lexbuf : Lexing.lexbuf)
: supplier =
fun () ->
let token = lexer lexbuf in
let startp = lexbuf.Lexing.lex_start_p
and endp = lexbuf.Lexing.lex_curr_p in
token, startp, endp
所以我做了一个lexer_lexbuf_to_supplier_sedlex
:
let lexer_lexbuf_to_supplier_sedlex
(lexer: Sedlexing.lexbuf -> token)
(lexbuf : Sedlexing.lexbuf)
: supplier =
fun () ->
let token = lexer lexbuf in
let startp, endp = Sedlexing.lexing_positions lexbuf in
token, startp, endp
目前有效。