Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.8
      1  1.8    mrg /*	$NetBSD: parse.c,v 1.8 2019/02/03 03:19:29 mrg Exp $	*/
      2  1.3    tls 
      3  1.1    cgd /*
      4  1.4    mrg  * Copyright (c) 1980, 1993
      5  1.4    mrg  *	The Regents of the University of California.  All rights reserved.
      6  1.7    agc  *
      7  1.7    agc  * Redistribution and use in source and binary forms, with or without
      8  1.7    agc  * modification, are permitted provided that the following conditions
      9  1.7    agc  * are met:
     10  1.7    agc  * 1. Redistributions of source code must retain the above copyright
     11  1.7    agc  *    notice, this list of conditions and the following disclaimer.
     12  1.7    agc  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.7    agc  *    notice, this list of conditions and the following disclaimer in the
     14  1.7    agc  *    documentation and/or other materials provided with the distribution.
     15  1.7    agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.7    agc  *    may be used to endorse or promote products derived from this software
     17  1.7    agc  *    without specific prior written permission.
     18  1.7    agc  *
     19  1.7    agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.7    agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.7    agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.7    agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.7    agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.7    agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.7    agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.7    agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.7    agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.7    agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.7    agc  * SUCH DAMAGE.
     30  1.7    agc  */
     31  1.7    agc 
     32  1.7    agc /*
     33  1.4    mrg  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
     34  1.1    cgd  * Copyright (c) 1985 Sun Microsystems, Inc.
     35  1.1    cgd  * All rights reserved.
     36  1.1    cgd  *
     37  1.1    cgd  * Redistribution and use in source and binary forms, with or without
     38  1.1    cgd  * modification, are permitted provided that the following conditions
     39  1.1    cgd  * are met:
     40  1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     41  1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     42  1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43  1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     44  1.1    cgd  *    documentation and/or other materials provided with the distribution.
     45  1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     46  1.1    cgd  *    must display the following acknowledgement:
     47  1.1    cgd  *	This product includes software developed by the University of
     48  1.1    cgd  *	California, Berkeley and its contributors.
     49  1.1    cgd  * 4. Neither the name of the University nor the names of its contributors
     50  1.1    cgd  *    may be used to endorse or promote products derived from this software
     51  1.1    cgd  *    without specific prior written permission.
     52  1.1    cgd  *
     53  1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  1.1    cgd  * SUCH DAMAGE.
     64  1.1    cgd  */
     65  1.1    cgd 
     66  1.5  lukem #include <sys/cdefs.h>
     67  1.1    cgd #ifndef lint
     68  1.4    mrg #if 0
     69  1.4    mrg static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
     70  1.4    mrg #else
     71  1.8    mrg __RCSID("$NetBSD: parse.c,v 1.8 2019/02/03 03:19:29 mrg Exp $");
     72  1.4    mrg #endif
     73  1.5  lukem #endif				/* not lint */
     74  1.1    cgd 
     75  1.1    cgd #include <stdio.h>
     76  1.1    cgd #include "indent_globs.h"
     77  1.1    cgd #include "indent_codes.h"
     78  1.1    cgd 
     79  1.6    wiz /* tk: the code for the construct scanned */
     80  1.5  lukem void
     81  1.6    wiz parse(int tk)
     82  1.1    cgd {
     83  1.5  lukem 	int     i;
     84  1.1    cgd 
     85  1.1    cgd #ifdef debug
     86  1.5  lukem 	printf("%2d - %s\n", tk, token);
     87  1.1    cgd #endif
     88  1.1    cgd 
     89  1.5  lukem 	while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
     90  1.5  lukem 		/* true if we have an if without an else */
     91  1.5  lukem 		ps.p_stack[ps.tos] = stmt;	/* apply the if(..) stmt ::=
     92  1.5  lukem 						 * stmt reduction */
     93  1.5  lukem 		reduce();	/* see if this allows any reduction */
     94  1.5  lukem 	}
     95  1.1    cgd 
     96  1.1    cgd 
     97  1.5  lukem 	switch (tk) {		/* go on and figure out what to do with the
     98  1.1    cgd 				 * input */
     99  1.1    cgd 
    100  1.5  lukem 	case decl:		/* scanned a declaration word */
    101  1.5  lukem 		ps.search_brace = btype_2;
    102  1.5  lukem 		/* indicate that following brace should be on same line */
    103  1.5  lukem 		if (ps.p_stack[ps.tos] != decl) {	/* only put one
    104  1.5  lukem 							 * declaration onto
    105  1.5  lukem 							 * stack */
    106  1.5  lukem 			break_comma = true;	/* while in declaration,
    107  1.5  lukem 						 * newline should be forced
    108  1.5  lukem 						 * after comma */
    109  1.5  lukem 			ps.p_stack[++ps.tos] = decl;
    110  1.5  lukem 			ps.il[ps.tos] = ps.i_l_follow;
    111  1.5  lukem 
    112  1.5  lukem 			if (ps.ljust_decl) {	/* only do if we want left
    113  1.5  lukem 						 * justified declarations */
    114  1.5  lukem 				ps.ind_level = 0;
    115  1.5  lukem 				for (i = ps.tos - 1; i > 0; --i)
    116  1.5  lukem 					if (ps.p_stack[i] == decl)
    117  1.5  lukem 						++ps.ind_level;	/* indentation is number
    118  1.5  lukem 								 * of declaration levels
    119  1.5  lukem 								 * deep we are */
    120  1.5  lukem 				ps.i_l_follow = ps.ind_level;
    121  1.5  lukem 			}
    122  1.5  lukem 		}
    123  1.5  lukem 		break;
    124  1.5  lukem 
    125  1.5  lukem 	case ifstmt:		/* scanned if (...) */
    126  1.5  lukem 		if (ps.p_stack[ps.tos] == elsehead && ps.else_if)	/* "else if ..." */
    127  1.5  lukem 			ps.i_l_follow = ps.il[ps.tos];
    128  1.8    mrg 		/* FALLTHROUGH */
    129  1.5  lukem 	case dolit:		/* 'do' */
    130  1.5  lukem 	case forstmt:		/* for (...) */
    131  1.5  lukem 		ps.p_stack[++ps.tos] = tk;
    132  1.5  lukem 		ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
    133  1.5  lukem 		++ps.i_l_follow;/* subsequent statements should be indented 1 */
    134  1.5  lukem 		ps.search_brace = btype_2;
    135  1.5  lukem 		break;
    136  1.5  lukem 
    137  1.5  lukem 	case lbrace:		/* scanned { */
    138  1.5  lukem 		break_comma = false;	/* don't break comma in an initial
    139  1.5  lukem 					 * list */
    140  1.5  lukem 		if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
    141  1.5  lukem 		    || ps.p_stack[ps.tos] == stmtl)
    142  1.5  lukem 			++ps.i_l_follow;	/* it is a random, isolated
    143  1.5  lukem 						 * stmt group or a declaration */
    144  1.5  lukem 		else {
    145  1.5  lukem 			if (s_code == e_code) {
    146  1.5  lukem 				/*
    147  1.5  lukem 				 * only do this if there is nothing on the line
    148  1.5  lukem 				 */
    149  1.5  lukem 				--ps.ind_level;
    150  1.5  lukem 				/*
    151  1.5  lukem 				 * it is a group as part of a while, for, etc.
    152  1.5  lukem 				 */
    153  1.5  lukem 				if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
    154  1.5  lukem 					--ps.ind_level;
    155  1.5  lukem 				/*
    156  1.5  lukem 				 * for a switch, brace should be two levels out from the code
    157  1.5  lukem 				 */
    158  1.5  lukem 			}
    159  1.5  lukem 		}
    160  1.5  lukem 
    161  1.5  lukem 		ps.p_stack[++ps.tos] = lbrace;
    162  1.5  lukem 		ps.il[ps.tos] = ps.ind_level;
    163  1.5  lukem 		ps.p_stack[++ps.tos] = stmt;
    164  1.5  lukem 		/* allow null stmt between braces */
    165  1.5  lukem 		ps.il[ps.tos] = ps.i_l_follow;
    166  1.5  lukem 		break;
    167  1.5  lukem 
    168  1.5  lukem 	case whilestmt:	/* scanned while (...) */
    169  1.5  lukem 		if (ps.p_stack[ps.tos] == dohead) {
    170  1.5  lukem 			/* it is matched with do stmt */
    171  1.5  lukem 			ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
    172  1.5  lukem 			ps.p_stack[++ps.tos] = whilestmt;
    173  1.5  lukem 			ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
    174  1.5  lukem 		} else {	/* it is a while loop */
    175  1.5  lukem 			ps.p_stack[++ps.tos] = whilestmt;
    176  1.5  lukem 			ps.il[ps.tos] = ps.i_l_follow;
    177  1.5  lukem 			++ps.i_l_follow;
    178  1.5  lukem 			ps.search_brace = btype_2;
    179  1.5  lukem 		}
    180  1.5  lukem 
    181  1.5  lukem 		break;
    182  1.1    cgd 
    183  1.5  lukem 	case elselit:		/* scanned an else */
    184  1.1    cgd 
    185  1.5  lukem 		if (ps.p_stack[ps.tos] != ifhead)
    186  1.5  lukem 			diag(1, "Unmatched 'else'");
    187  1.5  lukem 		else {
    188  1.5  lukem 			ps.ind_level = ps.il[ps.tos];	/* indentation for else
    189  1.5  lukem 							 * should be same as for
    190  1.5  lukem 							 * if */
    191  1.5  lukem 			ps.i_l_follow = ps.ind_level + 1;	/* everything following
    192  1.5  lukem 								 * should be in 1 level */
    193  1.5  lukem 			ps.p_stack[ps.tos] = elsehead;
    194  1.5  lukem 			/* remember if with else */
    195  1.5  lukem 			ps.search_brace = btype_2 | ps.else_if;
    196  1.5  lukem 		}
    197  1.5  lukem 		break;
    198  1.1    cgd 
    199  1.5  lukem 	case rbrace:		/* scanned a } */
    200  1.5  lukem 		/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
    201  1.5  lukem 		if (ps.p_stack[ps.tos - 1] == lbrace) {
    202  1.5  lukem 			ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
    203  1.5  lukem 			ps.p_stack[ps.tos] = stmt;
    204  1.5  lukem 		} else
    205  1.5  lukem 			diag(1, "Stmt nesting error.");
    206  1.5  lukem 		break;
    207  1.1    cgd 
    208  1.5  lukem 	case swstmt:		/* had switch (...) */
    209  1.5  lukem 		ps.p_stack[++ps.tos] = swstmt;
    210  1.5  lukem 		ps.cstk[ps.tos] = case_ind;
    211  1.5  lukem 		/* save current case indent level */
    212  1.5  lukem 		ps.il[ps.tos] = ps.i_l_follow;
    213  1.5  lukem 		case_ind = ps.i_l_follow + ps.case_indent;	/* cases should be one
    214  1.5  lukem 								 * level down from
    215  1.5  lukem 								 * switch */
    216  1.5  lukem 		ps.i_l_follow += ps.case_indent + 1;	/* statements should be
    217  1.5  lukem 							 * two levels in */
    218  1.5  lukem 		ps.search_brace = btype_2;
    219  1.5  lukem 		break;
    220  1.1    cgd 
    221  1.5  lukem 	case semicolon:	/* this indicates a simple stmt */
    222  1.5  lukem 		break_comma = false;	/* turn off flag to break after commas
    223  1.5  lukem 					 * in a declaration */
    224  1.5  lukem 		ps.p_stack[++ps.tos] = stmt;
    225  1.5  lukem 		ps.il[ps.tos] = ps.ind_level;
    226  1.5  lukem 		break;
    227  1.1    cgd 
    228  1.5  lukem 	default:		/* this is an error */
    229  1.5  lukem 		diag(1, "Unknown code to parser");
    230  1.5  lukem 		return;
    231  1.1    cgd 
    232  1.1    cgd 
    233  1.5  lukem 	}			/* end of switch */
    234  1.1    cgd 
    235  1.5  lukem 	reduce();		/* see if any reduction can be done */
    236  1.1    cgd 
    237  1.1    cgd #ifdef debug
    238  1.5  lukem 	for (i = 1; i <= ps.tos; ++i)
    239  1.5  lukem 		printf("(%d %d)", ps.p_stack[i], ps.il[i]);
    240  1.5  lukem 	printf("\n");
    241  1.1    cgd #endif
    242  1.1    cgd }
    243  1.1    cgd /*
    244  1.1    cgd  * NAME: reduce
    245  1.5  lukem  *
    246  1.1    cgd  * FUNCTION: Implements the reduce part of the parsing algorithm
    247  1.5  lukem  *
    248  1.1    cgd  * ALGORITHM: The following reductions are done.  Reductions are repeated
    249  1.1    cgd  *	until no more are possible.
    250  1.5  lukem  *
    251  1.1    cgd  * Old TOS		New TOS
    252  1.1    cgd  * <stmt> <stmt>	<stmtl>
    253  1.1    cgd  * <stmtl> <stmt>	<stmtl>
    254  1.1    cgd  * do <stmt>		"dostmt"
    255  1.1    cgd  * if <stmt>		"ifstmt"
    256  1.1    cgd  * switch <stmt>	<stmt>
    257  1.1    cgd  * decl <stmt>		<stmt>
    258  1.1    cgd  * "ifelse" <stmt>	<stmt>
    259  1.1    cgd  * for <stmt>		<stmt>
    260  1.1    cgd  * while <stmt>		<stmt>
    261  1.1    cgd  * "dostmt" while	<stmt>
    262  1.5  lukem  *
    263  1.1    cgd  * On each reduction, ps.i_l_follow (the indentation for the following line)
    264  1.1    cgd  * is set to the indentation level associated with the old TOS.
    265  1.5  lukem  *
    266  1.1    cgd  * PARAMETERS: None
    267  1.5  lukem  *
    268  1.1    cgd  * RETURNS: Nothing
    269  1.5  lukem  *
    270  1.1    cgd  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
    271  1.5  lukem  *
    272  1.1    cgd  * CALLS: None
    273  1.5  lukem  *
    274  1.1    cgd  * CALLED BY: parse
    275  1.5  lukem  *
    276  1.1    cgd  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
    277  1.5  lukem  *
    278  1.1    cgd  */
    279  1.1    cgd /*----------------------------------------------*\
    280  1.1    cgd |   REDUCTION PHASE				    |
    281  1.1    cgd \*----------------------------------------------*/
    282  1.5  lukem void
    283  1.6    wiz reduce(void)
    284  1.1    cgd {
    285  1.1    cgd 
    286  1.5  lukem 	int     i;
    287  1.1    cgd 
    288  1.5  lukem 	for (;;) {		/* keep looping until there is nothing left to
    289  1.1    cgd 				 * reduce */
    290  1.1    cgd 
    291  1.5  lukem 		switch (ps.p_stack[ps.tos]) {
    292  1.1    cgd 
    293  1.5  lukem 		case stmt:
    294  1.5  lukem 			switch (ps.p_stack[ps.tos - 1]) {
    295  1.1    cgd 
    296  1.5  lukem 			case stmt:
    297  1.5  lukem 			case stmtl:
    298  1.5  lukem 				/* stmtl stmt or stmt stmt */
    299  1.5  lukem 				ps.p_stack[--ps.tos] = stmtl;
    300  1.5  lukem 				break;
    301  1.5  lukem 
    302  1.5  lukem 			case dolit:	/* <do> <stmt> */
    303  1.5  lukem 				ps.p_stack[--ps.tos] = dohead;
    304  1.5  lukem 				ps.i_l_follow = ps.il[ps.tos];
    305  1.5  lukem 				break;
    306  1.5  lukem 
    307  1.5  lukem 			case ifstmt:
    308  1.5  lukem 				/* <if> <stmt> */
    309  1.5  lukem 				ps.p_stack[--ps.tos] = ifhead;
    310  1.5  lukem 				for (i = ps.tos - 1;
    311  1.5  lukem 				    (
    312  1.5  lukem 					ps.p_stack[i] != stmt
    313  1.5  lukem 					&&
    314  1.5  lukem 					ps.p_stack[i] != stmtl
    315  1.5  lukem 					&&
    316  1.5  lukem 					ps.p_stack[i] != lbrace
    317  1.5  lukem 				    );
    318  1.5  lukem 				    --i);
    319  1.5  lukem 				ps.i_l_follow = ps.il[i];
    320  1.5  lukem 				/*
    321  1.5  lukem 				 * for the time being, we will assume that there is no else on
    322  1.5  lukem 				 * this if, and set the indentation level accordingly. If an
    323  1.5  lukem 				 * else is scanned, it will be fixed up later
    324  1.5  lukem 				 */
    325  1.5  lukem 				break;
    326  1.5  lukem 
    327  1.5  lukem 			case swstmt:
    328  1.5  lukem 				/* <switch> <stmt> */
    329  1.5  lukem 				case_ind = ps.cstk[ps.tos - 1];
    330  1.5  lukem 
    331  1.8    mrg 				/* FALLTHROUGH */
    332  1.5  lukem 			case decl:	/* finish of a declaration */
    333  1.5  lukem 			case elsehead:
    334  1.5  lukem 				/* <<if> <stmt> else> <stmt> */
    335  1.5  lukem 			case forstmt:
    336  1.5  lukem 				/* <for> <stmt> */
    337  1.5  lukem 			case whilestmt:
    338  1.5  lukem 				/* <while> <stmt> */
    339  1.5  lukem 				ps.p_stack[--ps.tos] = stmt;
    340  1.5  lukem 				ps.i_l_follow = ps.il[ps.tos];
    341  1.5  lukem 				break;
    342  1.5  lukem 
    343  1.5  lukem 			default:	/* <anything else> <stmt> */
    344  1.5  lukem 				return;
    345  1.5  lukem 
    346  1.5  lukem 			}	/* end of section for <stmt> on top of stack */
    347  1.5  lukem 			break;
    348  1.5  lukem 
    349  1.5  lukem 		case whilestmt:/* while (...) on top */
    350  1.5  lukem 			if (ps.p_stack[ps.tos - 1] == dohead) {
    351  1.5  lukem 				/* it is termination of a do while */
    352  1.5  lukem 				ps.p_stack[--ps.tos] = stmt;
    353  1.5  lukem 				break;
    354  1.5  lukem 			} else
    355  1.5  lukem 				return;
    356  1.1    cgd 
    357  1.5  lukem 		default:	/* anything else on top */
    358  1.5  lukem 			return;
    359  1.1    cgd 
    360  1.5  lukem 		}
    361  1.1    cgd 	}
    362  1.1    cgd }
    363