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
|
|
|
|
|
|
|
|
type longname =
|
2010-06-26 16:53:25 +02:00
|
|
|
| Name of name
|
|
|
|
| Modname of qualident
|
2010-06-15 10:49:03 +02:00
|
|
|
|
|
|
|
and qualident = { qual: string; id: string }
|
|
|
|
|
2010-07-07 15:11:32 +02:00
|
|
|
type type_name = longname
|
|
|
|
|
|
|
|
type fun_name = longname
|
|
|
|
|
|
|
|
type field_name = longname
|
|
|
|
|
|
|
|
type constructor_name = longname
|
|
|
|
|
|
|
|
type constant_name = longname
|
|
|
|
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
module NamesM = struct
|
|
|
|
type t = name
|
|
|
|
let compare = compare
|
|
|
|
end
|
|
|
|
|
|
|
|
module NamesEnv =
|
|
|
|
struct
|
|
|
|
include (Map.Make(NamesM))
|
|
|
|
|
|
|
|
let append env0 env =
|
|
|
|
fold (fun key v env -> add key v env) env0 env
|
|
|
|
end
|
|
|
|
|
2010-07-13 14:03:39 +02:00
|
|
|
module LongNameEnv = Map.Make (struct
|
|
|
|
type t = longname
|
|
|
|
let compare = compare
|
|
|
|
end)
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
module S = Set.Make (struct type t = string let compare = compare end)
|
|
|
|
|
2010-06-15 14:05:26 +02:00
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
let shortname = function
|
|
|
|
| Name s -> s
|
|
|
|
| Modname { id = id; } -> id
|
|
|
|
|
|
|
|
let fullname = function
|
|
|
|
| Name s -> s
|
|
|
|
| Modname { qual = qual; id = id; } -> qual ^ "." ^ id
|
|
|
|
|
|
|
|
let mk_longname s =
|
|
|
|
try
|
|
|
|
let ind = String.index s '.' in
|
2010-07-19 11:40:55 +02:00
|
|
|
if ind = 0 || ind = String.length s - 1
|
|
|
|
then invalid_arg "mk_longname: ill-formed identifier";
|
2010-06-15 10:49:03 +02:00
|
|
|
let id = String.sub s (ind + 1) (String.length s - ind - 1) in
|
|
|
|
Modname { qual = String.sub s 0 ind; id = id; }
|
|
|
|
with Not_found -> Name s
|
|
|
|
|
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
|
2010-06-26 16:53:25 +02:00
|
|
|
| 'a' .. 'z' | 'A' .. 'Z' | '_' | '`' -> false
|
|
|
|
| _ -> true)
|
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-06-16 19:31:51 +02:00
|
|
|
in Format.fprintf ff "%s" n
|
2010-06-26 16:53:25 +02:00
|
|
|
|
2010-06-16 19:31:51 +02:00
|
|
|
let print_longname ff n =
|
|
|
|
match n with
|
2010-06-26 16:53:25 +02:00
|
|
|
| Name m -> print_name ff m
|
|
|
|
| Modname { qual = "Pervasives"; id = m } -> print_name ff m
|
|
|
|
| Modname { qual = m1; id = m2 } ->
|
|
|
|
Format.fprintf ff "%s." m1;
|
|
|
|
print_name ff m2
|
2010-06-16 19:31:51 +02:00
|
|
|
|
2010-08-18 23:45:07 +02:00
|
|
|
let opname ln = match ln with
|
|
|
|
| Name n -> n
|
|
|
|
| Modname { qual = "Pervasives"; id = m; } -> m
|
|
|
|
| Modname { qual = qual; id = id; } -> qual ^ "." ^ id
|