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