Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.76
      1  1.76  rillig /*	$NetBSD: parse.c,v 1.76 2023/06/14 19:05:40 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.76  rillig __RCSID("$NetBSD: parse.c,v 1.76 2023/06/14 19:05:40 rillig Exp $");
     42   1.1     cgd 
     43   1.9   kamil #include <err.h>
     44  1.76  rillig #include <stdlib.h>
     45  1.12  rillig 
     46   1.9   kamil #include "indent.h"
     47   1.9   kamil 
     48  1.68  rillig /*
     49  1.68  rillig  * Try to combine the statement on the top of the parse stack with the symbol
     50  1.68  rillig  * directly below it, replacing these two symbols with a single symbol.
     51  1.68  rillig  */
     52  1.68  rillig static bool
     53  1.69  rillig psyms_reduce_stmt(struct psym_stack *psyms)
     54  1.68  rillig {
     55  1.74  rillig 	switch (psyms->sym[psyms->len - 2]) {
     56  1.68  rillig 
     57  1.68  rillig 	case psym_stmt:
     58  1.74  rillig 		psyms->sym[psyms->len-- - 2] = psym_stmt;
     59  1.68  rillig 		return true;
     60  1.68  rillig 
     61  1.68  rillig 	case psym_do:
     62  1.74  rillig 		psyms->sym[psyms->len-- - 2] = psym_do_stmt;
     63  1.74  rillig 		ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
     64  1.68  rillig 		return true;
     65  1.68  rillig 
     66  1.68  rillig 	case psym_if_expr:
     67  1.74  rillig 		psyms->sym[psyms->len-- - 2] = psym_if_expr_stmt;
     68  1.74  rillig 		size_t i = psyms->len - 2;
     69  1.69  rillig 		while (psyms->sym[i] != psym_stmt &&
     70  1.69  rillig 		    psyms->sym[i] != psym_lbrace_block)
     71  1.68  rillig 			--i;
     72  1.69  rillig 		ps.ind_level_follow = psyms->ind_level[i];
     73  1.68  rillig 		/* For the time being, assume that there is no 'else' on this
     74  1.68  rillig 		 * 'if', and set the indentation level accordingly. If an
     75  1.68  rillig 		 * 'else' is scanned, it will be fixed up later. */
     76  1.68  rillig 		return true;
     77  1.68  rillig 
     78  1.68  rillig 	case psym_switch_expr:
     79  1.68  rillig 	case psym_decl:
     80  1.68  rillig 	case psym_if_expr_stmt_else:
     81  1.68  rillig 	case psym_for_exprs:
     82  1.68  rillig 	case psym_while_expr:
     83  1.74  rillig 		psyms->sym[psyms->len-- - 2] = psym_stmt;
     84  1.74  rillig 		ps.ind_level_follow = psyms->ind_level[psyms->len - 1];
     85  1.68  rillig 		return true;
     86  1.68  rillig 
     87  1.68  rillig 	default:
     88  1.68  rillig 		return false;
     89  1.68  rillig 	}
     90  1.68  rillig }
     91   1.1     cgd 
     92  1.45  rillig static int
     93  1.45  rillig decl_level(void)
     94  1.45  rillig {
     95  1.60  rillig 	int level = 0;
     96  1.74  rillig 	for (size_t i = ps.psyms.len - 2; i > 0; i--)
     97  1.69  rillig 		if (ps.psyms.sym[i] == psym_decl)
     98  1.60  rillig 			level++;
     99  1.60  rillig 	return level;
    100  1.45  rillig }
    101  1.45  rillig 
    102  1.76  rillig void
    103  1.72  rillig ps_push(parser_symbol psym, bool follow)
    104  1.64  rillig {
    105  1.76  rillig 	if (ps.psyms.len == ps.psyms.cap) {
    106  1.76  rillig 		ps.psyms.cap += 16;
    107  1.76  rillig 		ps.psyms.sym = nonnull(realloc(ps.psyms.sym,
    108  1.76  rillig 		    sizeof(ps.psyms.sym[0]) * ps.psyms.cap));
    109  1.76  rillig 		ps.psyms.ind_level = nonnull(realloc(ps.psyms.ind_level,
    110  1.76  rillig 		    sizeof(ps.psyms.ind_level[0]) * ps.psyms.cap));
    111  1.76  rillig 	}
    112  1.76  rillig 	ps.psyms.len++;
    113  1.76  rillig 	ps.psyms.sym[ps.psyms.len - 1] = psym;
    114  1.74  rillig 	ps.psyms.ind_level[ps.psyms.len - 1] =
    115  1.72  rillig 	    follow ? ps.ind_level_follow : ps.ind_level;
    116  1.64  rillig }
    117  1.64  rillig 
    118  1.68  rillig /*
    119  1.68  rillig  * Repeatedly try to reduce the top two symbols on the parse stack to a single
    120  1.68  rillig  * symbol, until no more reductions are possible.
    121  1.68  rillig  */
    122  1.68  rillig static void
    123  1.69  rillig psyms_reduce(struct psym_stack *psyms)
    124  1.68  rillig {
    125  1.68  rillig again:
    126  1.75  rillig 	if (psyms->len >= 2 && psyms->sym[psyms->len - 1] == psym_stmt
    127  1.74  rillig 	    && psyms_reduce_stmt(psyms))
    128  1.68  rillig 		goto again;
    129  1.74  rillig 	if (psyms->sym[psyms->len - 1] == psym_while_expr &&
    130  1.74  rillig 	    psyms->sym[psyms->len - 2] == psym_do_stmt) {
    131  1.74  rillig 		psyms->len -= 2;
    132  1.68  rillig 		goto again;
    133  1.68  rillig 	}
    134  1.68  rillig }
    135  1.68  rillig 
    136  1.65  rillig static bool
    137  1.65  rillig is_lbrace(parser_symbol psym)
    138  1.65  rillig {
    139  1.65  rillig 	return psym == psym_lbrace_block
    140  1.65  rillig 	    || psym == psym_lbrace_struct
    141  1.65  rillig 	    || psym == psym_lbrace_union
    142  1.65  rillig 	    || psym == psym_lbrace_enum;
    143  1.65  rillig }
    144  1.65  rillig 
    145  1.31  rillig /*
    146  1.71  rillig  * Shift the token onto the parser stack, then try to reduce it by combining it with
    147  1.31  rillig  * previous tokens.
    148  1.31  rillig  */
    149   1.5   lukem void
    150  1.39  rillig parse(parser_symbol psym)
    151   1.1     cgd {
    152  1.62  rillig 	debug_blank_line();
    153  1.60  rillig 	debug_println("parse token: %s", psym_name[psym]);
    154   1.1     cgd 
    155  1.69  rillig 	struct psym_stack *psyms = &ps.psyms;
    156  1.60  rillig 	if (psym != psym_else) {
    157  1.74  rillig 		while (psyms->sym[psyms->len - 1] == psym_if_expr_stmt) {
    158  1.74  rillig 			psyms->sym[psyms->len - 1] = psym_stmt;
    159  1.69  rillig 			psyms_reduce(&ps.psyms);
    160  1.60  rillig 		}
    161  1.34  rillig 	}
    162   1.1     cgd 
    163  1.60  rillig 	switch (psym) {
    164   1.1     cgd 
    165  1.71  rillig 	case psym_lbrace_block:
    166  1.71  rillig 	case psym_lbrace_struct:
    167  1.71  rillig 	case psym_lbrace_union:
    168  1.71  rillig 	case psym_lbrace_enum:
    169  1.71  rillig 		ps.break_after_comma = false;
    170  1.74  rillig 		if (psyms->sym[psyms->len - 1] == psym_decl
    171  1.74  rillig 		    || psyms->sym[psyms->len - 1] == psym_stmt)
    172  1.71  rillig 			++ps.ind_level_follow;
    173  1.71  rillig 		else if (code.len == 0) {
    174  1.71  rillig 			/* It is part of a while, for, etc. */
    175  1.71  rillig 			--ps.ind_level;
    176  1.71  rillig 
    177  1.71  rillig 			/* for a switch, brace should be two levels out from
    178  1.71  rillig 			 * the code */
    179  1.74  rillig 			if (psyms->sym[psyms->len - 1] == psym_switch_expr
    180  1.71  rillig 			    && opt.case_indent >= 1.0F)
    181  1.71  rillig 				--ps.ind_level;
    182  1.71  rillig 		}
    183  1.71  rillig 
    184  1.72  rillig 		ps_push(psym, false);
    185  1.72  rillig 		ps_push(psym_stmt, true);
    186  1.71  rillig 		break;
    187  1.71  rillig 
    188  1.71  rillig 	case psym_rbrace:
    189  1.71  rillig 		/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    190  1.74  rillig 		if (!(psyms->len >= 2
    191  1.74  rillig 			&& is_lbrace(psyms->sym[psyms->len - 2]))) {
    192  1.71  rillig 			diag(1, "Statement nesting error");
    193  1.71  rillig 			break;
    194  1.71  rillig 		}
    195  1.71  rillig 		ps.ind_level = ps.ind_level_follow =
    196  1.74  rillig 		    psyms->ind_level[psyms->len-- - 2];
    197  1.74  rillig 		psyms->sym[psyms->len - 1] = psym_stmt;
    198  1.71  rillig 		break;
    199  1.71  rillig 
    200  1.60  rillig 	case psym_decl:
    201  1.74  rillig 		if (psyms->sym[psyms->len - 1] == psym_decl)
    202  1.60  rillig 			break;	/* only put one declaration onto stack */
    203  1.60  rillig 
    204  1.63  rillig 		ps.break_after_comma = true;
    205  1.72  rillig 		ps_push(psym_decl, true);
    206  1.60  rillig 
    207  1.66  rillig 		if (opt.left_justify_decl)
    208  1.60  rillig 			ps.ind_level_follow = ps.ind_level = decl_level();
    209  1.60  rillig 		break;
    210  1.60  rillig 
    211  1.71  rillig 	case psym_stmt:
    212  1.71  rillig 		ps.break_after_comma = false;
    213  1.72  rillig 		ps_push(psym_stmt, false);
    214  1.71  rillig 		break;
    215  1.71  rillig 
    216  1.60  rillig 	case psym_if_expr:
    217  1.74  rillig 		if (psyms->sym[psyms->len - 1] == psym_if_expr_stmt_else
    218  1.66  rillig 		    && opt.else_if_in_same_line)
    219  1.74  rillig 			ps.ind_level_follow =
    220  1.74  rillig 			    psyms->ind_level[psyms->len-- - 1];
    221  1.60  rillig 		/* FALLTHROUGH */
    222  1.60  rillig 	case psym_do:
    223  1.60  rillig 	case psym_for_exprs:
    224  1.64  rillig 		ps.ind_level = ps.ind_level_follow++;
    225  1.72  rillig 		ps_push(psym, false);
    226  1.60  rillig 		break;
    227  1.60  rillig 
    228  1.60  rillig 	case psym_else:
    229  1.74  rillig 		if (psyms->sym[psyms->len - 1] != psym_if_expr_stmt) {
    230  1.60  rillig 			diag(1, "Unmatched 'else'");
    231  1.64  rillig 			break;
    232  1.60  rillig 		}
    233  1.74  rillig 		ps.ind_level = psyms->ind_level[psyms->len - 1];
    234  1.64  rillig 		ps.ind_level_follow = ps.ind_level + 1;
    235  1.74  rillig 		psyms->sym[psyms->len - 1] = psym_if_expr_stmt_else;
    236  1.60  rillig 		break;
    237  1.60  rillig 
    238  1.60  rillig 	case psym_switch_expr:
    239  1.72  rillig 		ps_push(psym_switch_expr, true);
    240  1.60  rillig 		ps.ind_level_follow += (int)opt.case_indent + 1;
    241  1.60  rillig 		break;
    242  1.60  rillig 
    243  1.71  rillig 	case psym_while_expr:
    244  1.74  rillig 		if (psyms->sym[psyms->len - 1] == psym_do_stmt) {
    245  1.71  rillig 			ps.ind_level = ps.ind_level_follow =
    246  1.74  rillig 			    psyms->ind_level[psyms->len - 1];
    247  1.72  rillig 			ps_push(psym_while_expr, false);
    248  1.71  rillig 		} else {
    249  1.72  rillig 			ps_push(psym_while_expr, true);
    250  1.71  rillig 			++ps.ind_level_follow;
    251  1.71  rillig 		}
    252  1.60  rillig 		break;
    253  1.60  rillig 
    254  1.60  rillig 	default:
    255  1.60  rillig 		diag(1, "Unknown code to parser");
    256  1.60  rillig 		return;
    257  1.17  rillig 	}
    258   1.5   lukem 
    259  1.71  rillig 	debug_psyms_stack("before reduction");
    260  1.69  rillig 	psyms_reduce(&ps.psyms);
    261  1.71  rillig 	debug_psyms_stack("after reduction");
    262   1.1     cgd }
    263