Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.63
      1  1.63  rillig /*	$NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $	*/
      2   1.3     tls 
      3   1.9   kamil /*-
      4   1.9   kamil  * SPDX-License-Identifier: BSD-4-Clause
      5   1.9   kamil  *
      6   1.9   kamil  * Copyright (c) 1985 Sun Microsystems, Inc.
      7   1.4     mrg  * Copyright (c) 1980, 1993
      8   1.4     mrg  *	The Regents of the University of California.  All rights reserved.
      9   1.1     cgd  * All rights reserved.
     10   1.1     cgd  *
     11   1.1     cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1     cgd  * modification, are permitted provided that the following conditions
     13   1.1     cgd  * are met:
     14   1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1     cgd  *    must display the following acknowledgement:
     21   1.1     cgd  *	This product includes software developed by the University of
     22   1.1     cgd  *	California, Berkeley and its contributors.
     23   1.1     cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1     cgd  *    may be used to endorse or promote products derived from this software
     25   1.1     cgd  *    without specific prior written permission.
     26   1.1     cgd  *
     27   1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1     cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1     cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1     cgd  * SUCH DAMAGE.
     38   1.1     cgd  */
     39   1.1     cgd 
     40   1.5   lukem #include <sys/cdefs.h>
     41  1.63  rillig __RCSID("$NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $");
     42   1.1     cgd 
     43   1.9   kamil #include <err.h>
     44  1.12  rillig 
     45   1.9   kamil #include "indent.h"
     46   1.9   kamil 
     47   1.9   kamil static void reduce(void);
     48   1.1     cgd 
     49  1.45  rillig static int
     50  1.45  rillig decl_level(void)
     51  1.45  rillig {
     52  1.60  rillig 	int level = 0;
     53  1.60  rillig 	for (int i = ps.tos - 1; i > 0; i--)
     54  1.60  rillig 		if (ps.s_sym[i] == psym_decl)
     55  1.60  rillig 			level++;
     56  1.60  rillig 	return level;
     57  1.45  rillig }
     58  1.45  rillig 
     59  1.31  rillig /*
     60  1.31  rillig  * Shift the token onto the parser stack, or reduce it by combining it with
     61  1.31  rillig  * previous tokens.
     62  1.31  rillig  */
     63   1.5   lukem void
     64  1.39  rillig parse(parser_symbol psym)
     65   1.1     cgd {
     66  1.62  rillig 	debug_blank_line();
     67  1.60  rillig 	debug_println("parse token: %s", psym_name[psym]);
     68   1.1     cgd 
     69  1.60  rillig 	if (psym != psym_else) {
     70  1.60  rillig 		while (ps.s_sym[ps.tos] == psym_if_expr_stmt) {
     71  1.60  rillig 			ps.s_sym[ps.tos] = psym_stmt;
     72  1.60  rillig 			reduce();
     73  1.60  rillig 		}
     74  1.34  rillig 	}
     75   1.1     cgd 
     76  1.60  rillig 	switch (psym) {
     77   1.1     cgd 
     78  1.60  rillig 	case psym_decl:
     79  1.60  rillig 		if (ps.s_sym[ps.tos] == psym_decl)
     80  1.60  rillig 			break;	/* only put one declaration onto stack */
     81  1.60  rillig 
     82  1.63  rillig 		ps.break_after_comma = true;
     83  1.60  rillig 		ps.s_sym[++ps.tos] = psym_decl;
     84  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level_follow;
     85  1.60  rillig 
     86  1.60  rillig 		if (opt.ljust_decl)
     87  1.60  rillig 			ps.ind_level_follow = ps.ind_level = decl_level();
     88  1.60  rillig 		break;
     89  1.60  rillig 
     90  1.60  rillig 	case psym_if_expr:
     91  1.61  rillig 		if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else
     92  1.61  rillig 		    && opt.else_if) {
     93  1.60  rillig 			/* Reduce "else if" to "if". This saves a lot of stack
     94  1.60  rillig 			 * space in case of a long "if-else-if ... else-if"
     95  1.60  rillig 			 * sequence. */
     96  1.60  rillig 			ps.ind_level_follow = ps.s_ind_level[ps.tos--];
     97  1.60  rillig 		}
     98  1.60  rillig 		/* FALLTHROUGH */
     99  1.60  rillig 	case psym_do:
    100  1.60  rillig 	case psym_for_exprs:
    101  1.60  rillig 		ps.s_sym[++ps.tos] = psym;
    102  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
    103  1.60  rillig 		++ps.ind_level_follow;
    104  1.60  rillig 		break;
    105  1.60  rillig 
    106  1.60  rillig 	case psym_lbrace:
    107  1.63  rillig 		ps.break_after_comma = false;
    108  1.61  rillig 		if (ps.s_sym[ps.tos] == psym_stmt
    109  1.61  rillig 		    || ps.s_sym[ps.tos] == psym_decl
    110  1.60  rillig 		    || ps.s_sym[ps.tos] == psym_stmt_list)
    111  1.60  rillig 			++ps.ind_level_follow;	/* it is a random, isolated
    112  1.60  rillig 						 * stmt group or a declaration
    113  1.60  rillig 						 */
    114  1.60  rillig 		else {
    115  1.60  rillig 			if (code.len == 0) {
    116  1.60  rillig 				/* it is a group as part of a while, for, etc.
    117  1.60  rillig 				 */
    118  1.60  rillig 				--ps.ind_level;
    119  1.60  rillig 
    120  1.60  rillig 				/* for a switch, brace should be two levels out
    121  1.60  rillig 				 * from the code */
    122  1.60  rillig 				if (ps.s_sym[ps.tos] == psym_switch_expr &&
    123  1.60  rillig 				    opt.case_indent >= 1.0F)
    124  1.60  rillig 					--ps.ind_level;
    125  1.60  rillig 			}
    126  1.60  rillig 		}
    127  1.60  rillig 
    128  1.60  rillig 		ps.s_sym[++ps.tos] = psym_lbrace;
    129  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level;
    130  1.60  rillig 		ps.s_sym[++ps.tos] = psym_stmt;
    131  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    132  1.60  rillig 		break;
    133  1.60  rillig 
    134  1.60  rillig 	case psym_while_expr:
    135  1.60  rillig 		if (ps.s_sym[ps.tos] == psym_do_stmt) {
    136  1.60  rillig 			/* it is matched with do stmt */
    137  1.61  rillig 			ps.ind_level =
    138  1.61  rillig 			    ps.ind_level_follow = ps.s_ind_level[ps.tos];
    139  1.60  rillig 			ps.s_sym[++ps.tos] = psym_while_expr;
    140  1.61  rillig 			ps.s_ind_level[ps.tos] = ps.ind_level;
    141  1.60  rillig 
    142  1.60  rillig 		} else {	/* it is a while loop */
    143  1.60  rillig 			ps.s_sym[++ps.tos] = psym_while_expr;
    144  1.60  rillig 			ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    145  1.60  rillig 			++ps.ind_level_follow;
    146  1.60  rillig 		}
    147  1.60  rillig 
    148  1.60  rillig 		break;
    149  1.60  rillig 
    150  1.60  rillig 	case psym_else:
    151  1.60  rillig 		if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
    152  1.60  rillig 			diag(1, "Unmatched 'else'");
    153  1.60  rillig 		else {
    154  1.60  rillig 			ps.ind_level = ps.s_ind_level[ps.tos];
    155  1.60  rillig 			ps.ind_level_follow = ps.ind_level + 1;
    156  1.60  rillig 			ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
    157  1.60  rillig 		}
    158  1.60  rillig 		break;
    159  1.60  rillig 
    160  1.60  rillig 	case psym_rbrace:
    161  1.60  rillig 		/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    162  1.60  rillig 		if (ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace) {
    163  1.61  rillig 			ps.ind_level = ps.ind_level_follow
    164  1.61  rillig 			    = ps.s_ind_level[--ps.tos];
    165  1.60  rillig 			ps.s_sym[ps.tos] = psym_stmt;
    166  1.60  rillig 		} else
    167  1.60  rillig 			diag(1, "Statement nesting error");
    168  1.60  rillig 		break;
    169  1.60  rillig 
    170  1.60  rillig 	case psym_switch_expr:
    171  1.60  rillig 		ps.s_sym[++ps.tos] = psym_switch_expr;
    172  1.60  rillig 		ps.s_case_ind_level[ps.tos] = case_ind;
    173  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    174  1.60  rillig 		case_ind = (float)ps.ind_level_follow + opt.case_indent;
    175  1.60  rillig 		ps.ind_level_follow += (int)opt.case_indent + 1;
    176  1.60  rillig 		break;
    177  1.60  rillig 
    178  1.60  rillig 	case psym_0:		/* a simple statement */
    179  1.63  rillig 		ps.break_after_comma = false;
    180  1.60  rillig 		ps.s_sym[++ps.tos] = psym_stmt;
    181  1.60  rillig 		ps.s_ind_level[ps.tos] = ps.ind_level;
    182  1.60  rillig 		break;
    183  1.60  rillig 
    184  1.60  rillig 	default:
    185  1.60  rillig 		diag(1, "Unknown code to parser");
    186  1.60  rillig 		return;
    187  1.17  rillig 	}
    188   1.5   lukem 
    189  1.60  rillig 	if (ps.tos >= STACKSIZE - 1)
    190  1.60  rillig 		errx(1, "Parser stack overflow");
    191   1.1     cgd 
    192  1.60  rillig 	debug_parse_stack("before reduction");
    193  1.60  rillig 	reduce();		/* see if any reduction can be done */
    194  1.60  rillig 	debug_parse_stack("after reduction");
    195   1.1     cgd }
    196   1.9   kamil 
    197  1.16  rillig /*
    198  1.16  rillig  * Try to combine the statement on the top of the parse stack with the symbol
    199  1.16  rillig  * directly below it, replacing these two symbols with a single symbol.
    200  1.16  rillig  */
    201  1.25  rillig static bool
    202  1.16  rillig reduce_stmt(void)
    203  1.16  rillig {
    204  1.60  rillig 	switch (ps.s_sym[ps.tos - 1]) {
    205  1.60  rillig 
    206  1.60  rillig 	case psym_stmt:
    207  1.60  rillig 	case psym_stmt_list:
    208  1.60  rillig 		ps.s_sym[--ps.tos] = psym_stmt_list;
    209  1.60  rillig 		return true;
    210  1.60  rillig 
    211  1.60  rillig 	case psym_do:
    212  1.60  rillig 		ps.s_sym[--ps.tos] = psym_do_stmt;
    213  1.60  rillig 		ps.ind_level_follow = ps.s_ind_level[ps.tos];
    214  1.60  rillig 		return true;
    215  1.60  rillig 
    216  1.60  rillig 	case psym_if_expr:
    217  1.60  rillig 		ps.s_sym[--ps.tos] = psym_if_expr_stmt;
    218  1.60  rillig 		int i = ps.tos - 1;
    219  1.60  rillig 		while (ps.s_sym[i] != psym_stmt &&
    220  1.60  rillig 		    ps.s_sym[i] != psym_stmt_list &&
    221  1.60  rillig 		    ps.s_sym[i] != psym_lbrace)
    222  1.60  rillig 			--i;
    223  1.60  rillig 		ps.ind_level_follow = ps.s_ind_level[i];
    224  1.60  rillig 		/* For the time being, assume that there is no 'else' on this
    225  1.60  rillig 		 * 'if', and set the indentation level accordingly. If an
    226  1.60  rillig 		 * 'else' is scanned, it will be fixed up later. */
    227  1.60  rillig 		return true;
    228  1.60  rillig 
    229  1.60  rillig 	case psym_switch_expr:
    230  1.60  rillig 		case_ind = ps.s_case_ind_level[ps.tos - 1];
    231  1.60  rillig 		/* FALLTHROUGH */
    232  1.60  rillig 	case psym_decl:		/* finish of a declaration */
    233  1.60  rillig 	case psym_if_expr_stmt_else:
    234  1.60  rillig 	case psym_for_exprs:
    235  1.60  rillig 	case psym_while_expr:
    236  1.60  rillig 		ps.s_sym[--ps.tos] = psym_stmt;
    237  1.60  rillig 		ps.ind_level_follow = ps.s_ind_level[ps.tos];
    238  1.60  rillig 		return true;
    239  1.16  rillig 
    240  1.60  rillig 	default:
    241  1.60  rillig 		return false;
    242  1.60  rillig 	}
    243  1.16  rillig }
    244  1.16  rillig 
    245   1.1     cgd /*
    246  1.59  rillig  * Repeatedly try to reduce the top two symbols on the parse stack to a single
    247  1.59  rillig  * symbol, until no more reductions are possible.
    248   1.1     cgd  */
    249   1.9   kamil static void
    250   1.6     wiz reduce(void)
    251   1.1     cgd {
    252  1.16  rillig again:
    253  1.60  rillig 	if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt())
    254  1.60  rillig 		goto again;
    255  1.60  rillig 	if (ps.s_sym[ps.tos] == psym_while_expr &&
    256  1.44  rillig 	    ps.s_sym[ps.tos - 1] == psym_do_stmt) {
    257  1.60  rillig 		ps.tos -= 2;
    258  1.60  rillig 		goto again;
    259  1.60  rillig 	}
    260   1.1     cgd }
    261