Escape newline in strings
[tiger.ml.git] / compiler / src / lib / tiger / tiger_error.ml
1 module Abs = Tiger_absyn
2 module Pos = Tiger_position
3 module Sym = Tiger_symbol
4 module Typ = Tiger_env_type
5
6 type t =
7 | Invalid_syntax of Pos.t
8 | Unknown_id of {id : Sym.t; pos : Pos.t}
9 | Unknown_type of {ty_id : Sym.t; pos : Pos.t}
10 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
11 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
12 | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t}
13 | Exp_not_a_record of {ty : Typ.t; pos : Pos.t}
14 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
15 | Wrong_type of
16 { expected : Typ.t
17 ; given : Typ.t
18 ; pos : Pos.t
19 }
20 | Wrong_type_of_expression_in_var_dec of
21 { var_id : Sym.t
22 ; expected : Typ.t
23 ; given : Typ.t
24 ; pos : Pos.t
25 }
26 | Wrong_type_used_as_record of
27 { ty_id : Sym.t
28 ; ty : Typ.t
29 ; pos : Pos.t
30 }
31 | Wrong_type_used_as_array of
32 { ty_id : Sym.t
33 ; ty : Typ.t
34 ; pos : Pos.t
35 }
36 | Wrong_type_of_field_value of
37 { field_id : Sym.t
38 ; expected : Typ.t
39 ; given : Typ.t
40 ; pos : Pos.t
41 }
42 | Wrong_type_of_arg of
43 { func : Sym.t
44 ; expected : Typ.t
45 ; given : Typ.t
46 ; pos : Pos.t
47 }
48 | Wrong_number_of_args of
49 { func : Sym.t
50 ; expected : int
51 ; given : int
52 ; pos : Pos.t
53 }
54 | Invalid_operand_type of
55 { oper : Abs.oper
56 ; valid : string list
57 ; given : Typ.t
58 ; pos : Pos.t
59 }
60 | Different_operand_types of
61 { oper : Abs.oper
62 ; left : Typ.t
63 ; right : Typ.t
64 ; pos : Pos.t
65 }
66
67 exception T of t
68
69 let raise t =
70 raise (T t)
71
72 let to_string =
73 let s = Printf.sprintf in
74 function
75 | Invalid_syntax pos ->
76 s "Invalid syntax in %s" (Pos.to_string pos)
77 | Unknown_id {id; pos} ->
78 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
79 | Unknown_type {ty_id; pos} ->
80 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
81 | Id_is_a_function {id; pos} ->
82 s "Identifier %S is a function, it cannot be used as a variable in %s"
83 (Sym.to_string id) (Pos.to_string pos)
84 | Id_not_a_function {id; pos} ->
85 s "Identifier %S is not a function, it cannot be called in %s"
86 (Sym.to_string id) (Pos.to_string pos)
87 | No_such_field_in_record {field; record; pos} ->
88 s "No field %S in record %S in %s"
89 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
90 | Exp_not_a_record {ty; pos} ->
91 s ( "The expression of type %S is not a record, it cannot be"
92 ^^"accessed in %s")
93 (Typ.to_string ty) (Pos.to_string pos)
94 | Exp_not_an_array {ty; pos} ->
95 s ( "The expression of type %S is not an array, it cannot be"
96 ^^"accessed in %s")
97 (Typ.to_string ty) (Pos.to_string pos)
98 | Wrong_type {expected; given; pos} ->
99 s "Type error: expected: %S, but given: %S, in %s"
100 (Typ.to_string expected)
101 (Typ.to_string given)
102 (Pos.to_string pos)
103 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
104 s ( "Wrong type of expression in declaration of %S. "
105 ^^"Expected: %S, given: %S. In %s")
106 (Sym.to_string var_id)
107 (Typ.to_string expected)
108 (Typ.to_string given)
109 (Pos.to_string pos)
110 | Wrong_type_used_as_array {ty_id; ty; pos} ->
111 s ( "Identifier %S is bound to type %S, not an array. "
112 ^^"It cannot be used in %s")
113 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
114 | Wrong_type_used_as_record {ty_id; ty; pos} ->
115 s ( "Identifier %S is bound to type %S, not a record. "
116 ^^"It cannot be used in %s")
117 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
118 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
119 s ( "Field %S is declared to be of type %S, but is bound to expression "
120 ^^"of type %S in %s")
121 (Sym.to_string field_id)
122 (Typ.to_string expected)
123 (Typ.to_string given)
124 (Pos.to_string pos)
125 | Wrong_type_of_arg {func; expected; given; pos} ->
126 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
127 ^^" in %s")
128 (Sym.to_string func)
129 (Typ.to_string expected)
130 (Typ.to_string given)
131 (Pos.to_string pos)
132 | Wrong_number_of_args {func; expected; given; pos} ->
133 s ( "Incorrect number of arguments to function %S, "
134 ^^"expected: %d, given: %d,"
135 ^^" in %s")
136 (Sym.to_string func) expected given (Pos.to_string pos)
137 | Invalid_operand_type {oper; valid; given; pos} ->
138 s ( "Invalid operand type %S for operator %S, which expects only: %s"
139 ^^". In %s")
140 (Typ.to_string given)
141 (Abs.op_show oper)
142 (String.concat ", " valid)
143 (Pos.to_string pos)
144 | Different_operand_types {oper; left; right; pos} ->
145 s "Operands of different types (%S %S %S) given in %s"
146 (Typ.to_string left)
147 (Abs.op_show oper)
148 (Typ.to_string right)
149 (Pos.to_string pos)
150
151 let is_unknown_id t =
152 match t with
153 | Unknown_id _ ->
154 true
155 | Invalid_syntax _
156 | Unknown_type _
157 | Id_is_a_function _
158 | Id_not_a_function _
159 | No_such_field_in_record _
160 | Exp_not_a_record _
161 | Exp_not_an_array _
162 | Wrong_type _
163 | Wrong_type_of_expression_in_var_dec _
164 | Wrong_type_used_as_array _
165 | Wrong_type_used_as_record _
166 | Wrong_type_of_field_value _
167 | Wrong_type_of_arg _
168 | Wrong_number_of_args _
169 | Invalid_operand_type _
170 | Different_operand_types _ ->
171 false
172
173 let is_unknown_type t =
174 match t with
175 | Unknown_type _ ->
176 true
177 | Unknown_id _
178 | Invalid_syntax _
179 | Id_is_a_function _
180 | Id_not_a_function _
181 | No_such_field_in_record _
182 | Exp_not_a_record _
183 | Exp_not_an_array _
184 | Wrong_type _
185 | Wrong_type_of_expression_in_var_dec _
186 | Wrong_type_used_as_array _
187 | Wrong_type_used_as_record _
188 | Wrong_type_of_field_value _
189 | Wrong_type_of_arg _
190 | Wrong_number_of_args _
191 | Invalid_operand_type _
192 | Different_operand_types _ ->
193 false
194
195 let is_wrong_type t =
196 match t with
197 | Wrong_type _ ->
198 true
199 | Unknown_type _
200 | Unknown_id _
201 | Invalid_syntax _
202 | Id_is_a_function _
203 | Id_not_a_function _
204 | No_such_field_in_record _
205 | Exp_not_a_record _
206 | Exp_not_an_array _
207 | Wrong_type_of_expression_in_var_dec _
208 | Wrong_type_used_as_array _
209 | Wrong_type_used_as_record _
210 | Wrong_type_of_field_value _
211 | Wrong_type_of_arg _
212 | Wrong_number_of_args _
213 | Invalid_operand_type _
214 | Different_operand_types _ ->
215 false
This page took 0.090805 seconds and 4 git commands to generate.