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 (* Auxiliary functions for parsing *)
17
18 type error =
19 Unbound of string
20
21 exception Error of error
22
23 let tbl_ident = (Hashtbl.create 57 : (string, Backend_var.t) Hashtbl.t)
24 let tbl_label = (Hashtbl.create 57 : (string, int) Hashtbl.t)
25
26 let ident_name s =
27 match String.index s '/' with
28 | exception Not_found -> s
29 | n -> String.sub s 0 n
30
31 let bind_ident s =
32 let id = Backend_var.create_local (ident_name s) in
33 Hashtbl.add tbl_ident s id;
34 Backend_var.With_provenance.create id
35
36 let find_ident s =
37 try
38 Hashtbl.find tbl_ident s
39 with Not_found ->
40 raise(Error(Unbound s))
41
42 let unbind_ident id =
43 Hashtbl.remove tbl_ident (Backend_var.With_provenance.name id)
44
45 let find_label s =
46 try
47 Hashtbl.find tbl_label s
48 with Not_found ->
49 let lbl = Lambda.next_raise_count () in
50 Hashtbl.add tbl_label s lbl;
51 lbl
52
53 let report_error = function
54 Unbound s ->
55 prerr_string "Unbound identifier "; prerr_string s; prerr_endline "."
56
57 let debuginfo ?(loc=Location.symbol_rloc ()) () =
58 Debuginfo.(from_location loc)
59