Module Name: src Committed By: rillig Date: Sat Jun 3 21:24:26 UTC 2023
Modified Files: src/usr.bin/indent: indent.c parse.c Log Message: indent: clean up handling of brace indentation No functional change. To generate a diff of this commit: cvs rdiff -u -r1.315 -r1.316 src/usr.bin/indent/indent.c cvs rdiff -u -r1.63 -r1.64 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/indent.c diff -u src/usr.bin/indent/indent.c:1.315 src/usr.bin/indent/indent.c:1.316 --- src/usr.bin/indent/indent.c:1.315 Fri Jun 2 15:07:46 2023 +++ src/usr.bin/indent/indent.c Sat Jun 3 21:24:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.315 2023/06/02 15:07:46 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.316 2023/06/03 21:24:26 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.315 2023/06/02 15:07:46 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.316 2023/06/03 21:24:26 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -534,7 +534,7 @@ process_lparen_or_lbracket(void) /* this is a kluge to make sure that declarations will be * aligned right if proc decl has an explicit type on it, i.e. * "int a(x) {..." */ - parse(psym_0); + parse(psym_stmt); ps.init_or_struct = false; } @@ -724,7 +724,7 @@ process_semicolon(void) ps.decl_ind = 0; if (ps.spaced_expr_psym == psym_0) { - parse(psym_0); /* let parser know about end of stmt */ + parse(psym_stmt); ps.force_nl = true; } } Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.63 src/usr.bin/indent/parse.c:1.64 --- src/usr.bin/indent/parse.c:1.63 Fri Jun 2 11:43:07 2023 +++ src/usr.bin/indent/parse.c Sat Jun 3 21:24:26 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.64 2023/06/03 21:24:26 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: parse.c,v 1.63 2023/06/02 11:43:07 rillig Exp $"); +__RCSID("$NetBSD: parse.c,v 1.64 2023/06/03 21:24:26 rillig Exp $"); #include <err.h> @@ -56,6 +56,20 @@ decl_level(void) return level; } +static void +ps_push(parser_symbol psym) +{ + ps.s_sym[++ps.tos] = psym; + ps.s_ind_level[ps.tos] = 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; +} + /* * Shift the token onto the parser stack, or reduce it by combining it with * previous tokens. @@ -80,27 +94,20 @@ parse(parser_symbol psym) break; /* only put one declaration onto stack */ ps.break_after_comma = true; - ps.s_sym[++ps.tos] = psym_decl; - ps.s_ind_level[ps.tos] = ps.ind_level_follow; + ps_push_follow(psym_decl); if (opt.ljust_decl) ps.ind_level_follow = ps.ind_level = decl_level(); break; case psym_if_expr: - if (ps.s_sym[ps.tos] == psym_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. */ + if (ps.s_sym[ps.tos] == psym_if_expr_stmt_else && opt.else_if) ps.ind_level_follow = ps.s_ind_level[ps.tos--]; - } /* FALLTHROUGH */ case psym_do: case psym_for_exprs: - ps.s_sym[++ps.tos] = psym; - ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow; - ++ps.ind_level_follow; + ps.ind_level = ps.ind_level_follow++; + ps_push(psym); break; case psym_lbrace: @@ -125,60 +132,52 @@ parse(parser_symbol psym) } } - ps.s_sym[++ps.tos] = psym_lbrace; - ps.s_ind_level[ps.tos] = ps.ind_level; - ps.s_sym[++ps.tos] = psym_stmt; - ps.s_ind_level[ps.tos] = ps.ind_level_follow; + ps_push(psym_lbrace); + ps_push_follow(psym_stmt); break; case psym_while_expr: if (ps.s_sym[ps.tos] == psym_do_stmt) { - /* it is matched with do stmt */ ps.ind_level = ps.ind_level_follow = ps.s_ind_level[ps.tos]; - ps.s_sym[++ps.tos] = psym_while_expr; - ps.s_ind_level[ps.tos] = ps.ind_level; - - } else { /* it is a while loop */ - ps.s_sym[++ps.tos] = psym_while_expr; - ps.s_ind_level[ps.tos] = ps.ind_level_follow; + ps_push(psym_while_expr); + } else { + ps_push_follow(psym_while_expr); ++ps.ind_level_follow; } break; case psym_else: - if (ps.s_sym[ps.tos] != psym_if_expr_stmt) + if (ps.s_sym[ps.tos] != psym_if_expr_stmt) { diag(1, "Unmatched 'else'"); - else { - ps.ind_level = ps.s_ind_level[ps.tos]; - ps.ind_level_follow = ps.ind_level + 1; - ps.s_sym[ps.tos] = psym_if_expr_stmt_else; + break; } + ps.ind_level = ps.s_ind_level[ps.tos]; + ps.ind_level_follow = ps.ind_level + 1; + ps.s_sym[ps.tos] = psym_if_expr_stmt_else; break; case psym_rbrace: /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ - if (ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace) { - ps.ind_level = ps.ind_level_follow - = ps.s_ind_level[--ps.tos]; - ps.s_sym[ps.tos] = psym_stmt; - } else + if (!(ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace)) { 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; break; case psym_switch_expr: - ps.s_sym[++ps.tos] = psym_switch_expr; + ps_push_follow(psym_switch_expr); ps.s_case_ind_level[ps.tos] = case_ind; - ps.s_ind_level[ps.tos] = ps.ind_level_follow; case_ind = (float)ps.ind_level_follow + opt.case_indent; ps.ind_level_follow += (int)opt.case_indent + 1; break; - case psym_0: /* a simple statement */ + case psym_stmt: ps.break_after_comma = false; - ps.s_sym[++ps.tos] = psym_stmt; - ps.s_ind_level[ps.tos] = ps.ind_level; + ps_push(psym_stmt); break; default: