Home | History | Annotate | Line # | Download | only in indent
parse.c revision 1.12
      1  1.12    rillig /*	$NetBSD: parse.c,v 1.12 2021/03/07 10:42:48 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 #ifndef lint
     42   1.9     kamil static char sccsid[] = "@(#)parse.c	8.1 (Berkeley) 6/6/93";
     43   1.9     kamil #endif /* not lint */
     44   1.9     kamil #endif
     45   1.9     kamil 
     46   1.5     lukem #include <sys/cdefs.h>
     47   1.1       cgd #ifndef lint
     48   1.9     kamil #if defined(__NetBSD__)
     49   1.9     kamil __RCSID("$FreeBSD$");
     50   1.4       mrg #else
     51   1.9     kamil __FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
     52   1.9     kamil #endif
     53   1.4       mrg #endif
     54   1.1       cgd 
     55   1.9     kamil #include <err.h>
     56   1.1       cgd #include <stdio.h>
     57  1.12    rillig 
     58   1.9     kamil #include "indent.h"
     59   1.9     kamil 
     60   1.9     kamil static void reduce(void);
     61   1.1       cgd 
     62   1.5     lukem void
     63   1.9     kamil parse(int tk) /* tk: the code for the construct scanned */
     64   1.1       cgd {
     65   1.9     kamil     int         i;
     66   1.1       cgd 
     67   1.1       cgd #ifdef debug
     68   1.9     kamil     printf("%2d - %s\n", tk, token);
     69   1.1       cgd #endif
     70   1.1       cgd 
     71   1.9     kamil     while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
     72   1.9     kamil 	/* true if we have an if without an else */
     73   1.9     kamil 	ps.p_stack[ps.tos] = stmt;	/* apply the if(..) stmt ::= stmt
     74   1.9     kamil 					 * reduction */
     75   1.9     kamil 	reduce();		/* see if this allows any reduction */
     76   1.9     kamil     }
     77   1.1       cgd 
     78   1.1       cgd 
     79   1.9     kamil     switch (tk) {		/* go on and figure out what to do with the
     80   1.1       cgd 				 * input */
     81   1.1       cgd 
     82   1.9     kamil     case decl:			/* scanned a declaration word */
     83   1.9     kamil 	ps.search_brace = opt.btype_2;
     84   1.9     kamil 	/* indicate that following brace should be on same line */
     85   1.9     kamil 	if (ps.p_stack[ps.tos] != decl) {	/* only put one declaration
     86   1.9     kamil 						 * onto stack */
     87   1.9     kamil 	    break_comma = true;	/* while in declaration, newline should be
     88   1.9     kamil 				 * forced after comma */
     89   1.9     kamil 	    ps.p_stack[++ps.tos] = decl;
     90   1.9     kamil 	    ps.il[ps.tos] = ps.i_l_follow;
     91   1.9     kamil 
     92   1.9     kamil 	    if (opt.ljust_decl) {/* only do if we want left justified
     93   1.9     kamil 				 * declarations */
     94   1.9     kamil 		ps.ind_level = 0;
     95   1.9     kamil 		for (i = ps.tos - 1; i > 0; --i)
     96   1.9     kamil 		    if (ps.p_stack[i] == decl)
     97   1.9     kamil 			++ps.ind_level;	/* indentation is number of
     98   1.9     kamil 					 * declaration levels deep we are */
     99   1.9     kamil 		ps.i_l_follow = ps.ind_level;
    100   1.9     kamil 	    }
    101   1.9     kamil 	}
    102   1.9     kamil 	break;
    103   1.5     lukem 
    104   1.9     kamil     case ifstmt:		/* scanned if (...) */
    105   1.9     kamil 	if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
    106   1.9     kamil 		/*
    107   1.9     kamil 		 * Note that the stack pointer here is decremented, effectively
    108   1.9     kamil 		 * reducing "else if" to "if". This saves a lot of stack space
    109   1.9     kamil 		 * in case of a long "if-else-if ... else-if" sequence.
    110   1.9     kamil 		 */
    111   1.9     kamil 		ps.i_l_follow = ps.il[ps.tos--];
    112   1.9     kamil 	/* the rest is the same as for dolit and forstmt */
    113   1.9     kamil 	/* FALLTHROUGH */
    114   1.9     kamil     case dolit:		/* 'do' */
    115   1.9     kamil     case forstmt:		/* for (...) */
    116   1.9     kamil 	ps.p_stack[++ps.tos] = tk;
    117   1.9     kamil 	ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
    118   1.9     kamil 	++ps.i_l_follow;	/* subsequent statements should be indented 1 */
    119   1.9     kamil 	ps.search_brace = opt.btype_2;
    120   1.9     kamil 	break;
    121   1.9     kamil 
    122   1.9     kamil     case lbrace:		/* scanned { */
    123   1.9     kamil 	break_comma = false;	/* don't break comma in an initial list */
    124   1.9     kamil 	if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
    125   1.9     kamil 		|| ps.p_stack[ps.tos] == stmtl)
    126   1.9     kamil 	    ++ps.i_l_follow;	/* it is a random, isolated stmt group or a
    127   1.9     kamil 				 * declaration */
    128   1.9     kamil 	else {
    129   1.9     kamil 	    if (s_code == e_code) {
    130   1.9     kamil 		/*
    131   1.9     kamil 		 * only do this if there is nothing on the line
    132   1.9     kamil 		 */
    133   1.9     kamil 		--ps.ind_level;
    134   1.9     kamil 		/*
    135   1.9     kamil 		 * it is a group as part of a while, for, etc.
    136   1.9     kamil 		 */
    137   1.9     kamil 		if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
    138   1.9     kamil 		    --ps.ind_level;
    139   1.9     kamil 		/*
    140   1.9     kamil 		 * for a switch, brace should be two levels out from the code
    141   1.9     kamil 		 */
    142   1.9     kamil 	    }
    143   1.9     kamil 	}
    144   1.5     lukem 
    145   1.9     kamil 	ps.p_stack[++ps.tos] = lbrace;
    146   1.9     kamil 	ps.il[ps.tos] = ps.ind_level;
    147   1.9     kamil 	ps.p_stack[++ps.tos] = stmt;
    148   1.9     kamil 	/* allow null stmt between braces */
    149   1.9     kamil 	ps.il[ps.tos] = ps.i_l_follow;
    150   1.9     kamil 	break;
    151   1.9     kamil 
    152   1.9     kamil     case whilestmt:		/* scanned while (...) */
    153   1.9     kamil 	if (ps.p_stack[ps.tos] == dohead) {
    154   1.9     kamil 	    /* it is matched with do stmt */
    155   1.9     kamil 	    ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
    156   1.9     kamil 	    ps.p_stack[++ps.tos] = whilestmt;
    157   1.9     kamil 	    ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
    158   1.9     kamil 	}
    159   1.9     kamil 	else {			/* it is a while loop */
    160   1.9     kamil 	    ps.p_stack[++ps.tos] = whilestmt;
    161   1.9     kamil 	    ps.il[ps.tos] = ps.i_l_follow;
    162   1.9     kamil 	    ++ps.i_l_follow;
    163   1.9     kamil 	    ps.search_brace = opt.btype_2;
    164   1.9     kamil 	}
    165   1.5     lukem 
    166   1.9     kamil 	break;
    167   1.5     lukem 
    168   1.9     kamil     case elselit:		/* scanned an else */
    169   1.1       cgd 
    170   1.9     kamil 	if (ps.p_stack[ps.tos] != ifhead)
    171  1.10  christos 	    diag(1, "Unmatched 'else'");
    172   1.9     kamil 	else {
    173   1.9     kamil 	    ps.ind_level = ps.il[ps.tos];	/* indentation for else should
    174   1.9     kamil 						 * be same as for if */
    175   1.9     kamil 	    ps.i_l_follow = ps.ind_level + 1;	/* everything following should
    176   1.9     kamil 						 * be in 1 level */
    177   1.9     kamil 	    ps.p_stack[ps.tos] = elsehead;
    178   1.9     kamil 	    /* remember if with else */
    179   1.9     kamil 	    ps.search_brace = opt.btype_2 | opt.else_if;
    180   1.9     kamil 	}
    181   1.9     kamil 	break;
    182   1.1       cgd 
    183   1.9     kamil     case rbrace:		/* scanned a } */
    184   1.9     kamil 	/* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
    185   1.9     kamil 	if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
    186   1.9     kamil 	    ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];
    187   1.9     kamil 	    ps.p_stack[ps.tos] = stmt;
    188   1.9     kamil 	}
    189   1.9     kamil 	else
    190  1.10  christos 	    diag(1, "Statement nesting error");
    191   1.9     kamil 	break;
    192   1.9     kamil 
    193   1.9     kamil     case swstmt:		/* had switch (...) */
    194   1.9     kamil 	ps.p_stack[++ps.tos] = swstmt;
    195   1.9     kamil 	ps.cstk[ps.tos] = case_ind;
    196   1.9     kamil 	/* save current case indent level */
    197   1.9     kamil 	ps.il[ps.tos] = ps.i_l_follow;
    198   1.9     kamil 	case_ind = ps.i_l_follow + opt.case_indent;	/* cases should be one
    199   1.9     kamil 							 * level down from
    200   1.9     kamil 							 * switch */
    201   1.9     kamil 	ps.i_l_follow += opt.case_indent + 1;	/* statements should be two
    202   1.9     kamil 						 * levels in */
    203   1.9     kamil 	ps.search_brace = opt.btype_2;
    204   1.9     kamil 	break;
    205   1.9     kamil 
    206   1.9     kamil     case semicolon:		/* this indicates a simple stmt */
    207   1.9     kamil 	break_comma = false;	/* turn off flag to break after commas in a
    208   1.9     kamil 				 * declaration */
    209   1.9     kamil 	ps.p_stack[++ps.tos] = stmt;
    210   1.9     kamil 	ps.il[ps.tos] = ps.ind_level;
    211   1.9     kamil 	break;
    212   1.9     kamil 
    213   1.9     kamil     default:			/* this is an error */
    214  1.10  christos 	diag(1, "Unknown code to parser");
    215   1.9     kamil 	return;
    216   1.1       cgd 
    217   1.1       cgd 
    218   1.9     kamil     }				/* end of switch */
    219   1.1       cgd 
    220   1.9     kamil     if (ps.tos >= STACKSIZE - 1)
    221   1.9     kamil 	errx(1, "Parser stack overflow");
    222   1.1       cgd 
    223   1.9     kamil     reduce();			/* see if any reduction can be done */
    224   1.1       cgd 
    225   1.1       cgd #ifdef debug
    226   1.9     kamil     for (i = 1; i <= ps.tos; ++i)
    227   1.9     kamil 	printf("(%d %d)", ps.p_stack[i], ps.il[i]);
    228   1.9     kamil     printf("\n");
    229   1.1       cgd #endif
    230   1.9     kamil 
    231   1.9     kamil     return;
    232   1.1       cgd }
    233   1.9     kamil 
    234   1.1       cgd /*
    235   1.1       cgd  * NAME: reduce
    236   1.5     lukem  *
    237   1.1       cgd  * FUNCTION: Implements the reduce part of the parsing algorithm
    238   1.5     lukem  *
    239   1.1       cgd  * ALGORITHM: The following reductions are done.  Reductions are repeated
    240   1.1       cgd  *	until no more are possible.
    241   1.5     lukem  *
    242   1.1       cgd  * Old TOS		New TOS
    243   1.1       cgd  * <stmt> <stmt>	<stmtl>
    244   1.1       cgd  * <stmtl> <stmt>	<stmtl>
    245   1.1       cgd  * do <stmt>		"dostmt"
    246   1.1       cgd  * if <stmt>		"ifstmt"
    247   1.1       cgd  * switch <stmt>	<stmt>
    248   1.1       cgd  * decl <stmt>		<stmt>
    249   1.1       cgd  * "ifelse" <stmt>	<stmt>
    250   1.1       cgd  * for <stmt>		<stmt>
    251   1.1       cgd  * while <stmt>		<stmt>
    252   1.1       cgd  * "dostmt" while	<stmt>
    253   1.5     lukem  *
    254   1.1       cgd  * On each reduction, ps.i_l_follow (the indentation for the following line)
    255   1.1       cgd  * is set to the indentation level associated with the old TOS.
    256   1.5     lukem  *
    257   1.1       cgd  * PARAMETERS: None
    258   1.5     lukem  *
    259   1.1       cgd  * RETURNS: Nothing
    260   1.5     lukem  *
    261   1.1       cgd  * GLOBALS: ps.cstk ps.i_l_follow = ps.il ps.p_stack = ps.tos =
    262   1.5     lukem  *
    263   1.1       cgd  * CALLS: None
    264   1.5     lukem  *
    265   1.1       cgd  * CALLED BY: parse
    266   1.5     lukem  *
    267  1.11    rillig  * HISTORY: initial coding	November 1976	D A Willcox of CAC
    268   1.5     lukem  *
    269   1.1       cgd  */
    270   1.1       cgd /*----------------------------------------------*\
    271   1.1       cgd |   REDUCTION PHASE				    |
    272   1.1       cgd \*----------------------------------------------*/
    273   1.9     kamil static void
    274   1.6       wiz reduce(void)
    275   1.1       cgd {
    276   1.9     kamil     int i;
    277   1.9     kamil 
    278   1.9     kamil     for (;;) {			/* keep looping until there is nothing left to
    279   1.9     kamil 				 * reduce */
    280   1.9     kamil 
    281   1.9     kamil 	switch (ps.p_stack[ps.tos]) {
    282   1.1       cgd 
    283   1.9     kamil 	case stmt:
    284   1.9     kamil 	    switch (ps.p_stack[ps.tos - 1]) {
    285   1.1       cgd 
    286   1.9     kamil 	    case stmt:
    287   1.9     kamil 	    case stmtl:
    288   1.9     kamil 		/* stmtl stmt or stmt stmt */
    289   1.9     kamil 		ps.p_stack[--ps.tos] = stmtl;
    290   1.9     kamil 		break;
    291   1.9     kamil 
    292   1.9     kamil 	    case dolit:	/* <do> <stmt> */
    293   1.9     kamil 		ps.p_stack[--ps.tos] = dohead;
    294   1.9     kamil 		ps.i_l_follow = ps.il[ps.tos];
    295   1.9     kamil 		break;
    296   1.9     kamil 
    297   1.9     kamil 	    case ifstmt:
    298   1.9     kamil 		/* <if> <stmt> */
    299   1.9     kamil 		ps.p_stack[--ps.tos] = ifhead;
    300   1.9     kamil 		for (i = ps.tos - 1;
    301   1.9     kamil 			(
    302   1.9     kamil 			 ps.p_stack[i] != stmt
    303   1.9     kamil 			 &&
    304   1.9     kamil 			 ps.p_stack[i] != stmtl
    305   1.9     kamil 			 &&
    306   1.9     kamil 			 ps.p_stack[i] != lbrace
    307   1.9     kamil 			 );
    308   1.9     kamil 			--i);
    309   1.9     kamil 		ps.i_l_follow = ps.il[i];
    310   1.9     kamil 		/*
    311   1.9     kamil 		 * for the time being, we will assume that there is no else on
    312   1.9     kamil 		 * this if, and set the indentation level accordingly. If an
    313   1.9     kamil 		 * else is scanned, it will be fixed up later
    314   1.9     kamil 		 */
    315   1.9     kamil 		break;
    316   1.9     kamil 
    317   1.9     kamil 	    case swstmt:
    318   1.9     kamil 		/* <switch> <stmt> */
    319   1.9     kamil 		case_ind = ps.cstk[ps.tos - 1];
    320   1.9     kamil 		/* FALLTHROUGH */
    321   1.9     kamil 	    case decl:		/* finish of a declaration */
    322   1.9     kamil 	    case elsehead:
    323   1.9     kamil 		/* <<if> <stmt> else> <stmt> */
    324   1.9     kamil 	    case forstmt:
    325   1.9     kamil 		/* <for> <stmt> */
    326   1.9     kamil 	    case whilestmt:
    327   1.9     kamil 		/* <while> <stmt> */
    328   1.9     kamil 		ps.p_stack[--ps.tos] = stmt;
    329   1.9     kamil 		ps.i_l_follow = ps.il[ps.tos];
    330   1.9     kamil 		break;
    331   1.1       cgd 
    332   1.9     kamil 	    default:		/* <anything else> <stmt> */
    333   1.9     kamil 		return;
    334   1.1       cgd 
    335   1.9     kamil 	    }			/* end of section for <stmt> on top of stack */
    336   1.9     kamil 	    break;
    337   1.1       cgd 
    338   1.9     kamil 	case whilestmt:	/* while (...) on top */
    339   1.9     kamil 	    if (ps.p_stack[ps.tos - 1] == dohead) {
    340   1.9     kamil 		/* it is termination of a do while */
    341   1.9     kamil 		ps.tos -= 2;
    342   1.9     kamil 		break;
    343   1.9     kamil 	    }
    344   1.9     kamil 	    else
    345   1.9     kamil 		return;
    346   1.1       cgd 
    347   1.9     kamil 	default:		/* anything else on top */
    348   1.9     kamil 	    return;
    349   1.1       cgd 
    350   1.1       cgd 	}
    351   1.9     kamil     }
    352   1.1       cgd }
    353