xref: /freebsd/usr.bin/indent/parse.c (revision e026a48c34d396fe7d0e382673a69bc047c0bfca)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1985 Sun Microsystems, Inc.
39b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  * All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
159b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
169b50d902SRodney W. Grimes  *    must display the following acknowledgement:
179b50d902SRodney W. Grimes  *	This product includes software developed by the University of
189b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
199b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
209b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
219b50d902SRodney W. Grimes  *    without specific prior written permission.
229b50d902SRodney W. Grimes  *
239b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
249b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
259b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
269b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
279b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
289b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
299b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
309b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
319b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
329b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
339b50d902SRodney W. Grimes  * SUCH DAMAGE.
349b50d902SRodney W. Grimes  */
357916863dSJens Schweikhardt #if 0
369b50d902SRodney W. Grimes #ifndef lint
379b50d902SRodney W. Grimes static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
389b50d902SRodney W. Grimes #endif /* not lint */
397916863dSJens Schweikhardt #endif
40e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
41e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <stdio.h>
449b50d902SRodney W. Grimes #include "indent_globs.h"
459b50d902SRodney W. Grimes #include "indent_codes.h"
467916863dSJens Schweikhardt #include "indent.h"
479b50d902SRodney W. Grimes 
487916863dSJens Schweikhardt static void reduce(void);
497916863dSJens Schweikhardt 
507916863dSJens Schweikhardt void
517916863dSJens Schweikhardt parse(int tk) /* tk: the code for the construct scanned */
529b50d902SRodney W. Grimes {
539b50d902SRodney W. Grimes     int         i;
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #ifdef debug
569b50d902SRodney W. Grimes     printf("%2d - %s\n", tk, token);
579b50d902SRodney W. Grimes #endif
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes     while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
609b50d902SRodney W. Grimes 	/* true if we have an if without an else */
619b50d902SRodney W. Grimes 	ps.p_stack[ps.tos] = stmt;	/* apply the if(..) stmt ::= stmt
629b50d902SRodney W. Grimes 					 * reduction */
639b50d902SRodney W. Grimes 	reduce();		/* see if this allows any reduction */
649b50d902SRodney W. Grimes     }
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes     switch (tk) {		/* go on and figure out what to do with the
689b50d902SRodney W. Grimes 				 * input */
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes     case decl:			/* scanned a declaration word */
719b50d902SRodney W. Grimes 	ps.search_brace = btype_2;
729b50d902SRodney W. Grimes 	/* indicate that following brace should be on same line */
739b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
749b50d902SRodney W. Grimes 						 * onto stack */
759b50d902SRodney W. Grimes 	    break_comma = true;	/* while in declaration, newline should be
769b50d902SRodney W. Grimes 				 * forced after comma */
779b50d902SRodney W. Grimes 	    ps.p_stack[++ps.tos] = decl;
789b50d902SRodney W. Grimes 	    ps.il[ps.tos] = ps.i_l_follow;
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes 	    if (ps.ljust_decl) {/* only do if we want left justified
819b50d902SRodney W. Grimes 				 * declarations */
829b50d902SRodney W. Grimes 		ps.ind_level = 0;
839b50d902SRodney W. Grimes 		for (i = ps.tos - 1; i > 0; --i)
849b50d902SRodney W. Grimes 		    if (ps.p_stack[i] == decl)
859b50d902SRodney W. Grimes 			++ps.ind_level;	/* indentation is number of
869b50d902SRodney W. Grimes 					 * declaration levels deep we are */
879b50d902SRodney W. Grimes 		ps.i_l_follow = ps.ind_level;
889b50d902SRodney W. Grimes 	    }
899b50d902SRodney W. Grimes 	}
909b50d902SRodney W. Grimes 	break;
919b50d902SRodney W. Grimes 
929b50d902SRodney W. Grimes     case ifstmt:		/* scanned if (...) */
939b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos] == elsehead && ps.else_if)	/* "else if ..." */
949b50d902SRodney W. Grimes 	    ps.i_l_follow = ps.il[ps.tos];
959b50d902SRodney W. Grimes     case dolit:		/* 'do' */
969b50d902SRodney W. Grimes     case forstmt:		/* for (...) */
979b50d902SRodney W. Grimes 	ps.p_stack[++ps.tos] = tk;
989b50d902SRodney W. Grimes 	ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
999b50d902SRodney W. Grimes 	++ps.i_l_follow;	/* subsequent statements should be indented 1 */
1009b50d902SRodney W. Grimes 	ps.search_brace = btype_2;
1019b50d902SRodney W. Grimes 	break;
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes     case lbrace:		/* scanned { */
1049b50d902SRodney W. Grimes 	break_comma = false;	/* don't break comma in an initial list */
1059b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
1069b50d902SRodney W. Grimes 		|| ps.p_stack[ps.tos] == stmtl)
1079b50d902SRodney W. Grimes 	    ++ps.i_l_follow;	/* it is a random, isolated stmt group or a
1089b50d902SRodney W. Grimes 				 * declaration */
1099b50d902SRodney W. Grimes 	else {
1109b50d902SRodney W. Grimes 	    if (s_code == e_code) {
1119b50d902SRodney W. Grimes 		/*
1129b50d902SRodney W. Grimes 		 * only do this if there is nothing on the line
1139b50d902SRodney W. Grimes 		 */
1149b50d902SRodney W. Grimes 		--ps.ind_level;
1159b50d902SRodney W. Grimes 		/*
1169b50d902SRodney W. Grimes 		 * it is a group as part of a while, for, etc.
1179b50d902SRodney W. Grimes 		 */
1189b50d902SRodney W. Grimes 		if (ps.p_stack[ps.tos] == swstmt && ps.case_indent >= 1)
1199b50d902SRodney W. Grimes 		    --ps.ind_level;
1209b50d902SRodney W. Grimes 		/*
1219b50d902SRodney W. Grimes 		 * for a switch, brace should be two levels out from the code
1229b50d902SRodney W. Grimes 		 */
1239b50d902SRodney W. Grimes 	    }
1249b50d902SRodney W. Grimes 	}
1259b50d902SRodney W. Grimes 
1269b50d902SRodney W. Grimes 	ps.p_stack[++ps.tos] = lbrace;
1279b50d902SRodney W. Grimes 	ps.il[ps.tos] = ps.ind_level;
1289b50d902SRodney W. Grimes 	ps.p_stack[++ps.tos] = stmt;
1299b50d902SRodney W. Grimes 	/* allow null stmt between braces */
1309b50d902SRodney W. Grimes 	ps.il[ps.tos] = ps.i_l_follow;
1319b50d902SRodney W. Grimes 	break;
1329b50d902SRodney W. Grimes 
1339b50d902SRodney W. Grimes     case whilestmt:		/* scanned while (...) */
1349b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos] == dohead) {
1359b50d902SRodney W. Grimes 	    /* it is matched with do stmt */
1369b50d902SRodney W. Grimes 	    ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
1379b50d902SRodney W. Grimes 	    ps.p_stack[++ps.tos] = whilestmt;
1389b50d902SRodney W. Grimes 	    ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
1399b50d902SRodney W. Grimes 	}
1409b50d902SRodney W. Grimes 	else {			/* it is a while loop */
1419b50d902SRodney W. Grimes 	    ps.p_stack[++ps.tos] = whilestmt;
1429b50d902SRodney W. Grimes 	    ps.il[ps.tos] = ps.i_l_follow;
1439b50d902SRodney W. Grimes 	    ++ps.i_l_follow;
1449b50d902SRodney W. Grimes 	    ps.search_brace = btype_2;
1459b50d902SRodney W. Grimes 	}
1469b50d902SRodney W. Grimes 
1479b50d902SRodney W. Grimes 	break;
1489b50d902SRodney W. Grimes 
1499b50d902SRodney W. Grimes     case elselit:		/* scanned an else */
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos] != ifhead)
1527916863dSJens Schweikhardt 	    diag2(1, "Unmatched 'else'");
1539b50d902SRodney W. Grimes 	else {
1549b50d902SRodney W. Grimes 	    ps.ind_level = ps.il[ps.tos];	/* indentation for else should
1559b50d902SRodney W. Grimes 						 * be same as for if */
1569b50d902SRodney W. Grimes 	    ps.i_l_follow = ps.ind_level + 1;	/* everything following should
1579b50d902SRodney W. Grimes 						 * be in 1 level */
1589b50d902SRodney W. Grimes 	    ps.p_stack[ps.tos] = elsehead;
1599b50d902SRodney W. Grimes 	    /* remember if with else */
1609b50d902SRodney W. Grimes 	    ps.search_brace = btype_2 | ps.else_if;
1619b50d902SRodney W. Grimes 	}
1629b50d902SRodney W. Grimes 	break;
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes     case rbrace:		/* scanned a } */
1659b50d902SRodney W. Grimes 	/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
1669b50d902SRodney W. Grimes 	if (ps.p_stack[ps.tos - 1] == lbrace) {
1679b50d902SRodney W. Grimes 	    ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
1689b50d902SRodney W. Grimes 	    ps.p_stack[ps.tos] = stmt;
1699b50d902SRodney W. Grimes 	}
1709b50d902SRodney W. Grimes 	else
1717916863dSJens Schweikhardt 	    diag2(1, "Stmt nesting error.");
1729b50d902SRodney W. Grimes 	break;
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes     case swstmt:		/* had switch (...) */
1759b50d902SRodney W. Grimes 	ps.p_stack[++ps.tos] = swstmt;
1769b50d902SRodney W. Grimes 	ps.cstk[ps.tos] = case_ind;
1779b50d902SRodney W. Grimes 	/* save current case indent level */
1789b50d902SRodney W. Grimes 	ps.il[ps.tos] = ps.i_l_follow;
1799b50d902SRodney W. Grimes 	case_ind = ps.i_l_follow + ps.case_indent;	/* cases should be one
1809b50d902SRodney W. Grimes 							 * level down from
1819b50d902SRodney W. Grimes 							 * switch */
1829b50d902SRodney W. Grimes 	ps.i_l_follow += ps.case_indent + 1;	/* statements should be two
1839b50d902SRodney W. Grimes 						 * levels in */
1849b50d902SRodney W. Grimes 	ps.search_brace = btype_2;
1859b50d902SRodney W. Grimes 	break;
1869b50d902SRodney W. Grimes 
1879b50d902SRodney W. Grimes     case semicolon:		/* this indicates a simple stmt */
1889b50d902SRodney W. Grimes 	break_comma = false;	/* turn off flag to break after commas in a
1899b50d902SRodney W. Grimes 				 * declaration */
1909b50d902SRodney W. Grimes 	ps.p_stack[++ps.tos] = stmt;
1919b50d902SRodney W. Grimes 	ps.il[ps.tos] = ps.ind_level;
1929b50d902SRodney W. Grimes 	break;
1939b50d902SRodney W. Grimes 
1949b50d902SRodney W. Grimes     default:			/* this is an error */
1957916863dSJens Schweikhardt 	diag2(1, "Unknown code to parser");
1969b50d902SRodney W. Grimes 	return;
1979b50d902SRodney W. Grimes 
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes     }				/* end of switch */
2009b50d902SRodney W. Grimes 
2019b50d902SRodney W. Grimes     reduce();			/* see if any reduction can be done */
2029b50d902SRodney W. Grimes 
2039b50d902SRodney W. Grimes #ifdef debug
2049b50d902SRodney W. Grimes     for (i = 1; i <= ps.tos; ++i)
2059b50d902SRodney W. Grimes 	printf("(%d %d)", ps.p_stack[i], ps.il[i]);
2069b50d902SRodney W. Grimes     printf("\n");
2079b50d902SRodney W. Grimes #endif
2089b50d902SRodney W. Grimes 
2099b50d902SRodney W. Grimes     return;
2109b50d902SRodney W. Grimes }
2119b50d902SRodney W. Grimes 
2129b50d902SRodney W. Grimes /*
2139b50d902SRodney W. Grimes  * NAME: reduce
2149b50d902SRodney W. Grimes  *
2159b50d902SRodney W. Grimes  * FUNCTION: Implements the reduce part of the parsing algorithm
2169b50d902SRodney W. Grimes  *
2179b50d902SRodney W. Grimes  * ALGORITHM: The following reductions are done.  Reductions are repeated
2189b50d902SRodney W. Grimes  *	until no more are possible.
2199b50d902SRodney W. Grimes  *
2209b50d902SRodney W. Grimes  * Old TOS		New TOS
2219b50d902SRodney W. Grimes  * <stmt> <stmt>	<stmtl>
2229b50d902SRodney W. Grimes  * <stmtl> <stmt>	<stmtl>
2239b50d902SRodney W. Grimes  * do <stmt>		"dostmt"
2249b50d902SRodney W. Grimes  * if <stmt>		"ifstmt"
2259b50d902SRodney W. Grimes  * switch <stmt>	<stmt>
2269b50d902SRodney W. Grimes  * decl <stmt>		<stmt>
2279b50d902SRodney W. Grimes  * "ifelse" <stmt>	<stmt>
2289b50d902SRodney W. Grimes  * for <stmt>		<stmt>
2299b50d902SRodney W. Grimes  * while <stmt>		<stmt>
2309b50d902SRodney W. Grimes  * "dostmt" while	<stmt>
2319b50d902SRodney W. Grimes  *
2329b50d902SRodney W. Grimes  * On each reduction, ps.i_l_follow (the indentation for the following line)
2339b50d902SRodney W. Grimes  * is set to the indentation level associated with the old TOS.
2349b50d902SRodney W. Grimes  *
2359b50d902SRodney W. Grimes  * PARAMETERS: None
2369b50d902SRodney W. Grimes  *
2379b50d902SRodney W. Grimes  * RETURNS: Nothing
2389b50d902SRodney W. Grimes  *
2399b50d902SRodney W. Grimes  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
2409b50d902SRodney W. Grimes  *
2419b50d902SRodney W. Grimes  * CALLS: None
2429b50d902SRodney W. Grimes  *
2439b50d902SRodney W. Grimes  * CALLED BY: parse
2449b50d902SRodney W. Grimes  *
2459b50d902SRodney W. Grimes  * HISTORY: initial coding 	November 1976	D A Willcox of CAC
2469b50d902SRodney W. Grimes  *
2479b50d902SRodney W. Grimes  */
2489b50d902SRodney W. Grimes /*----------------------------------------------*\
2499b50d902SRodney W. Grimes |   REDUCTION PHASE				    |
2509b50d902SRodney W. Grimes \*----------------------------------------------*/
2517916863dSJens Schweikhardt static void
2527916863dSJens Schweikhardt reduce(void)
2539b50d902SRodney W. Grimes {
25490af6a72SJuli Mallett     int i;
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes     for (;;) {			/* keep looping until there is nothing left to
2579b50d902SRodney W. Grimes 				 * reduce */
2589b50d902SRodney W. Grimes 
2599b50d902SRodney W. Grimes 	switch (ps.p_stack[ps.tos]) {
2609b50d902SRodney W. Grimes 
2619b50d902SRodney W. Grimes 	case stmt:
2629b50d902SRodney W. Grimes 	    switch (ps.p_stack[ps.tos - 1]) {
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 	    case stmt:
2659b50d902SRodney W. Grimes 	    case stmtl:
2669b50d902SRodney W. Grimes 		/* stmtl stmt or stmt stmt */
2679b50d902SRodney W. Grimes 		ps.p_stack[--ps.tos] = stmtl;
2689b50d902SRodney W. Grimes 		break;
2699b50d902SRodney W. Grimes 
2709b50d902SRodney W. Grimes 	    case dolit:	/* <do> <stmt> */
2719b50d902SRodney W. Grimes 		ps.p_stack[--ps.tos] = dohead;
2729b50d902SRodney W. Grimes 		ps.i_l_follow = ps.il[ps.tos];
2739b50d902SRodney W. Grimes 		break;
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes 	    case ifstmt:
2769b50d902SRodney W. Grimes 		/* <if> <stmt> */
2779b50d902SRodney W. Grimes 		ps.p_stack[--ps.tos] = ifhead;
2789b50d902SRodney W. Grimes 		for (i = ps.tos - 1;
2799b50d902SRodney W. Grimes 			(
2809b50d902SRodney W. Grimes 			 ps.p_stack[i] != stmt
2819b50d902SRodney W. Grimes 			 &&
2829b50d902SRodney W. Grimes 			 ps.p_stack[i] != stmtl
2839b50d902SRodney W. Grimes 			 &&
2849b50d902SRodney W. Grimes 			 ps.p_stack[i] != lbrace
2859b50d902SRodney W. Grimes 			 );
2869b50d902SRodney W. Grimes 			--i);
2879b50d902SRodney W. Grimes 		ps.i_l_follow = ps.il[i];
2889b50d902SRodney W. Grimes 		/*
2899b50d902SRodney W. Grimes 		 * for the time being, we will assume that there is no else on
2909b50d902SRodney W. Grimes 		 * this if, and set the indentation level accordingly. If an
2919b50d902SRodney W. Grimes 		 * else is scanned, it will be fixed up later
2929b50d902SRodney W. Grimes 		 */
2939b50d902SRodney W. Grimes 		break;
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes 	    case swstmt:
2969b50d902SRodney W. Grimes 		/* <switch> <stmt> */
2979b50d902SRodney W. Grimes 		case_ind = ps.cstk[ps.tos - 1];
2989b50d902SRodney W. Grimes 
2999b50d902SRodney W. Grimes 	    case decl:		/* finish of a declaration */
3009b50d902SRodney W. Grimes 	    case elsehead:
3019b50d902SRodney W. Grimes 		/* <<if> <stmt> else> <stmt> */
3029b50d902SRodney W. Grimes 	    case forstmt:
3039b50d902SRodney W. Grimes 		/* <for> <stmt> */
3049b50d902SRodney W. Grimes 	    case whilestmt:
3059b50d902SRodney W. Grimes 		/* <while> <stmt> */
3069b50d902SRodney W. Grimes 		ps.p_stack[--ps.tos] = stmt;
3079b50d902SRodney W. Grimes 		ps.i_l_follow = ps.il[ps.tos];
3089b50d902SRodney W. Grimes 		break;
3099b50d902SRodney W. Grimes 
3109b50d902SRodney W. Grimes 	    default:		/* <anything else> <stmt> */
3119b50d902SRodney W. Grimes 		return;
3129b50d902SRodney W. Grimes 
3139b50d902SRodney W. Grimes 	    }			/* end of section for <stmt> on top of stack */
3149b50d902SRodney W. Grimes 	    break;
3159b50d902SRodney W. Grimes 
3169b50d902SRodney W. Grimes 	case whilestmt:	/* while (...) on top */
3179b50d902SRodney W. Grimes 	    if (ps.p_stack[ps.tos - 1] == dohead) {
3189b50d902SRodney W. Grimes 		/* it is termination of a do while */
31934ba7379SRobert Nordier 		ps.tos -= 2;
3209b50d902SRodney W. Grimes 		break;
3219b50d902SRodney W. Grimes 	    }
3229b50d902SRodney W. Grimes 	    else
3239b50d902SRodney W. Grimes 		return;
3249b50d902SRodney W. Grimes 
3259b50d902SRodney W. Grimes 	default:		/* anything else on top */
3269b50d902SRodney W. Grimes 	    return;
3279b50d902SRodney W. Grimes 
3289b50d902SRodney W. Grimes 	}
3299b50d902SRodney W. Grimes     }
3309b50d902SRodney W. Grimes }
331