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