| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: parse.c,v 1.71 2023/06/10 16:43:56 rillig Exp $ */ | | 1 | /* $NetBSD: parse.c,v 1.72 2023/06/10 17:35:40 rillig Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * SPDX-License-Identifier: BSD-4-Clause | | 4 | * SPDX-License-Identifier: BSD-4-Clause |
5 | * | | 5 | * |
6 | * Copyright (c) 1985 Sun Microsystems, Inc. | | 6 | * Copyright (c) 1985 Sun Microsystems, Inc. |
7 | * Copyright (c) 1980, 1993 | | 7 | * Copyright (c) 1980, 1993 |
8 | * The Regents of the University of California. All rights reserved. | | 8 | * The Regents of the University of California. All rights reserved. |
9 | * All rights reserved. | | 9 | * All rights reserved. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
| @@ -28,27 +28,27 @@ | | | @@ -28,27 +28,27 @@ |
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
37 | * SUCH DAMAGE. | | 37 | * SUCH DAMAGE. |
38 | */ | | 38 | */ |
39 | | | 39 | |
40 | #include <sys/cdefs.h> | | 40 | #include <sys/cdefs.h> |
41 | __RCSID("$NetBSD: parse.c,v 1.71 2023/06/10 16:43:56 rillig Exp $"); | | 41 | __RCSID("$NetBSD: parse.c,v 1.72 2023/06/10 17:35:40 rillig Exp $"); |
42 | | | 42 | |
43 | #include <err.h> | | 43 | #include <err.h> |
44 | | | 44 | |
45 | #include "indent.h" | | 45 | #include "indent.h" |
46 | | | 46 | |
47 | /* | | 47 | /* |
48 | * Try to combine the statement on the top of the parse stack with the symbol | | 48 | * Try to combine the statement on the top of the parse stack with the symbol |
49 | * directly below it, replacing these two symbols with a single symbol. | | 49 | * directly below it, replacing these two symbols with a single symbol. |
50 | */ | | 50 | */ |
51 | static bool | | 51 | static bool |
52 | psyms_reduce_stmt(struct psym_stack *psyms) | | 52 | psyms_reduce_stmt(struct psym_stack *psyms) |
53 | { | | 53 | { |
54 | switch (psyms->sym[psyms->top - 1]) { | | 54 | switch (psyms->sym[psyms->top - 1]) { |
| @@ -91,37 +91,33 @@ psyms_reduce_stmt(struct psym_stack *psy | | | @@ -91,37 +91,33 @@ psyms_reduce_stmt(struct psym_stack *psy |
91 | } | | 91 | } |
92 | | | 92 | |
93 | static int | | 93 | static int |
94 | decl_level(void) | | 94 | decl_level(void) |
95 | { | | 95 | { |
96 | int level = 0; | | 96 | int level = 0; |
97 | for (int i = ps.psyms.top - 1; i > 0; i--) | | 97 | for (int i = ps.psyms.top - 1; i > 0; i--) |
98 | if (ps.psyms.sym[i] == psym_decl) | | 98 | if (ps.psyms.sym[i] == psym_decl) |
99 | level++; | | 99 | level++; |
100 | return level; | | 100 | return level; |
101 | } | | 101 | } |
102 | | | 102 | |
103 | static void | | 103 | static void |
104 | ps_push(parser_symbol psym) | | 104 | ps_push(parser_symbol psym, bool follow) |
105 | { | | | |
106 | ps.psyms.sym[++ps.psyms.top] = psym; | | | |
107 | ps.psyms.ind_level[ps.psyms.top] = ps.ind_level; | | | |
108 | } | | | |
109 | | | | |
110 | static void | | | |
111 | ps_push_follow(parser_symbol psym) | | | |
112 | { | | 105 | { |
| | | 106 | if (ps.psyms.top + 1 >= STACKSIZE) |
| | | 107 | errx(1, "Parser stack overflow"); |
113 | ps.psyms.sym[++ps.psyms.top] = psym; | | 108 | ps.psyms.sym[++ps.psyms.top] = psym; |
114 | ps.psyms.ind_level[ps.psyms.top] = ps.ind_level_follow; | | 109 | ps.psyms.ind_level[ps.psyms.top] = |
| | | 110 | follow ? ps.ind_level_follow : ps.ind_level; |
115 | } | | 111 | } |
116 | | | 112 | |
117 | /* | | 113 | /* |
118 | * Repeatedly try to reduce the top two symbols on the parse stack to a single | | 114 | * Repeatedly try to reduce the top two symbols on the parse stack to a single |
119 | * symbol, until no more reductions are possible. | | 115 | * symbol, until no more reductions are possible. |
120 | */ | | 116 | */ |
121 | static void | | 117 | static void |
122 | psyms_reduce(struct psym_stack *psyms) | | 118 | psyms_reduce(struct psym_stack *psyms) |
123 | { | | 119 | { |
124 | again: | | 120 | again: |
125 | if (psyms->sym[psyms->top] == psym_stmt && psyms_reduce_stmt(psyms)) | | 121 | if (psyms->sym[psyms->top] == psym_stmt && psyms_reduce_stmt(psyms)) |
126 | goto again; | | 122 | goto again; |
127 | if (psyms->sym[psyms->top] == psym_while_expr && | | 123 | if (psyms->sym[psyms->top] == psym_while_expr && |
| @@ -170,94 +166,91 @@ parse(parser_symbol psym) | | | @@ -170,94 +166,91 @@ parse(parser_symbol psym) |
170 | || psyms->sym[psyms->top] == psym_stmt_list) | | 166 | || psyms->sym[psyms->top] == psym_stmt_list) |
171 | ++ps.ind_level_follow; | | 167 | ++ps.ind_level_follow; |
172 | else if (code.len == 0) { | | 168 | else if (code.len == 0) { |
173 | /* It is part of a while, for, etc. */ | | 169 | /* It is part of a while, for, etc. */ |
174 | --ps.ind_level; | | 170 | --ps.ind_level; |
175 | | | 171 | |
176 | /* for a switch, brace should be two levels out from | | 172 | /* for a switch, brace should be two levels out from |
177 | * the code */ | | 173 | * the code */ |
178 | if (psyms->sym[psyms->top] == psym_switch_expr | | 174 | if (psyms->sym[psyms->top] == psym_switch_expr |
179 | && opt.case_indent >= 1.0F) | | 175 | && opt.case_indent >= 1.0F) |
180 | --ps.ind_level; | | 176 | --ps.ind_level; |
181 | } | | 177 | } |
182 | | | 178 | |
183 | ps_push(psym); | | 179 | ps_push(psym, false); |
184 | ps_push_follow(psym_stmt); | | 180 | ps_push(psym_stmt, true); |
185 | break; | | 181 | break; |
186 | | | 182 | |
187 | case psym_rbrace: | | 183 | case psym_rbrace: |
188 | /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ | | 184 | /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ |
189 | if (!(psyms->top > 0 | | 185 | if (!(psyms->top > 0 |
190 | && is_lbrace(psyms->sym[psyms->top - 1]))) { | | 186 | && is_lbrace(psyms->sym[psyms->top - 1]))) { |
191 | diag(1, "Statement nesting error"); | | 187 | diag(1, "Statement nesting error"); |
192 | break; | | 188 | break; |
193 | } | | 189 | } |
194 | ps.ind_level = ps.ind_level_follow = | | 190 | ps.ind_level = ps.ind_level_follow = |
195 | psyms->ind_level[--psyms->top]; | | 191 | psyms->ind_level[--psyms->top]; |
196 | psyms->sym[psyms->top] = psym_stmt; | | 192 | psyms->sym[psyms->top] = psym_stmt; |
197 | break; | | 193 | break; |
198 | | | 194 | |
199 | case psym_decl: | | 195 | case psym_decl: |
200 | if (psyms->sym[psyms->top] == psym_decl) | | 196 | if (psyms->sym[psyms->top] == psym_decl) |
201 | break; /* only put one declaration onto stack */ | | 197 | break; /* only put one declaration onto stack */ |
202 | | | 198 | |
203 | ps.break_after_comma = true; | | 199 | ps.break_after_comma = true; |
204 | ps_push_follow(psym_decl); | | 200 | ps_push(psym_decl, true); |
205 | | | 201 | |
206 | if (opt.left_justify_decl) | | 202 | if (opt.left_justify_decl) |
207 | ps.ind_level_follow = ps.ind_level = decl_level(); | | 203 | ps.ind_level_follow = ps.ind_level = decl_level(); |
208 | break; | | 204 | break; |
209 | | | 205 | |
210 | case psym_stmt: | | 206 | case psym_stmt: |
211 | ps.break_after_comma = false; | | 207 | ps.break_after_comma = false; |
212 | ps_push(psym_stmt); | | 208 | ps_push(psym_stmt, false); |
213 | break; | | 209 | break; |
214 | | | 210 | |
215 | case psym_if_expr: | | 211 | case psym_if_expr: |
216 | if (psyms->sym[psyms->top] == psym_if_expr_stmt_else | | 212 | if (psyms->sym[psyms->top] == psym_if_expr_stmt_else |
217 | && opt.else_if_in_same_line) | | 213 | && opt.else_if_in_same_line) |
218 | ps.ind_level_follow = psyms->ind_level[psyms->top--]; | | 214 | ps.ind_level_follow = psyms->ind_level[psyms->top--]; |
219 | /* FALLTHROUGH */ | | 215 | /* FALLTHROUGH */ |
220 | case psym_do: | | 216 | case psym_do: |
221 | case psym_for_exprs: | | 217 | case psym_for_exprs: |
222 | ps.ind_level = ps.ind_level_follow++; | | 218 | ps.ind_level = ps.ind_level_follow++; |
223 | ps_push(psym); | | 219 | ps_push(psym, false); |
224 | break; | | 220 | break; |
225 | | | 221 | |
226 | case psym_else: | | 222 | case psym_else: |
227 | if (psyms->sym[psyms->top] != psym_if_expr_stmt) { | | 223 | if (psyms->sym[psyms->top] != psym_if_expr_stmt) { |
228 | diag(1, "Unmatched 'else'"); | | 224 | diag(1, "Unmatched 'else'"); |
229 | break; | | 225 | break; |
230 | } | | 226 | } |
231 | ps.ind_level = psyms->ind_level[psyms->top]; | | 227 | ps.ind_level = psyms->ind_level[psyms->top]; |
232 | ps.ind_level_follow = ps.ind_level + 1; | | 228 | ps.ind_level_follow = ps.ind_level + 1; |
233 | psyms->sym[psyms->top] = psym_if_expr_stmt_else; | | 229 | psyms->sym[psyms->top] = psym_if_expr_stmt_else; |
234 | break; | | 230 | break; |
235 | | | 231 | |
236 | case psym_switch_expr: | | 232 | case psym_switch_expr: |
237 | ps_push_follow(psym_switch_expr); | | 233 | ps_push(psym_switch_expr, true); |
238 | ps.ind_level_follow += (int)opt.case_indent + 1; | | 234 | ps.ind_level_follow += (int)opt.case_indent + 1; |
239 | break; | | 235 | break; |
240 | | | 236 | |
241 | case psym_while_expr: | | 237 | case psym_while_expr: |
242 | if (psyms->sym[psyms->top] == psym_do_stmt) { | | 238 | if (psyms->sym[psyms->top] == psym_do_stmt) { |
243 | ps.ind_level = ps.ind_level_follow = | | 239 | ps.ind_level = ps.ind_level_follow = |
244 | psyms->ind_level[psyms->top]; | | 240 | psyms->ind_level[psyms->top]; |
245 | ps_push(psym_while_expr); | | 241 | ps_push(psym_while_expr, false); |
246 | } else { | | 242 | } else { |
247 | ps_push_follow(psym_while_expr); | | 243 | ps_push(psym_while_expr, true); |
248 | ++ps.ind_level_follow; | | 244 | ++ps.ind_level_follow; |
249 | } | | 245 | } |
250 | break; | | 246 | break; |
251 | | | 247 | |
252 | default: | | 248 | default: |
253 | diag(1, "Unknown code to parser"); | | 249 | diag(1, "Unknown code to parser"); |
254 | return; | | 250 | return; |
255 | } | | 251 | } |
256 | | | 252 | |
257 | if (psyms->top >= STACKSIZE - 1) | | | |
258 | errx(1, "Parser stack overflow"); | | | |
259 | | | | |
260 | debug_psyms_stack("before reduction"); | | 253 | debug_psyms_stack("before reduction"); |
261 | psyms_reduce(&ps.psyms); | | 254 | psyms_reduce(&ps.psyms); |
262 | debug_psyms_stack("after reduction"); | | 255 | debug_psyms_stack("after reduction"); |
263 | } | | 256 | } |