parse.c revision 1.82 1 1.82 rillig /* $NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $ */
2 1.3 tls
3 1.9 kamil /*-
4 1.9 kamil * SPDX-License-Identifier: BSD-4-Clause
5 1.9 kamil *
6 1.9 kamil * Copyright (c) 1985 Sun Microsystems, Inc.
7 1.4 mrg * Copyright (c) 1980, 1993
8 1.4 mrg * 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.5 lukem #include <sys/cdefs.h>
41 1.82 rillig __RCSID("$NetBSD: parse.c,v 1.82 2025/01/04 21:54:26 rillig Exp $");
42 1.1 cgd
43 1.76 rillig #include <stdlib.h>
44 1.12 rillig
45 1.9 kamil #include "indent.h"
46 1.9 kamil
47 1.82 rillig /* Replace the top 2 symbols with the given symbol. */
48 1.82 rillig static void
49 1.82 rillig psyms_replace2(parser_symbol psym)
50 1.82 rillig {
51 1.82 rillig ps.psyms.len--;
52 1.82 rillig ps.psyms.sym[ps.psyms.len - 1] = psym;
53 1.82 rillig }
54 1.82 rillig
55 1.68 rillig /*
56 1.68 rillig * Try to combine the statement on the top of the parse stack with the symbol
57 1.68 rillig * directly below it, replacing these two symbols with a single symbol.
58 1.68 rillig */
59 1.68 rillig static bool
60 1.77 rillig psyms_reduce_stmt(void)
61 1.68 rillig {
62 1.79 rillig switch (ps.psyms.sym[ps.psyms.len - 2]) {
63 1.68 rillig
64 1.68 rillig case psym_stmt:
65 1.82 rillig psyms_replace2(psym_stmt);
66 1.82 rillig ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
67 1.68 rillig return true;
68 1.68 rillig
69 1.68 rillig case psym_do:
70 1.82 rillig psyms_replace2(psym_do_stmt);
71 1.79 rillig ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
72 1.68 rillig return true;
73 1.68 rillig
74 1.68 rillig case psym_if_expr:
75 1.82 rillig psyms_replace2(psym_if_expr_stmt);
76 1.68 rillig /* For the time being, assume that there is no 'else' on this
77 1.68 rillig * 'if', and set the indentation level accordingly. If an
78 1.68 rillig * 'else' is scanned, it will be fixed up later. */
79 1.79 rillig size_t i = ps.psyms.len - 2;
80 1.79 rillig while (ps.psyms.sym[i] != psym_stmt
81 1.79 rillig && ps.psyms.sym[i] != psym_lbrace_block)
82 1.79 rillig i--;
83 1.79 rillig ps.ind_level_follow = ps.psyms.ind_level[i];
84 1.68 rillig return true;
85 1.68 rillig
86 1.68 rillig case psym_switch_expr:
87 1.68 rillig case psym_decl:
88 1.68 rillig case psym_if_expr_stmt_else:
89 1.68 rillig case psym_for_exprs:
90 1.68 rillig case psym_while_expr:
91 1.82 rillig psyms_replace2(psym_stmt);
92 1.79 rillig ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
93 1.68 rillig return true;
94 1.68 rillig
95 1.68 rillig default:
96 1.68 rillig return false;
97 1.68 rillig }
98 1.68 rillig }
99 1.1 cgd
100 1.45 rillig static int
101 1.79 rillig left_justify_decl_level(void)
102 1.45 rillig {
103 1.60 rillig int level = 0;
104 1.74 rillig for (size_t i = ps.psyms.len - 2; i > 0; i--)
105 1.69 rillig if (ps.psyms.sym[i] == psym_decl)
106 1.60 rillig level++;
107 1.60 rillig return level;
108 1.45 rillig }
109 1.45 rillig
110 1.76 rillig void
111 1.72 rillig ps_push(parser_symbol psym, bool follow)
112 1.64 rillig {
113 1.76 rillig if (ps.psyms.len == ps.psyms.cap) {
114 1.76 rillig ps.psyms.cap += 16;
115 1.76 rillig ps.psyms.sym = nonnull(realloc(ps.psyms.sym,
116 1.77 rillig sizeof(ps.psyms.sym[0]) * ps.psyms.cap));
117 1.76 rillig ps.psyms.ind_level = nonnull(realloc(ps.psyms.ind_level,
118 1.77 rillig sizeof(ps.psyms.ind_level[0]) * ps.psyms.cap));
119 1.76 rillig }
120 1.76 rillig ps.psyms.len++;
121 1.76 rillig ps.psyms.sym[ps.psyms.len - 1] = psym;
122 1.74 rillig ps.psyms.ind_level[ps.psyms.len - 1] =
123 1.72 rillig follow ? ps.ind_level_follow : ps.ind_level;
124 1.64 rillig }
125 1.64 rillig
126 1.68 rillig /*
127 1.68 rillig * Repeatedly try to reduce the top two symbols on the parse stack to a single
128 1.68 rillig * symbol, until no more reductions are possible.
129 1.68 rillig */
130 1.68 rillig static void
131 1.77 rillig psyms_reduce(void)
132 1.68 rillig {
133 1.68 rillig again:
134 1.79 rillig if (ps.psyms.len >= 2 && ps.psyms.sym[ps.psyms.len - 1] == psym_stmt
135 1.77 rillig && psyms_reduce_stmt())
136 1.68 rillig goto again;
137 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_while_expr &&
138 1.79 rillig ps.psyms.sym[ps.psyms.len - 2] == psym_do_stmt) {
139 1.79 rillig ps.psyms.len -= 2;
140 1.68 rillig goto again;
141 1.68 rillig }
142 1.68 rillig }
143 1.68 rillig
144 1.65 rillig static bool
145 1.65 rillig is_lbrace(parser_symbol psym)
146 1.65 rillig {
147 1.65 rillig return psym == psym_lbrace_block
148 1.65 rillig || psym == psym_lbrace_struct
149 1.65 rillig || psym == psym_lbrace_union
150 1.65 rillig || psym == psym_lbrace_enum;
151 1.65 rillig }
152 1.65 rillig
153 1.31 rillig /*
154 1.71 rillig * Shift the token onto the parser stack, then try to reduce it by combining it with
155 1.31 rillig * previous tokens.
156 1.31 rillig */
157 1.5 lukem void
158 1.39 rillig parse(parser_symbol psym)
159 1.1 cgd {
160 1.62 rillig debug_blank_line();
161 1.81 rillig debug_println("parse: %s", psym_name[psym]);
162 1.1 cgd
163 1.60 rillig if (psym != psym_else) {
164 1.79 rillig while (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt) {
165 1.79 rillig ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
166 1.77 rillig psyms_reduce();
167 1.60 rillig }
168 1.34 rillig }
169 1.1 cgd
170 1.60 rillig switch (psym) {
171 1.1 cgd
172 1.71 rillig case psym_lbrace_block:
173 1.71 rillig case psym_lbrace_struct:
174 1.71 rillig case psym_lbrace_union:
175 1.71 rillig case psym_lbrace_enum:
176 1.71 rillig ps.break_after_comma = false;
177 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_decl
178 1.79 rillig || ps.psyms.sym[ps.psyms.len - 1] == psym_stmt)
179 1.78 rillig ps.ind_level_follow++;
180 1.71 rillig else if (code.len == 0) {
181 1.71 rillig /* It is part of a while, for, etc. */
182 1.78 rillig ps.ind_level--;
183 1.71 rillig
184 1.71 rillig /* for a switch, brace should be two levels out from
185 1.71 rillig * the code */
186 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
187 1.71 rillig && opt.case_indent >= 1.0F)
188 1.78 rillig ps.ind_level--;
189 1.71 rillig }
190 1.71 rillig
191 1.72 rillig ps_push(psym, false);
192 1.72 rillig ps_push(psym_stmt, true);
193 1.71 rillig break;
194 1.71 rillig
195 1.71 rillig case psym_rbrace:
196 1.77 rillig /* stack should have <lbrace> <stmt> or <lbrace> <decl> */
197 1.79 rillig if (!(ps.psyms.len >= 2
198 1.79 rillig && is_lbrace(ps.psyms.sym[ps.psyms.len - 2]))) {
199 1.71 rillig diag(1, "Statement nesting error");
200 1.71 rillig break;
201 1.71 rillig }
202 1.79 rillig ps.psyms.len--;
203 1.79 rillig ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
204 1.79 rillig ps.ind_level_follow = ps.ind_level;
205 1.79 rillig ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
206 1.71 rillig break;
207 1.71 rillig
208 1.60 rillig case psym_decl:
209 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_decl)
210 1.60 rillig break; /* only put one declaration onto stack */
211 1.60 rillig
212 1.63 rillig ps.break_after_comma = true;
213 1.72 rillig ps_push(psym_decl, true);
214 1.60 rillig
215 1.79 rillig if (opt.left_justify_decl) {
216 1.79 rillig ps.ind_level = left_justify_decl_level();
217 1.79 rillig ps.ind_level_follow = ps.ind_level;
218 1.79 rillig }
219 1.60 rillig break;
220 1.60 rillig
221 1.71 rillig case psym_stmt:
222 1.71 rillig ps.break_after_comma = false;
223 1.72 rillig ps_push(psym_stmt, false);
224 1.71 rillig break;
225 1.71 rillig
226 1.60 rillig case psym_if_expr:
227 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else
228 1.79 rillig && opt.else_if_in_same_line) {
229 1.74 rillig ps.ind_level_follow =
230 1.79 rillig ps.psyms.ind_level[ps.psyms.len - 1];
231 1.79 rillig ps.psyms.len--;
232 1.79 rillig }
233 1.60 rillig /* FALLTHROUGH */
234 1.60 rillig case psym_do:
235 1.60 rillig case psym_for_exprs:
236 1.79 rillig ps.ind_level = ps.ind_level_follow;
237 1.79 rillig ps.ind_level_follow = ps.ind_level + 1;
238 1.72 rillig ps_push(psym, false);
239 1.60 rillig break;
240 1.60 rillig
241 1.60 rillig case psym_else:
242 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt) {
243 1.60 rillig diag(1, "Unmatched 'else'");
244 1.64 rillig break;
245 1.60 rillig }
246 1.79 rillig ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
247 1.64 rillig ps.ind_level_follow = ps.ind_level + 1;
248 1.79 rillig ps.psyms.sym[ps.psyms.len - 1] = psym_if_expr_stmt_else;
249 1.60 rillig break;
250 1.60 rillig
251 1.60 rillig case psym_switch_expr:
252 1.72 rillig ps_push(psym_switch_expr, true);
253 1.60 rillig ps.ind_level_follow += (int)opt.case_indent + 1;
254 1.60 rillig break;
255 1.60 rillig
256 1.71 rillig case psym_while_expr:
257 1.79 rillig if (ps.psyms.sym[ps.psyms.len - 1] == psym_do_stmt) {
258 1.79 rillig ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
259 1.79 rillig ps.ind_level_follow = ps.ind_level;
260 1.72 rillig ps_push(psym_while_expr, false);
261 1.71 rillig } else {
262 1.72 rillig ps_push(psym_while_expr, true);
263 1.78 rillig ps.ind_level_follow++;
264 1.71 rillig }
265 1.60 rillig break;
266 1.60 rillig
267 1.60 rillig default:
268 1.60 rillig diag(1, "Unknown code to parser");
269 1.60 rillig return;
270 1.17 rillig }
271 1.5 lukem
272 1.80 rillig #if debug
273 1.80 rillig static struct buffer before, after;
274 1.81 rillig buf_clear(&before);
275 1.81 rillig ps_psyms_to_string(&before, &ps);
276 1.80 rillig #endif
277 1.77 rillig psyms_reduce();
278 1.80 rillig #if debug
279 1.81 rillig buf_clear(&after);
280 1.81 rillig ps_psyms_to_string(&after, &ps);
281 1.81 rillig if (strcmp(before.s, after.s) != 0) {
282 1.81 rillig debug_println("psyms before: %s", before.s);
283 1.81 rillig debug_println("psyms after: %s", after.s);
284 1.81 rillig } else
285 1.81 rillig debug_println("psyms: %s", after.s);
286 1.80 rillig #endif
287 1.1 cgd }
288