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