xref: /original-bsd/usr.bin/pascal/src/yypanic.c (revision 49bee502)
1a67f1cdaSpeter /* Copyright (c) 1979 Regents of the University of California */
2a67f1cdaSpeter 
3*49bee502Sthien #ifndef lint
4*49bee502Sthien static	char sccsid[] = "@(#)yypanic.c 1.4 08/19/83";
5*49bee502Sthien #endif
6a67f1cdaSpeter 
7a67f1cdaSpeter #include "whoami.h"
8a67f1cdaSpeter #include "0.h"
9*49bee502Sthien #include "tree_ty.h"	/* must be included for yy.h */
10a67f1cdaSpeter #include "yy.h"
11a67f1cdaSpeter 
12a67f1cdaSpeter struct yytok oldpos;
13a67f1cdaSpeter /*
14a67f1cdaSpeter  * The routine yyPerror coordinates the panic when
15a67f1cdaSpeter  * the correction routines fail. Three types of panics
16a67f1cdaSpeter  * are possible - those in a declaration part, those
17a67f1cdaSpeter  * in a statement part, and those in an expression.
18a67f1cdaSpeter  *
19a67f1cdaSpeter  * Declaration part panics consider insertion of "begin",
20a67f1cdaSpeter  * expression part panics will stop on more symbols.
21a67f1cdaSpeter  * The panics are otherwise the same.
22a67f1cdaSpeter  *
23a67f1cdaSpeter  * ERROR MESSAGE SUPPRESSION STRATEGY: August 11, 1977
24a67f1cdaSpeter  *
25a67f1cdaSpeter  * If the parser has not made at least 2 moves since the last point of
26a67f1cdaSpeter  * error then we want to suppress the supplied error message.
27a67f1cdaSpeter  * Otherwise we print it.
28a67f1cdaSpeter  * We then skip input up to the next solid symbol.
29a67f1cdaSpeter  */
30a67f1cdaSpeter yyPerror(cp, kind)
31a67f1cdaSpeter 	char *cp;
32a67f1cdaSpeter 	register int kind;
33a67f1cdaSpeter {
34a67f1cdaSpeter 	register int ishifts, brlev;
35a67f1cdaSpeter 
36*49bee502Sthien 	copy((char *) (&oldpos), (char *) (&Y), sizeof oldpos);
37a67f1cdaSpeter 	brlev = 0;
38a67f1cdaSpeter 	if (yychar < 0)
39a67f1cdaSpeter 		yychar = yylex();
40a67f1cdaSpeter 	for (ishifts = yyshifts; ; yychar = yylex(), yyshifts++)
41a67f1cdaSpeter 		switch (yychar) {
42a67f1cdaSpeter 			case YILLCH:
43a67f1cdaSpeter 				yerror("Illegal character");
44a67f1cdaSpeter 				if (ishifts == yyshifts)
45a67f1cdaSpeter 					yyOshifts = 0;
46a67f1cdaSpeter 				continue;
47a67f1cdaSpeter 			case YEOF:
484db1c485Speter 				if (kind == PDECL) {
494db1c485Speter 					/*
504db1c485Speter 					 * we have paniced to end of file
514db1c485Speter 					 * during declarations. Separately
524db1c485Speter 					 * compiled segments can syntactically
534db1c485Speter 					 * exit without any error message, so
544db1c485Speter 					 * we force one here.
554db1c485Speter 					 */
564db1c485Speter 					yerror(cp);
574db1c485Speter 					continuation();
584db1c485Speter 					yyunexeof();
594db1c485Speter 				}
60a67f1cdaSpeter 				goto quiet;
61a67f1cdaSpeter 			case ';':
62a67f1cdaSpeter 				if (kind == PPROG)
63a67f1cdaSpeter 					continue;
64a67f1cdaSpeter 				if (kind == PDECL)
65a67f1cdaSpeter 					yychar = yylex();
66a67f1cdaSpeter 				goto resume;
67a67f1cdaSpeter 			case YEND:
68a67f1cdaSpeter 				if (kind == PPROG)
69a67f1cdaSpeter 					continue;
70a67f1cdaSpeter 			case YPROCEDURE:
71a67f1cdaSpeter 			case YFUNCTION:
72a67f1cdaSpeter 				goto resume;
73a67f1cdaSpeter 			case YLABEL:
74a67f1cdaSpeter 			case YTYPE:
75a67f1cdaSpeter 			case YCONST:
76a67f1cdaSpeter 			case YVAR:
77a67f1cdaSpeter 				if (kind == PSTAT) {
78a67f1cdaSpeter 					yerror("Declaration found when statement expected");
79a67f1cdaSpeter 					goto quiet;
80a67f1cdaSpeter 				}
81a67f1cdaSpeter 			case YBEGIN:
82a67f1cdaSpeter 				goto resume;
83a67f1cdaSpeter 			case YFOR:
84a67f1cdaSpeter 			case YREPEAT:
85a67f1cdaSpeter 			case YWHILE:
86a67f1cdaSpeter 			case YGOTO:
87a67f1cdaSpeter 			case YIF:
88a67f1cdaSpeter 				if (kind != PDECL)
89a67f1cdaSpeter 					goto resume;
90a67f1cdaSpeter 				yerror("Expected keyword begin after declarations, before statements");
91a67f1cdaSpeter 				unyylex(&Y);
92a67f1cdaSpeter 				yychar = YBEGIN;
93a67f1cdaSpeter 				yylval = nullsem(YBEGIN);
94a67f1cdaSpeter 				goto quiet;
95a67f1cdaSpeter 			case YTHEN:
96a67f1cdaSpeter 			case YELSE:
97a67f1cdaSpeter 			case YDO:
98a67f1cdaSpeter 				if (kind == PSTAT) {
99a67f1cdaSpeter 					yychar = yylex();
100a67f1cdaSpeter 					goto resume;
101a67f1cdaSpeter 				}
102a67f1cdaSpeter 				if (kind == PEXPR)
103a67f1cdaSpeter 					goto resume;
104a67f1cdaSpeter 				continue;
105a67f1cdaSpeter 			case ')':
106a67f1cdaSpeter 			case ']':
107a67f1cdaSpeter 				if (kind != PEXPR)
108a67f1cdaSpeter 					continue;
109a67f1cdaSpeter 				if (brlev == 0)
110a67f1cdaSpeter 					goto resume;
111a67f1cdaSpeter 				if (brlev > 0)
112a67f1cdaSpeter 					brlev--;
113a67f1cdaSpeter 				continue;
114a67f1cdaSpeter 			case '(':
115a67f1cdaSpeter 			case '[':
116a67f1cdaSpeter 				brlev++;
117a67f1cdaSpeter 				continue;
118a67f1cdaSpeter 			case ',':
119a67f1cdaSpeter 				if (brlev != 0)
120a67f1cdaSpeter 					continue;
121a67f1cdaSpeter 			case YOF:
122a67f1cdaSpeter 			case YTO:
123a67f1cdaSpeter 			case YDOWNTO:
124a67f1cdaSpeter 				if (kind == PEXPR)
125a67f1cdaSpeter 					goto resume;
126a67f1cdaSpeter 				continue;
127a67f1cdaSpeter #ifdef PI
128a67f1cdaSpeter 			/*
129a67f1cdaSpeter 			 * A rough approximation for now
130a67f1cdaSpeter 			 * Should be much more lenient on suppressing
131a67f1cdaSpeter 			 * warnings.
132a67f1cdaSpeter 			 */
133a67f1cdaSpeter 			case YID:
1340c2b8703Smckusic 				syneflg = TRUE;
135a67f1cdaSpeter 				continue;
136a67f1cdaSpeter #endif
137a67f1cdaSpeter 		}
138a67f1cdaSpeter resume:
139a67f1cdaSpeter 	if (yyOshifts >= 2) {
140a67f1cdaSpeter 		if (yychar != -1)
141a67f1cdaSpeter 			unyylex(&Y);
142*49bee502Sthien 		copy((char *) (&Y), (char *) (&oldpos), sizeof Y);
143a67f1cdaSpeter 		yerror(cp);
144a67f1cdaSpeter 		yychar = yylex();
145a67f1cdaSpeter 	}
146a67f1cdaSpeter quiet:
147a67f1cdaSpeter 	if (yyshifts - ishifts > 2 && opt('r')) {
148a67f1cdaSpeter 		setpfx('r');
149a67f1cdaSpeter 		yerror("Parsing resumes");
150a67f1cdaSpeter 	}
151a67f1cdaSpeter 	/*
152a67f1cdaSpeter 	 * If we paniced in the statement part,
153a67f1cdaSpeter 	 * and didn't stop at a ';', then we insert
154a67f1cdaSpeter 	 * a ';' to prevent the recovery from immediately
155a67f1cdaSpeter 	 * inserting one and complaining about it.
156a67f1cdaSpeter 	 */
157a67f1cdaSpeter 	if (kind == PSTAT && yychar != ';') {
158a67f1cdaSpeter 		unyylex(&Y);
159a67f1cdaSpeter 		yyshifts--;
160a67f1cdaSpeter 		yytshifts--;
161a67f1cdaSpeter 		yychar = ';';
162a67f1cdaSpeter 		yylval = nullsem(';');
163a67f1cdaSpeter 	}
164a67f1cdaSpeter }
165