pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / indent / parse.c
blob18d22a42e714e91fb612177ea6a04958c643f9dc
1 /**
2 * Copyright (c) 1985 Sun Microsystems, Inc.
3 * Copyright (c) 1980 The Regents of the University of California.
4 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley, the University of Illinois,
13 * Urbana, and Sun Microsystems, Inc. The name of either University
14 * or Sun Microsystems may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #define PUBLIC extern
22 #include "./globs.h"
23 #include "./codes.h"
24 #include "proto.h"
29 void parse(tk)
30 int tk; /* the code for the construct
31 scanned */
33 int i;
35 #ifdef debug
36 printf("%2d - %s\n", tk, token);
37 #endif
39 while (ps.p_stack[ps.tos] == ifhead && tk != elselit)
41 /* true if we have an if without an else */
42 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::=
43 stmt reduction */
44 reduce(); /* see if this allows any
45 reduction */
49 switch (tk)
50 { /* go on and figure out what to
51 do with the input */
53 case decl: /* scanned a declaration word */
54 ps.search_brace = btype_2;
55 /* indicate that following brace should be on same line */
56 if (ps.p_stack[ps.tos] != decl)
57 { /* only put one declaration
58 onto stack */
59 break_comma = true; /* while in declaration,
60 newline should be forced
61 after comma */
62 ps.p_stack[++ps.tos] = decl;
63 ps.il[ps.tos] = ps.i_l_follow;
65 if (ps.ljust_decl)
66 { /* only do if we want left
67 justified declarations */
68 ps.ind_level = 0;
69 for (i = ps.tos - 1; i > 0; --i)
70 if (ps.p_stack[i] == decl)
71 ++ps.ind_level; /* indentation is number of
72 declaration levels deep we
73 are */
74 ps.i_l_follow = ps.ind_level;
77 break;
79 case ifstmt: /* scanned if (...) */
80 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */
81 ps.i_l_follow = ps.il[ps.tos];
82 case dolit: /* 'do' */
83 case forstmt: /* for (...) */
84 ps.p_stack[++ps.tos] = tk;
85 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
86 ++ps.i_l_follow; /* subsequent statements should
87 be indented 1 */
88 ps.search_brace = btype_2;
89 break;
91 case lbrace: /* scanned { */
92 break_comma = false; /* don't break comma in an
93 initial list */
94 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
95 || ps.p_stack[ps.tos] == stmtl)
96 ++ps.i_l_follow; /* it is a random, isolated
97 stmt group or a declaration */
98 else
100 if (s_code == e_code)
102 /* only do this if there is nothing on the line */
103 --ps.ind_level;
104 /* it is a group as part of a while, for, etc. */
105 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
106 --ps.ind_level;
107 /* for a switch, brace should be two levels out from the
108 code */
112 ps.p_stack[++ps.tos] = lbrace;
113 ps.il[ps.tos] = ps.ind_level;
114 ps.p_stack[++ps.tos] = stmt;
115 /* allow null stmt between braces */
116 ps.il[ps.tos] = ps.i_l_follow;
117 break;
119 case whilestmt: /* scanned while (...) */
120 if (ps.p_stack[ps.tos] == dohead)
122 /* it is matched with do stmt */
123 ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
124 ps.p_stack[++ps.tos] = whilestmt;
125 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
126 } else
127 { /* it is a while loop */
128 ps.p_stack[++ps.tos] = whilestmt;
129 ps.il[ps.tos] = ps.i_l_follow;
130 ++ps.i_l_follow;
131 ps.search_brace = btype_2;
134 break;
136 case elselit: /* scanned an else */
138 if (ps.p_stack[ps.tos] != ifhead)
139 diag(1, "Unmatched 'else'");
140 else
142 ps.ind_level = ps.il[ps.tos]; /* indentation for else should
143 be same as for if */
144 ps.i_l_follow = ps.ind_level + 1; /* everything following
145 should be in 1 level */
146 ps.p_stack[ps.tos] = elsehead;
147 /* remember if with else */
148 ps.search_brace = btype_2 | ps.else_if;
150 break;
152 case rbrace: /* scanned a } */
153 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
154 if (ps.p_stack[ps.tos - 1] == lbrace)
156 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
157 ps.p_stack[ps.tos] = stmt;
158 } else
159 diag(1, "Stmt nesting error.");
160 break;
162 case swstmt: /* had switch (...) */
163 ps.p_stack[++ps.tos] = swstmt;
164 ps.cstk[ps.tos] = case_ind;
165 /* save current case indent level */
166 ps.il[ps.tos] = ps.i_l_follow;
167 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one
168 level down from
169 switch */
170 ps.i_l_follow += ps.case_indent + 1; /* statements should be
171 two levels in */
172 ps.search_brace = btype_2;
173 break;
175 case semicolon: /* this indicates a simple stmt */
176 break_comma = false; /* turn off flag to break after
177 commas in a declaration */
178 ps.p_stack[++ps.tos] = stmt;
179 ps.il[ps.tos] = ps.ind_level;
180 break;
182 default: /* this is an error */
183 diag(1, "Unknown code to parser");
184 return;
187 } /* end of switch */
189 reduce(); /* see if any reduction can be
190 done */
192 #ifdef debug
193 for (i = 1; i <= ps.tos; ++i)
194 printf("(%d %d)", ps.p_stack[i], ps.il[i]);
195 printf("\n");
196 #endif
198 return;
202 * Copyright (C) 1976 by the Board of Trustees of the University of Illinois
204 * All rights reserved
207 * NAME: reduce
209 * FUNCTION: Implements the reduce part of the parsing algorithm
211 * ALGORITHM: The following reductions are done. Reductions are repeated until
212 * no more are possible.
214 * Old TOS New TOS <stmt> <stmt> <stmtl> <stmtl> <stmt> <stmtl> do
215 * <stmt> "dostmt" if <stmt> "ifstmt" switch <stmt> <stmt> decl
216 * <stmt> <stmt> "ifelse" <stmt> <stmt> for <stmt> <stmt> while
217 * <stmt> <stmt> "dostmt" while <stmt>
219 * On each reduction, ps.i_l_follow (the indentation for the following line) is
220 * set to the indentation level associated with the old TOS.
222 * PARAMETERS: None
224 * RETURNS: Nothing
226 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
228 * CALLS: None
230 * CALLED BY: parse
232 * HISTORY: initial coding November 1976 D A Willcox of CAC
234 */\f
235 /*----------------------------------------------*\
236 | REDUCTION PHASE |
237 \*----------------------------------------------*/
238 void reduce()
241 register int i;
243 for (;;)
244 { /* keep looping until there is
245 nothing left to reduce */
247 switch (ps.p_stack[ps.tos])
250 case stmt:
251 switch (ps.p_stack[ps.tos - 1])
254 case stmt:
255 case stmtl:
256 /* stmtl stmt or stmt stmt */
257 ps.p_stack[--ps.tos] = stmtl;
258 break;
260 case dolit: /* <do> <stmt> */
261 ps.p_stack[--ps.tos] = dohead;
262 ps.i_l_follow = ps.il[ps.tos];
263 break;
265 case ifstmt:
266 /* <if> <stmt> */
267 ps.p_stack[--ps.tos] = ifhead;
268 for (i = ps.tos - 1;
270 ps.p_stack[i] != stmt
272 ps.p_stack[i] != stmtl
274 ps.p_stack[i] != lbrace
276 --i);
277 ps.i_l_follow = ps.il[i];
278 /* for the time being, we will assume that there is no else
279 on this if, and set the indentation level accordingly.
280 If an else is scanned, it will be fixed up later */
281 break;
283 case swstmt:
284 /* <switch> <stmt> */
285 case_ind = ps.cstk[ps.tos - 1];
287 case decl: /* finish of a declaration */
288 case elsehead:
289 /* <<if> <stmt> else> <stmt> */
290 case forstmt:
291 /* <for> <stmt> */
292 case whilestmt:
293 /* <while> <stmt> */
294 ps.p_stack[--ps.tos] = stmt;
295 ps.i_l_follow = ps.il[ps.tos];
296 break;
298 default: /* <anything else> <stmt> */
299 return;
301 } /* end of section for <stmt> on
302 top of stack */
303 break;
305 case whilestmt: /* while (...) on top */
306 if (ps.p_stack[ps.tos - 1] == dohead)
308 /* it is termination of a do while */
309 ps.p_stack[--ps.tos] = stmt;
310 break;
311 } else
312 return;
314 default: /* anything else on top */
315 return;