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 /* Based on public-domain code from Berkeley Yacc */
17
18 /* routines for printing error messages */
19
20 #include "defs.h"
21
22 /* String displayed if we can't malloc a buffer for the UTF-8 conversion */
23 static char *unknown = "<unknown; out of memory>";
24
25 void fatal(char *msg)
26 {
27 fprintf(stderr, "%s: f - %s\n", myname, msg);
28 done(2);
29 }
30
31
32 void no_space(void)
33 {
34 fprintf(stderr, "%s: f - out of space\n", myname);
35 done(2);
36 }
37
38
39 void open_error(char_os *filename)
40 {
41 char *u8 = caml_stat_strdup_of_os(filename);
42 fprintf(stderr, "%s: f - cannot open \"%s\"\n", myname, (u8 ? u8 : unknown));
43 done(2);
44 }
45
46
47 void unexpected_EOF(void)
48 {
49 fprintf(stderr, "File \"%s\", line %d: unexpected end-of-file\n",
50 virtual_input_file_name, lineno);
51 done(1);
52 }
53
54
55 void print_pos(char *st_line, char *st_cptr)
56 {
57 register char *s;
58
59 if (st_line == 0) return;
60 for (s = st_line; *s != '\n'; ++s)
61 {
62 if (isprint((unsigned char) *s) || *s == '\t')
63 putc(*s, stderr);
64 else
65 putc('?', stderr);
66 }
67 putc('\n', stderr);
68 for (s = st_line; s < st_cptr; ++s)
69 {
70 if (*s == '\t')
71 putc('\t', stderr);
72 else
73 putc(' ', stderr);
74 }
75 putc('^', stderr);
76 putc('\n', stderr);
77 }
78
79
80 void syntax_error(int st_lineno, char *st_line, char *st_cptr)
81 {
82 fprintf(stderr, "File \"%s\", line %d: syntax error\n",
83 virtual_input_file_name, st_lineno);
84 print_pos(st_line, st_cptr);
85 done(1);
86 }
87
88
89 void unterminated_comment(int c_lineno, char *c_line, char *c_cptr)
90 {
91 fprintf(stderr, "File \"%s\", line %d: unmatched /*\n",
92 virtual_input_file_name, c_lineno);
93 print_pos(c_line, c_cptr);
94 done(1);
95 }
96
97
98 void unterminated_string(int s_lineno, char *s_line, char *s_cptr)
99 {
100 fprintf(stderr, "File \"%s\", line %d: unterminated string\n",
101 virtual_input_file_name, s_lineno);
102 print_pos(s_line, s_cptr);
103 done(1);
104 }
105
106
107 void unterminated_text(int t_lineno, char *t_line, char *t_cptr)
108 {
109 fprintf(stderr, "File \"%s\", line %d: unmatched %%{\n",
110 virtual_input_file_name, t_lineno);
111 print_pos(t_line, t_cptr);
112 done(1);
113 }
114
115
116 void illegal_tag(int t_lineno, char *t_line, char *t_cptr)
117 {
118 fprintf(stderr, "File \"%s\", line %d: illegal tag\n",
119 virtual_input_file_name, t_lineno);
120 print_pos(t_line, t_cptr);
121 done(1);
122 }
123
124
125 void illegal_character(char *c_cptr)
126 {
127 fprintf(stderr, "File \"%s\", line %d: illegal character\n",
128 virtual_input_file_name, lineno);
129 print_pos(line, c_cptr);
130 done(1);
131 }
132
133
134 void used_reserved(char *s)
135 {
136 fprintf(stderr, "File \"%s\", line %d: illegal use of reserved symbol \
137 `%s'\n", virtual_input_file_name, lineno, s);
138 done(1);
139 }
140
141
142 void tokenized_start(char *s)
143 {
144 fprintf(stderr, "File \"%s\", line %d: the start symbol `%s' cannot \
145 be declared to be a token\n", virtual_input_file_name, lineno, s);
146 done(1);
147 }
148
149
150 void retyped_warning(char *s)
151 {
152 fprintf(stderr, "File \"%s\", line %d: warning: the type of `%s' has been \
153 redeclared\n", virtual_input_file_name, lineno, s);
154 }
155
156
157 void reprec_warning(char *s)
158 {
159 fprintf(stderr, "File \"%s\", line %d: warning: the precedence of `%s' has \
160 been redeclared\n", virtual_input_file_name, lineno, s);
161 }
162
163
164 void revalued_warning(char *s)
165 {
166 fprintf(stderr, "File \"%s\", line %d: warning: the value of `%s' has been \
167 redeclared\n", virtual_input_file_name, lineno, s);
168 }
169
170
171 void terminal_start(char *s)
172 {
173 fprintf(stderr, "File \"%s\", line %d: the entry point `%s' is a \
174 token\n", virtual_input_file_name, lineno, s);
175 done(1);
176 }
177
178 void too_many_entries(void)
179 {
180 fprintf(stderr, "File \"%s\", line %d: more than 256 entry points\n",
181 virtual_input_file_name, lineno);
182 done(1);
183 }
184
185
186 void no_grammar(void)
187 {
188 fprintf(stderr, "File \"%s\", line %d: no grammar has been specified\n",
189 virtual_input_file_name, lineno);
190 done(1);
191 }
192
193
194 void terminal_lhs(int s_lineno)
195 {
196 fprintf(stderr, "File \"%s\", line %d: a token appears on the lhs \
197 of a production\n", virtual_input_file_name, s_lineno);
198 done(1);
199 }
200
201
202 void prec_redeclared(void)
203 {
204 fprintf(stderr, "File \"%s\", line %d: warning: conflicting %%prec \
205 specifiers\n", virtual_input_file_name, lineno);
206 }
207
208
209 void unterminated_action(int a_lineno, char *a_line, char *a_cptr)
210 {
211 fprintf(stderr, "File \"%s\", line %d: unterminated action\n",
212 virtual_input_file_name, a_lineno);
213 print_pos(a_line, a_cptr);
214 done(1);
215 }
216
217
218 void dollar_warning(int a_lineno, int i)
219 {
220 fprintf(stderr, "File \"%s\", line %d: warning: $%d references beyond the \
221 end of the current rule\n", virtual_input_file_name, a_lineno, i);
222 }
223
224
225 void dollar_error(int a_lineno, char *a_line, char *a_cptr)
226 {
227 fprintf(stderr, "File \"%s\", line %d: illegal $-name\n",
228 virtual_input_file_name, a_lineno);
229 print_pos(a_line, a_cptr);
230 done(1);
231 }
232
233
234 void untyped_lhs(void)
235 {
236 fprintf(stderr, "File \"%s\", line %d: $$ is untyped\n",
237 virtual_input_file_name, lineno);
238 done(1);
239 }
240
241
242 void untyped_rhs(int i, char *s)
243 {
244 fprintf(stderr, "File \"%s\", line %d: $%d (%s) is untyped\n",
245 virtual_input_file_name, lineno, i, s);
246 done(1);
247 }
248
249
250 void unknown_rhs(int i)
251 {
252 fprintf(stderr, "File \"%s\", line %d: $%d is unbound\n",
253 virtual_input_file_name, lineno, i);
254 done(1);
255 }
256
257 void illegal_token_ref(int i, char *name)
258 {
259 fprintf(stderr, "File \"%s\", line %d: $%d refers to terminal `%s', \
260 which has no argument\n",
261 virtual_input_file_name, lineno, i, name);
262 done(1);
263 }
264
265 void default_action_error(void)
266 {
267 fprintf(stderr, "File \"%s\", line %d: no action specified for this \
268 production\n",
269 virtual_input_file_name, lineno);
270 done(1);
271 }
272
273
274 void undefined_goal(char *s)
275 {
276 fprintf(stderr, "%s: e - the start symbol `%s' is undefined\n", myname, s);
277 done(1);
278 }
279
280 void undefined_symbol(char *s)
281 {
282 fprintf(stderr, "%s: e - the symbol `%s' is undefined\n", myname, s);
283 done(1);
284 }
285
286
287 void entry_without_type(char *s)
288 {
289 fprintf(stderr,
290 "%s: e - no type has been declared for the start symbol `%s'\n",
291 myname, s);
292 done(1);
293 }
294
295 void polymorphic_entry_point(char *s)
296 {
297 fprintf(stderr,
298 "%s: e - the start symbol `%s' has a polymorphic type\n",
299 myname, s);
300 done(1);
301 }
302
303 void forbidden_conflicts(void)
304 {
305 fprintf(stderr,
306 "%s: the grammar has conflicts, but --strict was specified\n",
307 myname);
308 done(1);
309 }
310