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: extract the stack of parser symbols t...



details:   https://anonhg.NetBSD.org/src/rev/95305f08edd3
branches:  trunk
changeset: 376263:95305f08edd3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Wed Jun 07 15:46:11 2023 +0000

description:
indent: extract the stack of parser symbols to a separate struct

No functional change.

diffstat:

 usr.bin/indent/debug.c  |  10 +++--
 usr.bin/indent/indent.c |  25 ++++++------
 usr.bin/indent/indent.h |  12 ++++--
 usr.bin/indent/io.c     |  17 ++++----
 usr.bin/indent/lexi.c   |   6 +-
 usr.bin/indent/parse.c  |  97 +++++++++++++++++++++++++-----------------------
 6 files changed, 89 insertions(+), 78 deletions(-)

diffs (truncated from 481 to 300 lines):

diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/debug.c    Wed Jun 07 15:46:11 2023 +0000
@@ -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
diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/indent.c   Wed Jun 07 15:46:11 2023 +0000
@@ -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();
        }
diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/indent.h   Wed Jun 07 15:46:11 2023 +0000
@@ -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 */
 
diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/io.c       Wed Jun 07 15:46:11 2023 +0000
@@ -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;
                }
diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/lexi.c     Wed Jun 07 15:46:11 2023 +0000
@@ -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()) {
diff -r a254848508d8 -r 95305f08edd3 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Wed Jun 07 15:25:08 2023 +0000
+++ b/usr.bin/indent/parse.c    Wed Jun 07 15:46:11 2023 +0000
@@ -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 @@
  * 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];



Home | Main Index | Thread Index | Old Index