xref: /netbsd-src/external/bsd/byacc/dist/error.c (revision 3d8f5b8dd9f188f856affd984b68f2960ff20d65)
1 /*	$NetBSD: error.c,v 1.15 2024/09/14 21:29:02 christos Exp $	*/
2 
3 #include "defs.h"
4 
5 #include <sys/cdefs.h>
6 __RCSID("$NetBSD: error.c,v 1.15 2024/09/14 21:29:02 christos Exp $");
7 /* Id: error.c,v 1.17 2023/05/18 21:38:35 tom Exp  */
8 
9 /* routines for printing error messages  */
10 
11 __dead void
12 fatal(const char *msg)
13 {
14     fprintf(stderr, "%s: f - %s\n", myname, msg);
15     done(2);
16 }
17 
18 __dead void
19 on_error(void)
20 {
21     const char *msg;
22     if (errno && (msg = strerror(errno)) != NULL)
23 	fatal(msg);
24     else
25 	fatal("unknown error");
26 }
27 
28 __dead void
29 open_error(const char *filename)
30 {
31     fprintf(stderr, "%s: f - cannot open \"%s\"\n", myname, filename);
32     done(2);
33 }
34 
35 void
36 missing_brace(void)
37 {
38     fprintf(stderr, "%s: e - line %d of \"%s\", missing '}'\n",
39 	    myname, lineno, input_file_name);
40     done(1);
41 }
42 
43 void
44 unexpected_EOF(void)
45 {
46     fprintf(stderr, "%s: e - line %d of \"%s\", unexpected end-of-file\n",
47 	    myname, lineno, input_file_name);
48     done(1);
49 }
50 
51 static void
52 print_pos(const char *st_line, const char *st_cptr)
53 {
54     const char *s;
55 
56     if (st_line == 0)
57 	return;
58     for (s = st_line; *s != '\n'; ++s)
59     {
60 	if (isprint(UCH(*s)) || *s == '\t')
61 	    putc(*s, stderr);
62 	else
63 	    putc('?', stderr);
64     }
65     putc('\n', stderr);
66     for (s = st_line; s < st_cptr; ++s)
67     {
68 	if (*s == '\t')
69 	    putc('\t', stderr);
70 	else
71 	    putc(' ', stderr);
72     }
73     putc('^', stderr);
74     putc('\n', stderr);
75 }
76 
77 __dead void
78 syntax_error(int st_lineno, const char *st_line, const char *st_cptr)
79 {
80     fprintf(stderr, "%s: e - line %d of \"%s\", syntax error\n",
81 	    myname, st_lineno, input_file_name);
82     print_pos(st_line, st_cptr);
83     done(1);
84 }
85 
86 __dead void
87 unexpected_value(const struct ainfo *a)
88 {
89     fprintf(stderr, "%s: e - line %d of \"%s\", unexpected value\n",
90 	    myname, a->a_lineno, input_file_name);
91     print_pos(a->a_line, a->a_cptr);
92     done(1);
93 }
94 
95 __dead void
96 unterminated_comment(const struct ainfo *a)
97 {
98     fprintf(stderr, "%s: e - line %d of \"%s\", unmatched /*\n",
99 	    myname, a->a_lineno, input_file_name);
100     print_pos(a->a_line, a->a_cptr);
101     done(1);
102 }
103 
104 __dead void
105 unterminated_string(const struct ainfo *a)
106 {
107     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated string\n",
108 	    myname, a->a_lineno, input_file_name);
109     print_pos(a->a_line, a->a_cptr);
110     done(1);
111 }
112 
113 __dead void
114 unterminated_text(const struct ainfo *a)
115 {
116     fprintf(stderr, "%s: e - line %d of \"%s\", unmatched %%{\n",
117 	    myname, a->a_lineno, input_file_name);
118     print_pos(a->a_line, a->a_cptr);
119     done(1);
120 }
121 
122 __dead void
123 unterminated_union(const struct ainfo *a)
124 {
125     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated %%union \
126 declaration\n", myname, a->a_lineno, input_file_name);
127     print_pos(a->a_line, a->a_cptr);
128     done(1);
129 }
130 
131 __dead void
132 over_unionized(const char *u_cptr)
133 {
134     fprintf(stderr, "%s: e - line %d of \"%s\", too many %%union \
135 declarations\n", myname, lineno, input_file_name);
136     print_pos(line, u_cptr);
137     done(1);
138 }
139 
140 __dead void
141 illegal_tag(int t_lineno, const char *t_line, const char *t_cptr)
142 {
143     fprintf(stderr, "%s: e - line %d of \"%s\", illegal tag\n",
144 	    myname, t_lineno, input_file_name);
145     print_pos(t_line, t_cptr);
146     done(1);
147 }
148 
149 __dead void
150 illegal_character(const char *c_cptr)
151 {
152     fprintf(stderr, "%s: e - line %d of \"%s\", illegal character\n",
153 	    myname, lineno, input_file_name);
154     print_pos(line, c_cptr);
155     done(1);
156 }
157 
158 __dead void
159 used_reserved(const char *s)
160 {
161     fprintf(stderr,
162 	    "%s: e - line %d of \"%s\", illegal use of reserved symbol \
163 %s\n", myname, lineno, input_file_name, s);
164     done(1);
165 }
166 
167 __dead void
168 tokenized_start(const char *s)
169 {
170     fprintf(stderr,
171 	    "%s: e - line %d of \"%s\", the start symbol %s cannot be \
172 declared to be a token\n", myname, lineno, input_file_name, s);
173     done(1);
174 }
175 
176 void
177 retyped_warning(const char *s)
178 {
179     fprintf(stderr, "%s: w - line %d of \"%s\", the type of %s has been \
180 redeclared\n", myname, lineno, input_file_name, s);
181 }
182 
183 void
184 reprec_warning(const char *s)
185 {
186     fprintf(stderr,
187 	    "%s: w - line %d of \"%s\", the precedence of %s has been \
188 redeclared\n", myname, lineno, input_file_name, s);
189 }
190 
191 void
192 revalued_warning(const char *s)
193 {
194     fprintf(stderr, "%s: w - line %d of \"%s\", the value of %s has been \
195 redeclared\n", myname, lineno, input_file_name, s);
196 }
197 
198 void
199 terminal_start(const char *s)
200 {
201     fprintf(stderr, "%s: e - line %d of \"%s\", the start symbol %s is a \
202 token\n", myname, lineno, input_file_name, s);
203     done(1);
204 }
205 
206 void
207 restarted_warning(void)
208 {
209     fprintf(stderr, "%s: w - line %d of \"%s\", the start symbol has been \
210 redeclared\n", myname, lineno, input_file_name);
211 }
212 
213 void
214 no_grammar(void)
215 {
216     fprintf(stderr, "%s: e - line %d of \"%s\", no grammar has been \
217 specified\n", myname, lineno, input_file_name);
218     done(1);
219 }
220 
221 void
222 terminal_lhs(int s_lineno)
223 {
224     fprintf(stderr, "%s: e - line %d of \"%s\", a token appears on the lhs \
225 of a production\n", myname, s_lineno, input_file_name);
226     done(1);
227 }
228 
229 void
230 prec_redeclared(void)
231 {
232     fprintf(stderr, "%s: w - line %d of  \"%s\", conflicting %%prec \
233 specifiers\n", myname, lineno, input_file_name);
234 }
235 
236 void
237 unterminated_action(const struct ainfo *a)
238 {
239     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated action\n",
240 	    myname, a->a_lineno, input_file_name);
241     print_pos(a->a_line, a->a_cptr);
242     done(1);
243 }
244 
245 void
246 dollar_warning(int a_lineno, int i)
247 {
248     fprintf(stderr, "%s: w - line %d of \"%s\", $%d references beyond the \
249 end of the current rule\n", myname, a_lineno, input_file_name, i);
250 }
251 
252 __dead void
253 dollar_error(int a_lineno, const char *a_line, const char *a_cptr)
254 {
255     fprintf(stderr, "%s: e - line %d of \"%s\", illegal $-name\n",
256 	    myname, a_lineno, input_file_name);
257     print_pos(a_line, a_cptr);
258     done(1);
259 }
260 
261 void
262 dislocations_warning(void)
263 {
264     fprintf(stderr, "%s: e - line %d of \"%s\", expected %%locations\n",
265 	    myname, lineno, input_file_name);
266 }
267 
268 __dead void
269 untyped_lhs(void)
270 {
271     fprintf(stderr, "%s: e - line %d of \"%s\", $$ is untyped\n",
272 	    myname, lineno, input_file_name);
273     done(1);
274 }
275 
276 __dead void
277 untyped_rhs(int i, const char *s)
278 {
279     fprintf(stderr, "%s: e - line %d of \"%s\", $%d (%s) is untyped\n",
280 	    myname, lineno, input_file_name, i, s);
281     done(1);
282 }
283 
284 __dead void
285 unknown_rhs(int i)
286 {
287     fprintf(stderr, "%s: e - line %d of \"%s\", $%d is untyped\n",
288 	    myname, lineno, input_file_name, i);
289     done(1);
290 }
291 
292 void
293 default_action_warning(const char *s)
294 {
295     fprintf(stderr,
296 	    "%s: w - line %d of \"%s\", the default action for %s assigns an \
297 undefined value to $$\n",
298 	    myname, lineno, input_file_name, s);
299 }
300 
301 void
302 undefined_goal(const char *s)
303 {
304     fprintf(stderr, "%s: e - the start symbol %s is undefined\n", myname, s);
305     done(1);
306 }
307 
308 void
309 undefined_symbol_warning(const char *s)
310 {
311     fprintf(stderr, "%s: w - the symbol %s is undefined\n", myname, s);
312 }
313 
314 #if ! defined(YYBTYACC)
315 void
316 unsupported_flag_warning(const char *flag, const char *details)
317 {
318     fprintf(stderr, "%s: w - %s flag unsupported, %s\n",
319 	    myname, flag, details);
320 }
321 #endif
322 
323 #if defined(YYBTYACC)
324 void
325 at_warning(int a_lineno, int i)
326 {
327     fprintf(stderr, "%s: w - line %d of \"%s\", @%d references beyond the \
328 end of the current rule\n", myname, a_lineno, input_file_name, i);
329 }
330 
331 void
332 at_error(int a_lineno, const char *a_line, const char *a_cptr)
333 {
334     fprintf(stderr,
335 	    "%s: e - line %d of \"%s\", illegal @$ or @N reference\n",
336 	    myname, a_lineno, input_file_name);
337     print_pos(a_line, a_cptr);
338     done(1);
339 }
340 
341 void
342 unterminated_arglist(const struct ainfo *a)
343 {
344     fprintf(stderr,
345 	    "%s: e - line %d of \"%s\", unterminated argument list\n",
346 	    myname, a->a_lineno, input_file_name);
347     print_pos(a->a_line, a->a_cptr);
348     done(1);
349 }
350 
351 void
352 arg_number_disagree_warning(int a_lineno, const char *a_name)
353 {
354     fprintf(stderr, "%s: w - line %d of \"%s\", number of arguments of %s "
355 	    "doesn't agree with previous declaration\n",
356 	    myname, a_lineno, input_file_name, a_name);
357 }
358 
359 void
360 bad_formals(void)
361 {
362     fprintf(stderr, "%s: e - line %d of \"%s\", bad formal argument list\n",
363 	    myname, lineno, input_file_name);
364     print_pos(line, cptr);
365     done(1);
366 }
367 
368 void
369 arg_type_disagree_warning(int a_lineno, int i, const char *a_name)
370 {
371     fprintf(stderr, "%s: w - line %d of \"%s\", type of argument %d "
372 	    "to %s doesn't agree with previous declaration\n",
373 	    myname, a_lineno, input_file_name, i, a_name);
374 }
375 
376 void
377 unknown_arg_warning(int d_lineno, const char *dlr_opt,
378 		    const char *d_arg,
379 		    const char *d_line,
380 		    const char *d_cptr)
381 {
382     fprintf(stderr, "%s: w - line %d of \"%s\", unknown argument %s%s\n",
383 	    myname, d_lineno, input_file_name, dlr_opt, d_arg);
384     print_pos(d_line, d_cptr);
385 }
386 
387 void
388 untyped_arg_warning(int a_lineno, const char *dlr_opt, const char *a_name)
389 {
390     fprintf(stderr, "%s: w - line %d of \"%s\", untyped argument %s%s\n",
391 	    myname, a_lineno, input_file_name, dlr_opt, a_name);
392 }
393 
394 void
395 wrong_number_args_warning(const char *which, const char *a_name)
396 {
397     fprintf(stderr,
398 	    "%s: w - line %d of \"%s\", wrong number of %sarguments for %s\n",
399 	    myname, lineno, input_file_name, which, a_name);
400     print_pos(line, cptr);
401 }
402 
403 void
404 wrong_type_for_arg_warning(int i, const char *a_name)
405 {
406     fprintf(stderr,
407 	    "%s: w - line %d of \"%s\", wrong type for default argument %d to %s\n",
408 	    myname, lineno, input_file_name, i, a_name);
409     print_pos(line, cptr);
410 }
411 
412 void
413 start_requires_args(const char *a_name)
414 {
415     fprintf(stderr,
416 	    "%s: w - line %d of \"%s\", start symbol %s requires arguments\n",
417 	    myname, 0, input_file_name, a_name);
418 
419 }
420 
421 void
422 destructor_redeclared_warning(const struct ainfo *a)
423 {
424     fprintf(stderr, "%s: w - line %d of \"%s\", destructor redeclared\n",
425 	    myname, a->a_lineno, input_file_name);
426     print_pos(a->a_line, a->a_cptr);
427 }
428 #endif
429