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