xref: /freebsd/usr.bin/indent/parse.c (revision 88b24a62d1fae2125f0f40115755d47ce37d4e12)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1985 Sun Microsystems, Inc.
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  */
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
41 #endif /* not lint */
42 #endif
43 
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <err.h>
49 #include <stdio.h>
50 
51 #include "indent_globs.h"
52 #include "indent_codes.h"
53 #include "indent.h"
54 
55 static void reduce(void);
56 
57 void
58 parse(int tk) /* tk: the code for the construct scanned */
59 {
60     int         i;
61 
62 #ifdef debug
63     printf("%2d - %s\n", tk, token);
64 #endif
65 
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
69 					 * reduction */
70 	reduce();		/* see if this allows any reduction */
71     }
72 
73 
74     switch (tk) {		/* go on and figure out what to do with the
75 				 * input */
76 
77     case decl:			/* scanned a declaration word */
78 	ps.search_brace = btype_2;
79 	/* indicate that following brace should be on same line */
80 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
81 						 * onto stack */
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;
86 
87 	    if (ps.ljust_decl) {/* only do if we want left justified
88 				 * declarations */
89 		ps.ind_level = 0;
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;
95 	    }
96 	}
97 	break;
98 
99     case ifstmt:		/* scanned if (...) */
100 	if (ps.p_stack[ps.tos] == elsehead && ps.else_if)	/* "else if ..." */
101 		/*
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.
105 		 */
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 = btype_2;
114 	break;
115 
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
121 				 * declaration */
122 	else {
123 	    if (s_code == e_code) {
124 		/*
125 		 * only do this if there is nothing on the line
126 		 */
127 		--ps.ind_level;
128 		/*
129 		 * it is a group as part of a while, for, etc.
130 		 */
131 		if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
132 		    --ps.ind_level;
133 		/*
134 		 * for a switch, brace should be two levels out from the code
135 		 */
136 	    }
137 	}
138 
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;
144 	break;
145 
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;
152 	}
153 	else {			/* it is a while loop */
154 	    ps.p_stack[++ps.tos] = whilestmt;
155 	    ps.il[ps.tos] = ps.i_l_follow;
156 	    ++ps.i_l_follow;
157 	    ps.search_brace = btype_2;
158 	}
159 
160 	break;
161 
162     case elselit:		/* scanned an else */
163 
164 	if (ps.p_stack[ps.tos] != ifhead)
165 	    diag2(1, "Unmatched 'else'");
166 	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
170 						 * be in 1 level */
171 	    ps.p_stack[ps.tos] = elsehead;
172 	    /* remember if with else */
173 	    ps.search_brace = btype_2 | ps.else_if;
174 	}
175 	break;
176 
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;
182 	}
183 	else
184 	    diag2(1, "Statement nesting error");
185 	break;
186 
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 + ps.case_indent;	/* cases should be one
193 							 * level down from
194 							 * switch */
195 	ps.i_l_follow += ps.case_indent + 1;	/* statements should be two
196 						 * levels in */
197 	ps.search_brace = btype_2;
198 	break;
199 
200     case semicolon:		/* this indicates a simple stmt */
201 	break_comma = false;	/* turn off flag to break after commas in a
202 				 * declaration */
203 	ps.p_stack[++ps.tos] = stmt;
204 	ps.il[ps.tos] = ps.ind_level;
205 	break;
206 
207     default:			/* this is an error */
208 	diag2(1, "Unknown code to parser");
209 	return;
210 
211 
212     }				/* end of switch */
213 
214     if (ps.tos >= nitems(ps.p_stack) - 1)
215 	errx(1, "Parser stack overflow");
216 
217     reduce();			/* see if any reduction can be done */
218 
219 #ifdef debug
220     for (i = 1; i <= ps.tos; ++i)
221 	printf("(%d %d)", ps.p_stack[i], ps.il[i]);
222     printf("\n");
223 #endif
224 
225     return;
226 }
227 
228 /*
229  * NAME: reduce
230  *
231  * FUNCTION: Implements the reduce part of the parsing algorithm
232  *
233  * ALGORITHM: The following reductions are done.  Reductions are repeated
234  *	until no more are possible.
235  *
236  * Old TOS		New TOS
237  * <stmt> <stmt>	<stmtl>
238  * <stmtl> <stmt>	<stmtl>
239  * do <stmt>		"dostmt"
240  * if <stmt>		"ifstmt"
241  * switch <stmt>	<stmt>
242  * decl <stmt>		<stmt>
243  * "ifelse" <stmt>	<stmt>
244  * for <stmt>		<stmt>
245  * while <stmt>		<stmt>
246  * "dostmt" while	<stmt>
247  *
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.
250  *
251  * PARAMETERS: None
252  *
253  * RETURNS: Nothing
254  *
255  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
256  *
257  * CALLS: None
258  *
259  * CALLED BY: parse
260  *
261  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
262  *
263  */
264 /*----------------------------------------------*\
265 |   REDUCTION PHASE				    |
266 \*----------------------------------------------*/
267 static void
268 reduce(void)
269 {
270     int i;
271 
272     for (;;) {			/* keep looping until there is nothing left to
273 				 * reduce */
274 
275 	switch (ps.p_stack[ps.tos]) {
276 
277 	case stmt:
278 	    switch (ps.p_stack[ps.tos - 1]) {
279 
280 	    case stmt:
281 	    case stmtl:
282 		/* stmtl stmt or stmt stmt */
283 		ps.p_stack[--ps.tos] = stmtl;
284 		break;
285 
286 	    case dolit:	/* <do> <stmt> */
287 		ps.p_stack[--ps.tos] = dohead;
288 		ps.i_l_follow = ps.il[ps.tos];
289 		break;
290 
291 	    case ifstmt:
292 		/* <if> <stmt> */
293 		ps.p_stack[--ps.tos] = ifhead;
294 		for (i = ps.tos - 1;
295 			(
296 			 ps.p_stack[i] != stmt
297 			 &&
298 			 ps.p_stack[i] != stmtl
299 			 &&
300 			 ps.p_stack[i] != lbrace
301 			 );
302 			--i);
303 		ps.i_l_follow = ps.il[i];
304 		/*
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
308 		 */
309 		break;
310 
311 	    case swstmt:
312 		/* <switch> <stmt> */
313 		case_ind = ps.cstk[ps.tos - 1];
314 		/* FALLTHROUGH */
315 	    case decl:		/* finish of a declaration */
316 	    case elsehead:
317 		/* <<if> <stmt> else> <stmt> */
318 	    case forstmt:
319 		/* <for> <stmt> */
320 	    case whilestmt:
321 		/* <while> <stmt> */
322 		ps.p_stack[--ps.tos] = stmt;
323 		ps.i_l_follow = ps.il[ps.tos];
324 		break;
325 
326 	    default:		/* <anything else> <stmt> */
327 		return;
328 
329 	    }			/* end of section for <stmt> on top of stack */
330 	    break;
331 
332 	case whilestmt:	/* while (...) on top */
333 	    if (ps.p_stack[ps.tos - 1] == dohead) {
334 		/* it is termination of a do while */
335 		ps.tos -= 2;
336 		break;
337 	    }
338 	    else
339 		return;
340 
341 	default:		/* anything else on top */
342 	    return;
343 
344 	}
345     }
346 }
347