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: condense code for handling spaced exp...
details: https://anonhg.NetBSD.org/src/rev/466b15de4ded
branches: trunk
changeset: 374777:466b15de4ded
user: rillig <rillig%NetBSD.org@localhost>
date: Fri May 12 08:40:54 2023 +0000
description:
indent: condense code for handling spaced expressions
No functional change outside debug mode.
diffstat:
usr.bin/indent/indent.c | 54 +++++++++++++++++++++---------------------------
usr.bin/indent/indent.h | 26 +++++++----------------
usr.bin/indent/lexi.c | 13 ++++++++---
usr.bin/indent/parse.c | 21 ++----------------
4 files changed, 44 insertions(+), 70 deletions(-)
diffs (truncated from 307 to 300 lines):
diff -r d0471ff16c3a -r 466b15de4ded usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Fri May 12 02:26:48 2023 +0000
+++ b/usr.bin/indent/indent.c Fri May 12 08:40:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.252 2023/05/11 19:01:35 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.253 2023/05/12 08:40:54 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.252 2023/05/11 19:01:35 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.253 2023/05/12 08:40:54 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif
@@ -492,7 +492,8 @@ process_lparen_or_lbracket(void)
debug_println("paren_indents[%d] is now %d",
ps.nparen - 1, ps.paren[ps.nparen - 1].indent);
- if (ps.spaced_expr && ps.nparen == 1 && opt.extra_expr_indent
+ if (ps.spaced_expr_psym != psym_semicolon
+ && ps.nparen == 1 && opt.extra_expr_indent
&& ps.paren[0].indent < 2 * opt.indent_size) {
ps.paren[0].indent = (short)(2 * opt.indent_size);
debug_println("paren_indents[0] is now %d", ps.paren[0].indent);
@@ -534,14 +535,12 @@ process_rparen_or_rbracket(void)
*code.e++ = token.s[0];
- if (ps.spaced_expr && ps.nparen == 0) {
- ps.spaced_expr = false;
+ if (ps.spaced_expr_psym != psym_semicolon && ps.nparen == 0) {
ps.force_nl = true;
ps.next_unary = true;
- ps.in_stmt_or_decl = false; /* don't use stmt continuation
- * indentation */
-
- parse_stmt_head(ps.hd);
+ ps.in_stmt_or_decl = false;
+ parse(ps.spaced_expr_psym);
+ ps.spaced_expr_psym = psym_semicolon;
}
}
@@ -652,24 +651,23 @@ process_semicolon(void)
* structure declaration before, we
* aren't anymore */
- if ((!ps.spaced_expr || ps.hd != hd_for) && ps.nparen > 0) {
-
+ if (ps.nparen > 0 && ps.spaced_expr_psym != psym_for_exprs) {
/*
* There were unbalanced parentheses in the statement. It is a bit
* complicated, because the semicolon might be in a for statement.
*/
diag(1, "Unbalanced parentheses");
ps.nparen = 0;
- if (ps.spaced_expr) {
- ps.spaced_expr = false;
- parse_stmt_head(ps.hd);
+ if (ps.spaced_expr_psym != psym_semicolon) {
+ parse(ps.spaced_expr_psym);
+ ps.spaced_expr_psym = psym_semicolon;
}
}
*code.e++ = ';';
ps.want_blank = true;
ps.in_stmt_or_decl = ps.nparen > 0;
- if (!ps.spaced_expr) {
+ if (ps.spaced_expr_psym == psym_semicolon) {
parse(psym_semicolon); /* let parser know about end of stmt */
ps.force_nl = true;
}
@@ -705,9 +703,9 @@ process_lbrace(void)
if (ps.nparen > 0) {
diag(1, "Unbalanced parentheses");
ps.nparen = 0;
- if (ps.spaced_expr) {
- ps.spaced_expr = false;
- parse_stmt_head(ps.hd);
+ if (ps.spaced_expr_psym != psym_semicolon) {
+ parse(ps.spaced_expr_psym);
+ ps.spaced_expr_psym = psym_semicolon;
ps.ind_level = ps.ind_level_follow;
}
}
@@ -749,7 +747,7 @@ process_rbrace(void)
if (ps.nparen > 0) { /* check for unclosed if, for, else. */
diag(1, "Unbalanced parentheses");
ps.nparen = 0;
- ps.spaced_expr = false;
+ ps.spaced_expr_psym = psym_semicolon;
}
ps.just_saw_decl = 0;
@@ -863,12 +861,12 @@ process_ident(lexer_symbol lsym)
ps.want_blank = false;
}
- } else if (ps.spaced_expr && ps.nparen == 0) {
- ps.spaced_expr = false;
+ } else if (ps.spaced_expr_psym != psym_semicolon && ps.nparen == 0) {
ps.force_nl = true;
ps.next_unary = true;
ps.in_stmt_or_decl = false;
- parse_stmt_head(ps.hd);
+ parse(ps.spaced_expr_psym);
+ ps.spaced_expr_psym = psym_semicolon;
}
}
@@ -1102,23 +1100,19 @@ main_loop(void)
break;
case lsym_switch:
- ps.spaced_expr = true;
- ps.hd = hd_switch;
+ ps.spaced_expr_psym = psym_switch_expr;
goto copy_token;
case lsym_for:
- ps.spaced_expr = true;
- ps.hd = hd_for;
+ ps.spaced_expr_psym = psym_for_exprs;
goto copy_token;
case lsym_if:
- ps.spaced_expr = true;
- ps.hd = hd_if;
+ ps.spaced_expr_psym = psym_if_expr;
goto copy_token;
case lsym_while:
- ps.spaced_expr = true;
- ps.hd = hd_while;
+ ps.spaced_expr_psym = psym_while_expr;
goto copy_token;
case lsym_do:
diff -r d0471ff16c3a -r 466b15de4ded usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h Fri May 12 02:26:48 2023 +0000
+++ b/usr.bin/indent/indent.h Fri May 12 08:40:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.119 2023/05/11 19:01:35 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.120 2023/05/12 08:40:54 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -127,14 +127,6 @@ typedef enum parser_symbol {
psym_while_expr, /* 'while' '(' expr ')' */
} parser_symbol;
-typedef enum stmt_head {
- hd_0, /* placeholder for uninitialized */
- hd_for,
- hd_if,
- hd_switch,
- hd_while,
-} stmt_head;
-
/* A range of characters, in some cases null-terminated. */
struct buffer {
char *s; /* start of the usable text */
@@ -342,11 +334,11 @@ extern struct parser_state {
int s_ind_level[STACKSIZE];
float s_case_ind_level[STACKSIZE];
- stmt_head hd; /* the type of statement for 'if (...)', 'for
- * (...)', etc */
- bool spaced_expr; /* whether we are in a parenthesized expression
- * that should be surrounded by spaces, such as
- * in 'if', 'while', 'switch'. */
+ parser_symbol spaced_expr_psym; /* the parser symbol to be shifted
+ * after the parenthesized expression
+ * from a 'for', 'if', 'switch' or
+ * 'while'; or psym_semicolon */
+
int quest_level; /* when this is positive, we have seen a '?'
* without the matching ':' in a '?:'
* expression */
@@ -363,11 +355,10 @@ extern struct parser_state {
#define array_length(array) (sizeof(array) / sizeof((array)[0]))
#ifdef debug
-void
-debug_vis_range(const char *, const char *, const char *,
- const char *);
+void debug_vis_range(const char *, const char *, const char *, const char *);
void debug_printf(const char *, ...)__printflike(1, 2);
void debug_println(const char *, ...)__printflike(1, 2);
+const char *psym_name(parser_symbol);
#else
#define debug_printf(fmt, ...) do { } while (false)
#define debug_println(fmt, ...) do { } while (false)
@@ -395,7 +386,6 @@ void output_line(void);
void output_line_ff(void);
void inp_read_line(void);
void parse(parser_symbol);
-void parse_stmt_head(stmt_head);
void process_comment(void);
void set_option(const char *, const char *);
void load_profiles(const char *);
diff -r d0471ff16c3a -r 466b15de4ded usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c Fri May 12 02:26:48 2023 +0000
+++ b/usr.bin/indent/lexi.c Fri May 12 08:40:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.175 2023/05/11 11:25:47 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.176 2023/05/12 08:40:54 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 (
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.175 2023/05/11 11:25:47 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.176 2023/05/12 08:40:54 rillig Exp $");
#elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
#endif
@@ -261,6 +261,12 @@ debug_full_parser_state(void)
debug_println("%3d -> %3d ps." #name, prev_ps.name, ps.name); \
else if (debug_full_parser_state()) \
debug_println(" %3d ps." #name, ps.name)
+#define debug_ps_enum(name, repr) \
+ if (ps.name != prev_ps.name) \
+ debug_println("%3s -> %3s ps." #name, \
+ repr(prev_ps.name), repr(ps.name)); \
+ else if (debug_full_parser_state()) \
+ debug_println("%10s ps." #name, repr(ps.name))
static bool
ps_paren_has_changed(const struct parser_state *prev_ps)
@@ -351,8 +357,7 @@ debug_lexi(lexer_symbol lsym)
// The debug output for the parser symbols is done in 'parse' instead.
- // No debug output for hd.
- debug_ps_bool(spaced_expr);
+ debug_ps_enum(spaced_expr_psym, psym_name);
debug_ps_int(quest_level);
prev_ps = ps;
diff -r d0471ff16c3a -r 466b15de4ded usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c Fri May 12 02:26:48 2023 +0000
+++ b/usr.bin/indent/parse.c Fri May 12 08:40:54 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.50 2023/05/11 09:28:53 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.51 2023/05/12 08:40:54 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -43,12 +43,11 @@ static char sccsid[] = "@(#)parse.c 8.1
#include <sys/cdefs.h>
#if defined(__NetBSD__)
-__RCSID("$NetBSD: parse.c,v 1.50 2023/05/11 09:28:53 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.51 2023/05/12 08:40:54 rillig Exp $");
#else
__FBSDID("$FreeBSD: head/usr.bin/indent/parse.c 337651 2018-08-11 19:20:06Z pstef $");
#endif
-#include <assert.h>
#include <err.h>
#include <stdio.h>
@@ -57,7 +56,7 @@ static char sccsid[] = "@(#)parse.c 8.1
static void reduce(void);
#ifdef debug
-static const char *
+const char *
psym_name(parser_symbol psym)
{
static const char *const name[] = {
@@ -78,8 +77,6 @@ psym_name(parser_symbol psym)
"while_expr",
};
- assert(array_length(name) == (int)psym_while_expr + 1);
-
return name[psym];
}
#endif
@@ -235,18 +232,6 @@ parse(parser_symbol psym)
#endif
}
-void
-parse_stmt_head(stmt_head hd)
-{
- static const parser_symbol psym[] = {
- [hd_for] = psym_for_exprs,
- [hd_if] = psym_if_expr,
- [hd_switch] = psym_switch_expr,
- [hd_while] = psym_while_expr
Home |
Main Index |
Thread Index |
Old Index