parse.c revision 1.31 1 1.31 rillig /* $NetBSD: parse.c,v 1.31 2021/10/07 21:43:20 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.9 kamil #if 0
41 1.9 kamil static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
42 1.9 kamil #endif
43 1.9 kamil
44 1.5 lukem #include <sys/cdefs.h>
45 1.9 kamil #if defined(__NetBSD__)
46 1.9 kamil __RCSID("$FreeBSD$");
47 1.4 mrg #else
48 1.9 kamil __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
49 1.9 kamil #endif
50 1.1 cgd
51 1.9 kamil #include <err.h>
52 1.1 cgd #include <stdio.h>
53 1.12 rillig
54 1.9 kamil #include "indent.h"
55 1.9 kamil
56 1.9 kamil static void reduce(void);
57 1.1 cgd
58 1.31 rillig /*
59 1.31 rillig * Shift the token onto the parser stack, or reduce it by combining it with
60 1.31 rillig * previous tokens.
61 1.31 rillig */
62 1.5 lukem void
63 1.19 rillig parse(token_type ttype)
64 1.1 cgd {
65 1.1 cgd
66 1.1 cgd #ifdef debug
67 1.22 rillig printf("parse token: '%s' \"%s\"\n", token_type_name(ttype), token.s);
68 1.1 cgd #endif
69 1.1 cgd
70 1.19 rillig while (ps.p_stack[ps.tos] == if_expr_stmt && ttype != keyword_else) {
71 1.9 kamil /* true if we have an if without an else */
72 1.9 kamil ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
73 1.9 kamil * reduction */
74 1.9 kamil reduce(); /* see if this allows any reduction */
75 1.9 kamil }
76 1.1 cgd
77 1.30 rillig switch (ttype) {
78 1.1 cgd
79 1.9 kamil case decl: /* scanned a declaration word */
80 1.9 kamil ps.search_brace = opt.btype_2;
81 1.9 kamil /* indicate that following brace should be on same line */
82 1.9 kamil if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
83 1.9 kamil * onto stack */
84 1.9 kamil break_comma = true; /* while in declaration, newline should be
85 1.9 kamil * forced after comma */
86 1.9 kamil ps.p_stack[++ps.tos] = decl;
87 1.26 rillig ps.il[ps.tos] = ps.ind_level_follow;
88 1.9 kamil
89 1.30 rillig if (opt.ljust_decl) {
90 1.9 kamil ps.ind_level = 0;
91 1.28 rillig for (int i = ps.tos - 1; i > 0; --i)
92 1.9 kamil if (ps.p_stack[i] == decl)
93 1.9 kamil ++ps.ind_level; /* indentation is number of
94 1.9 kamil * declaration levels deep we are */
95 1.26 rillig ps.ind_level_follow = ps.ind_level;
96 1.9 kamil }
97 1.9 kamil }
98 1.9 kamil break;
99 1.5 lukem
100 1.17 rillig case if_expr: /* 'if' '(' <expr> ')' */
101 1.17 rillig if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
102 1.17 rillig /*
103 1.30 rillig * Reduce "else if" to "if". This saves a lot of stack space in
104 1.27 rillig * case of a long "if-else-if ... else-if" sequence.
105 1.17 rillig */
106 1.26 rillig ps.ind_level_follow = ps.il[ps.tos--];
107 1.17 rillig }
108 1.9 kamil /* FALLTHROUGH */
109 1.30 rillig case keyword_do:
110 1.17 rillig case for_exprs: /* 'for' (...) */
111 1.19 rillig ps.p_stack[++ps.tos] = ttype;
112 1.26 rillig ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
113 1.26 rillig ++ps.ind_level_follow; /* subsequent statements should be indented 1 */
114 1.9 kamil ps.search_brace = opt.btype_2;
115 1.9 kamil break;
116 1.9 kamil
117 1.30 rillig case lbrace:
118 1.9 kamil break_comma = false; /* don't break comma in an initial list */
119 1.9 kamil if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
120 1.17 rillig || ps.p_stack[ps.tos] == stmt_list)
121 1.26 rillig ++ps.ind_level_follow; /* it is a random, isolated stmt
122 1.26 rillig * group or a declaration */
123 1.9 kamil else {
124 1.20 rillig if (code.s == code.e) {
125 1.31 rillig /* it is a group as part of a while, for, etc. */
126 1.9 kamil --ps.ind_level;
127 1.9 kamil /*
128 1.31 rillig * for a switch, brace should be two levels out from the code
129 1.9 kamil */
130 1.17 rillig if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
131 1.9 kamil --ps.ind_level;
132 1.9 kamil }
133 1.9 kamil }
134 1.5 lukem
135 1.9 kamil ps.p_stack[++ps.tos] = lbrace;
136 1.9 kamil ps.il[ps.tos] = ps.ind_level;
137 1.9 kamil ps.p_stack[++ps.tos] = stmt;
138 1.9 kamil /* allow null stmt between braces */
139 1.26 rillig ps.il[ps.tos] = ps.ind_level_follow;
140 1.9 kamil break;
141 1.9 kamil
142 1.17 rillig case while_expr: /* 'while' '(' <expr> ')' */
143 1.17 rillig if (ps.p_stack[ps.tos] == do_stmt) {
144 1.9 kamil /* it is matched with do stmt */
145 1.26 rillig ps.ind_level = ps.ind_level_follow = ps.il[ps.tos];
146 1.17 rillig ps.p_stack[++ps.tos] = while_expr;
147 1.26 rillig ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
148 1.18 rillig } else { /* it is a while loop */
149 1.17 rillig ps.p_stack[++ps.tos] = while_expr;
150 1.26 rillig ps.il[ps.tos] = ps.ind_level_follow;
151 1.26 rillig ++ps.ind_level_follow;
152 1.9 kamil ps.search_brace = opt.btype_2;
153 1.9 kamil }
154 1.5 lukem
155 1.9 kamil break;
156 1.5 lukem
157 1.17 rillig case keyword_else:
158 1.17 rillig if (ps.p_stack[ps.tos] != if_expr_stmt)
159 1.10 christos diag(1, "Unmatched 'else'");
160 1.9 kamil else {
161 1.9 kamil ps.ind_level = ps.il[ps.tos]; /* indentation for else should
162 1.9 kamil * be same as for if */
163 1.30 rillig ps.ind_level_follow = ps.ind_level + 1;
164 1.17 rillig ps.p_stack[ps.tos] = if_expr_stmt_else;
165 1.9 kamil /* remember if with else */
166 1.9 kamil ps.search_brace = opt.btype_2 | opt.else_if;
167 1.9 kamil }
168 1.9 kamil break;
169 1.1 cgd
170 1.30 rillig case rbrace:
171 1.17 rillig /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
172 1.9 kamil if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
173 1.26 rillig ps.ind_level = ps.ind_level_follow = ps.il[--ps.tos];
174 1.9 kamil ps.p_stack[ps.tos] = stmt;
175 1.18 rillig } else
176 1.10 christos diag(1, "Statement nesting error");
177 1.9 kamil break;
178 1.9 kamil
179 1.17 rillig case switch_expr: /* had switch (...) */
180 1.17 rillig ps.p_stack[++ps.tos] = switch_expr;
181 1.9 kamil ps.cstk[ps.tos] = case_ind;
182 1.9 kamil /* save current case indent level */
183 1.26 rillig ps.il[ps.tos] = ps.ind_level_follow;
184 1.29 rillig /* cases should be one level deeper than the switch */
185 1.29 rillig case_ind = (float)ps.ind_level_follow + opt.case_indent;
186 1.29 rillig /* statements should be two levels deeper */
187 1.29 rillig ps.ind_level_follow += (int)opt.case_indent + 1;
188 1.9 kamil ps.search_brace = opt.btype_2;
189 1.9 kamil break;
190 1.9 kamil
191 1.9 kamil case semicolon: /* this indicates a simple stmt */
192 1.9 kamil break_comma = false; /* turn off flag to break after commas in a
193 1.9 kamil * declaration */
194 1.9 kamil ps.p_stack[++ps.tos] = stmt;
195 1.9 kamil ps.il[ps.tos] = ps.ind_level;
196 1.9 kamil break;
197 1.9 kamil
198 1.30 rillig default:
199 1.10 christos diag(1, "Unknown code to parser");
200 1.9 kamil return;
201 1.30 rillig }
202 1.1 cgd
203 1.9 kamil if (ps.tos >= STACKSIZE - 1)
204 1.9 kamil errx(1, "Parser stack overflow");
205 1.1 cgd
206 1.9 kamil reduce(); /* see if any reduction can be done */
207 1.1 cgd
208 1.1 cgd #ifdef debug
209 1.14 rillig printf("parse stack:");
210 1.28 rillig for (int i = 1; i <= ps.tos; ++i)
211 1.14 rillig printf(" ('%s' at %d)", token_type_name(ps.p_stack[i]), ps.il[i]);
212 1.14 rillig if (ps.tos == 0)
213 1.27 rillig printf(" empty");
214 1.9 kamil printf("\n");
215 1.1 cgd #endif
216 1.1 cgd }
217 1.9 kamil
218 1.16 rillig /*----------------------------------------------*\
219 1.16 rillig | REDUCTION PHASE |
220 1.16 rillig \*----------------------------------------------*/
221 1.16 rillig
222 1.16 rillig /*
223 1.16 rillig * Try to combine the statement on the top of the parse stack with the symbol
224 1.16 rillig * directly below it, replacing these two symbols with a single symbol.
225 1.16 rillig */
226 1.25 rillig static bool
227 1.16 rillig reduce_stmt(void)
228 1.16 rillig {
229 1.16 rillig switch (ps.p_stack[ps.tos - 1]) {
230 1.16 rillig
231 1.27 rillig case stmt: /* stmt stmt */
232 1.27 rillig case stmt_list: /* stmt_list stmt */
233 1.17 rillig ps.p_stack[--ps.tos] = stmt_list;
234 1.16 rillig return true;
235 1.16 rillig
236 1.27 rillig case keyword_do: /* 'do' <stmt> */
237 1.17 rillig ps.p_stack[--ps.tos] = do_stmt;
238 1.26 rillig ps.ind_level_follow = ps.il[ps.tos];
239 1.16 rillig return true;
240 1.16 rillig
241 1.27 rillig case if_expr: /* 'if' '(' <expr> ')' <stmt> */
242 1.17 rillig ps.p_stack[--ps.tos] = if_expr_stmt;
243 1.16 rillig int i = ps.tos - 1;
244 1.16 rillig while (ps.p_stack[i] != stmt &&
245 1.17 rillig ps.p_stack[i] != stmt_list &&
246 1.16 rillig ps.p_stack[i] != lbrace)
247 1.16 rillig --i;
248 1.26 rillig ps.ind_level_follow = ps.il[i];
249 1.16 rillig /*
250 1.27 rillig * for the time being, we will assume that there is no else on this
251 1.29 rillig * if, and set the indentation level accordingly. If an 'else' is
252 1.27 rillig * scanned, it will be fixed up later
253 1.16 rillig */
254 1.16 rillig return true;
255 1.16 rillig
256 1.27 rillig case switch_expr: /* 'switch' '(' <expr> ')' <stmt> */
257 1.16 rillig case_ind = ps.cstk[ps.tos - 1];
258 1.16 rillig /* FALLTHROUGH */
259 1.27 rillig case decl: /* finish of a declaration */
260 1.27 rillig case if_expr_stmt_else: /* 'if' '(' <expr> ')' <stmt> 'else' <stmt> */
261 1.27 rillig case for_exprs: /* 'for' '(' ... ')' <stmt> */
262 1.27 rillig case while_expr: /* 'while' '(' <expr> ')' <stmt> */
263 1.16 rillig ps.p_stack[--ps.tos] = stmt;
264 1.26 rillig ps.ind_level_follow = ps.il[ps.tos];
265 1.16 rillig return true;
266 1.16 rillig
267 1.27 rillig default: /* <anything else> <stmt> */
268 1.16 rillig return false;
269 1.16 rillig }
270 1.16 rillig }
271 1.16 rillig
272 1.1 cgd /*
273 1.16 rillig * Repeatedly try to reduce the top two symbols on the parse stack to a
274 1.16 rillig * single symbol, until no more reductions are possible.
275 1.5 lukem *
276 1.1 cgd * On each reduction, ps.i_l_follow (the indentation for the following line)
277 1.1 cgd * is set to the indentation level associated with the old TOS.
278 1.1 cgd */
279 1.9 kamil static void
280 1.6 wiz reduce(void)
281 1.1 cgd {
282 1.16 rillig again:
283 1.16 rillig if (ps.p_stack[ps.tos] == stmt) {
284 1.16 rillig if (reduce_stmt())
285 1.16 rillig goto again;
286 1.17 rillig } else if (ps.p_stack[ps.tos] == while_expr) {
287 1.17 rillig if (ps.p_stack[ps.tos - 1] == do_stmt) {
288 1.16 rillig ps.tos -= 2;
289 1.16 rillig goto again;
290 1.1 cgd }
291 1.9 kamil }
292 1.1 cgd }
293