]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/indent/parse.c
Fix kernel panic in mps(4) driver.
[FreeBSD/FreeBSD.git] / usr.bin / indent / parse.c
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.
6  *
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.
22  *
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.
34  */
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)parse.c     8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <err.h>
46 #include <stdio.h>
47 #include "indent_globs.h"
48 #include "indent_codes.h"
49 #include "indent.h"
50
51 static void reduce(void);
52
53 void
54 parse(int tk) /* tk: the code for the construct scanned */
55 {
56     int         i;
57
58 #ifdef debug
59     printf("%2d - %s\n", tk, token);
60 #endif
61
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
65                                          * reduction */
66         reduce();               /* see if this allows any reduction */
67     }
68
69
70     switch (tk) {               /* go on and figure out what to do with the
71                                  * input */
72
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
77                                                  * onto stack */
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;
82
83             if (ps.ljust_decl) {/* only do if we want left justified
84                                  * declarations */
85                 ps.ind_level = 0;
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;
91             }
92         }
93         break;
94
95     case ifstmt:                /* scanned if (...) */
96         if (ps.p_stack[ps.tos] == elsehead && ps.else_if)       /* "else if ..." */
97                 /*
98                  * Note that the stack pointer here is decremented, effectively
99                  * reducing "else if" to "if". This saves a lot of stack space
100                  * in case of a long "if-else-if ... else-if" sequence.
101                  */
102                 ps.i_l_follow = ps.il[ps.tos--];
103         /* the rest is the same as for dolit and forstmt */
104     case dolit:         /* 'do' */
105     case forstmt:               /* for (...) */
106         ps.p_stack[++ps.tos] = tk;
107         ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
108         ++ps.i_l_follow;        /* subsequent statements should be indented 1 */
109         ps.search_brace = btype_2;
110         break;
111
112     case lbrace:                /* scanned { */
113         break_comma = false;    /* don't break comma in an initial list */
114         if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
115                 || ps.p_stack[ps.tos] == stmtl)
116             ++ps.i_l_follow;    /* it is a random, isolated stmt group or a
117                                  * declaration */
118         else {
119             if (s_code == e_code) {
120                 /*
121                  * only do this if there is nothing on the line
122                  */
123                 --ps.ind_level;
124                 /*
125                  * it is a group as part of a while, for, etc.
126                  */
127                 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
128                     --ps.ind_level;
129                 /*
130                  * for a switch, brace should be two levels out from the code
131                  */
132             }
133         }
134
135         ps.p_stack[++ps.tos] = lbrace;
136         ps.il[ps.tos] = ps.ind_level;
137         ps.p_stack[++ps.tos] = stmt;
138         /* allow null stmt between braces */
139         ps.il[ps.tos] = ps.i_l_follow;
140         break;
141
142     case whilestmt:             /* scanned while (...) */
143         if (ps.p_stack[ps.tos] == dohead) {
144             /* it is matched with do stmt */
145             ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
146             ps.p_stack[++ps.tos] = whilestmt;
147             ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
148         }
149         else {                  /* it is a while loop */
150             ps.p_stack[++ps.tos] = whilestmt;
151             ps.il[ps.tos] = ps.i_l_follow;
152             ++ps.i_l_follow;
153             ps.search_brace = btype_2;
154         }
155
156         break;
157
158     case elselit:               /* scanned an else */
159
160         if (ps.p_stack[ps.tos] != ifhead)
161             diag2(1, "Unmatched 'else'");
162         else {
163             ps.ind_level = ps.il[ps.tos];       /* indentation for else should
164                                                  * be same as for if */
165             ps.i_l_follow = ps.ind_level + 1;   /* everything following should
166                                                  * be in 1 level */
167             ps.p_stack[ps.tos] = elsehead;
168             /* remember if with else */
169             ps.search_brace = btype_2 | ps.else_if;
170         }
171         break;
172
173     case rbrace:                /* scanned a } */
174         /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
175         if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
176             ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
177             ps.p_stack[ps.tos] = stmt;
178         }
179         else
180             diag2(1, "Statement nesting error");
181         break;
182
183     case swstmt:                /* had switch (...) */
184         ps.p_stack[++ps.tos] = swstmt;
185         ps.cstk[ps.tos] = case_ind;
186         /* save current case indent level */
187         ps.il[ps.tos] = ps.i_l_follow;
188         case_ind = ps.i_l_follow + ps.case_indent;      /* cases should be one
189                                                          * level down from
190                                                          * switch */
191         ps.i_l_follow += ps.case_indent + 1;    /* statements should be two
192                                                  * levels in */
193         ps.search_brace = btype_2;
194         break;
195
196     case semicolon:             /* this indicates a simple stmt */
197         break_comma = false;    /* turn off flag to break after commas in a
198                                  * declaration */
199         ps.p_stack[++ps.tos] = stmt;
200         ps.il[ps.tos] = ps.ind_level;
201         break;
202
203     default:                    /* this is an error */
204         diag2(1, "Unknown code to parser");
205         return;
206
207
208     }                           /* end of switch */
209
210     if (ps.tos >= STACKSIZE - 1)
211         errx(1, "Parser stack overflow");
212
213     reduce();                   /* see if any reduction can be done */
214
215 #ifdef debug
216     for (i = 1; i <= ps.tos; ++i)
217         printf("(%d %d)", ps.p_stack[i], ps.il[i]);
218     printf("\n");
219 #endif
220
221     return;
222 }
223
224 /*
225  * NAME: reduce
226  *
227  * FUNCTION: Implements the reduce part of the parsing algorithm
228  *
229  * ALGORITHM: The following reductions are done.  Reductions are repeated
230  *      until no more are possible.
231  *
232  * Old TOS              New TOS
233  * <stmt> <stmt>        <stmtl>
234  * <stmtl> <stmt>       <stmtl>
235  * do <stmt>            "dostmt"
236  * if <stmt>            "ifstmt"
237  * switch <stmt>        <stmt>
238  * decl <stmt>          <stmt>
239  * "ifelse" <stmt>      <stmt>
240  * for <stmt>           <stmt>
241  * while <stmt>         <stmt>
242  * "dostmt" while       <stmt>
243  *
244  * On each reduction, ps.i_l_follow (the indentation for the following line)
245  * is set to the indentation level associated with the old TOS.
246  *
247  * PARAMETERS: None
248  *
249  * RETURNS: Nothing
250  *
251  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
252  *
253  * CALLS: None
254  *
255  * CALLED BY: parse
256  *
257  * HISTORY: initial coding      November 1976   D A Willcox of CAC
258  *
259  */
260 /*----------------------------------------------*\
261 |   REDUCTION PHASE                                 |
262 \*----------------------------------------------*/
263 static void
264 reduce(void)
265 {
266     int i;
267
268     for (;;) {                  /* keep looping until there is nothing left to
269                                  * reduce */
270
271         switch (ps.p_stack[ps.tos]) {
272
273         case stmt:
274             switch (ps.p_stack[ps.tos - 1]) {
275
276             case stmt:
277             case stmtl:
278                 /* stmtl stmt or stmt stmt */
279                 ps.p_stack[--ps.tos] = stmtl;
280                 break;
281
282             case dolit: /* <do> <stmt> */
283                 ps.p_stack[--ps.tos] = dohead;
284                 ps.i_l_follow = ps.il[ps.tos];
285                 break;
286
287             case ifstmt:
288                 /* <if> <stmt> */
289                 ps.p_stack[--ps.tos] = ifhead;
290                 for (i = ps.tos - 1;
291                         (
292                          ps.p_stack[i] != stmt
293                          &&
294                          ps.p_stack[i] != stmtl
295                          &&
296                          ps.p_stack[i] != lbrace
297                          );
298                         --i);
299                 ps.i_l_follow = ps.il[i];
300                 /*
301                  * for the time being, we will assume that there is no else on
302                  * this if, and set the indentation level accordingly. If an
303                  * else is scanned, it will be fixed up later
304                  */
305                 break;
306
307             case swstmt:
308                 /* <switch> <stmt> */
309                 case_ind = ps.cstk[ps.tos - 1];
310                 /* FALLTHROUGH */
311             case decl:          /* finish of a declaration */
312             case elsehead:
313                 /* <<if> <stmt> else> <stmt> */
314             case forstmt:
315                 /* <for> <stmt> */
316             case whilestmt:
317                 /* <while> <stmt> */
318                 ps.p_stack[--ps.tos] = stmt;
319                 ps.i_l_follow = ps.il[ps.tos];
320                 break;
321
322             default:            /* <anything else> <stmt> */
323                 return;
324
325             }                   /* end of section for <stmt> on top of stack */
326             break;
327
328         case whilestmt: /* while (...) on top */
329             if (ps.p_stack[ps.tos - 1] == dohead) {
330                 /* it is termination of a do while */
331                 ps.tos -= 2;
332                 break;
333             }
334             else
335                 return;
336
337         default:                /* anything else on top */
338             return;
339
340         }
341     }
342 }