ident.ml 6.03 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
7 8 9 10 11 12 13 14 15 16 17 18 19
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

François Bobot's avatar
François Bobot committed
20
open Stdlib
21 22
open Util

MARCHE Claude's avatar
MARCHE Claude committed
23 24
(** Labels *)

Andrei Paskevich's avatar
Andrei Paskevich committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
type label = {
  lab_string : string;
  lab_tag    : int;
}

module Hslab = Hashcons.Make (struct
  type t = label
  let equal lab1 lab2 = lab1.lab_string = lab2.lab_string
  let hash lab = Hashtbl.hash lab.lab_string
  let tag n lab = { lab with lab_tag = n }
end)

let create_label s = Hslab.hashcons {
  lab_string = s;
  lab_tag    = -1
}

let lab_equal : label -> label -> bool = (==)

let lab_hash (lab : label) = lab.lab_tag
MARCHE Claude's avatar
MARCHE Claude committed
45

46 47 48
(** Identifiers *)

type ident = {
49 50 51 52
  id_string : string;               (* non-unique name *)
  id_label  : label list;           (* identifier labels *)
  id_loc    : Loc.position option;  (* optional location *)
  id_tag    : Hashweak.tag;         (* unique magical tag *)
53 54
}

Andrei Paskevich's avatar
Andrei Paskevich committed
55
module Id = WeakStructMake (struct
56
  type t = ident
57 58 59 60 61 62
  let tag id = id.id_tag
end)

module Sid = Id.S
module Mid = Id.M
module Hid = Id.H
63

64 65
type preid = ident

66
let id_equal : ident -> ident -> bool = (==)
67

Andrei Paskevich's avatar
Andrei Paskevich committed
68
let id_hash id = Hashweak.tag_hash id.id_tag
69

Andrei Paskevich's avatar
Andrei Paskevich committed
70
(* constructors *)
71

Andrei Paskevich's avatar
Andrei Paskevich committed
72 73
let id_register = let r = ref 0 in fun id ->
  { id with id_tag = (incr r; Hashweak.create_tag !r) }
74

75
let create_ident name labels loc = {
76
  id_string = name;
Andrei Paskevich's avatar
Andrei Paskevich committed
77
  id_label  = labels;
78
  id_loc    = loc;
Andrei Paskevich's avatar
Andrei Paskevich committed
79
  id_tag    = Hashweak.dummy_tag;
80 81
}

82 83
let id_fresh ?(label = []) ?loc nm =
  create_ident nm label loc
Andrei Paskevich's avatar
Andrei Paskevich committed
84

85 86
let id_user ?(label = []) nm loc =
  create_ident nm label (Some loc)
87

88 89
let id_clone ?(label = []) id =
  create_ident id.id_string (label @ id.id_label) id.id_loc
90

91 92
let id_derive ?(label = []) nm id =
  create_ident nm (label @ id.id_label) id.id_loc
Francois Bobot's avatar
Francois Bobot committed
93

94 95
(** Unique names for pretty printing *)

96 97
type ident_printer = {
  indices   : (string, int) Hashtbl.t;
Andrei Paskevich's avatar
Andrei Paskevich committed
98
  values    : string Hid.t;
99 100 101
  sanitizer : string -> string;
  blacklist : string list;
}
102 103

let find_unique indices name =
104 105 106 107 108 109 110 111
  let specname ind = name ^ string_of_int ind in
  let testname ind = Hashtbl.mem indices (specname ind) in
  let rec advance ind =
    if testname ind then advance (succ ind) else ind in
  let rec retreat ind =
    if ind = 1 || testname (pred ind) then ind else retreat (pred ind) in
  let fetch ind =
    if testname ind then advance (succ ind) else retreat ind in
112
  let name = try
113
    let ind = fetch (succ (Hashtbl.find indices name)) in
114
    Hashtbl.replace indices name ind;
115
    specname ind
116 117 118
  with Not_found -> name in
  Hashtbl.replace indices name 0;
  name
119

120 121
let reserve indices name = ignore (find_unique indices name)

122 123
let same x = x

124
let create_ident_printer ?(sanitizer = same) sl =
125
  let indices = Hashtbl.create 1997 in
126 127
  List.iter (reserve indices) sl;
  { indices   = indices;
Andrei Paskevich's avatar
Andrei Paskevich committed
128
    values    = Hid.create 1997;
129 130
    sanitizer = sanitizer;
    blacklist = sl }
131

132
let id_unique printer ?(sanitizer = same) id =
133
  try
Andrei Paskevich's avatar
Andrei Paskevich committed
134
    Hid.find printer.values id
135
  with Not_found ->
136
    let name = sanitizer (printer.sanitizer id.id_string) in
137
    let name = find_unique printer.indices name in
Andrei Paskevich's avatar
Andrei Paskevich committed
138
    Hid.replace printer.values id name;
139 140
    name

141
let string_unique printer s = find_unique printer.indices s
142

143
let forget_id printer id =
144
  try
Andrei Paskevich's avatar
Andrei Paskevich committed
145
    let name = Hid.find printer.values id in
146
    Hashtbl.remove printer.indices name;
Andrei Paskevich's avatar
Andrei Paskevich committed
147
    Hid.remove printer.values id
148 149
  with Not_found -> ()

150
let forget_all printer =
Andrei Paskevich's avatar
Andrei Paskevich committed
151
  Hid.clear printer.values;
152 153
  Hashtbl.clear printer.indices;
  List.iter (reserve printer.indices) printer.blacklist
154

155 156
(** Sanitizers *)

157
let unsanitizable = Debug.register_flag "unsanitizable"
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

let char_to_alpha c = match c with
  | 'a'..'z' | 'A'..'Z' -> String.make 1 c
  | ' ' -> "sp" | '_'  -> "us" | '#' -> "sh"
  | '`' -> "bq" | '~'  -> "tl" | '!' -> "ex"
  | '@' -> "at" | '$'  -> "dl" | '%' -> "pc"
  | '^' -> "cf" | '&'  -> "et" | '*' -> "as"
  | '(' -> "lp" | ')'  -> "rp" | '-' -> "mn"
  | '+' -> "pl" | '='  -> "eq" | '[' -> "lb"
  | ']' -> "rb" | '{'  -> "lc" | '}' -> "rc"
  | ':' -> "cl" | '\'' -> "qt" | '"' -> "dq"
  | '<' -> "ls" | '>'  -> "gt" | '/' -> "sl"
  | '?' -> "qu" | '\\' -> "bs" | '|' -> "br"
  | ';' -> "sc" | ','  -> "cm" | '.' -> "dt"
  | '0' -> "zr" | '1'  -> "un" | '2' -> "du"
  | '3' -> "tr" | '4'  -> "qr" | '5' -> "qn"
  | '6' -> "sx" | '7'  -> "st" | '8' -> "oc"
175 176 177 178
  | '9' -> "nn" | '\n' -> "br"
  | _ ->
    Debug.dprintf unsanitizable "Unsanitizable : '%c' can't be sanitized@." c;
    "zz"
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

let char_to_lalpha c = String.uncapitalize (char_to_alpha c)
let char_to_ualpha c = String.capitalize (char_to_alpha c)

let char_to_alnum c =
  match c with '0'..'9' -> String.make 1 c | _ -> char_to_alpha c

let char_to_alnumus c =
  match c with '_' | ' ' -> "_" | _ -> char_to_alnum c

let sanitizer head rest n =
  let lst = ref [] in
  let code c = lst := rest c :: !lst in
  let n = if n = "" then "zilch" else n in
  String.iter code n;
  let rst = List.tl (List.rev !lst) in
  let cs = head (String.get n 0) :: rst in
  String.concat "" cs