2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1985 Sun Microsystems, Inc.
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 __FBSDID("$FreeBSD$");
51 #include "indent_globs.h"
52 #include "indent_codes.h"
55 static void reduce(void);
58 parse(int tk) /* tk: the code for the construct scanned */
63 printf("%2d - %s\n", tk, token);
66 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
67 /* true if we have an if without an else */
68 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
70 reduce(); /* see if this allows any reduction */
74 switch (tk) { /* go on and figure out what to do with the
77 case decl: /* scanned a declaration word */
78 ps.search_brace = opt.btype_2;
79 /* indicate that following brace should be on same line */
80 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
82 break_comma = true; /* while in declaration, newline should be
83 * forced after comma */
84 ps.p_stack[++ps.tos] = decl;
85 ps.il[ps.tos] = ps.i_l_follow;
87 if (opt.ljust_decl) {/* only do if we want left justified
90 for (i = ps.tos - 1; i > 0; --i)
91 if (ps.p_stack[i] == decl)
92 ++ps.ind_level; /* indentation is number of
93 * declaration levels deep we are */
94 ps.i_l_follow = ps.ind_level;
99 case ifstmt: /* scanned if (...) */
100 if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
102 * Note that the stack pointer here is decremented, effectively
103 * reducing "else if" to "if". This saves a lot of stack space
104 * in case of a long "if-else-if ... else-if" sequence.
106 ps.i_l_follow = ps.il[ps.tos--];
107 /* the rest is the same as for dolit and forstmt */
108 case dolit: /* 'do' */
109 case forstmt: /* for (...) */
110 ps.p_stack[++ps.tos] = tk;
111 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
112 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
113 ps.search_brace = opt.btype_2;
116 case lbrace: /* scanned { */
117 break_comma = false; /* don't break comma in an initial list */
118 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
119 || ps.p_stack[ps.tos] == stmtl)
120 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
123 if (s_code == e_code) {
125 * only do this if there is nothing on the line
129 * it is a group as part of a while, for, etc.
131 if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
134 * for a switch, brace should be two levels out from the code
139 ps.p_stack[++ps.tos] = lbrace;
140 ps.il[ps.tos] = ps.ind_level;
141 ps.p_stack[++ps.tos] = stmt;
142 /* allow null stmt between braces */
143 ps.il[ps.tos] = ps.i_l_follow;
146 case whilestmt: /* scanned while (...) */
147 if (ps.p_stack[ps.tos] == dohead) {
148 /* it is matched with do stmt */
149 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
150 ps.p_stack[++ps.tos] = whilestmt;
151 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
153 else { /* it is a while loop */
154 ps.p_stack[++ps.tos] = whilestmt;
155 ps.il[ps.tos] = ps.i_l_follow;
157 ps.search_brace = opt.btype_2;
162 case elselit: /* scanned an else */
164 if (ps.p_stack[ps.tos] != ifhead)
165 diag2(1, "Unmatched 'else'");
167 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
168 * be same as for if */
169 ps.i_l_follow = ps.ind_level + 1; /* everything following should
171 ps.p_stack[ps.tos] = elsehead;
172 /* remember if with else */
173 ps.search_brace = opt.btype_2 | opt.else_if;
177 case rbrace: /* scanned a } */
178 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
179 if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
180 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
181 ps.p_stack[ps.tos] = stmt;
184 diag2(1, "Statement nesting error");
187 case swstmt: /* had switch (...) */
188 ps.p_stack[++ps.tos] = swstmt;
189 ps.cstk[ps.tos] = case_ind;
190 /* save current case indent level */
191 ps.il[ps.tos] = ps.i_l_follow;
192 case_ind = ps.i_l_follow + opt.case_indent; /* cases should be one
195 ps.i_l_follow += opt.case_indent + 1; /* statements should be two
197 ps.search_brace = opt.btype_2;
200 case semicolon: /* this indicates a simple stmt */
201 break_comma = false; /* turn off flag to break after commas in a
203 ps.p_stack[++ps.tos] = stmt;
204 ps.il[ps.tos] = ps.ind_level;
207 default: /* this is an error */
208 diag2(1, "Unknown code to parser");
212 } /* end of switch */
214 if (ps.tos >= (int)nitems(ps.p_stack) - 1)
215 errx(1, "Parser stack overflow");
217 reduce(); /* see if any reduction can be done */
220 for (i = 1; i <= ps.tos; ++i)
221 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
231 * FUNCTION: Implements the reduce part of the parsing algorithm
233 * ALGORITHM: The following reductions are done. Reductions are repeated
234 * until no more are possible.
237 * <stmt> <stmt> <stmtl>
238 * <stmtl> <stmt> <stmtl>
241 * switch <stmt> <stmt>
243 * "ifelse" <stmt> <stmt>
245 * while <stmt> <stmt>
246 * "dostmt" while <stmt>
248 * On each reduction, ps.i_l_follow (the indentation for the following line)
249 * is set to the indentation level associated with the old TOS.
255 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
261 * HISTORY: initial coding November 1976 D A Willcox of CAC
264 /*----------------------------------------------*\
266 \*----------------------------------------------*/
272 for (;;) { /* keep looping until there is nothing left to
275 switch (ps.p_stack[ps.tos]) {
278 switch (ps.p_stack[ps.tos - 1]) {
282 /* stmtl stmt or stmt stmt */
283 ps.p_stack[--ps.tos] = stmtl;
286 case dolit: /* <do> <stmt> */
287 ps.p_stack[--ps.tos] = dohead;
288 ps.i_l_follow = ps.il[ps.tos];
293 ps.p_stack[--ps.tos] = ifhead;
296 ps.p_stack[i] != stmt
298 ps.p_stack[i] != stmtl
300 ps.p_stack[i] != lbrace
303 ps.i_l_follow = ps.il[i];
305 * for the time being, we will assume that there is no else on
306 * this if, and set the indentation level accordingly. If an
307 * else is scanned, it will be fixed up later
312 /* <switch> <stmt> */
313 case_ind = ps.cstk[ps.tos - 1];
315 case decl: /* finish of a declaration */
317 /* <<if> <stmt> else> <stmt> */
322 ps.p_stack[--ps.tos] = stmt;
323 ps.i_l_follow = ps.il[ps.tos];
326 default: /* <anything else> <stmt> */
329 } /* end of section for <stmt> on top of stack */
332 case whilestmt: /* while (...) on top */
333 if (ps.p_stack[ps.tos - 1] == dohead) {
334 /* it is termination of a do while */
341 default: /* anything else on top */