summaryrefslogtreecommitdiff
path: root/usr.bin/indent/parse.c
blob: 2e7f343ddeff1e18970ccb52b619f4a3403615ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*	$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $	*/

/*-
 * SPDX-License-Identifier: BSD-4-Clause
 *
 * Copyright (c) 1985 Sun Microsystems, Inc.
 * Copyright (c) 1980, 1993
 *	The Regents of the University of California.  All rights reserved.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: parse.c,v 1.79 2023/06/18 06:56:32 rillig Exp $");

#include <stdlib.h>

#include "indent.h"

/*
 * Try to combine the statement on the top of the parse stack with the symbol
 * directly below it, replacing these two symbols with a single symbol.
 */
static bool
psyms_reduce_stmt(void)
{
	switch (ps.psyms.sym[ps.psyms.len - 2]) {

	case psym_stmt:
		ps.psyms.len--;
		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
		return true;

	case psym_do:
		ps.psyms.len--;
		ps.psyms.sym[ps.psyms.len - 1] = psym_do_stmt;
		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
		return true;

	case psym_if_expr:
		ps.psyms.len--;
		ps.psyms.sym[ps.psyms.len - 1] = psym_if_expr_stmt;
		/* For the time being, assume that there is no 'else' on this
		 * 'if', and set the indentation level accordingly. If an
		 * 'else' is scanned, it will be fixed up later. */
		size_t i = ps.psyms.len - 2;
		while (ps.psyms.sym[i] != psym_stmt
		    && ps.psyms.sym[i] != psym_lbrace_block)
			i--;
		ps.ind_level_follow = ps.psyms.ind_level[i];
		return true;

	case psym_switch_expr:
	case psym_decl:
	case psym_if_expr_stmt_else:
	case psym_for_exprs:
	case psym_while_expr:
		ps.psyms.len--;
		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
		ps.ind_level_follow = ps.psyms.ind_level[ps.psyms.len - 1];
		return true;

	default:
		return false;
	}
}

static int
left_justify_decl_level(void)
{
	int level = 0;
	for (size_t i = ps.psyms.len - 2; i > 0; i--)
		if (ps.psyms.sym[i] == psym_decl)
			level++;
	return level;
}

void
ps_push(parser_symbol psym, bool follow)
{
	if (ps.psyms.len == ps.psyms.cap) {
		ps.psyms.cap += 16;
		ps.psyms.sym = nonnull(realloc(ps.psyms.sym,
			sizeof(ps.psyms.sym[0]) * ps.psyms.cap));
		ps.psyms.ind_level = nonnull(realloc(ps.psyms.ind_level,
			sizeof(ps.psyms.ind_level[0]) * ps.psyms.cap));
	}
	ps.psyms.len++;
	ps.psyms.sym[ps.psyms.len - 1] = psym;
	ps.psyms.ind_level[ps.psyms.len - 1] =
	    follow ? ps.ind_level_follow : ps.ind_level;
}

/*
 * Repeatedly try to reduce the top two symbols on the parse stack to a single
 * symbol, until no more reductions are possible.
 */
static void
psyms_reduce(void)
{
again:
	if (ps.psyms.len >= 2 && ps.psyms.sym[ps.psyms.len - 1] == psym_stmt
	    && psyms_reduce_stmt())
		goto again;
	if (ps.psyms.sym[ps.psyms.len - 1] == psym_while_expr &&
	    ps.psyms.sym[ps.psyms.len - 2] == psym_do_stmt) {
		ps.psyms.len -= 2;
		goto again;
	}
}

static bool
is_lbrace(parser_symbol psym)
{
	return psym == psym_lbrace_block
	    || psym == psym_lbrace_struct
	    || psym == psym_lbrace_union
	    || psym == psym_lbrace_enum;
}

/*
 * Shift the token onto the parser stack, then try to reduce it by combining it with
 * previous tokens.
 */
void
parse(parser_symbol psym)
{
	debug_blank_line();
	debug_println("parse token: %s", psym_name[psym]);

	if (psym != psym_else) {
		while (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt) {
			ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
			psyms_reduce();
		}
	}

	switch (psym) {

	case psym_lbrace_block:
	case psym_lbrace_struct:
	case psym_lbrace_union:
	case psym_lbrace_enum:
		ps.break_after_comma = false;
		if (ps.psyms.sym[ps.psyms.len - 1] == psym_decl
		    || ps.psyms.sym[ps.psyms.len - 1] == psym_stmt)
			ps.ind_level_follow++;
		else if (code.len == 0) {
			/* It is part of a while, for, etc. */
			ps.ind_level--;

			/* for a switch, brace should be two levels out from
			 * the code */
			if (ps.psyms.sym[ps.psyms.len - 1] == psym_switch_expr
			    && opt.case_indent >= 1.0F)
				ps.ind_level--;
		}

		ps_push(psym, false);
		ps_push(psym_stmt, true);
		break;

	case psym_rbrace:
		/* stack should have <lbrace> <stmt> or <lbrace> <decl> */
		if (!(ps.psyms.len >= 2
			&& is_lbrace(ps.psyms.sym[ps.psyms.len - 2]))) {
			diag(1, "Statement nesting error");
			break;
		}
		ps.psyms.len--;
		ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
		ps.ind_level_follow = ps.ind_level;
		ps.psyms.sym[ps.psyms.len - 1] = psym_stmt;
		break;

	case psym_decl:
		if (ps.psyms.sym[ps.psyms.len - 1] == psym_decl)
			break;	/* only put one declaration onto stack */

		ps.break_after_comma = true;
		ps_push(psym_decl, true);

		if (opt.left_justify_decl) {
			ps.ind_level = left_justify_decl_level();
			ps.ind_level_follow = ps.ind_level;
		}
		break;

	case psym_stmt:
		ps.break_after_comma = false;
		ps_push(psym_stmt, false);
		break;

	case psym_if_expr:
		if (ps.psyms.sym[ps.psyms.len - 1] == psym_if_expr_stmt_else
		    && opt.else_if_in_same_line) {
			ps.ind_level_follow =
			    ps.psyms.ind_level[ps.psyms.len - 1];
			ps.psyms.len--;
		}
		/* FALLTHROUGH */
	case psym_do:
	case psym_for_exprs:
		ps.ind_level = ps.ind_level_follow;
		ps.ind_level_follow = ps.ind_level + 1;
		ps_push(psym, false);
		break;

	case psym_else:
		if (ps.psyms.sym[ps.psyms.len - 1] != psym_if_expr_stmt) {
			diag(1, "Unmatched 'else'");
			break;
		}
		ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
		ps.ind_level_follow = ps.ind_level + 1;
		ps.psyms.sym[ps.psyms.len - 1] = psym_if_expr_stmt_else;
		break;

	case psym_switch_expr:
		ps_push(psym_switch_expr, true);
		ps.ind_level_follow += (int)opt.case_indent + 1;
		break;

	case psym_while_expr:
		if (ps.psyms.sym[ps.psyms.len - 1] == psym_do_stmt) {
			ps.ind_level = ps.psyms.ind_level[ps.psyms.len - 1];
			ps.ind_level_follow = ps.ind_level;
			ps_push(psym_while_expr, false);
		} else {
			ps_push(psym_while_expr, true);
			ps.ind_level_follow++;
		}
		break;

	default:
		diag(1, "Unknown code to parser");
		return;
	}

	debug_psyms_stack("before reduction");
	psyms_reduce();
	debug_psyms_stack("after reduction");
}