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