parse.c revision 1.56 1 /* $NetBSD: parse.c,v 1.56 2023/05/15 07:28:45 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 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: parse.c,v 1.56 2023/05/15 07:28:45 rillig Exp $");
42
43 #include <err.h>
44 #include <stdio.h>
45
46 #include "indent.h"
47
48 static void reduce(void);
49
50 static int
51 decl_level(void)
52 {
53 int level = 0;
54 for (int i = ps.tos - 1; i > 0; i--)
55 if (ps.s_sym[i] == psym_decl)
56 level++;
57 return level;
58 }
59
60 /*
61 * Shift the token onto the parser stack, or reduce it by combining it with
62 * previous tokens.
63 */
64 void
65 parse(parser_symbol psym)
66 {
67 debug_println("parse token: %s", psym_name[psym]);
68
69 if (psym != psym_else) {
70 while (ps.s_sym[ps.tos] == psym_if_expr_stmt) {
71 ps.s_sym[ps.tos] = psym_stmt;
72 reduce();
73 }
74 }
75
76 switch (psym) {
77
78 case psym_decl:
79 if (ps.s_sym[ps.tos] == psym_decl)
80 break; /* only put one declaration onto stack */
81
82 break_comma = true; /* while in a declaration, force a newline
83 * after comma */
84 ps.s_sym[++ps.tos] = psym_decl;
85 ps.s_ind_level[ps.tos] = ps.ind_level_follow;
86
87 if (opt.ljust_decl)
88 ps.ind_level_follow = ps.ind_level = decl_level();
89 break;
90
91 case psym_if_expr:
92 if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else && opt.else_if) {
93 /*
94 * Reduce "else if" to "if". This saves a lot of stack space in
95 * case of a long "if-else-if ... else-if" sequence.
96 */
97 ps.ind_level_follow = ps.s_ind_level[ps.tos--];
98 }
99 /* FALLTHROUGH */
100 case psym_do:
101 case psym_for_exprs:
102 ps.s_sym[++ps.tos] = psym;
103 ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
104 ++ps.ind_level_follow; /* subsequent statements should be indented 1 */
105 break;
106
107 case psym_lbrace:
108 break_comma = false; /* don't break comma in an initializer list */
109 if (ps.s_sym[ps.tos] == psym_stmt || ps.s_sym[ps.tos] == psym_decl
110 || ps.s_sym[ps.tos] == psym_stmt_list)
111 ++ps.ind_level_follow; /* it is a random, isolated stmt group
112 * or a declaration */
113 else {
114 if (code.len == 0) {
115 /* it is a group as part of a while, for, etc. */
116 --ps.ind_level;
117
118 /*
119 * for a switch, brace should be two levels out from the code
120 */
121 if (ps.s_sym[ps.tos] == psym_switch_expr &&
122 opt.case_indent >= 1.0F)
123 --ps.ind_level;
124 }
125 }
126
127 ps.s_sym[++ps.tos] = psym_lbrace;
128 ps.s_ind_level[ps.tos] = ps.ind_level;
129 ps.s_sym[++ps.tos] = psym_stmt;
130 ps.s_ind_level[ps.tos] = ps.ind_level_follow;
131 break;
132
133 case psym_while_expr:
134 if (ps.s_sym[ps.tos] == psym_do_stmt) {
135 /* it is matched with do stmt */
136 ps.ind_level = ps.ind_level_follow = ps.s_ind_level[ps.tos];
137 ps.s_sym[++ps.tos] = psym_while_expr;
138 ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
139
140 } else { /* it is a while loop */
141 ps.s_sym[++ps.tos] = psym_while_expr;
142 ps.s_ind_level[ps.tos] = ps.ind_level_follow;
143 ++ps.ind_level_follow;
144 }
145
146 break;
147
148 case psym_else:
149 if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
150 diag(1, "Unmatched 'else'");
151 else {
152 ps.ind_level = ps.s_ind_level[ps.tos];
153 ps.ind_level_follow = ps.ind_level + 1;
154 ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
155 }
156 break;
157
158 case psym_rbrace:
159 /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
160 if (ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace) {
161 ps.ind_level = ps.ind_level_follow = ps.s_ind_level[--ps.tos];
162 ps.s_sym[ps.tos] = psym_stmt;
163 } else
164 diag(1, "Statement nesting error");
165 break;
166
167 case psym_switch_expr:
168 ps.s_sym[++ps.tos] = psym_switch_expr;
169 ps.s_case_ind_level[ps.tos] = case_ind;
170 ps.s_ind_level[ps.tos] = ps.ind_level_follow;
171 case_ind = (float)ps.ind_level_follow + opt.case_indent;
172 ps.ind_level_follow += (int)opt.case_indent + 1;
173 break;
174
175 case psym_0: /* a simple statement */
176 break_comma = false; /* don't break after comma in a declaration */
177 ps.s_sym[++ps.tos] = psym_stmt;
178 ps.s_ind_level[ps.tos] = ps.ind_level;
179 break;
180
181 default:
182 diag(1, "Unknown code to parser");
183 return;
184 }
185
186 if (ps.tos >= STACKSIZE - 1)
187 errx(1, "Parser stack overflow");
188
189 debug_parse_stack("before reduction");
190 reduce(); /* see if any reduction can be done */
191 debug_parse_stack("after reduction");
192 }
193
194 /*
195 * Try to combine the statement on the top of the parse stack with the symbol
196 * directly below it, replacing these two symbols with a single symbol.
197 */
198 static bool
199 reduce_stmt(void)
200 {
201 switch (ps.s_sym[ps.tos - 1]) {
202
203 case psym_stmt:
204 case psym_stmt_list:
205 ps.s_sym[--ps.tos] = psym_stmt_list;
206 return true;
207
208 case psym_do:
209 ps.s_sym[--ps.tos] = psym_do_stmt;
210 ps.ind_level_follow = ps.s_ind_level[ps.tos];
211 return true;
212
213 case psym_if_expr:
214 ps.s_sym[--ps.tos] = psym_if_expr_stmt;
215 int i = ps.tos - 1;
216 while (ps.s_sym[i] != psym_stmt &&
217 ps.s_sym[i] != psym_stmt_list &&
218 ps.s_sym[i] != psym_lbrace)
219 --i;
220 ps.ind_level_follow = ps.s_ind_level[i];
221 /*
222 * For the time being, assume that there is no 'else' on this 'if',
223 * and set the indentation level accordingly. If an 'else' is scanned,
224 * it will be fixed up later.
225 */
226 return true;
227
228 case psym_switch_expr:
229 case_ind = ps.s_case_ind_level[ps.tos - 1];
230 /* FALLTHROUGH */
231 case psym_decl: /* finish of a declaration */
232 case psym_if_expr_stmt_else:
233 case psym_for_exprs:
234 case psym_while_expr:
235 ps.s_sym[--ps.tos] = psym_stmt;
236 ps.ind_level_follow = ps.s_ind_level[ps.tos];
237 return true;
238
239 default:
240 return false;
241 }
242 }
243
244 /*
245 * Repeatedly try to reduce the top two symbols on the parse stack to a
246 * single symbol, until no more reductions are possible.
247 */
248 static void
249 reduce(void)
250 {
251 again:
252 if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt())
253 goto again;
254 if (ps.s_sym[ps.tos] == psym_while_expr &&
255 ps.s_sym[ps.tos - 1] == psym_do_stmt) {
256 ps.tos -= 2;
257 goto again;
258 }
259 }
260