Source

ocaml-indent / pos.ml

module Position = struct
  open Sexplib.Conv

  type t = Lexing.position =  {
    pos_fname : string;
    pos_lnum : int;
    pos_bol : int;
    pos_cnum : int;
  } with sexp

  let to_string t = 
    Printf.sprintf "%s%d:%d" 
      (if t.pos_fname = "" then "" else t.pos_fname ^ ":")
      t.pos_lnum 
      (t.pos_cnum - t.pos_bol)

  let zero = { pos_fname = "";
               pos_lnum = 1;
               pos_bol = 0;
               pos_cnum = 0 }

  let zero = { pos_fname = "";
               pos_lnum = 0;
               pos_bol = 0;
               pos_cnum = 0 }

  let columns p = p.pos_cnum - p.pos_bol
end

module Region = struct
  open Position
  type t = Position.t * Position.t with sexp
  let lnum (p,_) = p.pos_lnum
  let columns (p,_) = columns p
  let zero = (Position.zero, Position.zero)
  let move_chars diff (p,p') = 
    { p  with pos_cnum = p .pos_cnum + diff },
    { p' with pos_cnum = p'.pos_cnum + diff } 
end