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