Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/indent indent: rename parser stack variables
details: https://anonhg.NetBSD.org/src/rev/cdee9ea7b611
branches: trunk
changeset: 1024352:cdee9ea7b611
user: rillig <rillig%NetBSD.org@localhost>
date: Wed Oct 20 05:26:46 2021 +0000
description:
indent: rename parser stack variables
No functional change.
diffstat:
usr.bin/indent/indent.c | 12 ++--
usr.bin/indent/indent.h | 12 +++--
usr.bin/indent/parse.c | 105 ++++++++++++++++++++++++-----------------------
3 files changed, 66 insertions(+), 63 deletions(-)
diffs (truncated from 328 to 300 lines):
diff -r 4f51661bc462 -r cdee9ea7b611 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Wed Oct 20 05:14:21 2021 +0000
+++ b/usr.bin/indent/indent.c Wed Oct 20 05:26:46 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.142 2021/10/20 05:14:21 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.143 2021/10/20 05:26:46 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.142 2021/10/20 05:14:21 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.143 2021/10/20 05:26:46 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -428,7 +428,7 @@
{
found_err = false;
- ps.p_stack[0] = stmt;
+ ps.s_ttype[0] = stmt;
ps.last_nl = true;
ps.last_token = semicolon;
buf_init(&com);
@@ -982,7 +982,7 @@
static void
process_rbrace(bool *sp_sw, int *decl_ind, const int *di_stack)
{
- if (ps.p_stack[ps.tos] == decl && !ps.block_init) /* semicolons can be
+ if (ps.s_ttype[ps.tos] == decl && !ps.block_init) /* semicolons can be
* omitted in
* declarations */
parse(semicolon);
@@ -1016,8 +1016,8 @@
blank_line_before = false;
parse(rbrace); /* let parser know about this */
ps.search_brace = opt.cuddle_else
- && ps.p_stack[ps.tos] == if_expr_stmt
- && ps.il[ps.tos] >= ps.ind_level;
+ && ps.s_ttype[ps.tos] == if_expr_stmt
+ && ps.s_ind_level[ps.tos] >= ps.ind_level;
if (ps.tos <= 1 && opt.blanklines_after_procs && ps.decl_nest <= 0)
blank_line_after = true;
diff -r 4f51661bc462 -r cdee9ea7b611 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h Wed Oct 20 05:14:21 2021 +0000
+++ b/usr.bin/indent/indent.h Wed Oct 20 05:26:46 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.40 2021/10/20 05:14:21 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.41 2021/10/20 05:26:46 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -251,9 +251,12 @@
extern struct parser_state {
token_type last_token;
- token_type p_stack[STACKSIZE];
- int il[STACKSIZE]; /* this stack stores indentation levels */
- float cstk[STACKSIZE]; /* used to store case stmt indentation levels */
+
+ int tos; /* pointer to top of stack */
+ token_type s_ttype[STACKSIZE];
+ int s_ind_level[STACKSIZE];
+ float s_case_ind_level[STACKSIZE];
+
int comment_delta; /* used to set up indentation for all lines of
* a boxed comment after the first one */
int n_comment_delta; /* remembers how many columns there were
@@ -311,7 +314,6 @@
enum keyword_kind keyword;
bool dumped_decl_indent;
bool in_parameter_declaration;
- int tos; /* pointer to top of stack */
char procname[100]; /* The name of the current procedure */
int just_saw_decl;
diff -r 4f51661bc462 -r cdee9ea7b611 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c Wed Oct 20 05:14:21 2021 +0000
+++ b/usr.bin/indent/parse.c Wed Oct 20 05:26:46 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.35 2021/10/08 23:47:41 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.36 2021/10/20 05:26:46 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -66,8 +66,8 @@
token_type_name(ttype), token.s);
if (ttype != keyword_else) {
- while (ps.p_stack[ps.tos] == if_expr_stmt) {
- ps.p_stack[ps.tos] = stmt;
+ while (ps.s_ttype[ps.tos] == if_expr_stmt) {
+ ps.s_ttype[ps.tos] = stmt;
reduce();
}
}
@@ -78,17 +78,17 @@
ps.search_brace = opt.brace_same_line;
/* indicate that following brace should be on same line */
- if (ps.p_stack[ps.tos] != decl) { /* only put one declaration
+ if (ps.s_ttype[ps.tos] != decl) { /* only put one declaration
* onto stack */
break_comma = true; /* while in declaration, newline should be
* forced after comma */
- ps.p_stack[++ps.tos] = decl;
- ps.il[ps.tos] = ps.ind_level_follow;
+ ps.s_ttype[++ps.tos] = decl;
+ ps.s_ind_level[ps.tos] = ps.ind_level_follow;
if (opt.ljust_decl) {
ps.ind_level = 0;
for (int i = ps.tos - 1; i > 0; --i)
- if (ps.p_stack[i] == decl)
+ if (ps.s_ttype[i] == decl)
++ps.ind_level; /* indentation is number of
* declaration levels deep we are */
ps.ind_level_follow = ps.ind_level;
@@ -97,26 +97,26 @@
break;
case if_expr: /* 'if' '(' <expr> ')' */
- if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
+ if (ps.s_ttype[ps.tos] == if_expr_stmt_else && opt.else_if) {
/*
* Reduce "else if" to "if". This saves a lot of stack space in
* case of a long "if-else-if ... else-if" sequence.
*/
- ps.ind_level_follow = ps.il[ps.tos--];
+ ps.ind_level_follow = ps.s_ind_level[ps.tos--];
}
/* FALLTHROUGH */
case keyword_do:
case for_exprs: /* 'for' (...) */
- ps.p_stack[++ps.tos] = ttype;
- ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
+ ps.s_ttype[++ps.tos] = ttype;
+ ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
++ps.ind_level_follow; /* subsequent statements should be indented 1 */
ps.search_brace = opt.brace_same_line;
break;
case lbrace:
break_comma = false; /* don't break comma in an initializer list */
- if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
- || ps.p_stack[ps.tos] == stmt_list)
+ if (ps.s_ttype[ps.tos] == stmt || ps.s_ttype[ps.tos] == decl
+ || ps.s_ttype[ps.tos] == stmt_list)
++ps.ind_level_follow; /* it is a random, isolated stmt
* group or a declaration */
else {
@@ -127,28 +127,28 @@
/*
* for a switch, brace should be two levels out from the code
*/
- if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
+ if (ps.s_ttype[ps.tos] == switch_expr && opt.case_indent >= 1)
--ps.ind_level;
}
}
- ps.p_stack[++ps.tos] = lbrace;
- ps.il[ps.tos] = ps.ind_level;
- ps.p_stack[++ps.tos] = stmt;
+ ps.s_ttype[++ps.tos] = lbrace;
+ ps.s_ind_level[ps.tos] = ps.ind_level;
+ ps.s_ttype[++ps.tos] = stmt;
/* allow null stmt between braces */
- ps.il[ps.tos] = ps.ind_level_follow;
+ ps.s_ind_level[ps.tos] = ps.ind_level_follow;
break;
case while_expr: /* 'while' '(' <expr> ')' */
- if (ps.p_stack[ps.tos] == do_stmt) {
+ if (ps.s_ttype[ps.tos] == do_stmt) {
/* it is matched with do stmt */
- ps.ind_level = ps.ind_level_follow = ps.il[ps.tos];
- ps.p_stack[++ps.tos] = while_expr;
- ps.il[ps.tos] = ps.ind_level = ps.ind_level_follow;
+ ps.ind_level = ps.ind_level_follow = ps.s_ind_level[ps.tos];
+ ps.s_ttype[++ps.tos] = while_expr;
+ ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
} else { /* it is a while loop */
- ps.p_stack[++ps.tos] = while_expr;
- ps.il[ps.tos] = ps.ind_level_follow;
+ ps.s_ttype[++ps.tos] = while_expr;
+ ps.s_ind_level[ps.tos] = ps.ind_level_follow;
++ps.ind_level_follow;
ps.search_brace = opt.brace_same_line;
}
@@ -156,13 +156,13 @@
break;
case keyword_else:
- if (ps.p_stack[ps.tos] != if_expr_stmt)
+ if (ps.s_ttype[ps.tos] != if_expr_stmt)
diag(1, "Unmatched 'else'");
else {
- ps.ind_level = ps.il[ps.tos]; /* indentation for else should
- * be same as for if */
+ /* The indentation for 'else' should be the same as for 'if'. */
+ ps.ind_level = ps.s_ind_level[ps.tos];
ps.ind_level_follow = ps.ind_level + 1;
- ps.p_stack[ps.tos] = if_expr_stmt_else;
+ ps.s_ttype[ps.tos] = if_expr_stmt_else;
/* remember if with else */
ps.search_brace = opt.brace_same_line || opt.else_if;
}
@@ -170,18 +170,18 @@
case rbrace:
/* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
- if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
- ps.ind_level = ps.ind_level_follow = ps.il[--ps.tos];
- ps.p_stack[ps.tos] = stmt;
+ if (ps.tos > 0 && ps.s_ttype[ps.tos - 1] == lbrace) {
+ ps.ind_level = ps.ind_level_follow = ps.s_ind_level[--ps.tos];
+ ps.s_ttype[ps.tos] = stmt;
} else
diag(1, "Statement nesting error");
break;
case switch_expr: /* had switch (...) */
- ps.p_stack[++ps.tos] = switch_expr;
- ps.cstk[ps.tos] = case_ind;
+ ps.s_ttype[++ps.tos] = switch_expr;
+ ps.s_case_ind_level[ps.tos] = case_ind;
/* save current case indent level */
- ps.il[ps.tos] = ps.ind_level_follow;
+ ps.s_ind_level[ps.tos] = ps.ind_level_follow;
/* cases should be one level deeper than the switch */
case_ind = (float)ps.ind_level_follow + opt.case_indent;
/* statements should be two levels deeper */
@@ -192,8 +192,8 @@
case semicolon: /* this indicates a simple stmt */
break_comma = false; /* turn off flag to break after commas in a
* declaration */
- ps.p_stack[++ps.tos] = stmt;
- ps.il[ps.tos] = ps.ind_level;
+ ps.s_ttype[++ps.tos] = stmt;
+ ps.s_ind_level[ps.tos] = ps.ind_level;
break;
default:
@@ -209,7 +209,8 @@
#ifdef debug
printf("parse stack:");
for (int i = 1; i <= ps.tos; ++i)
- printf(" ('%s' at %d)", token_type_name(ps.p_stack[i]), ps.il[i]);
+ printf(" ('%s' at %d)",
+ token_type_name(ps.s_ttype[i]), ps.s_ind_level[i]);
if (ps.tos == 0)
printf(" empty");
printf("\n");
@@ -227,26 +228,26 @@
static bool
reduce_stmt(void)
{
- switch (ps.p_stack[ps.tos - 1]) {
+ switch (ps.s_ttype[ps.tos - 1]) {
case stmt: /* stmt stmt */
case stmt_list: /* stmt_list stmt */
- ps.p_stack[--ps.tos] = stmt_list;
+ ps.s_ttype[--ps.tos] = stmt_list;
return true;
case keyword_do: /* 'do' <stmt> */
- ps.p_stack[--ps.tos] = do_stmt;
- ps.ind_level_follow = ps.il[ps.tos];
+ ps.s_ttype[--ps.tos] = do_stmt;
+ ps.ind_level_follow = ps.s_ind_level[ps.tos];
return true;
case if_expr: /* 'if' '(' <expr> ')' <stmt> */
- ps.p_stack[--ps.tos] = if_expr_stmt;
+ ps.s_ttype[--ps.tos] = if_expr_stmt;
int i = ps.tos - 1;
- while (ps.p_stack[i] != stmt &&
- ps.p_stack[i] != stmt_list &&
- ps.p_stack[i] != lbrace)
+ while (ps.s_ttype[i] != stmt &&
+ ps.s_ttype[i] != stmt_list &&
+ ps.s_ttype[i] != lbrace)
--i;
- ps.ind_level_follow = ps.il[i];
+ ps.ind_level_follow = ps.s_ind_level[i];
/*
* for the time being, we will assume that there is no else on this
* if, and set the indentation level accordingly. If an 'else' is
@@ -255,14 +256,14 @@
return true;
case switch_expr: /* 'switch' '(' <expr> ')' <stmt> */
- case_ind = ps.cstk[ps.tos - 1];
Home |
Main Index |
Thread Index |
Old Index