Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.32
      1 /*	$NetBSD: parse.c,v 1.32 2021/10/07 21:52:54 rillig Exp $	*/
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-4-Clause
      5  *
      6  * Copyright (c) 1985 Sun Microsystems, Inc.
      7  * Copyright (c) 1980, 1993
      8  *	The Regents of the University of California.  All rights reserved.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #if 0
     41 static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
     42 #endif
     43 
     44 #include <sys/cdefs.h>
     45 #if defined(__NetBSD__)
     46 __RCSID("$FreeBSD$");
     47 #else
     48 __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
     49 #endif
     50 
     51 #include <err.h>
     52 #include <stdio.h>
     53 
     54 #include "indent.h"
     55 
     56 static void reduce(void);
     57 
     58 /*
     59  * Shift the token onto the parser stack, or reduce it by combining it with
     60  * previous tokens.
     61  */
     62 void
     63 parse(token_type ttype)
     64 {
     65 
     66 #ifdef debug
     67     printf("parse token: '%s' \"%s\"\n", token_type_name(ttype), token.s);
     68 #endif
     69 
     70     while (ps.p_stack[ps.tos] == if_expr_stmt && ttype != keyword_else) {
     71 	/* true if we have an if without an else */
     72 	ps.p_stack[ps.tos] = stmt;	/* apply the if(..) stmt ::= stmt
     73 					 * reduction */
     74 	reduce();		/* see if this allows any reduction */
     75     }
     76 
     77     switch (ttype) {
     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 
     83 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
     84 						 * onto stack */
     85 	    break_comma = true;	/* while in declaration, newline should be
     86 				 * forced after comma */
     87 	    ps.p_stack[++ps.tos] = decl;
     88 	    ps.il[ps.tos] = ps.ind_level_follow;
     89 
     90 	    if (opt.ljust_decl) {
     91 		ps.ind_level = 0;
     92 		for (int 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.ind_level_follow = ps.ind_level;
     97 	    }
     98 	}
     99 	break;
    100 
    101     case if_expr:		/* 'if' '(' <expr> ')' */
    102 	if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
    103 	    /*
    104 	     * Reduce "else if" to "if". This saves a lot of stack space in
    105 	     * case of a long "if-else-if ... else-if" sequence.
    106 	     */
    107 	    ps.ind_level_follow = ps.il[ps.tos--];
    108 	}
    109 	/* FALLTHROUGH */
    110     case keyword_do:
    111     case for_exprs:		/* 'for' (...) */
    112 	ps.p_stack[++ps.tos] = ttype;
    113 	ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
    114 	++ps.ind_level_follow;	/* subsequent statements should be indented 1 */
    115 	ps.search_brace = opt.btype_2;
    116 	break;
    117 
    118     case lbrace:
    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] == stmt_list)
    122 	    ++ps.ind_level_follow;	/* it is a random, isolated stmt
    123 				 * group or a declaration */
    124 	else {
    125 	    if (code.s == code.e) {
    126 		/* it is a group as part of a while, for, etc. */
    127 		--ps.ind_level;
    128 
    129 		/*
    130 		 * for a switch, brace should be two levels out from the code
    131 		 */
    132 		if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
    133 		    --ps.ind_level;
    134 	    }
    135 	}
    136 
    137 	ps.p_stack[++ps.tos] = lbrace;
    138 	ps.il[ps.tos] = ps.ind_level;
    139 	ps.p_stack[++ps.tos] = stmt;
    140 	/* allow null stmt between braces */
    141 	ps.il[ps.tos] = ps.ind_level_follow;
    142 	break;
    143 
    144     case while_expr:		/* 'while' '(' <expr> ')' */
    145 	if (ps.p_stack[ps.tos] == do_stmt) {
    146 	    /* it is matched with do stmt */
    147 	    ps.ind_level = ps.ind_level_follow = ps.il[ps.tos];
    148 	    ps.p_stack[++ps.tos] = while_expr;
    149 	    ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
    150 
    151 	} else {		/* it is a while loop */
    152 	    ps.p_stack[++ps.tos] = while_expr;
    153 	    ps.il[ps.tos] = ps.ind_level_follow;
    154 	    ++ps.ind_level_follow;
    155 	    ps.search_brace = opt.btype_2;
    156 	}
    157 
    158 	break;
    159 
    160     case keyword_else:
    161 	if (ps.p_stack[ps.tos] != if_expr_stmt)
    162 	    diag(1, "Unmatched 'else'");
    163 	else {
    164 	    ps.ind_level = ps.il[ps.tos];	/* indentation for else should
    165 						 * be same as for if */
    166 	    ps.ind_level_follow = ps.ind_level + 1;
    167 	    ps.p_stack[ps.tos] = if_expr_stmt_else;
    168 	    /* remember if with else */
    169 	    ps.search_brace = opt.btype_2 | opt.else_if;
    170 	}
    171 	break;
    172 
    173     case rbrace:
    174 	/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    175 	if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
    176 	    ps.ind_level = ps.ind_level_follow = ps.il[--ps.tos];
    177 	    ps.p_stack[ps.tos] = stmt;
    178 	} else
    179 	    diag(1, "Statement nesting error");
    180 	break;
    181 
    182     case switch_expr:		/* had switch (...) */
    183 	ps.p_stack[++ps.tos] = switch_expr;
    184 	ps.cstk[ps.tos] = case_ind;
    185 	/* save current case indent level */
    186 	ps.il[ps.tos] = ps.ind_level_follow;
    187 	/* cases should be one level deeper than the switch */
    188 	case_ind = (float)ps.ind_level_follow + opt.case_indent;
    189 	/* statements should be two levels deeper */
    190 	ps.ind_level_follow += (int)opt.case_indent + 1;
    191 	ps.search_brace = opt.btype_2;
    192 	break;
    193 
    194     case semicolon:		/* this indicates a simple stmt */
    195 	break_comma = false;	/* turn off flag to break after commas in a
    196 				 * declaration */
    197 	ps.p_stack[++ps.tos] = stmt;
    198 	ps.il[ps.tos] = ps.ind_level;
    199 	break;
    200 
    201     default:
    202 	diag(1, "Unknown code to parser");
    203 	return;
    204     }
    205 
    206     if (ps.tos >= STACKSIZE - 1)
    207 	errx(1, "Parser stack overflow");
    208 
    209     reduce();			/* see if any reduction can be done */
    210 
    211 #ifdef debug
    212     printf("parse stack:");
    213     for (int i = 1; i <= ps.tos; ++i)
    214 	printf(" ('%s' at %d)", token_type_name(ps.p_stack[i]), ps.il[i]);
    215     if (ps.tos == 0)
    216 	printf(" empty");
    217     printf("\n");
    218 #endif
    219 }
    220 
    221 /*----------------------------------------------*\
    222 |   REDUCTION PHASE				 |
    223 \*----------------------------------------------*/
    224 
    225 /*
    226  * Try to combine the statement on the top of the parse stack with the symbol
    227  * directly below it, replacing these two symbols with a single symbol.
    228  */
    229 static bool
    230 reduce_stmt(void)
    231 {
    232     switch (ps.p_stack[ps.tos - 1]) {
    233 
    234     case stmt:			/* stmt stmt */
    235     case stmt_list:		/* stmt_list stmt */
    236 	ps.p_stack[--ps.tos] = stmt_list;
    237 	return true;
    238 
    239     case keyword_do:		/* 'do' <stmt> */
    240 	ps.p_stack[--ps.tos] = do_stmt;
    241 	ps.ind_level_follow = ps.il[ps.tos];
    242 	return true;
    243 
    244     case if_expr:		/* 'if' '(' <expr> ')' <stmt> */
    245 	ps.p_stack[--ps.tos] = if_expr_stmt;
    246 	int i = ps.tos - 1;
    247 	while (ps.p_stack[i] != stmt &&
    248 	       ps.p_stack[i] != stmt_list &&
    249 	       ps.p_stack[i] != lbrace)
    250 	    --i;
    251 	ps.ind_level_follow = ps.il[i];
    252 	/*
    253 	 * for the time being, we will assume that there is no else on this
    254 	 * if, and set the indentation level accordingly. If an 'else' is
    255 	 * scanned, it will be fixed up later
    256 	 */
    257 	return true;
    258 
    259     case switch_expr:		/* 'switch' '(' <expr> ')' <stmt> */
    260 	case_ind = ps.cstk[ps.tos - 1];
    261 	/* FALLTHROUGH */
    262     case decl:			/* finish of a declaration */
    263     case if_expr_stmt_else:	/* 'if' '(' <expr> ')' <stmt> 'else' <stmt> */
    264     case for_exprs:		/* 'for' '(' ... ')' <stmt> */
    265     case while_expr:		/* 'while' '(' <expr> ')' <stmt> */
    266 	ps.p_stack[--ps.tos] = stmt;
    267 	ps.ind_level_follow = ps.il[ps.tos];
    268 	return true;
    269 
    270     default:			/* <anything else> <stmt> */
    271 	return false;
    272     }
    273 }
    274 
    275 /*
    276  * Repeatedly try to reduce the top two symbols on the parse stack to a
    277  * single symbol, until no more reductions are possible.
    278  *
    279  * On each reduction, ps.i_l_follow (the indentation for the following line)
    280  * is set to the indentation level associated with the old TOS.
    281  */
    282 static void
    283 reduce(void)
    284 {
    285 again:
    286     if (ps.p_stack[ps.tos] == stmt) {
    287 	if (reduce_stmt())
    288 	    goto again;
    289     } else if (ps.p_stack[ps.tos] == while_expr) {
    290 	if (ps.p_stack[ps.tos - 1] == do_stmt) {
    291 	    ps.tos -= 2;
    292 	    goto again;
    293 	}
    294     }
    295 }
    296