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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)parse.c 8.1 (Berkeley) 6/6/93 32 * $FreeBSD: src/usr.bin/indent/parse.c,v 1.10 2003/06/15 09:28:17 charnier Exp $ 33 */ 34 35 #include <stdio.h> 36 #include "indent_globs.h" 37 #include "indent_codes.h" 38 #include "indent.h" 39 40 static void reduce(void); 41 42 void 43 parse(int tk) /* tk: the code for the construct scanned */ 44 { 45 int i; 46 47 #ifdef debug 48 printf("%2d - %s\n", tk, token); 49 #endif 50 51 while (ps.p_stack[ps.tos] == ifhead && tk != elselit) { 52 /* true if we have an if without an else */ 53 ps.p_stack[ps.tos] = stmt; /* apply the if(..) stmt ::= stmt 54 * reduction */ 55 reduce(); /* see if this allows any reduction */ 56 } 57 58 59 switch (tk) { /* go on and figure out what to do with the 60 * input */ 61 62 case decl: /* scanned a declaration word */ 63 ps.search_brace = btype_2; 64 /* indicate that following brace should be on same line */ 65 if (ps.p_stack[ps.tos] != decl) { /* only put one declaration 66 * onto stack */ 67 break_comma = true; /* while in declaration, newline should be 68 * forced after comma */ 69 ps.p_stack[++ps.tos] = decl; 70 ps.il[ps.tos] = ps.i_l_follow; 71 72 if (ps.ljust_decl) {/* only do if we want left justified 73 * declarations */ 74 ps.ind_level = 0; 75 for (i = ps.tos - 1; i > 0; --i) 76 if (ps.p_stack[i] == decl) 77 ++ps.ind_level; /* indentation is number of 78 * declaration levels deep we are */ 79 ps.i_l_follow = ps.ind_level; 80 } 81 } 82 break; 83 84 case ifstmt: /* scanned if (...) */ 85 if (ps.p_stack[ps.tos] == elsehead && ps.else_if) /* "else if ..." */ 86 ps.i_l_follow = ps.il[ps.tos]; 87 case dolit: /* 'do' */ 88 case forstmt: /* for (...) */ 89 ps.p_stack[++ps.tos] = tk; 90 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow; 91 ++ps.i_l_follow; /* subsequent statements should be indented 1 */ 92 ps.search_brace = btype_2; 93 break; 94 95 case lbrace: /* scanned { */ 96 break_comma = false; /* don't break comma in an initial list */ 97 if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl 98 || ps.p_stack[ps.tos] == stmtl) 99 ++ps.i_l_follow; /* it is a random, isolated stmt group or a 100 * declaration */ 101 else { 102 if (s_code == e_code) { 103 /* 104 * only do this if there is nothing on the line 105 */ 106 --ps.ind_level; 107 /* 108 * it is a group as part of a while, for, etc. 109 */ 110 if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1) 111 --ps.ind_level; 112 /* 113 * for a switch, brace should be two levels out from the code 114 */ 115 } 116 } 117 118 ps.p_stack[++ps.tos] = lbrace; 119 ps.il[ps.tos] = ps.ind_level; 120 ps.p_stack[++ps.tos] = stmt; 121 /* allow null stmt between braces */ 122 ps.il[ps.tos] = ps.i_l_follow; 123 break; 124 125 case whilestmt: /* scanned while (...) */ 126 if (ps.p_stack[ps.tos] == dohead) { 127 /* it is matched with do stmt */ 128 ps.ind_level = ps.i_l_follow = ps.il[ps.tos]; 129 ps.p_stack[++ps.tos] = whilestmt; 130 ps.il[ps.tos] = ps.ind_level = ps.i_l_follow; 131 } 132 else { /* it is a while loop */ 133 ps.p_stack[++ps.tos] = whilestmt; 134 ps.il[ps.tos] = ps.i_l_follow; 135 ++ps.i_l_follow; 136 ps.search_brace = btype_2; 137 } 138 139 break; 140 141 case elselit: /* scanned an else */ 142 143 if (ps.p_stack[ps.tos] != ifhead) 144 diag2(1, "Unmatched 'else'"); 145 else { 146 ps.ind_level = ps.il[ps.tos]; /* indentation for else should 147 * be same as for if */ 148 ps.i_l_follow = ps.ind_level + 1; /* everything following should 149 * be in 1 level */ 150 ps.p_stack[ps.tos] = elsehead; 151 /* remember if with else */ 152 ps.search_brace = btype_2 | ps.else_if; 153 } 154 break; 155 156 case rbrace: /* scanned a } */ 157 /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */ 158 if (ps.p_stack[ps.tos - 1] == lbrace) { 159 ps.ind_level = ps.i_l_follow = ps.il[--ps.tos]; 160 ps.p_stack[ps.tos] = stmt; 161 } 162 else 163 diag2(1, "Statement nesting error"); 164 break; 165 166 case swstmt: /* had switch (...) */ 167 ps.p_stack[++ps.tos] = swstmt; 168 ps.cstk[ps.tos] = case_ind; 169 /* save current case indent level */ 170 ps.il[ps.tos] = ps.i_l_follow; 171 case_ind = ps.i_l_follow + ps.case_indent; /* cases should be one 172 * level down from 173 * switch */ 174 ps.i_l_follow += ps.case_indent + 1; /* statements should be two 175 * levels in */ 176 ps.search_brace = btype_2; 177 break; 178 179 case semicolon: /* this indicates a simple stmt */ 180 break_comma = false; /* turn off flag to break after commas in a 181 * declaration */ 182 ps.p_stack[++ps.tos] = stmt; 183 ps.il[ps.tos] = ps.ind_level; 184 break; 185 186 default: /* this is an error */ 187 diag2(1, "Unknown code to parser"); 188 return; 189 190 191 } /* end of switch */ 192 193 reduce(); /* see if any reduction can be done */ 194 195 #ifdef debug 196 for (i = 1; i <= ps.tos; ++i) 197 printf("(%d %d)", ps.p_stack[i], ps.il[i]); 198 printf("\n"); 199 #endif 200 201 return; 202 } 203 204 /* 205 * NAME: reduce 206 * 207 * FUNCTION: Implements the reduce part of the parsing algorithm 208 * 209 * ALGORITHM: The following reductions are done. Reductions are repeated 210 * until no more are possible. 211 * 212 * Old TOS New TOS 213 * <stmt> <stmt> <stmtl> 214 * <stmtl> <stmt> <stmtl> 215 * do <stmt> "dostmt" 216 * if <stmt> "ifstmt" 217 * switch <stmt> <stmt> 218 * decl <stmt> <stmt> 219 * "ifelse" <stmt> <stmt> 220 * for <stmt> <stmt> 221 * while <stmt> <stmt> 222 * "dostmt" while <stmt> 223 * 224 * On each reduction, ps.i_l_follow (the indentation for the following line) 225 * is set to the indentation level associated with the old TOS. 226 * 227 * PARAMETERS: None 228 * 229 * RETURNS: Nothing 230 * 231 * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos = 232 * 233 * CALLS: None 234 * 235 * CALLED BY: parse 236 * 237 * HISTORY: initial coding November 1976 D A Willcox of CAC 238 * 239 */ 240 /*----------------------------------------------*\ 241 | REDUCTION PHASE | 242 \*----------------------------------------------*/ 243 static void 244 reduce(void) 245 { 246 int i; 247 248 for (;;) { /* keep looping until there is nothing left to 249 * reduce */ 250 251 switch (ps.p_stack[ps.tos]) { 252 253 case stmt: 254 switch (ps.p_stack[ps.tos - 1]) { 255 256 case stmt: 257 case stmtl: 258 /* stmtl stmt or stmt stmt */ 259 ps.p_stack[--ps.tos] = stmtl; 260 break; 261 262 case dolit: /* <do> <stmt> */ 263 ps.p_stack[--ps.tos] = dohead; 264 ps.i_l_follow = ps.il[ps.tos]; 265 break; 266 267 case ifstmt: 268 /* <if> <stmt> */ 269 ps.p_stack[--ps.tos] = ifhead; 270 for (i = ps.tos - 1; 271 ( 272 ps.p_stack[i] != stmt 273 && 274 ps.p_stack[i] != stmtl 275 && 276 ps.p_stack[i] != lbrace 277 ); 278 --i); 279 ps.i_l_follow = ps.il[i]; 280 /* 281 * for the time being, we will assume that there is no else on 282 * this if, and set the indentation level accordingly. If an 283 * else is scanned, it will be fixed up later 284 */ 285 break; 286 287 case swstmt: 288 /* <switch> <stmt> */ 289 case_ind = ps.cstk[ps.tos - 1]; 290 291 case decl: /* finish of a declaration */ 292 case elsehead: 293 /* <<if> <stmt> else> <stmt> */ 294 case forstmt: 295 /* <for> <stmt> */ 296 case whilestmt: 297 /* <while> <stmt> */ 298 ps.p_stack[--ps.tos] = stmt; 299 ps.i_l_follow = ps.il[ps.tos]; 300 break; 301 302 default: /* <anything else> <stmt> */ 303 return; 304 305 } /* end of section for <stmt> on top of stack */ 306 break; 307 308 case whilestmt: /* while (...) on top */ 309 if (ps.p_stack[ps.tos - 1] == dohead) { 310 /* it is termination of a do while */ 311 ps.tos -= 2; 312 break; 313 } 314 else 315 return; 316 317 default: /* anything else on top */ 318 return; 319 320 } 321 } 322 } 323