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