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