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>
47 #include "indent_globs.h"
48 #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 */
109 case dolit: /* 'do' */
110 case forstmt: /* for (...) */
111 ps.p_stack[++ps.tos] = tk;
112 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
113 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
114 ps.search_brace = opt.btype_2;
117 case lbrace: /* scanned { */
118 break_comma = false; /* don't break comma in an initial list */
119 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
120 || ps.p_stack[ps.tos] == stmtl)
121 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
124 if (s_code == e_code) {
126 * only do this if there is nothing on the line
130 * it is a group as part of a while, for, etc.
132 if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
135 * for a switch, brace should be two levels out from the code
140 ps.p_stack[++ps.tos] = lbrace;
141 ps.il[ps.tos] = ps.ind_level;
142 ps.p_stack[++ps.tos] = stmt;
143 /* allow null stmt between braces */
144 ps.il[ps.tos] = ps.i_l_follow;
147 case whilestmt: /* scanned while (...) */
148 if (ps.p_stack[ps.tos] == dohead) {
149 /* it is matched with do stmt */
150 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
151 ps.p_stack[++ps.tos] = whilestmt;
152 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
154 else { /* it is a while loop */
155 ps.p_stack[++ps.tos] = whilestmt;
156 ps.il[ps.tos] = ps.i_l_follow;
158 ps.search_brace = opt.btype_2;
163 case elselit: /* scanned an else */
165 if (ps.p_stack[ps.tos] != ifhead)
166 diag2(1, "Unmatched 'else'");
168 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
169 * be same as for if */
170 ps.i_l_follow = ps.ind_level + 1; /* everything following should
172 ps.p_stack[ps.tos] = elsehead;
173 /* remember if with else */
174 ps.search_brace = opt.btype_2 | opt.else_if;
178 case rbrace: /* scanned a } */
179 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
180 if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
181 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
182 ps.p_stack[ps.tos] = stmt;
185 diag2(1, "Statement nesting error");
188 case swstmt: /* had switch (...) */
189 ps.p_stack[++ps.tos] = swstmt;
190 ps.cstk[ps.tos] = case_ind;
191 /* save current case indent level */
192 ps.il[ps.tos] = ps.i_l_follow;
193 case_ind = ps.i_l_follow + opt.case_indent; /* cases should be one
196 ps.i_l_follow += opt.case_indent + 1; /* statements should be two
198 ps.search_brace = opt.btype_2;
201 case semicolon: /* this indicates a simple stmt */
202 break_comma = false; /* turn off flag to break after commas in a
204 ps.p_stack[++ps.tos] = stmt;
205 ps.il[ps.tos] = ps.ind_level;
208 default: /* this is an error */
209 diag2(1, "Unknown code to parser");
213 } /* end of switch */
215 if (ps.tos >= STACKSIZE - 1)
216 errx(1, "Parser stack overflow");
218 reduce(); /* see if any reduction can be done */
221 for (i = 1; i <= ps.tos; ++i)
222 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
232 * FUNCTION: Implements the reduce part of the parsing algorithm
234 * ALGORITHM: The following reductions are done. Reductions are repeated
235 * until no more are possible.
238 * <stmt> <stmt> <stmtl>
239 * <stmtl> <stmt> <stmtl>
242 * switch <stmt> <stmt>
244 * "ifelse" <stmt> <stmt>
246 * while <stmt> <stmt>
247 * "dostmt" while <stmt>
249 * On each reduction, ps.i_l_follow (the indentation for the following line)
250 * is set to the indentation level associated with the old TOS.
256 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
262 * HISTORY: initial coding November 1976 D A Willcox of CAC
265 /*----------------------------------------------*\
267 \*----------------------------------------------*/
273 for (;;) { /* keep looping until there is nothing left to
276 switch (ps.p_stack[ps.tos]) {
279 switch (ps.p_stack[ps.tos - 1]) {
283 /* stmtl stmt or stmt stmt */
284 ps.p_stack[--ps.tos] = stmtl;
287 case dolit: /* <do> <stmt> */
288 ps.p_stack[--ps.tos] = dohead;
289 ps.i_l_follow = ps.il[ps.tos];
294 ps.p_stack[--ps.tos] = ifhead;
297 ps.p_stack[i] != stmt
299 ps.p_stack[i] != stmtl
301 ps.p_stack[i] != lbrace
304 ps.i_l_follow = ps.il[i];
306 * for the time being, we will assume that there is no else on
307 * this if, and set the indentation level accordingly. If an
308 * else is scanned, it will be fixed up later
313 /* <switch> <stmt> */
314 case_ind = ps.cstk[ps.tos - 1];
316 case decl: /* finish of a declaration */
318 /* <<if> <stmt> else> <stmt> */
323 ps.p_stack[--ps.tos] = stmt;
324 ps.i_l_follow = ps.il[ps.tos];
327 default: /* <anything else> <stmt> */
330 } /* end of section for <stmt> on top of stack */
333 case whilestmt: /* while (...) on top */
334 if (ps.p_stack[ps.tos - 1] == dohead) {
335 /* it is termination of a do while */
342 default: /* anything else on top */