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