1 (******************************************************************************
4 * Copyright (C) 2005- Jane Street Holding, LLC *
5 * Contact: opensource@janestreet.com *
6 * WWW: http://www.janestreet.com/ocaml *
7 * Author: Markus Mottl *
9 * This library is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU Lesser General Public *
11 * License as published by the Free Software Foundation; either *
12 * version 2 of the License, or (at your option) any later version. *
14 * This library is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
19 * You should have received a copy of the GNU Lesser General Public *
20 * License along with this library; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
23 ******************************************************************************)
25 (* Conv_error: Module for Handling Errors during Automated S-expression
31 (* Errors concerning tuples *)
33 let tuple_of_size_n_expected loc n sexp
=
34 of_sexp_error
(sprintf
"%s_of_sexp: tuple of size %d expected" loc n
) sexp
37 (* Errors concerning sum types *)
39 let stag_no_args loc sexp
=
40 of_sexp_error
(loc ^
"_of_sexp: sum tag does not take arguments") sexp
42 let stag_incorrect_n_args loc tag sexp
=
44 sprintf
"%s_of_sexp: sum tag %S has incorrect number of arguments" loc tag
46 of_sexp_error
msg sexp
48 let stag_takes_args loc sexp
=
49 of_sexp_error
(loc ^
"_of_sexp: sum tag must be a structured value") sexp
51 let nested_list_invalid_sum loc sexp
=
52 of_sexp_error
(loc ^
"_of_sexp: a nested list is an invalid sum") sexp
54 let empty_list_invalid_sum loc sexp
=
55 of_sexp_error
(loc ^
"_of_sexp: the empty list is an invalid sum") sexp
57 let unexpected_stag loc sexp
=
58 of_sexp_error
(loc ^
"_of_sexp: unexpected sum tag") sexp
61 (* Errors concerning records *)
63 let record_only_pairs_expected loc sexp
=
66 "_of_sexp: record conversion: only pairs expected, \
67 their first element must be an atom" in
68 of_sexp_error
msg sexp
70 let record_superfluous_fields ~what ~loc rev_fld_names sexp
=
71 let fld_names_str = String.concat
" " (List.rev rev_fld_names
) in
72 let msg = sprintf
"%s_of_sexp: %s: %s" loc what
fld_names_str in
73 of_sexp_error
msg sexp
75 let record_duplicate_fields loc rev_fld_names sexp
=
76 record_superfluous_fields ~what
:"duplicate fields" ~loc rev_fld_names sexp
78 let record_extra_fields loc rev_fld_names sexp
=
79 record_superfluous_fields ~what
:"extra fields" ~loc rev_fld_names sexp
81 let rec record_get_undefined_loop fields
= function
82 | [] -> String.concat
" " (List.rev fields
)
83 | (true, field
) :: rest
-> record_get_undefined_loop (field
:: fields
) rest
84 | _
:: rest
-> record_get_undefined_loop fields rest
86 let record_undefined_elements loc sexp lst
=
87 let undefined = record_get_undefined_loop [] lst
in
89 sprintf
"%s_of_sexp: the following record elements were undefined: %s"
92 of_sexp_error
msg sexp
94 let record_list_instead_atom loc sexp
=
95 let msg = loc ^
"_of_sexp: list instead of atom for record expected" in
96 of_sexp_error
msg sexp
98 let record_poly_field_value loc sexp
=
101 "_of_sexp: cannot convert values of types resulting from polymorphic \
104 of_sexp_error
msg sexp
107 (* Errors concerning polymorphic variants *)
109 exception No_variant_match
of string * Sexp.t
111 let no_variant_match loc sexp
=
112 raise
(No_variant_match
(loc ^
"_of_sexp", sexp
))
114 let no_matching_variant_found loc sexp
=
115 of_sexp_error
(loc ^
": no matching variant found") sexp
117 let ptag_no_args loc sexp
=
119 loc ^
"_of_sexp: polymorphic variant does not take arguments") sexp
121 let ptag_incorrect_n_args loc cnstr sexp
=
124 "%s_of_sexp: polymorphic variant tag %S has incorrect number of arguments"
127 of_sexp_error
msg sexp
129 let nested_list_invalid_poly_var loc sexp
=
131 loc ^
"_of_sexp: a nested list is an invalid polymorphic variant") sexp
133 let empty_list_invalid_poly_var loc sexp
=
135 loc ^
"_of_sexp: the empty list is an invalid polymorphic variant") sexp
137 let silly_type loc sexp
=
138 of_sexp_error
(loc ^
"_of_sexp: trying to convert a silly type") sexp
140 let empty_type loc sexp
=
141 of_sexp_error
(loc ^
"_of_sexp: trying to convert an empty type") sexp