2010-06-15 14:05:26 +02:00
|
|
|
(** Define qualified names "Module.name" (longname)
|
2010-06-26 16:53:25 +02:00
|
|
|
[shortname] longname -> name
|
|
|
|
[fullname] longname -> Module.name *)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
type name = string
|
2011-02-07 14:24:17 +01:00
|
|
|
type module_name = name
|
|
|
|
|
|
|
|
type modul =
|
|
|
|
| Pervasives
|
|
|
|
| LocalModule
|
|
|
|
| Module of module_name
|
|
|
|
| QualModule of qualname
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
and qualname = { qual: modul; name: name }
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
type type_name = qualname
|
|
|
|
type fun_name = qualname
|
|
|
|
type field_name = qualname
|
|
|
|
type constructor_name = qualname
|
|
|
|
type constant_name = qualname
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-07-07 15:11:32 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let pervasives_qn name = { qual = Pervasives; name = name }
|
|
|
|
|
|
|
|
let local_qn name = { qual = LocalModule; name = name }
|
|
|
|
|
2010-07-07 15:11:32 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
module NamesEnv = struct
|
|
|
|
include (Map.Make(struct type t = name let compare = compare end))
|
|
|
|
let append env0 env = fold (fun key v env -> add key v env) env0 env
|
2010-06-15 10:49:03 +02:00
|
|
|
end
|
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
module ModulEnv = struct
|
|
|
|
include (Map.Make(struct type t = modul let compare = compare end))
|
|
|
|
let append env0 env = fold (fun key v env -> add key v env) env0 env
|
|
|
|
end
|
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
module QualEnv = struct
|
|
|
|
include (Map.Make(struct type t = qualname let compare = compare end))
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
(** [append env' env] appends env' to env *)
|
|
|
|
let append env' env = fold (fun key v env -> add key v env) env' env
|
2010-06-15 10:49:03 +02:00
|
|
|
end
|
|
|
|
|
2011-05-12 00:54:02 +02:00
|
|
|
module NamesSet = Set.Make (struct type t = string let compare = compare end)
|
2010-09-10 13:59:38 +02:00
|
|
|
module QualSet = Set.Make (struct type t = qualname let compare = compare end)
|
2011-03-08 09:22:02 +01:00
|
|
|
module ModulSet = Set.Make (struct type t = modul let compare = compare end)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-06-15 14:05:26 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let shortname { name = n; } = n
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-02-07 14:24:17 +01:00
|
|
|
let modul { qual = m; } = m
|
|
|
|
|
|
|
|
let rec modul_to_string m = match m with
|
|
|
|
| Pervasives -> "Pervasives"
|
2011-06-09 14:12:32 +02:00
|
|
|
| LocalModule -> "#$%@#_LOCAL_MODULE"
|
2011-02-07 14:24:17 +01:00
|
|
|
| Module n -> n
|
|
|
|
| QualModule {qual = q; name = n} -> (modul_to_string q) ^"."^ n
|
|
|
|
|
|
|
|
let fullname {qual = q; name = n} = modul_to_string q ^ "." ^ n
|
|
|
|
|
|
|
|
let rec modul_of_string_list = function
|
|
|
|
| [] -> LocalModule
|
|
|
|
| ["Pervasives"] -> Pervasives
|
|
|
|
| [q] -> Module q
|
|
|
|
| q::q_l -> QualModule {qual = modul_of_string_list q_l; name = q}
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
let qualname_of_string s =
|
2011-02-07 14:24:17 +01:00
|
|
|
let q_l_n = Misc.split_string s "." in
|
|
|
|
match List.rev q_l_n with
|
2011-05-23 09:24:57 +02:00
|
|
|
| [] -> Misc.internal_error "Names"
|
2011-02-07 14:24:17 +01:00
|
|
|
| n::q_l -> { qual = modul_of_string_list q_l; name = n }
|
|
|
|
|
|
|
|
let modul_of_string s =
|
|
|
|
let q_l = Misc.split_string s "." in
|
|
|
|
modul_of_string_list (List.rev q_l)
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
(** Are infix
|
2010-06-26 16:53:25 +02:00
|
|
|
[or], [quo], [mod], [land], [lor], [lxor], [lsl], [lsr], [asr]
|
|
|
|
and every names not beginning with 'a' .. 'z' | 'A' .. 'Z' | '_' | '`'*)
|
2010-06-16 19:31:51 +02:00
|
|
|
let is_infix s =
|
|
|
|
let module StrSet = Set.Make(String) in
|
|
|
|
let infix_set =
|
|
|
|
List.fold_right StrSet.add
|
|
|
|
["or"; "quo"; "mod"; "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr"]
|
|
|
|
StrSet.empty in
|
|
|
|
if StrSet.mem s infix_set then true
|
|
|
|
else (match String.get s 0 with
|
2011-02-07 14:24:17 +01:00
|
|
|
| 'a' .. 'z' | 'A' .. 'Z' | '_' | '`' | '~' -> false
|
2010-06-26 16:53:25 +02:00
|
|
|
| _ -> true)
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-09-09 00:35:06 +02:00
|
|
|
open Format
|
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
let print_name ff n =
|
|
|
|
let n = if is_infix n
|
2010-06-26 16:53:25 +02:00
|
|
|
then "( " ^ (n ^ " )") (* do not remove the space around n, since for example
|
|
|
|
"(*" would create bugs *)
|
|
|
|
else n
|
2010-09-09 00:35:06 +02:00
|
|
|
in fprintf ff "%s" n
|
|
|
|
|
2010-12-14 18:29:55 +01:00
|
|
|
(** Use a printer to generate a string compatible with a name *)
|
|
|
|
let print_pp_to_name p x =
|
|
|
|
Misc.sanitize_string (Misc.print_pp_to_string p x)
|