1. camlspotter
  2. olfend

Source

olfend / path.ml

open Spotlib.Spot
open Sexplib.Conv

type t = 
  | Ident of Ident.t
  | Dot of t * Ident.t * int
with sexp

let of_ident id = Ident id

let rec to_string = function
  | Ident id -> Ident.to_string id
  | Dot (Ident predef, id, pos) when Ident.name predef = "*predef*" -> Printf.sprintf "%s/%d" (Ident.to_string id) pos
  | Dot (t, id, pos) -> Printf.sprintf "%s.%s/%d" (to_string t) (Ident.to_string id) pos

let ppr p = Treeprint.Printer.string (to_string p) 
let format = Treeprint.Printer.format ppr

let rec name = function
  | Ident id -> Ident.name id
  | Dot (Ident predef, n, _) when Ident.name predef = "*predef*" -> Ident.name n
  | Dot (t, n, _) -> Printf.sprintf "%s.%s" (name t) (Ident.name n)

let compare = compare

let rec localize path_in idpostbl = function
  | (Ident id as p) when Ident.stamp id = -1 -> p
  | Ident id -> 
      begin try 
        Dot (path_in, id, List.assoc id idpostbl )
      with Not_found -> Dot (path_in, id, -1) (* woooo right? *)
      end
  | Dot (p, name, pos) -> Dot (localize path_in idpostbl p, name, pos)

let postfix = function
  | Ident id | Dot (_, id, _) -> Ident.name id