Module of module types for OrderedType,ComparableType,Printable,Serializable,Discrete...
[ocaml.git] / parsing / longident.ml
blob57652ea1af645539b37b851984402b6ef87fe8e5
1 (***********************************************************************)
2 (* *)
3 (* Objective Caml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed *)
9 (* under the terms of the Q Public License version 1.0. *)
10 (* *)
11 (***********************************************************************)
13 (* $Id$ *)
15 type t =
16 Lident of string
17 | Ldot of t * string
18 | Lapply of t * t
20 let rec flat accu = function
21 Lident s -> s :: accu
22 | Ldot(lid, s) -> flat (s :: accu) lid
23 | Lapply(l1, l2) -> Misc.fatal_error "Longident.flat"
25 let flatten lid = flat [] lid
27 let rec split_at_dots s pos =
28 try
29 let dot = String.index_from s pos '.' in
30 String.sub s pos (dot - pos) :: split_at_dots s (dot + 1)
31 with Not_found ->
32 [String.sub s pos (String.length s - pos)]
34 let parse s =
35 match split_at_dots s 0 with
36 [] -> Lident "" (* should not happen, but don't put assert false
37 so as not to crash the toplevel (see Genprintval) *)
38 | hd :: tl -> List.fold_left (fun p s -> Ldot(p, s)) (Lident hd) tl