xref: /openbsd/usr.bin/indent/parse.c (revision 133306f0)
1 /*	$OpenBSD: parse.c,v 1.4 2001/01/08 07:14:42 pjanzen Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
7  * Copyright (c) 1985 Sun Microsystems, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 /*static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";*/
41 static char rcsid[] = "$OpenBSD: parse.c,v 1.4 2001/01/08 07:14:42 pjanzen Exp $";
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 #include "indent_globs.h"
46 #include "indent_codes.h"
47 
48 void reduce();
49 
50 void
51 parse(tk)
52     int         tk;		/* the code for the construct scanned */
53 {
54     int         i;
55 
56 #ifdef debug
57     printf("%2d - %s\n", tk, token);
58 #endif
59 
60     while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
61 	/* true if we have an if without an else */
62 	ps.p_stack[ps.tos] = stmt;	/* apply the if(..) stmt ::= stmt
63 					 * reduction */
64 	reduce();		/* see if this allows any reduction */
65     }
66 
67 
68     switch (tk) {		/* go on and figure out what to do with the
69 				 * input */
70 
71     case decl:			/* scanned a declaration word */
72 	ps.search_brace = btype_2;
73 	/* indicate that following brace should be on same line */
74 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
75 						 * onto stack */
76 	    break_comma = true;	/* while in declaration, newline should be
77 				 * forced after comma */
78 	    ps.p_stack[++ps.tos] = decl;
79 	    ps.il[ps.tos] = ps.i_l_follow;
80 
81 	    if (ps.ljust_decl) {/* only do if we want left justified
82 				 * declarations */
83 		ps.ind_level = 0;
84 		for (i = ps.tos - 1; i > 0; --i)
85 		    if (ps.p_stack[i] == decl)
86 			++ps.ind_level;	/* indentation is number of
87 					 * declaration levels deep we are */
88 		ps.i_l_follow = ps.ind_level;
89 	    }
90 	}
91 	break;
92 
93     case ifstmt:		/* scanned if (...) */
94 	if (ps.p_stack[ps.tos] == elsehead && ps.else_if)	/* "else if ..." */
95 	    ps.i_l_follow = ps.il[ps.tos];
96     case dolit:		/* 'do' */
97     case forstmt:		/* for (...) */
98 	ps.p_stack[++ps.tos] = tk;
99 	ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
100 	++ps.i_l_follow;	/* subsequent statements should be indented 1 */
101 	ps.search_brace = btype_2;
102 	break;
103 
104     case lbrace:		/* scanned { */
105 	break_comma = false;	/* don't break comma in an initial list */
106 	if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
107 		|| ps.p_stack[ps.tos] == stmtl)
108 	    ++ps.i_l_follow;	/* it is a random, isolated stmt group or a
109 				 * declaration */
110 	else {
111 	    if (s_code == e_code) {
112 		/*
113 		 * only do this if there is nothing on the line
114 		 */
115 		--ps.ind_level;
116 		/*
117 		 * it is a group as part of a while, for, etc.
118 		 */
119 		if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
120 		    --ps.ind_level;
121 		/*
122 		 * for a switch, brace should be two levels out from the code
123 		 */
124 	    }
125 	}
126 
127 	ps.p_stack[++ps.tos] = lbrace;
128 	ps.il[ps.tos] = ps.ind_level;
129 	ps.p_stack[++ps.tos] = stmt;
130 	/* allow null stmt between braces */
131 	ps.il[ps.tos] = ps.i_l_follow;
132 	break;
133 
134     case whilestmt:		/* scanned while (...) */
135 	if (ps.p_stack[ps.tos] == dohead) {
136 	    /* it is matched with do stmt */
137 	    ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
138 	    ps.p_stack[++ps.tos] = whilestmt;
139 	    ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
140 	}
141 	else {			/* it is a while loop */
142 	    ps.p_stack[++ps.tos] = whilestmt;
143 	    ps.il[ps.tos] = ps.i_l_follow;
144 	    ++ps.i_l_follow;
145 	    ps.search_brace = btype_2;
146 	}
147 
148 	break;
149 
150     case elselit:		/* scanned an else */
151 
152 	if (ps.p_stack[ps.tos] != ifhead)
153 	    diag(1, "Unmatched 'else'");
154 	else {
155 	    ps.ind_level = ps.il[ps.tos];	/* indentation for else should
156 						 * be same as for if */
157 	    ps.i_l_follow = ps.ind_level + 1;	/* everything following should
158 						 * be in 1 level */
159 	    ps.p_stack[ps.tos] = elsehead;
160 	    /* remember if with else */
161 	    ps.search_brace = btype_2 | ps.else_if;
162 	}
163 	break;
164 
165     case rbrace:		/* scanned a } */
166 	/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
167 	if (ps.p_stack[ps.tos - 1] == lbrace) {
168 	    ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
169 	    ps.p_stack[ps.tos] = stmt;
170 	}
171 	else
172 	    diag(1, "Stmt nesting error.");
173 	break;
174 
175     case swstmt:		/* had switch (...) */
176 	ps.p_stack[++ps.tos] = swstmt;
177 	ps.cstk[ps.tos] = case_ind;
178 	/* save current case indent level */
179 	ps.il[ps.tos] = ps.i_l_follow;
180 	case_ind = ps.i_l_follow + ps.case_indent;	/* cases should be one
181 							 * level down from
182 							 * switch */
183 	ps.i_l_follow += ps.case_indent + 1;	/* statements should be two
184 						 * levels in */
185 	ps.search_brace = btype_2;
186 	break;
187 
188     case semicolon:		/* this indicates a simple stmt */
189 	break_comma = false;	/* turn off flag to break after commas in a
190 				 * declaration */
191 	ps.p_stack[++ps.tos] = stmt;
192 	ps.il[ps.tos] = ps.ind_level;
193 	break;
194 
195     default:			/* this is an error */
196 	diag(1, "Unknown code to parser");
197 	return;
198 
199 
200     }				/* end of switch */
201 
202     reduce();			/* see if any reduction can be done */
203 
204 #ifdef debug
205     for (i = 1; i <= ps.tos; ++i)
206 	printf("(%d %d)", ps.p_stack[i], ps.il[i]);
207     printf("\n");
208 #endif
209 
210     return;
211 }
212 
213 /*
214  * NAME: reduce
215  *
216  * FUNCTION: Implements the reduce part of the parsing algorithm
217  *
218  * ALGORITHM: The following reductions are done.  Reductions are repeated
219  *	until no more are possible.
220  *
221  * Old TOS		New TOS
222  * <stmt> <stmt>	<stmtl>
223  * <stmtl> <stmt>	<stmtl>
224  * do <stmt>		"dostmt"
225  * if <stmt>		"ifstmt"
226  * switch <stmt>	<stmt>
227  * decl <stmt>		<stmt>
228  * "ifelse" <stmt>	<stmt>
229  * for <stmt>		<stmt>
230  * while <stmt>		<stmt>
231  * "dostmt" while	<stmt>
232  *
233  * On each reduction, ps.i_l_follow (the indentation for the following line)
234  * is set to the indentation level associated with the old TOS.
235  *
236  * PARAMETERS: None
237  *
238  * RETURNS: Nothing
239  *
240  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
241  *
242  * CALLS: None
243  *
244  * CALLED BY: parse
245  *
246  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
247  *
248  */
249 /*----------------------------------------------*\
250 |   REDUCTION PHASE				    |
251 \*----------------------------------------------*/
252 void
253 reduce()
254 {
255 
256     register int i;
257 
258     for (;;) {			/* keep looping until there is nothing left to
259 				 * reduce */
260 
261 	switch (ps.p_stack[ps.tos]) {
262 
263 	case stmt:
264 	    switch (ps.p_stack[ps.tos - 1]) {
265 
266 	    case stmt:
267 	    case stmtl:
268 		/* stmtl stmt or stmt stmt */
269 		ps.p_stack[--ps.tos] = stmtl;
270 		break;
271 
272 	    case dolit:	/* <do> <stmt> */
273 		ps.p_stack[--ps.tos] = dohead;
274 		ps.i_l_follow = ps.il[ps.tos];
275 		break;
276 
277 	    case ifstmt:
278 		/* <if> <stmt> */
279 		ps.p_stack[--ps.tos] = ifhead;
280 		for (i = ps.tos - 1;
281 			(
282 			 ps.p_stack[i] != stmt
283 			 &&
284 			 ps.p_stack[i] != stmtl
285 			 &&
286 			 ps.p_stack[i] != lbrace
287 			 );
288 			--i);
289 		ps.i_l_follow = ps.il[i];
290 		/*
291 		 * for the time being, we will assume that there is no else on
292 		 * this if, and set the indentation level accordingly. If an
293 		 * else is scanned, it will be fixed up later
294 		 */
295 		break;
296 
297 	    case swstmt:
298 		/* <switch> <stmt> */
299 		case_ind = ps.cstk[ps.tos - 1];
300 
301 	    case decl:		/* finish of a declaration */
302 	    case elsehead:
303 		/* <<if> <stmt> else> <stmt> */
304 	    case forstmt:
305 		/* <for> <stmt> */
306 	    case whilestmt:
307 		/* <while> <stmt> */
308 		ps.p_stack[--ps.tos] = stmt;
309 		ps.i_l_follow = ps.il[ps.tos];
310 		break;
311 
312 	    default:		/* <anything else> <stmt> */
313 		return;
314 
315 	    }			/* end of section for <stmt> on top of stack */
316 	    break;
317 
318 	case whilestmt:	/* while (...) on top */
319 	    if (ps.p_stack[ps.tos - 1] == dohead) {
320 		/* it is termination of a do while */
321 		ps.p_stack[--ps.tos] = stmt;
322 		break;
323 	    }
324 	    else
325 		return;
326 
327 	default:		/* anything else on top */
328 	    return;
329 
330 	}
331     }
332 }
333