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