Source

olfend / tag.ml

open Sexplib.Sexp

(* Variant tag *)
type t = int

let parse s =
  let len = String.length s in
  try
    if s.[len-1] = ':' then
      Some (int_of_string (String.sub s 0 (len-1)))
    else None
  with
  | _ -> None

let sexp_of_t n = Atom (Printf.sprintf "%d:" n)
let t_of_sexp = function
  | List _ -> assert false
  | Atom s ->
      let len = String.length s in
      assert (s.[len-1] = ':');
      int_of_string (String.sub s 0 (len-1))

let of_int x = x
let to_int x = x
let next x = x + 1
let zero = 0

let ppr n = let open Treeprint.Printer in string (Printf.sprintf ":%d" n)
let format = Treeprint.Printer.format ppr