From 1f79e8f1f0f59748497665ccee544163c5136562 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Fri, 29 Sep 2023 09:27:22 +0200 Subject: Fixed a wrong location for string --- lib/lexbuf.ml | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'lib/lexbuf.ml') diff --git a/lib/lexbuf.ml b/lib/lexbuf.ml index 2ed9099..8ddba2d 100644 --- a/lib/lexbuf.ml +++ b/lib/lexbuf.ml @@ -1,14 +1,30 @@ -type t = Sedlexing.lexbuf +type t = { buffer : Sedlexing.lexbuf; mutable start_p : Lexing.position option } -let buffer : t -> Sedlexing.lexbuf = fun t -> t -let start : t -> unit = fun t -> Sedlexing.start t +let buffer : t -> Sedlexing.lexbuf = fun t -> t.buffer +let start : t -> unit = fun t -> Sedlexing.start t.buffer let positions : t -> Lexing.position * Lexing.position = - fun t -> Sedlexing.lexing_positions t + fun t -> Sedlexing.lexing_positions t.buffer -let content : t -> string = fun t -> Sedlexing.Utf8.lexeme t -let from_lexbuf : Sedlexing.lexbuf -> t = fun t -> t +let content : t -> string = fun t -> Sedlexing.Utf8.lexeme t.buffer + +let from_lexbuf : Sedlexing.lexbuf -> t = + fun t -> { buffer = t; start_p = None } + +let set_start_position : t -> Lexing.position -> unit = + fun t position -> t.start_p <- Some position let tokenize : (t -> 'a) -> t -> unit -> 'a * Lexing.position * Lexing.position = - fun f lexbuf -> Sedlexing.with_tokenizer f lexbuf + fun f t -> + let lexer () = + (* Clear the previous registered start position if any *) + t.start_p <- None; + let token = f t in + let default, curr_p = positions t in + + let start_p = Option.value ~default t.start_p in + + (token, start_p, curr_p) + in + lexer -- cgit v1.2.3