aboutsummaryrefslogtreecommitdiff
path: root/lib/lexbuf.ml
blob: 8ddba2d4fc3f169afa9956d201bfcc536a32d0c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
type t = { buffer : Sedlexing.lexbuf; mutable start_p : Lexing.position option }

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.buffer

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 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