Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.35
      1 /*	$NetBSD: parse.c,v 1.35 2021/10/08 23:47:41 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     debug_println("parse token: '%s' \"%s\"",
     66 	token_type_name(ttype), token.s);
     67 
     68     if (ttype != keyword_else) {
     69 	while (ps.p_stack[ps.tos] == if_expr_stmt) {
     70 	    ps.p_stack[ps.tos] = stmt;
     71 	    reduce();
     72 	}
     73     }
     74 
     75     switch (ttype) {
     76 
     77     case decl:			/* scanned a declaration word */
     78 	ps.search_brace = opt.brace_same_line;
     79 	/* indicate that following brace should be on same line */
     80 
     81 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
     82 						 * onto stack */
     83 	    break_comma = true;	/* while in declaration, newline should be
     84 				 * forced after comma */
     85 	    ps.p_stack[++ps.tos] = decl;
     86 	    ps.il[ps.tos] = ps.ind_level_follow;
     87 
     88 	    if (opt.ljust_decl) {
     89 		ps.ind_level = 0;
     90 		for (int i = ps.tos - 1; i > 0; --i)
     91 		    if (ps.p_stack[i] == decl)
     92 			++ps.ind_level;	/* indentation is number of
     93 					 * declaration levels deep we are */
     94 		ps.ind_level_follow = ps.ind_level;
     95 	    }
     96 	}
     97 	break;
     98 
     99     case if_expr:		/* 'if' '(' <expr> ')' */
    100 	if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
    101 	    /*
    102 	     * Reduce "else if" to "if". This saves a lot of stack space in
    103 	     * case of a long "if-else-if ... else-if" sequence.
    104 	     */
    105 	    ps.ind_level_follow = ps.il[ps.tos--];
    106 	}
    107 	/* FALLTHROUGH */
    108     case keyword_do:
    109     case for_exprs:		/* 'for' (...) */
    110 	ps.p_stack[++ps.tos] = ttype;
    111 	ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
    112 	++ps.ind_level_follow;	/* subsequent statements should be indented 1 */
    113 	ps.search_brace = opt.brace_same_line;
    114 	break;
    115 
    116     case lbrace:
    117 	break_comma = false;	/* don't break comma in an initializer list */
    118 	if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
    119 		|| ps.p_stack[ps.tos] == stmt_list)
    120 	    ++ps.ind_level_follow;	/* it is a random, isolated stmt
    121 				 * group or a declaration */
    122 	else {
    123 	    if (code.s == code.e) {
    124 		/* it is a group as part of a while, for, etc. */
    125 		--ps.ind_level;
    126 
    127 		/*
    128 		 * for a switch, brace should be two levels out from the code
    129 		 */
    130 		if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
    131 		    --ps.ind_level;
    132 	    }
    133 	}
    134 
    135 	ps.p_stack[++ps.tos] = lbrace;
    136 	ps.il[ps.tos] = ps.ind_level;
    137 	ps.p_stack[++ps.tos] = stmt;
    138 	/* allow null stmt between braces */
    139 	ps.il[ps.tos] = ps.ind_level_follow;
    140 	break;
    141 
    142     case while_expr:		/* 'while' '(' <expr> ')' */
    143 	if (ps.p_stack[ps.tos] == do_stmt) {
    144 	    /* it is matched with do stmt */
    145 	    ps.ind_level = ps.ind_level_follow = ps.il[ps.tos];
    146 	    ps.p_stack[++ps.tos] = while_expr;
    147 	    ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
    148 
    149 	} else {		/* it is a while loop */
    150 	    ps.p_stack[++ps.tos] = while_expr;
    151 	    ps.il[ps.tos] = ps.ind_level_follow;
    152 	    ++ps.ind_level_follow;
    153 	    ps.search_brace = opt.brace_same_line;
    154 	}
    155 
    156 	break;
    157 
    158     case keyword_else:
    159 	if (ps.p_stack[ps.tos] != if_expr_stmt)
    160 	    diag(1, "Unmatched 'else'");
    161 	else {
    162 	    ps.ind_level = ps.il[ps.tos];	/* indentation for else should
    163 						 * be same as for if */
    164 	    ps.ind_level_follow = ps.ind_level + 1;
    165 	    ps.p_stack[ps.tos] = if_expr_stmt_else;
    166 	    /* remember if with else */
    167 	    ps.search_brace = opt.brace_same_line || opt.else_if;
    168 	}
    169 	break;
    170 
    171     case rbrace:
    172 	/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    173 	if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
    174 	    ps.ind_level = ps.ind_level_follow = ps.il[--ps.tos];
    175 	    ps.p_stack[ps.tos] = stmt;
    176 	} else
    177 	    diag(1, "Statement nesting error");
    178 	break;
    179 
    180     case switch_expr:		/* had switch (...) */
    181 	ps.p_stack[++ps.tos] = switch_expr;
    182 	ps.cstk[ps.tos] = case_ind;
    183 	/* save current case indent level */
    184 	ps.il[ps.tos] = ps.ind_level_follow;
    185 	/* cases should be one level deeper than the switch */
    186 	case_ind = (float)ps.ind_level_follow + opt.case_indent;
    187 	/* statements should be two levels deeper */
    188 	ps.ind_level_follow += (int)opt.case_indent + 1;
    189 	ps.search_brace = opt.brace_same_line;
    190 	break;
    191 
    192     case semicolon:		/* this indicates a simple stmt */
    193 	break_comma = false;	/* turn off flag to break after commas in a
    194 				 * declaration */
    195 	ps.p_stack[++ps.tos] = stmt;
    196 	ps.il[ps.tos] = ps.ind_level;
    197 	break;
    198 
    199     default:
    200 	diag(1, "Unknown code to parser");
    201 	return;
    202     }
    203 
    204     if (ps.tos >= STACKSIZE - 1)
    205 	errx(1, "Parser stack overflow");
    206 
    207     reduce();			/* see if any reduction can be done */
    208 
    209 #ifdef debug
    210     printf("parse stack:");
    211     for (int i = 1; i <= ps.tos; ++i)
    212 	printf(" ('%s' at %d)", token_type_name(ps.p_stack[i]), ps.il[i]);
    213     if (ps.tos == 0)
    214 	printf(" empty");
    215     printf("\n");
    216 #endif
    217 }
    218 
    219 /*----------------------------------------------*\
    220 |   REDUCTION PHASE				 |
    221 \*----------------------------------------------*/
    222 
    223 /*
    224  * Try to combine the statement on the top of the parse stack with the symbol
    225  * directly below it, replacing these two symbols with a single symbol.
    226  */
    227 static bool
    228 reduce_stmt(void)
    229 {
    230     switch (ps.p_stack[ps.tos - 1]) {
    231 
    232     case stmt:			/* stmt stmt */
    233     case stmt_list:		/* stmt_list stmt */
    234 	ps.p_stack[--ps.tos] = stmt_list;
    235 	return true;
    236 
    237     case keyword_do:		/* 'do' <stmt> */
    238 	ps.p_stack[--ps.tos] = do_stmt;
    239 	ps.ind_level_follow = ps.il[ps.tos];
    240 	return true;
    241 
    242     case if_expr:		/* 'if' '(' <expr> ')' <stmt> */
    243 	ps.p_stack[--ps.tos] = if_expr_stmt;
    244 	int i = ps.tos - 1;
    245 	while (ps.p_stack[i] != stmt &&
    246 	       ps.p_stack[i] != stmt_list &&
    247 	       ps.p_stack[i] != lbrace)
    248 	    --i;
    249 	ps.ind_level_follow = ps.il[i];
    250 	/*
    251 	 * for the time being, we will assume that there is no else on this
    252 	 * if, and set the indentation level accordingly. If an 'else' is
    253 	 * scanned, it will be fixed up later
    254 	 */
    255 	return true;
    256 
    257     case switch_expr:		/* 'switch' '(' <expr> ')' <stmt> */
    258 	case_ind = ps.cstk[ps.tos - 1];
    259 	/* FALLTHROUGH */
    260     case decl:			/* finish of a declaration */
    261     case if_expr_stmt_else:	/* 'if' '(' <expr> ')' <stmt> 'else' <stmt> */
    262     case for_exprs:		/* 'for' '(' ... ')' <stmt> */
    263     case while_expr:		/* 'while' '(' <expr> ')' <stmt> */
    264 	ps.p_stack[--ps.tos] = stmt;
    265 	ps.ind_level_follow = ps.il[ps.tos];
    266 	return true;
    267 
    268     default:			/* <anything else> <stmt> */
    269 	return false;
    270     }
    271 }
    272 
    273 /*
    274  * Repeatedly try to reduce the top two symbols on the parse stack to a
    275  * single symbol, until no more reductions are possible.
    276  *
    277  * On each reduction, ps.i_l_follow (the indentation for the following line)
    278  * is set to the indentation level associated with the old TOS.
    279  */
    280 static void
    281 reduce(void)
    282 {
    283 again:
    284     if (ps.p_stack[ps.tos] == stmt) {
    285 	if (reduce_stmt())
    286 	    goto again;
    287     } else if (ps.p_stack[ps.tos] == while_expr) {
    288 	if (ps.p_stack[ps.tos - 1] == do_stmt) {
    289 	    ps.tos -= 2;
    290 	    goto again;
    291 	}
    292     }
    293 }
    294