forked from tturpin/ocaml-binannot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longident.ml
55 lines (45 loc) · 1.9 KB
/
longident.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(***********************************************************************)
(* *)
(* Objective Caml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the Q Public License version 1.0. *)
(* *)
(***********************************************************************)
(* $Id$ *)
type lid =
Lident of string
| Ldot of lid * string
| Lapply of lid * lid
let rec flat accu = function
Lident s -> s :: accu
| Ldot(lid, s) -> flat (s :: accu) lid
| Lapply(l1, l2) -> Misc.fatal_error "Longident.flat"
let flatten lid = flat [] lid
let last = function
Lident s -> s
| Ldot(lid, s) -> s
| Lapply(l1, l2) -> Misc.fatal_error "Longident.last"
let rec split_at_dots s pos =
try
let dot = String.index_from s pos '.' in
String.sub s pos (dot - pos) :: split_at_dots s (dot + 1)
with Not_found ->
[String.sub s pos (String.length s - pos)]
let parse s =
match split_at_dots s 0 with
[] -> Lident "" (* should not happen, but don't put assert false
so as not to crash the toplevel (see Genprintval) *)
| hd :: tl -> List.fold_left (fun p s -> Ldot(p, s)) (Lident hd) tl
type t = {
lid : lid;
loc : Location.t
}
let flatten lid = flatten lid.lid
let last lid = last lid.lid
let parse loc lid = {lid = parse lid ; loc = loc}
let longident loc lid = {lid = lid ; loc = loc}
let lident loc name = longident loc (Lident name)