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