Module Name: src
Committed By: rillig
Date: Wed Jun 7 15:46:12 UTC 2023
Modified Files:
src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c parse.c
Log Message:
indent: extract the stack of parser symbols to a separate struct
No functional change.
To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.337 -r1.338 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.201 -r1.202 src/usr.bin/indent/io.c
cvs rdiff -u -r1.215 -r1.216 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.68 -r1.69 src/usr.bin/indent/parse.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.38 src/usr.bin/indent/debug.c:1.39
--- src/usr.bin/indent/debug.c:1.38 Wed Jun 7 15:25:08 2023
+++ src/usr.bin/indent/debug.c Wed Jun 7 15:46:11 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.38 2023/06/07 15:25:08 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.39 2023/06/07 15:46:11 rillig Exp $");
#include <stdarg.h>
@@ -359,8 +359,10 @@ void
debug_parse_stack(const char *situation)
{
debug_printf("parse stack %s:", situation);
- for (int i = 0; i <= ps.tos; ++i)
- debug_printf(" %d %s", ps.s_ind_level[i], psym_name[ps.s_sym[i]]);
+ const struct psym_stack *psyms = &ps.psyms;
+ for (int i = 0; i <= psyms->top; ++i)
+ debug_printf(" %d %s",
+ psyms->ind_level[i], psym_name[psyms->sym[i]]);
debug_println("");
}
#endif
Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.337 src/usr.bin/indent/indent.c:1.338
--- src/usr.bin/indent/indent.c:1.337 Tue Jun 6 04:37:26 2023
+++ src/usr.bin/indent/indent.c Wed Jun 7 15:46:11 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.337 2023/06/06 04:37:26 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.338 2023/06/07 15:46:11 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -176,7 +176,7 @@ ind_add(int ind, const char *s, size_t l
static void
init_globals(void)
{
- ps.s_sym[0] = psym_stmt_list;
+ ps.psyms.sym[0] = psym_stmt_list;
ps.prev_lsym = lsym_semicolon;
ps.next_col_1 = true;
ps.lbrace_kind = psym_lbrace_block;
@@ -374,7 +374,7 @@ process_eof(void)
output_line();
}
- if (ps.tos > 1) /* check for balanced braces */
+ if (ps.psyms.top > 1) /* check for balanced braces */
diag(1, "Stuff missing from end of file");
fflush(output);
@@ -415,7 +415,8 @@ process_newline(void)
&& lab.len == 0 /* for preprocessing lines */
&& com.len == 0)
goto stay_in_line;
- if (ps.s_sym[ps.tos] == psym_switch_expr && opt.brace_same_line) {
+ if (ps.psyms.sym[ps.psyms.top] == psym_switch_expr
+ && opt.brace_same_line) {
ps.force_nl = true;
goto stay_in_line;
}
@@ -476,7 +477,7 @@ process_lparen(void)
&& opt.continuation_indent == opt.indent_size)
ps.extra_expr_indent = eei_yes;
- if (ps.init_or_struct && ps.tos <= 2) {
+ if (ps.init_or_struct && ps.psyms.top <= 2) {
/* A kludge to correctly align function definitions. */
parse(psym_stmt);
ps.init_or_struct = false;
@@ -734,9 +735,9 @@ process_semicolon(void)
static void
process_lbrace(void)
{
- parser_symbol psym = ps.s_sym[ps.tos];
+ parser_symbol psym = ps.psyms.sym[ps.psyms.top];
if (ps.prev_lsym == lsym_rparen
- && ps.tos >= 2
+ && ps.psyms.top >= 2
&& !(psym == psym_for_exprs || psym == psym_if_expr
|| psym == psym_switch_expr || psym == psym_while_expr)) {
ps.block_init = true;
@@ -835,14 +836,14 @@ process_rbrace(void)
ps.in_decl = true;
}
- if (ps.tos == 2)
+ if (ps.psyms.top == 2)
out.line_kind = lk_func_end;
parse(psym_rbrace);
if (!ps.init_or_struct
- && ps.s_sym[ps.tos] != psym_do_stmt
- && ps.s_sym[ps.tos] != psym_if_expr_stmt)
+ && ps.psyms.sym[ps.psyms.top] != psym_do_stmt
+ && ps.psyms.sym[ps.psyms.top] != psym_if_expr_stmt)
ps.force_nl = true;
}
@@ -877,7 +878,7 @@ process_type(void)
{
parse(psym_decl); /* let the parser worry about indentation */
- if (ps.prev_lsym == lsym_rparen && ps.tos <= 1) {
+ if (ps.prev_lsym == lsym_rparen && ps.psyms.top <= 1) {
if (code.len > 0)
output_line();
}
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.176 src/usr.bin/indent/indent.h:1.177
--- src/usr.bin/indent/indent.h:1.176 Tue Jun 6 04:37:26 2023
+++ src/usr.bin/indent/indent.h Wed Jun 7 15:46:12 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.176 2023/06/06 04:37:26 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.177 2023/06/07 15:46:12 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -269,6 +269,12 @@ typedef struct paren_level_props {
} cast; /* whether the parentheses form a type cast */
} paren_level_props;
+struct psym_stack {
+ int top; /* pointer to top of stack */
+ parser_symbol sym[STACKSIZE];
+ int ind_level[STACKSIZE];
+};
+
/*
* The parser state determines the layout of the formatted text.
*
@@ -350,9 +356,7 @@ extern struct parser_state {
eei_last
} extra_expr_indent;
- int tos; /* pointer to top of stack */
- parser_symbol s_sym[STACKSIZE];
- int s_ind_level[STACKSIZE];
+ struct psym_stack psyms;
/* Spacing inside a statement or declaration */
Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.201 src/usr.bin/indent/io.c:1.202
--- src/usr.bin/indent/io.c:1.201 Tue Jun 6 05:27:56 2023
+++ src/usr.bin/indent/io.c Wed Jun 7 15:46:12 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.201 2023/06/06 05:27:56 rillig Exp $ */
+/* $NetBSD: io.c,v 1.202 2023/06/07 15:46:12 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.201 2023/06/06 05:27:56 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.202 2023/06/07 15:46:12 rillig Exp $");
#include <stdio.h>
@@ -179,7 +179,7 @@ is_blank_line_optional(void)
{
if (out.prev_line_kind == lk_stmt_head)
return wrote_newlines >= 1;
- if (ps.tos >= 2)
+ if (ps.psyms.top >= 2)
return wrote_newlines >= 2;
return wrote_newlines >= 3;
}
@@ -187,10 +187,10 @@ is_blank_line_optional(void)
static int
compute_case_label_indent(void)
{
- int i = ps.tos;
- while (i > 0 && ps.s_sym[i] != psym_switch_expr)
+ int i = ps.psyms.top;
+ while (i > 0 && ps.psyms.sym[i] != psym_switch_expr)
i--;
- float case_ind = (float)ps.s_ind_level[i] + opt.case_indent;
+ float case_ind = (float)ps.psyms.ind_level[i] + opt.case_indent;
return (int)(case_ind * (float)opt.indent_size);
}
@@ -235,7 +235,8 @@ compute_code_indent(void)
int base_ind = ps.ind_level * opt.indent_size;
if (ps.line_start_nparen == 0) {
- if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum)
+ if (ps.psyms.top >= 1
+ && ps.psyms.sym[ps.psyms.top - 1] == psym_lbrace_enum)
return base_ind;
if (ps.in_stmt_cont)
return base_ind + opt.continuation_indent;
@@ -338,7 +339,7 @@ output_line(void)
ps.in_stmt_cont = false;
if (opt.blank_line_after_decl && ps.declaration == decl_end
- && ps.tos > 1) {
+ && ps.psyms.top > 1) {
ps.declaration = decl_no;
ps.blank_line_after_decl = true;
}
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.215 src/usr.bin/indent/lexi.c:1.216
--- src/usr.bin/indent/lexi.c:1.215 Tue Jun 6 05:11:11 2023
+++ src/usr.bin/indent/lexi.c Wed Jun 7 15:46:12 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.215 2023/06/06 05:11:11 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.216 2023/06/07 15:46:12 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.215 2023/06/06 05:11:11 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.216 2023/06/07 15:46:12 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -412,7 +412,7 @@ found_typename:
}
}
- if (inp_p[0] == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
+ if (inp_p[0] == '(' && ps.psyms.top <= 1 && ps.ind_level == 0 &&
!ps.in_func_def_params && !ps.block_init) {
if (ps.nparen == 0 && probably_looking_at_definition()) {
Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.68 src/usr.bin/indent/parse.c:1.69
--- src/usr.bin/indent/parse.c:1.68 Tue Jun 6 05:11:11 2023
+++ src/usr.bin/indent/parse.c Wed Jun 7 15:46:12 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.68 2023/06/06 05:11:11 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.68 2023/06/06 05:11:11 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.69 2023/06/07 15:46:12 rillig Exp $");
#include <err.h>
@@ -49,28 +49,28 @@ __RCSID("$NetBSD: parse.c,v 1.68 2023/06
* directly below it, replacing these two symbols with a single symbol.
*/
static bool
-reduce_stmt(void)
+psyms_reduce_stmt(struct psym_stack *psyms)
{
- switch (ps.s_sym[ps.tos - 1]) {
+ switch (psyms->sym[psyms->top - 1]) {
case psym_stmt:
case psym_stmt_list:
- ps.s_sym[--ps.tos] = psym_stmt_list;
+ psyms->sym[--psyms->top] = psym_stmt_list;
return true;
case psym_do:
- ps.s_sym[--ps.tos] = psym_do_stmt;
- ps.ind_level_follow = ps.s_ind_level[ps.tos];
+ psyms->sym[--psyms->top] = psym_do_stmt;
+ ps.ind_level_follow = psyms->ind_level[psyms->top];
return true;
case psym_if_expr:
- ps.s_sym[--ps.tos] = psym_if_expr_stmt;
- int i = ps.tos - 1;
- while (ps.s_sym[i] != psym_stmt &&
- ps.s_sym[i] != psym_stmt_list &&
- ps.s_sym[i] != psym_lbrace_block)
+ psyms->sym[--psyms->top] = psym_if_expr_stmt;
+ int i = psyms->top - 1;
+ while (psyms->sym[i] != psym_stmt &&
+ psyms->sym[i] != psym_stmt_list &&
+ psyms->sym[i] != psym_lbrace_block)
--i;
- ps.ind_level_follow = ps.s_ind_level[i];
+ ps.ind_level_follow = psyms->ind_level[i];
/* 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. */
@@ -81,8 +81,8 @@ reduce_stmt(void)
case psym_if_expr_stmt_else:
case psym_for_exprs:
case psym_while_expr:
- ps.s_sym[--ps.tos] = psym_stmt;
- ps.ind_level_follow = ps.s_ind_level[ps.tos];
+ psyms->sym[--psyms->top] = psym_stmt;
+ ps.ind_level_follow = psyms->ind_level[psyms->top];
return true;
default:
@@ -94,8 +94,8 @@ static int
decl_level(void)
{
int level = 0;
- for (int i = ps.tos - 1; i > 0; i--)
- if (ps.s_sym[i] == psym_decl)
+ for (int i = ps.psyms.top - 1; i > 0; i--)
+ if (ps.psyms.sym[i] == psym_decl)
level++;
return level;
}
@@ -103,15 +103,15 @@ decl_level(void)
static void
ps_push(parser_symbol psym)
{
- ps.s_sym[++ps.tos] = psym;
- ps.s_ind_level[ps.tos] = ps.ind_level;
+ ps.psyms.sym[++ps.psyms.top] = psym;
+ ps.psyms.ind_level[ps.psyms.top] = ps.ind_level;
}
static void
ps_push_follow(parser_symbol psym)
{
- ps.s_sym[++ps.tos] = psym;
- ps.s_ind_level[ps.tos] = ps.ind_level_follow;
+ ps.psyms.sym[++ps.psyms.top] = psym;
+ ps.psyms.ind_level[ps.psyms.top] = ps.ind_level_follow;
}
/*
@@ -119,14 +119,14 @@ ps_push_follow(parser_symbol psym)
* symbol, until no more reductions are possible.
*/
static void
-reduce(void)
+psyms_reduce(struct psym_stack *psyms)
{
again:
- if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt())
+ if (psyms->sym[psyms->top] == psym_stmt && psyms_reduce_stmt(psyms))
goto again;
- if (ps.s_sym[ps.tos] == psym_while_expr &&
- ps.s_sym[ps.tos - 1] == psym_do_stmt) {
- ps.tos -= 2;
+ if (psyms->sym[psyms->top] == psym_while_expr &&
+ psyms->sym[psyms->top - 1] == psym_do_stmt) {
+ psyms->top -= 2;
goto again;
}
}
@@ -150,17 +150,18 @@ parse(parser_symbol psym)
debug_blank_line();
debug_println("parse token: %s", psym_name[psym]);
+ struct psym_stack *psyms = &ps.psyms;
if (psym != psym_else) {
- while (ps.s_sym[ps.tos] == psym_if_expr_stmt) {
- ps.s_sym[ps.tos] = psym_stmt;
- reduce();
+ while (psyms->sym[psyms->top] == psym_if_expr_stmt) {
+ psyms->sym[psyms->top] = psym_stmt;
+ psyms_reduce(&ps.psyms);
}
}
switch (psym) {
case psym_decl:
- if (ps.s_sym[ps.tos] == psym_decl)
+ if (psyms->sym[psyms->top] == psym_decl)
break; /* only put one declaration onto stack */
ps.break_after_comma = true;
@@ -171,9 +172,9 @@ parse(parser_symbol psym)
break;
case psym_if_expr:
- if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else
+ if (psyms->sym[psyms->top] == psym_if_expr_stmt_else
&& opt.else_if_in_same_line)
- ps.ind_level_follow = ps.s_ind_level[ps.tos--];
+ ps.ind_level_follow = psyms->ind_level[psyms->top--];
/* FALLTHROUGH */
case psym_do:
case psym_for_exprs:
@@ -186,9 +187,9 @@ parse(parser_symbol psym)
case psym_lbrace_union:
case psym_lbrace_enum:
ps.break_after_comma = false;
- if (ps.s_sym[ps.tos] == psym_stmt
- || ps.s_sym[ps.tos] == psym_decl
- || ps.s_sym[ps.tos] == psym_stmt_list)
+ if (psyms->sym[psyms->top] == psym_stmt
+ || psyms->sym[psyms->top] == psym_decl
+ || psyms->sym[psyms->top] == psym_stmt_list)
++ps.ind_level_follow; /* it is a random, isolated
* stmt group or a declaration
*/
@@ -200,8 +201,8 @@ parse(parser_symbol psym)
/* for a switch, brace should be two levels out
* from the code */
- if (ps.s_sym[ps.tos] == psym_switch_expr &&
- opt.case_indent >= 1.0F)
+ if (psyms->sym[psyms->top] == psym_switch_expr
+ && opt.case_indent >= 1.0F)
--ps.ind_level;
}
}
@@ -211,9 +212,9 @@ parse(parser_symbol psym)
break;
case psym_while_expr:
- if (ps.s_sym[ps.tos] == psym_do_stmt) {
+ if (psyms->sym[psyms->top] == psym_do_stmt) {
ps.ind_level =
- ps.ind_level_follow = ps.s_ind_level[ps.tos];
+ ps.ind_level_follow = psyms->ind_level[psyms->top];
ps_push(psym_while_expr);
} else {
ps_push_follow(psym_while_expr);
@@ -223,23 +224,25 @@ parse(parser_symbol psym)
break;
case psym_else:
- if (ps.s_sym[ps.tos] != psym_if_expr_stmt) {
+ if (psyms->sym[psyms->top] != psym_if_expr_stmt) {
diag(1, "Unmatched 'else'");
break;
}
- ps.ind_level = ps.s_ind_level[ps.tos];
+ ps.ind_level = psyms->ind_level[psyms->top];
ps.ind_level_follow = ps.ind_level + 1;
- ps.s_sym[ps.tos] = psym_if_expr_stmt_else;
+ psyms->sym[psyms->top] = psym_if_expr_stmt_else;
break;
case psym_rbrace:
/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
- if (!(ps.tos > 0 && is_lbrace(ps.s_sym[ps.tos - 1]))) {
+ if (!(psyms->top > 0
+ && is_lbrace(psyms->sym[psyms->top - 1]))) {
diag(1, "Statement nesting error");
break;
}
- ps.ind_level = ps.ind_level_follow = ps.s_ind_level[--ps.tos];
- ps.s_sym[ps.tos] = psym_stmt;
+ ps.ind_level = ps.ind_level_follow =
+ psyms->ind_level[--psyms->top];
+ psyms->sym[psyms->top] = psym_stmt;
break;
case psym_switch_expr:
@@ -257,10 +260,10 @@ parse(parser_symbol psym)
return;
}
- if (ps.tos >= STACKSIZE - 1)
+ if (psyms->top >= STACKSIZE - 1)
errx(1, "Parser stack overflow");
debug_parse_stack("before reduction");
- reduce(); /* see if any reduction can be done */
+ psyms_reduce(&ps.psyms);
debug_parse_stack("after reduction");
}