1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 type t =
17 Lident of string
18 | Ldot of t * string
19 | Lapply of t * t
20
21 let rec flat accu = function
22 Lident s -> s :: accu
23 | Ldot(lid, s) -> flat (s :: accu) lid
24 | Lapply(_, _) -> Misc.fatal_error "Longident.flat"
25
26 let flatten lid = flat [] lid
27
28 let last = function
29 Lident s -> s
30 | Ldot(_, s) -> s
31 | Lapply(_, _) -> Misc.fatal_error "Longident.last"
32
33 let rec split_at_dots s pos =
34 try
35 let dot = String.index_from s pos '.' in
36 String.sub s pos (dot - pos) :: split_at_dots s (dot + 1)
37 with Not_found ->
38 [String.sub s pos (String.length s - pos)]
39
40 let unflatten l =
41 match l with
42 | [] -> None
43 | hd :: tl -> Some (List.fold_left (fun p s -> Ldot(p, s)) (Lident hd) tl)
44
45 let parse s =
46 match unflatten (split_at_dots s 0) with
47 | None -> Lident "" (* should not happen, but don't put assert false
48 so as not to crash the toplevel (see Genprintval) *)
49 | Some v -> v
50