2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 #include "indent_globs.h"
48 #include "indent_codes.h"
51 static void reduce(void);
54 parse(int tk) /* tk: the code for the construct scanned */
59 printf("%2d - %s\n", tk, token);
62 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
63 /* true if we have an if without an else */
64 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
66 reduce(); /* see if this allows any reduction */
70 switch (tk) { /* go on and figure out what to do with the
73 case decl: /* scanned a declaration word */
74 ps.search_brace = btype_2;
75 /* indicate that following brace should be on same line */
76 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
78 break_comma = true; /* while in declaration, newline should be
79 * forced after comma */
80 ps.p_stack[++ps.tos] = decl;
81 ps.il[ps.tos] = ps.i_l_follow;
83 if (ps.ljust_decl) {/* only do if we want left justified
86 for (i = ps.tos - 1; i > 0; --i)
87 if (ps.p_stack[i] == decl)
88 ++ps.ind_level; /* indentation is number of
89 * declaration levels deep we are */
90 ps.i_l_follow = ps.ind_level;
95 case ifstmt: /* scanned if (...) */
96 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
97 ps.i_l_follow = ps.il[ps.tos];
98 /* the rest is the same as for dolit and forstmt */
99 case dolit: /* 'do' */
100 case forstmt: /* for (...) */
101 ps.p_stack[++ps.tos] = tk;
102 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
103 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
104 ps.search_brace = btype_2;
107 case lbrace: /* scanned { */
108 break_comma = false; /* don't break comma in an initial list */
109 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
110 || ps.p_stack[ps.tos] == stmtl)
111 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
114 if (s_code == e_code) {
116 * only do this if there is nothing on the line
120 * it is a group as part of a while, for, etc.
122 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
125 * for a switch, brace should be two levels out from the code
130 ps.p_stack[++ps.tos] = lbrace;
131 ps.il[ps.tos] = ps.ind_level;
132 ps.p_stack[++ps.tos] = stmt;
133 /* allow null stmt between braces */
134 ps.il[ps.tos] = ps.i_l_follow;
137 case whilestmt: /* scanned while (...) */
138 if (ps.p_stack[ps.tos] == dohead) {
139 /* it is matched with do stmt */
140 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
141 ps.p_stack[++ps.tos] = whilestmt;
142 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
144 else { /* it is a while loop */
145 ps.p_stack[++ps.tos] = whilestmt;
146 ps.il[ps.tos] = ps.i_l_follow;
148 ps.search_brace = btype_2;
153 case elselit: /* scanned an else */
155 if (ps.p_stack[ps.tos] != ifhead)
156 diag2(1, "Unmatched 'else'");
158 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
159 * be same as for if */
160 ps.i_l_follow = ps.ind_level + 1; /* everything following should
162 ps.p_stack[ps.tos] = elsehead;
163 /* remember if with else */
164 ps.search_brace = btype_2 | ps.else_if;
168 case rbrace: /* scanned a } */
169 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
170 if (ps.p_stack[ps.tos - 1] == lbrace) {
171 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
172 ps.p_stack[ps.tos] = stmt;
175 diag2(1, "Statement nesting error");
178 case swstmt: /* had switch (...) */
179 ps.p_stack[++ps.tos] = swstmt;
180 ps.cstk[ps.tos] = case_ind;
181 /* save current case indent level */
182 ps.il[ps.tos] = ps.i_l_follow;
183 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
186 ps.i_l_follow += ps.case_indent + 1; /* statements should be two
188 ps.search_brace = btype_2;
191 case semicolon: /* this indicates a simple stmt */
192 break_comma = false; /* turn off flag to break after commas in a
194 ps.p_stack[++ps.tos] = stmt;
195 ps.il[ps.tos] = ps.ind_level;
198 default: /* this is an error */
199 diag2(1, "Unknown code to parser");
203 } /* end of switch */
205 if (ps.tos >= STACKSIZE)
206 errx(1, "Parser stack overflow");
208 reduce(); /* see if any reduction can be done */
211 for (i = 1; i <= ps.tos; ++i)
212 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
222 * FUNCTION: Implements the reduce part of the parsing algorithm
224 * ALGORITHM: The following reductions are done. Reductions are repeated
225 * until no more are possible.
228 * <stmt> <stmt> <stmtl>
229 * <stmtl> <stmt> <stmtl>
232 * switch <stmt> <stmt>
234 * "ifelse" <stmt> <stmt>
236 * while <stmt> <stmt>
237 * "dostmt" while <stmt>
239 * On each reduction, ps.i_l_follow (the indentation for the following line)
240 * is set to the indentation level associated with the old TOS.
246 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
252 * HISTORY: initial coding November 1976 D A Willcox of CAC
255 /*----------------------------------------------*\
257 \*----------------------------------------------*/
263 for (;;) { /* keep looping until there is nothing left to
266 switch (ps.p_stack[ps.tos]) {
269 switch (ps.p_stack[ps.tos - 1]) {
273 /* stmtl stmt or stmt stmt */
274 ps.p_stack[--ps.tos] = stmtl;
277 case dolit: /* <do> <stmt> */
278 ps.p_stack[--ps.tos] = dohead;
279 ps.i_l_follow = ps.il[ps.tos];
284 ps.p_stack[--ps.tos] = ifhead;
287 ps.p_stack[i] != stmt
289 ps.p_stack[i] != stmtl
291 ps.p_stack[i] != lbrace
294 ps.i_l_follow = ps.il[i];
296 * for the time being, we will assume that there is no else on
297 * this if, and set the indentation level accordingly. If an
298 * else is scanned, it will be fixed up later
303 /* <switch> <stmt> */
304 case_ind = ps.cstk[ps.tos - 1];
306 case decl: /* finish of a declaration */
308 /* <<if> <stmt> else> <stmt> */
313 ps.p_stack[--ps.tos] = stmt;
314 ps.i_l_follow = ps.il[ps.tos];
317 default: /* <anything else> <stmt> */
320 } /* end of section for <stmt> on top of stack */
323 case whilestmt: /* while (...) on top */
324 if (ps.p_stack[ps.tos - 1] == dohead) {
325 /* it is termination of a do while */
332 default: /* anything else on top */