parse.c revision 1.8 1 /* $NetBSD: parse.c,v 1.8 2019/02/03 03:19:29 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
34 * Copyright (c) 1985 Sun Microsystems, Inc.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 */
65
66 #include <sys/cdefs.h>
67 #ifndef lint
68 #if 0
69 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
70 #else
71 __RCSID("$NetBSD: parse.c,v 1.8 2019/02/03 03:19:29 mrg Exp $");
72 #endif
73 #endif /* not lint */
74
75 #include <stdio.h>
76 #include "indent_globs.h"
77 #include "indent_codes.h"
78
79 /* tk: the code for the construct scanned */
80 void
81 parse(int tk)
82 {
83 int i;
84
85 #ifdef debug
86 printf("%2d - %s\n", tk, token);
87 #endif
88
89 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
90 /* true if we have an if without an else */
91 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::=
92 * stmt reduction */
93 reduce(); /* see if this allows any reduction */
94 }
95
96
97 switch (tk) { /* go on and figure out what to do with the
98 * input */
99
100 case decl: /* scanned a declaration word */
101 ps.search_brace = btype_2;
102 /* indicate that following brace should be on same line */
103 if (ps.p_stack[ps.tos] != decl) { /* only put one
104 * declaration onto
105 * stack */
106 break_comma = true; /* while in declaration,
107 * newline should be forced
108 * after comma */
109 ps.p_stack[++ps.tos] = decl;
110 ps.il[ps.tos] = ps.i_l_follow;
111
112 if (ps.ljust_decl) { /* only do if we want left
113 * justified declarations */
114 ps.ind_level = 0;
115 for (i = ps.tos - 1; i > 0; --i)
116 if (ps.p_stack[i] == decl)
117 ++ps.ind_level; /* indentation is number
118 * of declaration levels
119 * deep we are */
120 ps.i_l_follow = ps.ind_level;
121 }
122 }
123 break;
124
125 case ifstmt: /* scanned if (...) */
126 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
127 ps.i_l_follow = ps.il[ps.tos];
128 /* FALLTHROUGH */
129 case dolit: /* 'do' */
130 case forstmt: /* for (...) */
131 ps.p_stack[++ps.tos] = tk;
132 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
133 ++ps.i_l_follow;/* subsequent statements should be indented 1 */
134 ps.search_brace = btype_2;
135 break;
136
137 case lbrace: /* scanned { */
138 break_comma = false; /* don't break comma in an initial
139 * list */
140 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
141 || ps.p_stack[ps.tos] == stmtl)
142 ++ps.i_l_follow; /* it is a random, isolated
143 * stmt group or a declaration */
144 else {
145 if (s_code == e_code) {
146 /*
147 * only do this if there is nothing on the line
148 */
149 --ps.ind_level;
150 /*
151 * it is a group as part of a while, for, etc.
152 */
153 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
154 --ps.ind_level;
155 /*
156 * for a switch, brace should be two levels out from the code
157 */
158 }
159 }
160
161 ps.p_stack[++ps.tos] = lbrace;
162 ps.il[ps.tos] = ps.ind_level;
163 ps.p_stack[++ps.tos] = stmt;
164 /* allow null stmt between braces */
165 ps.il[ps.tos] = ps.i_l_follow;
166 break;
167
168 case whilestmt: /* scanned while (...) */
169 if (ps.p_stack[ps.tos] == dohead) {
170 /* it is matched with do stmt */
171 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
172 ps.p_stack[++ps.tos] = whilestmt;
173 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
174 } else { /* it is a while loop */
175 ps.p_stack[++ps.tos] = whilestmt;
176 ps.il[ps.tos] = ps.i_l_follow;
177 ++ps.i_l_follow;
178 ps.search_brace = btype_2;
179 }
180
181 break;
182
183 case elselit: /* scanned an else */
184
185 if (ps.p_stack[ps.tos] != ifhead)
186 diag(1, "Unmatched 'else'");
187 else {
188 ps.ind_level = ps.il[ps.tos]; /* indentation for else
189 * should be same as for
190 * if */
191 ps.i_l_follow = ps.ind_level + 1; /* everything following
192 * should be in 1 level */
193 ps.p_stack[ps.tos] = elsehead;
194 /* remember if with else */
195 ps.search_brace = btype_2 | ps.else_if;
196 }
197 break;
198
199 case rbrace: /* scanned a } */
200 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
201 if (ps.p_stack[ps.tos - 1] == lbrace) {
202 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
203 ps.p_stack[ps.tos] = stmt;
204 } else
205 diag(1, "Stmt nesting error.");
206 break;
207
208 case swstmt: /* had switch (...) */
209 ps.p_stack[++ps.tos] = swstmt;
210 ps.cstk[ps.tos] = case_ind;
211 /* save current case indent level */
212 ps.il[ps.tos] = ps.i_l_follow;
213 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
214 * level down from
215 * switch */
216 ps.i_l_follow += ps.case_indent + 1; /* statements should be
217 * two levels in */
218 ps.search_brace = btype_2;
219 break;
220
221 case semicolon: /* this indicates a simple stmt */
222 break_comma = false; /* turn off flag to break after commas
223 * in a declaration */
224 ps.p_stack[++ps.tos] = stmt;
225 ps.il[ps.tos] = ps.ind_level;
226 break;
227
228 default: /* this is an error */
229 diag(1, "Unknown code to parser");
230 return;
231
232
233 } /* end of switch */
234
235 reduce(); /* see if any reduction can be done */
236
237 #ifdef debug
238 for (i = 1; i <= ps.tos; ++i)
239 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
240 printf("\n");
241 #endif
242 }
243 /*
244 * NAME: reduce
245 *
246 * FUNCTION: Implements the reduce part of the parsing algorithm
247 *
248 * ALGORITHM: The following reductions are done. Reductions are repeated
249 * until no more are possible.
250 *
251 * Old TOS New TOS
252 * <stmt> <stmt> <stmtl>
253 * <stmtl> <stmt> <stmtl>
254 * do <stmt> "dostmt"
255 * if <stmt> "ifstmt"
256 * switch <stmt> <stmt>
257 * decl <stmt> <stmt>
258 * "ifelse" <stmt> <stmt>
259 * for <stmt> <stmt>
260 * while <stmt> <stmt>
261 * "dostmt" while <stmt>
262 *
263 * On each reduction, ps.i_l_follow (the indentation for the following line)
264 * is set to the indentation level associated with the old TOS.
265 *
266 * PARAMETERS: None
267 *
268 * RETURNS: Nothing
269 *
270 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
271 *
272 * CALLS: None
273 *
274 * CALLED BY: parse
275 *
276 * HISTORY: initial coding November 1976 D A Willcox of CAC
277 *
278 */
279 /*----------------------------------------------*\
280 | REDUCTION PHASE |
281 \*----------------------------------------------*/
282 void
283 reduce(void)
284 {
285
286 int i;
287
288 for (;;) { /* keep looping until there is nothing left to
289 * reduce */
290
291 switch (ps.p_stack[ps.tos]) {
292
293 case stmt:
294 switch (ps.p_stack[ps.tos - 1]) {
295
296 case stmt:
297 case stmtl:
298 /* stmtl stmt or stmt stmt */
299 ps.p_stack[--ps.tos] = stmtl;
300 break;
301
302 case dolit: /* <do> <stmt> */
303 ps.p_stack[--ps.tos] = dohead;
304 ps.i_l_follow = ps.il[ps.tos];
305 break;
306
307 case ifstmt:
308 /* <if> <stmt> */
309 ps.p_stack[--ps.tos] = ifhead;
310 for (i = ps.tos - 1;
311 (
312 ps.p_stack[i] != stmt
313 &&
314 ps.p_stack[i] != stmtl
315 &&
316 ps.p_stack[i] != lbrace
317 );
318 --i);
319 ps.i_l_follow = ps.il[i];
320 /*
321 * for the time being, we will assume that there is no else on
322 * this if, and set the indentation level accordingly. If an
323 * else is scanned, it will be fixed up later
324 */
325 break;
326
327 case swstmt:
328 /* <switch> <stmt> */
329 case_ind = ps.cstk[ps.tos - 1];
330
331 /* FALLTHROUGH */
332 case decl: /* finish of a declaration */
333 case elsehead:
334 /* <<if> <stmt> else> <stmt> */
335 case forstmt:
336 /* <for> <stmt> */
337 case whilestmt:
338 /* <while> <stmt> */
339 ps.p_stack[--ps.tos] = stmt;
340 ps.i_l_follow = ps.il[ps.tos];
341 break;
342
343 default: /* <anything else> <stmt> */
344 return;
345
346 } /* end of section for <stmt> on top of stack */
347 break;
348
349 case whilestmt:/* while (...) on top */
350 if (ps.p_stack[ps.tos - 1] == dohead) {
351 /* it is termination of a do while */
352 ps.p_stack[--ps.tos] = stmt;
353 break;
354 } else
355 return;
356
357 default: /* anything else on top */
358 return;
359
360 }
361 }
362 }
363