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 __FBSDID("$FreeBSD$");
49 #include "indent_globs.h"
50 #include "indent_codes.h"
57 static void reduce(void);
60 parse(int tk) /* tk: the code for the construct scanned */
65 printf("%2d - %s\n", tk, token);
68 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
69 /* true if we have an if without an else */
70 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
72 reduce(); /* see if this allows any reduction */
76 switch (tk) { /* go on and figure out what to do with the
79 case decl: /* scanned a declaration word */
80 ps.search_brace = opt.btype_2;
81 /* indicate that following brace should be on same line */
82 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
84 break_comma = true; /* while in declaration, newline should be
85 * forced after comma */
86 ps.p_stack[++ps.tos] = decl;
87 ps.il[ps.tos] = ps.i_l_follow;
89 if (opt.ljust_decl) {/* only do if we want left justified
92 for (i = ps.tos - 1; i > 0; --i)
93 if (ps.p_stack[i] == decl)
94 ++ps.ind_level; /* indentation is number of
95 * declaration levels deep we are */
96 ps.i_l_follow = ps.ind_level;
101 case ifstmt: /* scanned if (...) */
102 if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
104 * Note that the stack pointer here is decremented, effectively
105 * reducing "else if" to "if". This saves a lot of stack space
106 * in case of a long "if-else-if ... else-if" sequence.
108 ps.i_l_follow = ps.il[ps.tos--];
109 /* the rest is the same as for dolit and forstmt */
111 case dolit: /* 'do' */
112 case forstmt: /* for (...) */
113 ps.p_stack[++ps.tos] = tk;
114 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
115 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
116 ps.search_brace = opt.btype_2;
119 case lbrace: /* scanned { */
120 break_comma = false; /* don't break comma in an initial list */
121 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
122 || ps.p_stack[ps.tos] == stmtl)
123 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
126 if (s_code == e_code) {
128 * only do this if there is nothing on the line
132 * it is a group as part of a while, for, etc.
134 if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
137 * for a switch, brace should be two levels out from the code
142 ps.p_stack[++ps.tos] = lbrace;
143 ps.il[ps.tos] = ps.ind_level;
144 ps.p_stack[++ps.tos] = stmt;
145 /* allow null stmt between braces */
146 ps.il[ps.tos] = ps.i_l_follow;
149 case whilestmt: /* scanned while (...) */
150 if (ps.p_stack[ps.tos] == dohead) {
151 /* it is matched with do stmt */
152 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
153 ps.p_stack[++ps.tos] = whilestmt;
154 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
156 else { /* it is a while loop */
157 ps.p_stack[++ps.tos] = whilestmt;
158 ps.il[ps.tos] = ps.i_l_follow;
160 ps.search_brace = opt.btype_2;
165 case elselit: /* scanned an else */
167 if (ps.p_stack[ps.tos] != ifhead)
168 diag2(1, "Unmatched 'else'");
170 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
171 * be same as for if */
172 ps.i_l_follow = ps.ind_level + 1; /* everything following should
174 ps.p_stack[ps.tos] = elsehead;
175 /* remember if with else */
176 ps.search_brace = opt.btype_2 | opt.else_if;
180 case rbrace: /* scanned a } */
181 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
182 if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
183 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
184 ps.p_stack[ps.tos] = stmt;
187 diag2(1, "Statement nesting error");
190 case swstmt: /* had switch (...) */
191 ps.p_stack[++ps.tos] = swstmt;
192 ps.cstk[ps.tos] = case_ind;
193 /* save current case indent level */
194 ps.il[ps.tos] = ps.i_l_follow;
195 case_ind = ps.i_l_follow + opt.case_indent; /* cases should be one
198 ps.i_l_follow += opt.case_indent + 1; /* statements should be two
200 ps.search_brace = opt.btype_2;
203 case semicolon: /* this indicates a simple stmt */
204 break_comma = false; /* turn off flag to break after commas in a
206 ps.p_stack[++ps.tos] = stmt;
207 ps.il[ps.tos] = ps.ind_level;
210 default: /* this is an error */
211 diag2(1, "Unknown code to parser");
215 } /* end of switch */
217 if (ps.tos >= STACKSIZE - 1)
218 errx(1, "Parser stack overflow");
220 reduce(); /* see if any reduction can be done */
223 for (i = 1; i <= ps.tos; ++i)
224 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
234 * FUNCTION: Implements the reduce part of the parsing algorithm
236 * ALGORITHM: The following reductions are done. Reductions are repeated
237 * until no more are possible.
240 * <stmt> <stmt> <stmtl>
241 * <stmtl> <stmt> <stmtl>
244 * switch <stmt> <stmt>
246 * "ifelse" <stmt> <stmt>
248 * while <stmt> <stmt>
249 * "dostmt" while <stmt>
251 * On each reduction, ps.i_l_follow (the indentation for the following line)
252 * is set to the indentation level associated with the old TOS.
258 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
264 * HISTORY: initial coding November 1976 D A Willcox of CAC
267 /*----------------------------------------------*\
269 \*----------------------------------------------*/
275 for (;;) { /* keep looping until there is nothing left to
278 switch (ps.p_stack[ps.tos]) {
281 switch (ps.p_stack[ps.tos - 1]) {
285 /* stmtl stmt or stmt stmt */
286 ps.p_stack[--ps.tos] = stmtl;
289 case dolit: /* <do> <stmt> */
290 ps.p_stack[--ps.tos] = dohead;
291 ps.i_l_follow = ps.il[ps.tos];
296 ps.p_stack[--ps.tos] = ifhead;
299 ps.p_stack[i] != stmt
301 ps.p_stack[i] != stmtl
303 ps.p_stack[i] != lbrace
306 ps.i_l_follow = ps.il[i];
308 * for the time being, we will assume that there is no else on
309 * this if, and set the indentation level accordingly. If an
310 * else is scanned, it will be fixed up later
315 /* <switch> <stmt> */
316 case_ind = ps.cstk[ps.tos - 1];
318 case decl: /* finish of a declaration */
320 /* <<if> <stmt> else> <stmt> */
325 ps.p_stack[--ps.tos] = stmt;
326 ps.i_l_follow = ps.il[ps.tos];
329 default: /* <anything else> <stmt> */
332 } /* end of section for <stmt> on top of stack */
335 case whilestmt: /* while (...) on top */
336 if (ps.p_stack[ps.tos - 1] == dohead) {
337 /* it is termination of a do while */
344 default: /* anything else on top */