Source file position.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
open! Core
open! Import

include Value.Make_subtype (struct
    let name = "position"
    let here = [%here]
    let is_in_subtype = Value.is_integer
  end)

let of_int_exn int = int |> Value.of_int_exn |> of_value_exn
let to_int t = t |> to_value |> Value.to_int_exn
let to_byte_position = Funcall.Wrap.("position-bytes" <: t @-> return int)
let of_byte_position = Funcall.Wrap.("byte-to-position" <: int @-> return t)
let compare t1 t2 = Int.compare (to_int t1) (to_int t2)
let add t1 t2 = of_int_exn (to_int t1 + t2)
let sub t1 t2 = of_int_exn (to_int t1 - t2)
let diff t1 t2 = to_int t1 - to_int t2

include Comparable.Make_plain (struct
    type nonrec t = t [@@deriving compare, sexp_of]
  end)