Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.59
      1 /*	$NetBSD: parse.c,v 1.59 2023/05/16 12:46:43 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 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: parse.c,v 1.59 2023/05/16 12:46:43 rillig Exp $");
     42 
     43 #include <err.h>
     44 
     45 #include "indent.h"
     46 
     47 static void reduce(void);
     48 
     49 static int
     50 decl_level(void)
     51 {
     52     int level = 0;
     53     for (int i = ps.tos - 1; i > 0; i--)
     54 	if (ps.s_sym[i] == psym_decl)
     55 	    level++;
     56     return level;
     57 }
     58 
     59 /*
     60  * Shift the token onto the parser stack, or reduce it by combining it with
     61  * previous tokens.
     62  */
     63 void
     64 parse(parser_symbol psym)
     65 {
     66     debug_println("parse token: %s", psym_name[psym]);
     67 
     68     if (psym != psym_else) {
     69 	while (ps.s_sym[ps.tos] == psym_if_expr_stmt) {
     70 	    ps.s_sym[ps.tos] = psym_stmt;
     71 	    reduce();
     72 	}
     73     }
     74 
     75     switch (psym) {
     76 
     77     case psym_decl:
     78 	if (ps.s_sym[ps.tos] == psym_decl)
     79 	    break;		/* only put one declaration onto stack */
     80 
     81 	break_comma = true;	/* while in a declaration, force a newline
     82 				 * after comma */
     83 	ps.s_sym[++ps.tos] = psym_decl;
     84 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
     85 
     86 	if (opt.ljust_decl)
     87 	    ps.ind_level_follow = ps.ind_level = decl_level();
     88 	break;
     89 
     90     case psym_if_expr:
     91 	if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else && opt.else_if) {
     92 	    /*
     93 	     * Reduce "else if" to "if". This saves a lot of stack space in
     94 	     * case of a long "if-else-if ... else-if" sequence.
     95 	     */
     96 	    ps.ind_level_follow = ps.s_ind_level[ps.tos--];
     97 	}
     98 	/* FALLTHROUGH */
     99     case psym_do:
    100     case psym_for_exprs:
    101 	ps.s_sym[++ps.tos] = psym;
    102 	ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
    103 	++ps.ind_level_follow;
    104 	break;
    105 
    106     case psym_lbrace:
    107 	break_comma = false;	/* don't break comma in an initializer list */
    108 	if (ps.s_sym[ps.tos] == psym_stmt || ps.s_sym[ps.tos] == psym_decl
    109 		|| ps.s_sym[ps.tos] == psym_stmt_list)
    110 	    ++ps.ind_level_follow;	/* it is a random, isolated stmt group
    111 					 * or a declaration */
    112 	else {
    113 	    if (code.len == 0) {
    114 		/* it is a group as part of a while, for, etc. */
    115 		--ps.ind_level;
    116 
    117 		/*
    118 		 * for a switch, brace should be two levels out from the code
    119 		 */
    120 		if (ps.s_sym[ps.tos] == psym_switch_expr &&
    121 			opt.case_indent >= 1.0F)
    122 		    --ps.ind_level;
    123 	    }
    124 	}
    125 
    126 	ps.s_sym[++ps.tos] = psym_lbrace;
    127 	ps.s_ind_level[ps.tos] = ps.ind_level;
    128 	ps.s_sym[++ps.tos] = psym_stmt;
    129 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    130 	break;
    131 
    132     case psym_while_expr:
    133 	if (ps.s_sym[ps.tos] == psym_do_stmt) {
    134 	    /* it is matched with do stmt */
    135 	    ps.ind_level = ps.ind_level_follow = ps.s_ind_level[ps.tos];
    136 	    ps.s_sym[++ps.tos] = psym_while_expr;
    137 	    ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
    138 
    139 	} else {		/* it is a while loop */
    140 	    ps.s_sym[++ps.tos] = psym_while_expr;
    141 	    ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    142 	    ++ps.ind_level_follow;
    143 	}
    144 
    145 	break;
    146 
    147     case psym_else:
    148 	if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
    149 	    diag(1, "Unmatched 'else'");
    150 	else {
    151 	    ps.ind_level = ps.s_ind_level[ps.tos];
    152 	    ps.ind_level_follow = ps.ind_level + 1;
    153 	    ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
    154 	}
    155 	break;
    156 
    157     case psym_rbrace:
    158 	/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    159 	if (ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace) {
    160 	    ps.ind_level = ps.ind_level_follow = ps.s_ind_level[--ps.tos];
    161 	    ps.s_sym[ps.tos] = psym_stmt;
    162 	} else
    163 	    diag(1, "Statement nesting error");
    164 	break;
    165 
    166     case psym_switch_expr:
    167 	ps.s_sym[++ps.tos] = psym_switch_expr;
    168 	ps.s_case_ind_level[ps.tos] = case_ind;
    169 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    170 	case_ind = (float)ps.ind_level_follow + opt.case_indent;
    171 	ps.ind_level_follow += (int)opt.case_indent + 1;
    172 	break;
    173 
    174     case psym_0:		/* a simple statement */
    175 	break_comma = false;	/* don't break after comma in a declaration */
    176 	ps.s_sym[++ps.tos] = psym_stmt;
    177 	ps.s_ind_level[ps.tos] = ps.ind_level;
    178 	break;
    179 
    180     default:
    181 	diag(1, "Unknown code to parser");
    182 	return;
    183     }
    184 
    185     if (ps.tos >= STACKSIZE - 1)
    186 	errx(1, "Parser stack overflow");
    187 
    188     debug_parse_stack("before reduction");
    189     reduce();			/* see if any reduction can be done */
    190     debug_parse_stack("after reduction");
    191 }
    192 
    193 /*
    194  * Try to combine the statement on the top of the parse stack with the symbol
    195  * directly below it, replacing these two symbols with a single symbol.
    196  */
    197 static bool
    198 reduce_stmt(void)
    199 {
    200     switch (ps.s_sym[ps.tos - 1]) {
    201 
    202     case psym_stmt:
    203     case psym_stmt_list:
    204 	ps.s_sym[--ps.tos] = psym_stmt_list;
    205 	return true;
    206 
    207     case psym_do:
    208 	ps.s_sym[--ps.tos] = psym_do_stmt;
    209 	ps.ind_level_follow = ps.s_ind_level[ps.tos];
    210 	return true;
    211 
    212     case psym_if_expr:
    213 	ps.s_sym[--ps.tos] = psym_if_expr_stmt;
    214 	int i = ps.tos - 1;
    215 	while (ps.s_sym[i] != psym_stmt &&
    216 		ps.s_sym[i] != psym_stmt_list &&
    217 		ps.s_sym[i] != psym_lbrace)
    218 	    --i;
    219 	ps.ind_level_follow = ps.s_ind_level[i];
    220 	/*
    221 	 * For the time being, assume that there is no 'else' on this 'if',
    222 	 * and set the indentation level accordingly. If an 'else' is scanned,
    223 	 * it will be fixed up later.
    224 	 */
    225 	return true;
    226 
    227     case psym_switch_expr:
    228 	case_ind = ps.s_case_ind_level[ps.tos - 1];
    229 	/* FALLTHROUGH */
    230     case psym_decl:		/* finish of a declaration */
    231     case psym_if_expr_stmt_else:
    232     case psym_for_exprs:
    233     case psym_while_expr:
    234 	ps.s_sym[--ps.tos] = psym_stmt;
    235 	ps.ind_level_follow = ps.s_ind_level[ps.tos];
    236 	return true;
    237 
    238     default:
    239 	return false;
    240     }
    241 }
    242 
    243 /*
    244  * Repeatedly try to reduce the top two symbols on the parse stack to a single
    245  * symbol, until no more reductions are possible.
    246  */
    247 static void
    248 reduce(void)
    249 {
    250 again:
    251     if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt())
    252 	goto again;
    253     if (ps.s_sym[ps.tos] == psym_while_expr &&
    254 	    ps.s_sym[ps.tos - 1] == psym_do_stmt) {
    255 	ps.tos -= 2;
    256 	goto again;
    257     }
    258 }
    259