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