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