Sync zoneinfo database with tzdata2008c from elsie.
[dragonfly.git] / usr.bin / indent / parse.c
bloba4d5fbb381e8641aff444ffd5f31b33aeaa610b8
1 /*
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980, 1993
4 * The Regents of the University of California. All rights reserved.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
33 * SUCH DAMAGE.
35 * @(#)parse.c 8.1 (Berkeley) 6/6/93
36 * $FreeBSD: src/usr.bin/indent/parse.c,v 1.3.2.3 2001/12/06 19:28:47 schweikh Exp $
37 * $DragonFly: src/usr.bin/indent/parse.c,v 1.3 2005/04/10 20:55:38 drhodus Exp $
39 #if 0
40 #endif
42 #include <stdio.h>
43 #include "indent_globs.h"
44 #include "indent_codes.h"
45 #include "indent.h"
47 static void reduce(void);
49 void
50 parse(int tk) /* tk: the code for the construct scanned */
52 int i;
54 #ifdef debug
55 printf("%2d - %s\n", tk, token);
56 #endif
58 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
59 /* true if we have an if without an else */
60 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt
61 * reduction */
62 reduce(); /* see if this allows any reduction */
66 switch (tk) { /* go on and figure out what to do with the
67 * input */
69 case decl: /* scanned a declaration word */
70 ps.search_brace = btype_2;
71 /* indicate that following brace should be on same line */
72 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
73 * onto stack */
74 break_comma = true; /* while in declaration, newline should be
75 * forced after comma */
76 ps.p_stack[++ps.tos] = decl;
77 ps.il[ps.tos] = ps.i_l_follow;
79 if (ps.ljust_decl) {/* only do if we want left justified
80 * declarations */
81 ps.ind_level = 0;
82 for (i = ps.tos - 1; i > 0; --i)
83 if (ps.p_stack[i] == decl)
84 ++ps.ind_level; /* indentation is number of
85 * declaration levels deep we are */
86 ps.i_l_follow = ps.ind_level;
89 break;
91 case ifstmt: /* scanned if (...) */
92 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
93 ps.i_l_follow = ps.il[ps.tos];
94 case dolit: /* 'do' */
95 case forstmt: /* for (...) */
96 ps.p_stack[++ps.tos] = tk;
97 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
98 ++ps.i_l_follow; /* subsequent statements should be indented 1 */
99 ps.search_brace = btype_2;
100 break;
102 case lbrace: /* scanned { */
103 break_comma = false; /* don't break comma in an initial list */
104 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
105 || ps.p_stack[ps.tos] == stmtl)
106 ++ps.i_l_follow; /* it is a random, isolated stmt group or a
107 * declaration */
108 else {
109 if (s_code == e_code) {
111 * only do this if there is nothing on the line
113 --ps.ind_level;
115 * it is a group as part of a while, for, etc.
117 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
118 --ps.ind_level;
120 * for a switch, brace should be two levels out from the code
125 ps.p_stack[++ps.tos] = lbrace;
126 ps.il[ps.tos] = ps.ind_level;
127 ps.p_stack[++ps.tos] = stmt;
128 /* allow null stmt between braces */
129 ps.il[ps.tos] = ps.i_l_follow;
130 break;
132 case whilestmt: /* scanned while (...) */
133 if (ps.p_stack[ps.tos] == dohead) {
134 /* it is matched with do stmt */
135 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
136 ps.p_stack[++ps.tos] = whilestmt;
137 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
139 else { /* it is a while loop */
140 ps.p_stack[++ps.tos] = whilestmt;
141 ps.il[ps.tos] = ps.i_l_follow;
142 ++ps.i_l_follow;
143 ps.search_brace = btype_2;
146 break;
148 case elselit: /* scanned an else */
150 if (ps.p_stack[ps.tos] != ifhead)
151 diag2(1, "Unmatched 'else'");
152 else {
153 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
154 * be same as for if */
155 ps.i_l_follow = ps.ind_level + 1; /* everything following should
156 * be in 1 level */
157 ps.p_stack[ps.tos] = elsehead;
158 /* remember if with else */
159 ps.search_brace = btype_2 | ps.else_if;
161 break;
163 case rbrace: /* scanned a } */
164 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
165 if (ps.p_stack[ps.tos - 1] == lbrace) {
166 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
167 ps.p_stack[ps.tos] = stmt;
169 else
170 diag2(1, "Stmt nesting error.");
171 break;
173 case swstmt: /* had switch (...) */
174 ps.p_stack[++ps.tos] = swstmt;
175 ps.cstk[ps.tos] = case_ind;
176 /* save current case indent level */
177 ps.il[ps.tos] = ps.i_l_follow;
178 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
179 * level down from
180 * switch */
181 ps.i_l_follow += ps.case_indent + 1; /* statements should be two
182 * levels in */
183 ps.search_brace = btype_2;
184 break;
186 case semicolon: /* this indicates a simple stmt */
187 break_comma = false; /* turn off flag to break after commas in a
188 * declaration */
189 ps.p_stack[++ps.tos] = stmt;
190 ps.il[ps.tos] = ps.ind_level;
191 break;
193 default: /* this is an error */
194 diag2(1, "Unknown code to parser");
195 return;
198 } /* end of switch */
200 reduce(); /* see if any reduction can be done */
202 #ifdef debug
203 for (i = 1; i <= ps.tos; ++i)
204 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
205 printf("\n");
206 #endif
208 return;
212 * NAME: reduce
214 * FUNCTION: Implements the reduce part of the parsing algorithm
216 * ALGORITHM: The following reductions are done. Reductions are repeated
217 * until no more are possible.
219 * Old TOS New TOS
220 * <stmt> <stmt> <stmtl>
221 * <stmtl> <stmt> <stmtl>
222 * do <stmt> "dostmt"
223 * if <stmt> "ifstmt"
224 * switch <stmt> <stmt>
225 * decl <stmt> <stmt>
226 * "ifelse" <stmt> <stmt>
227 * for <stmt> <stmt>
228 * while <stmt> <stmt>
229 * "dostmt" while <stmt>
231 * On each reduction, ps.i_l_follow (the indentation for the following line)
232 * is set to the indentation level associated with the old TOS.
234 * PARAMETERS: None
236 * RETURNS: Nothing
238 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
240 * CALLS: None
242 * CALLED BY: parse
244 * HISTORY: initial coding November 1976 D A Willcox of CAC
247 /*----------------------------------------------*\
248 | REDUCTION PHASE |
249 \*----------------------------------------------*/
250 static void
251 reduce(void)
253 int i;
255 for (;;) { /* keep looping until there is nothing left to
256 * reduce */
258 switch (ps.p_stack[ps.tos]) {
260 case stmt:
261 switch (ps.p_stack[ps.tos - 1]) {
263 case stmt:
264 case stmtl:
265 /* stmtl stmt or stmt stmt */
266 ps.p_stack[--ps.tos] = stmtl;
267 break;
269 case dolit: /* <do> <stmt> */
270 ps.p_stack[--ps.tos] = dohead;
271 ps.i_l_follow = ps.il[ps.tos];
272 break;
274 case ifstmt:
275 /* <if> <stmt> */
276 ps.p_stack[--ps.tos] = ifhead;
277 for (i = ps.tos - 1;
279 ps.p_stack[i] != stmt
281 ps.p_stack[i] != stmtl
283 ps.p_stack[i] != lbrace
285 --i);
286 ps.i_l_follow = ps.il[i];
288 * for the time being, we will assume that there is no else on
289 * this if, and set the indentation level accordingly. If an
290 * else is scanned, it will be fixed up later
292 break;
294 case swstmt:
295 /* <switch> <stmt> */
296 case_ind = ps.cstk[ps.tos - 1];
298 case decl: /* finish of a declaration */
299 case elsehead:
300 /* <<if> <stmt> else> <stmt> */
301 case forstmt:
302 /* <for> <stmt> */
303 case whilestmt:
304 /* <while> <stmt> */
305 ps.p_stack[--ps.tos] = stmt;
306 ps.i_l_follow = ps.il[ps.tos];
307 break;
309 default: /* <anything else> <stmt> */
310 return;
312 } /* end of section for <stmt> on top of stack */
313 break;
315 case whilestmt: /* while (...) on top */
316 if (ps.p_stack[ps.tos - 1] == dohead) {
317 /* it is termination of a do while */
318 ps.tos -= 2;
319 break;
321 else
322 return;
324 default: /* anything else on top */
325 return;