Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.45
      1  1.45    rillig /*	$NetBSD: parse.c,v 1.45 2021/10/29 22:52:50 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.9     kamil #if 0
     41   1.9     kamil static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
     42   1.9     kamil #endif
     43   1.9     kamil 
     44   1.5     lukem #include <sys/cdefs.h>
     45   1.9     kamil #if defined(__NetBSD__)
     46   1.9     kamil __RCSID("$FreeBSD$");
     47   1.4       mrg #else
     48   1.9     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
     49   1.9     kamil #endif
     50   1.1       cgd 
     51  1.39    rillig #include <assert.h>
     52   1.9     kamil #include <err.h>
     53   1.1       cgd #include <stdio.h>
     54  1.12    rillig 
     55   1.9     kamil #include "indent.h"
     56   1.9     kamil 
     57   1.9     kamil static void reduce(void);
     58   1.1       cgd 
     59  1.39    rillig #ifdef debug
     60  1.41    rillig static const char *
     61  1.39    rillig psym_name(parser_symbol psym)
     62  1.39    rillig {
     63  1.39    rillig     static const char *const name[] = {
     64  1.39    rillig 	"semicolon",
     65  1.39    rillig 	"lbrace",
     66  1.39    rillig 	"rbrace",
     67  1.39    rillig 	"decl",
     68  1.39    rillig 	"stmt",
     69  1.39    rillig 	"stmt_list",
     70  1.39    rillig 	"for_exprs",
     71  1.39    rillig 	"if_expr",
     72  1.39    rillig 	"if_expr_stmt",
     73  1.39    rillig 	"if_expr_stmt_else",
     74  1.39    rillig 	"else",
     75  1.39    rillig 	"switch_expr",
     76  1.39    rillig 	"do",
     77  1.39    rillig 	"do_stmt",
     78  1.39    rillig 	"while_expr",
     79  1.39    rillig     };
     80  1.39    rillig 
     81  1.39    rillig     assert(array_length(name) == (int)psym_while_expr + 1);
     82  1.39    rillig 
     83  1.39    rillig     return name[psym];
     84  1.39    rillig }
     85  1.39    rillig #endif
     86  1.39    rillig 
     87  1.45    rillig static int
     88  1.45    rillig decl_level(void)
     89  1.45    rillig {
     90  1.45    rillig     int level = 0;
     91  1.45    rillig     for (int i = ps.tos - 1; i > 0; i--)
     92  1.45    rillig 	if (ps.s_sym[i] == psym_decl)
     93  1.45    rillig 	    level++;
     94  1.45    rillig     return level;
     95  1.45    rillig }
     96  1.45    rillig 
     97  1.31    rillig /*
     98  1.31    rillig  * Shift the token onto the parser stack, or reduce it by combining it with
     99  1.31    rillig  * previous tokens.
    100  1.31    rillig  */
    101   1.5     lukem void
    102  1.39    rillig parse(parser_symbol psym)
    103   1.1       cgd {
    104  1.41    rillig     debug_println("parse token: '%s'", psym_name(psym));
    105   1.1       cgd 
    106  1.39    rillig     if (psym != psym_else) {
    107  1.39    rillig 	while (ps.s_sym[ps.tos] == psym_if_expr_stmt) {
    108  1.39    rillig 	    ps.s_sym[ps.tos] = psym_stmt;
    109  1.34    rillig 	    reduce();
    110  1.34    rillig 	}
    111   1.9     kamil     }
    112   1.1       cgd 
    113  1.39    rillig     switch (psym) {
    114   1.1       cgd 
    115  1.39    rillig     case psym_decl:
    116  1.40    rillig 	ps.search_stmt = opt.brace_same_line;
    117   1.9     kamil 	/* indicate that following brace should be on same line */
    118  1.32    rillig 
    119  1.45    rillig 	if (ps.s_sym[ps.tos] == psym_decl)
    120  1.45    rillig 	    break;		/* only put one declaration onto stack */
    121  1.45    rillig 
    122  1.45    rillig 	break_comma = true;	/* while in a declaration, force a newline
    123  1.45    rillig 				 * after comma */
    124  1.45    rillig 	ps.s_sym[++ps.tos] = psym_decl;
    125  1.45    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    126   1.9     kamil 
    127  1.45    rillig 	if (opt.ljust_decl)
    128  1.45    rillig 	    ps.ind_level_follow = ps.ind_level = decl_level();
    129   1.9     kamil 	break;
    130   1.5     lukem 
    131  1.39    rillig     case psym_if_expr:		/* 'if' '(' <expr> ')' */
    132  1.39    rillig 	if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else && opt.else_if) {
    133  1.17    rillig 	    /*
    134  1.30    rillig 	     * Reduce "else if" to "if". This saves a lot of stack space in
    135  1.27    rillig 	     * case of a long "if-else-if ... else-if" sequence.
    136  1.17    rillig 	     */
    137  1.36    rillig 	    ps.ind_level_follow = ps.s_ind_level[ps.tos--];
    138  1.17    rillig 	}
    139   1.9     kamil 	/* FALLTHROUGH */
    140  1.39    rillig     case psym_do:
    141  1.42    rillig     case psym_for_exprs:	/* 'for' (...) */
    142  1.39    rillig 	ps.s_sym[++ps.tos] = psym;
    143  1.36    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
    144  1.26    rillig 	++ps.ind_level_follow;	/* subsequent statements should be indented 1 */
    145  1.40    rillig 	ps.search_stmt = opt.brace_same_line;
    146   1.9     kamil 	break;
    147   1.9     kamil 
    148  1.39    rillig     case psym_lbrace:
    149  1.35    rillig 	break_comma = false;	/* don't break comma in an initializer list */
    150  1.39    rillig 	if (ps.s_sym[ps.tos] == psym_stmt || ps.s_sym[ps.tos] == psym_decl
    151  1.39    rillig 		|| ps.s_sym[ps.tos] == psym_stmt_list)
    152  1.37    rillig 	    ++ps.ind_level_follow;	/* it is a random, isolated stmt group
    153  1.37    rillig 					 * or a declaration */
    154   1.9     kamil 	else {
    155  1.20    rillig 	    if (code.s == code.e) {
    156  1.31    rillig 		/* it is a group as part of a while, for, etc. */
    157   1.9     kamil 		--ps.ind_level;
    158  1.32    rillig 
    159   1.9     kamil 		/*
    160  1.31    rillig 		 * for a switch, brace should be two levels out from the code
    161   1.9     kamil 		 */
    162  1.39    rillig 		if (ps.s_sym[ps.tos] == psym_switch_expr &&
    163  1.39    rillig 			opt.case_indent >= 1)
    164   1.9     kamil 		    --ps.ind_level;
    165   1.9     kamil 	    }
    166   1.9     kamil 	}
    167   1.5     lukem 
    168  1.39    rillig 	ps.s_sym[++ps.tos] = psym_lbrace;
    169  1.36    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level;
    170  1.39    rillig 	ps.s_sym[++ps.tos] = psym_stmt;
    171   1.9     kamil 	/* allow null stmt between braces */
    172  1.36    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    173   1.9     kamil 	break;
    174   1.9     kamil 
    175  1.39    rillig     case psym_while_expr:	/* 'while' '(' <expr> ')' */
    176  1.39    rillig 	if (ps.s_sym[ps.tos] == psym_do_stmt) {
    177   1.9     kamil 	    /* it is matched with do stmt */
    178  1.36    rillig 	    ps.ind_level = ps.ind_level_follow = ps.s_ind_level[ps.tos];
    179  1.39    rillig 	    ps.s_sym[++ps.tos] = psym_while_expr;
    180  1.36    rillig 	    ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
    181  1.32    rillig 
    182  1.18    rillig 	} else {		/* it is a while loop */
    183  1.39    rillig 	    ps.s_sym[++ps.tos] = psym_while_expr;
    184  1.36    rillig 	    ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    185  1.26    rillig 	    ++ps.ind_level_follow;
    186  1.40    rillig 	    ps.search_stmt = opt.brace_same_line;
    187   1.9     kamil 	}
    188   1.5     lukem 
    189   1.9     kamil 	break;
    190   1.5     lukem 
    191  1.39    rillig     case psym_else:
    192  1.39    rillig 	if (ps.s_sym[ps.tos] != psym_if_expr_stmt)
    193  1.10  christos 	    diag(1, "Unmatched 'else'");
    194   1.9     kamil 	else {
    195  1.36    rillig 	    /* The indentation for 'else' should be the same as for 'if'. */
    196  1.36    rillig 	    ps.ind_level = ps.s_ind_level[ps.tos];
    197  1.30    rillig 	    ps.ind_level_follow = ps.ind_level + 1;
    198  1.39    rillig 	    ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
    199   1.9     kamil 	    /* remember if with else */
    200  1.40    rillig 	    ps.search_stmt = opt.brace_same_line || opt.else_if;
    201   1.9     kamil 	}
    202   1.9     kamil 	break;
    203   1.1       cgd 
    204  1.39    rillig     case psym_rbrace:
    205  1.17    rillig 	/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
    206  1.39    rillig 	if (ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace) {
    207  1.36    rillig 	    ps.ind_level = ps.ind_level_follow = ps.s_ind_level[--ps.tos];
    208  1.39    rillig 	    ps.s_sym[ps.tos] = psym_stmt;
    209  1.18    rillig 	} else
    210  1.10  christos 	    diag(1, "Statement nesting error");
    211   1.9     kamil 	break;
    212   1.9     kamil 
    213  1.39    rillig     case psym_switch_expr:	/* had switch (...) */
    214  1.39    rillig 	ps.s_sym[++ps.tos] = psym_switch_expr;
    215  1.36    rillig 	ps.s_case_ind_level[ps.tos] = case_ind;
    216   1.9     kamil 	/* save current case indent level */
    217  1.36    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level_follow;
    218  1.29    rillig 	/* cases should be one level deeper than the switch */
    219  1.29    rillig 	case_ind = (float)ps.ind_level_follow + opt.case_indent;
    220  1.29    rillig 	/* statements should be two levels deeper */
    221  1.29    rillig 	ps.ind_level_follow += (int)opt.case_indent + 1;
    222  1.40    rillig 	ps.search_stmt = opt.brace_same_line;
    223   1.9     kamil 	break;
    224   1.9     kamil 
    225  1.39    rillig     case psym_semicolon:	/* this indicates a simple stmt */
    226   1.9     kamil 	break_comma = false;	/* turn off flag to break after commas in a
    227   1.9     kamil 				 * declaration */
    228  1.39    rillig 	ps.s_sym[++ps.tos] = psym_stmt;
    229  1.36    rillig 	ps.s_ind_level[ps.tos] = ps.ind_level;
    230   1.9     kamil 	break;
    231   1.9     kamil 
    232  1.30    rillig     default:
    233  1.10  christos 	diag(1, "Unknown code to parser");
    234   1.9     kamil 	return;
    235  1.30    rillig     }
    236   1.1       cgd 
    237   1.9     kamil     if (ps.tos >= STACKSIZE - 1)
    238   1.9     kamil 	errx(1, "Parser stack overflow");
    239   1.1       cgd 
    240   1.9     kamil     reduce();			/* see if any reduction can be done */
    241   1.1       cgd 
    242   1.1       cgd #ifdef debug
    243  1.14    rillig     printf("parse stack:");
    244  1.28    rillig     for (int i = 1; i <= ps.tos; ++i)
    245  1.39    rillig 	printf(" ('%s' at %d)", psym_name(ps.s_sym[i]), ps.s_ind_level[i]);
    246  1.14    rillig     if (ps.tos == 0)
    247  1.27    rillig 	printf(" empty");
    248   1.9     kamil     printf("\n");
    249   1.1       cgd #endif
    250   1.1       cgd }
    251   1.9     kamil 
    252  1.39    rillig void
    253  1.44    rillig parse_stmt_head(stmt_head hd)
    254  1.39    rillig {
    255  1.39    rillig     static const parser_symbol psym[] = {
    256  1.39    rillig 	[hd_for] = psym_for_exprs,
    257  1.39    rillig 	[hd_if] = psym_if_expr,
    258  1.39    rillig 	[hd_switch] = psym_switch_expr,
    259  1.39    rillig 	[hd_while] = psym_while_expr
    260  1.39    rillig     };
    261  1.39    rillig     parse(psym[hd]);
    262  1.39    rillig }
    263  1.39    rillig 
    264  1.16    rillig /*
    265  1.16    rillig  * Try to combine the statement on the top of the parse stack with the symbol
    266  1.16    rillig  * directly below it, replacing these two symbols with a single symbol.
    267  1.16    rillig  */
    268  1.25    rillig static bool
    269  1.16    rillig reduce_stmt(void)
    270  1.16    rillig {
    271  1.39    rillig     switch (ps.s_sym[ps.tos - 1]) {
    272  1.16    rillig 
    273  1.43    rillig     case psym_stmt:
    274  1.43    rillig     case psym_stmt_list:
    275  1.39    rillig 	ps.s_sym[--ps.tos] = psym_stmt_list;
    276  1.16    rillig 	return true;
    277  1.16    rillig 
    278  1.43    rillig     case psym_do:
    279  1.39    rillig 	ps.s_sym[--ps.tos] = psym_do_stmt;
    280  1.36    rillig 	ps.ind_level_follow = ps.s_ind_level[ps.tos];
    281  1.16    rillig 	return true;
    282  1.16    rillig 
    283  1.43    rillig     case psym_if_expr:
    284  1.39    rillig 	ps.s_sym[--ps.tos] = psym_if_expr_stmt;
    285  1.16    rillig 	int i = ps.tos - 1;
    286  1.39    rillig 	while (ps.s_sym[i] != psym_stmt &&
    287  1.42    rillig 		ps.s_sym[i] != psym_stmt_list &&
    288  1.42    rillig 		ps.s_sym[i] != psym_lbrace)
    289  1.16    rillig 	    --i;
    290  1.36    rillig 	ps.ind_level_follow = ps.s_ind_level[i];
    291  1.16    rillig 	/*
    292  1.43    rillig 	 * For the time being, assume that there is no 'else' on this 'if',
    293  1.44    rillig 	 * and set the indentation level accordingly. If an 'else' is scanned,
    294  1.44    rillig 	 * it will be fixed up later.
    295  1.16    rillig 	 */
    296  1.16    rillig 	return true;
    297  1.16    rillig 
    298  1.43    rillig     case psym_switch_expr:
    299  1.36    rillig 	case_ind = ps.s_case_ind_level[ps.tos - 1];
    300  1.16    rillig 	/* FALLTHROUGH */
    301  1.39    rillig     case psym_decl:		/* finish of a declaration */
    302  1.43    rillig     case psym_if_expr_stmt_else:
    303  1.43    rillig     case psym_for_exprs:
    304  1.43    rillig     case psym_while_expr:
    305  1.39    rillig 	ps.s_sym[--ps.tos] = psym_stmt;
    306  1.36    rillig 	ps.ind_level_follow = ps.s_ind_level[ps.tos];
    307  1.16    rillig 	return true;
    308  1.16    rillig 
    309  1.43    rillig     default:
    310  1.16    rillig 	return false;
    311  1.16    rillig     }
    312  1.16    rillig }
    313  1.16    rillig 
    314   1.1       cgd /*
    315  1.16    rillig  * Repeatedly try to reduce the top two symbols on the parse stack to a
    316  1.16    rillig  * single symbol, until no more reductions are possible.
    317   1.1       cgd  */
    318   1.9     kamil static void
    319   1.6       wiz reduce(void)
    320   1.1       cgd {
    321  1.16    rillig again:
    322  1.44    rillig     if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt())
    323  1.44    rillig 	goto again;
    324  1.44    rillig     if (ps.s_sym[ps.tos] == psym_while_expr &&
    325  1.44    rillig 	    ps.s_sym[ps.tos - 1] == psym_do_stmt) {
    326  1.44    rillig 	ps.tos -= 2;		/* XXX: why not reduce to stmt? */
    327  1.44    rillig 	goto again;
    328   1.9     kamil     }
    329   1.1       cgd }
    330