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_state.p_l_follow and pa...



details:   https://anonhg.NetBSD.org/src/rev/9c06a36ca80f
branches:  trunk
changeset: 361536:9c06a36ca80f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 13 12:43:26 2022 +0000

description:
indent: rename parser_state.p_l_follow and paren_level

The previous variable names were misleading.

Paren_level is not the current level of parentheses but the one from the
beginning of the current output line.  For better accuracy, rename it to
line_start_paren_level.

P_l_follow is not the level of parentheses that will be active at some
point in the future, as the previous name suggested.  Instead, it is the
level of parentheses right now.  For better accuracy, rename it to
nparen.  This nicely matches its main usage, which is as index to the
parser_state.paren array.

No binary change.

diffstat:

 usr.bin/indent/indent.c |  64 ++++++++++++++++++++++++------------------------
 usr.bin/indent/indent.h |  17 +++++++-----
 usr.bin/indent/io.c     |  16 ++++++------
 usr.bin/indent/lexi.c   |  32 ++++++++++++------------
 4 files changed, 66 insertions(+), 63 deletions(-)

diffs (truncated from 423 to 300 lines):

diff -r bfff325d46d8 -r 9c06a36ca80f usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Feb 13 12:24:24 2022 +0000
+++ b/usr.bin/indent/indent.c   Sun Feb 13 12:43:26 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.241 2022/02/13 12:20:09 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.242 2022/02/13 12:43:26 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.241 2022/02/13 12:20:09 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.242 2022/02/13 12:43:26 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -650,7 +650,7 @@
 static void
 process_newline(void)
 {
-    if (ps.prev_token == lsym_comma && ps.p_l_follow == 0 && !ps.block_init &&
+    if (ps.prev_token == lsym_comma && ps.nparen == 0 && !ps.block_init &&
        !opt.break_after_comma && break_comma &&
        com.s == com.e)
        goto stay_in_line;
@@ -683,15 +683,15 @@
 static void
 process_lparen_or_lbracket(int decl_ind, bool tabs_to_var, bool spaced_expr)
 {
-    if (++ps.p_l_follow == array_length(ps.paren)) {
+    if (++ps.nparen == array_length(ps.paren)) {
        diag(0, "Reached internal limit of %zu unclosed parentheses",
            array_length(ps.paren));
-       ps.p_l_follow--;
+       ps.nparen--;
     }
 
     if (token.s[0] == '(' && ps.in_decl
        && !ps.block_init && !ps.decl_indent_done &&
-       !ps.is_function_definition && ps.paren_level == 0) {
+       !ps.is_function_definition && ps.line_start_nparen == 0) {
        /* function pointer declarations */
        code_add_decl_indent(decl_ind, tabs_to_var);
        ps.decl_indent_done = true;
@@ -700,11 +700,11 @@
     ps.want_blank = false;
     *code.e++ = token.s[0];
 
-    ps.paren[ps.p_l_follow - 1].indent = (short)ind_add(0, code.s, code.e);
+    ps.paren[ps.nparen - 1].indent = (short)ind_add(0, code.s, code.e);
     debug_println("paren_indents[%d] is now %d",
-       ps.p_l_follow - 1, ps.paren[ps.p_l_follow - 1].indent);
+       ps.nparen - 1, ps.paren[ps.nparen - 1].indent);
 
-    if (spaced_expr && ps.p_l_follow == 1 && opt.extra_expr_indent
+    if (spaced_expr && 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);
@@ -721,32 +721,32 @@
 
     /* parenthesized type following sizeof or offsetof is not a cast */
     if (ps.prev_token == lsym_offsetof || ps.prev_token == lsym_sizeof)
-       ps.paren[ps.p_l_follow - 1].no_cast = true;
+       ps.paren[ps.nparen - 1].no_cast = true;
 }
 
 static void
 process_rparen_or_rbracket(bool *spaced_expr, bool *force_nl, stmt_head hd)
 {
-    if (ps.paren[ps.p_l_follow - 1].maybe_cast &&
-       !ps.paren[ps.p_l_follow - 1].no_cast) {
+    if (ps.paren[ps.nparen - 1].maybe_cast &&
+       !ps.paren[ps.nparen - 1].no_cast) {
        ps.next_unary = true;
-       ps.paren[ps.p_l_follow - 1].maybe_cast = false;
+       ps.paren[ps.nparen - 1].maybe_cast = false;
        ps.want_blank = opt.space_after_cast;
     } else
        ps.want_blank = true;
-    ps.paren[ps.p_l_follow - 1].no_cast = false;
+    ps.paren[ps.nparen - 1].no_cast = false;
 
-    if (ps.p_l_follow > 0)
-       ps.p_l_follow--;
+    if (ps.nparen > 0)
+       ps.nparen--;
     else
        diag(0, "Extra '%c'", *token.s);
 
     if (code.e == code.s)      /* if the paren starts the line */
-       ps.paren_level = ps.p_l_follow; /* then indent it */
+       ps.line_start_nparen = ps.nparen;       /* then indent it */
 
     *code.e++ = token.s[0];
 
-    if (*spaced_expr && ps.p_l_follow == 0) {  /* check for end of 'if
+    if (*spaced_expr && ps.nparen == 0) {      /* check for end of 'if
                                                 * (...)', or some such */
        *spaced_expr = false;
        *force_nl = true;       /* must force newline after if */
@@ -778,7 +778,7 @@
 process_unary_op(int decl_ind, bool tabs_to_var)
 {
     if (!ps.decl_indent_done && ps.in_decl && !ps.block_init &&
-       !ps.is_function_definition && ps.paren_level == 0) {
+       !ps.is_function_definition && ps.line_start_nparen == 0) {
        /* pointer declarations */
        code_add_decl_indent(decl_ind - (int)buf_len(&token), tabs_to_var);
        ps.decl_indent_done = true;
@@ -861,7 +861,7 @@
     ps.just_saw_decl--;
 
     if (ps.in_decl && code.s == code.e && !ps.block_init &&
-       !ps.decl_indent_done && ps.paren_level == 0) {
+       !ps.decl_indent_done && ps.line_start_nparen == 0) {
        /* indent stray semicolons in declarations */
        code_add_decl_indent(decl_ind - 1, tabs_to_var);
        ps.decl_indent_done = true;
@@ -871,14 +871,14 @@
                                         * structure declaration before, we
                                         * aren't anymore */
 
-    if ((!*spaced_expr || hd != hd_for) && ps.p_l_follow > 0) {
+    if ((!*spaced_expr || hd != hd_for) && ps.nparen > 0) {
 
        /*
         * 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.p_l_follow = 0;
+       ps.nparen = 0;
        if (*spaced_expr) {     /* 'if', 'while', etc. */
            *spaced_expr = false;
            parse_stmt_head(hd);
@@ -886,7 +886,7 @@
     }
     *code.e++ = ';';
     ps.want_blank = true;
-    ps.in_stmt_or_decl = ps.p_l_follow > 0;
+    ps.in_stmt_or_decl = ps.nparen > 0;
 
     if (!*spaced_expr) {       /* if not if for (;;) */
        parse(psym_semicolon);  /* let parser know about end of stmt */
@@ -926,9 +926,9 @@
     if (ps.in_func_def_params)
        blank_line_before = false;
 
-    if (ps.p_l_follow > 0) {
+    if (ps.nparen > 0) {
        diag(1, "Unbalanced parentheses");
-       ps.p_l_follow = 0;
+       ps.nparen = 0;
        if (*spaced_expr) {     /* check for unclosed 'if', 'for', etc. */
            *spaced_expr = false;
            parse_stmt_head(hd);
@@ -972,9 +972,9 @@
        parse(psym_semicolon);
     }
 
-    if (ps.p_l_follow > 0) {   /* check for unclosed if, for, else. */
+    if (ps.nparen > 0) {       /* check for unclosed if, for, else. */
        diag(1, "Unbalanced parentheses");
-       ps.p_l_follow = 0;
+       ps.nparen = 0;
        *spaced_expr = false;
     }
 
@@ -1095,13 +1095,13 @@
            ps.want_blank = false;
 
        } else if (!ps.block_init && !ps.decl_indent_done &&
-           ps.paren_level == 0) {
+               ps.line_start_nparen == 0) {
            code_add_decl_indent(decl_ind, tabs_to_var);
            ps.decl_indent_done = true;
            ps.want_blank = false;
        }
 
-    } else if (*spaced_expr && ps.p_l_follow == 0) {
+    } else if (*spaced_expr && ps.nparen == 0) {
        *spaced_expr = false;
        *force_nl = true;
        ps.next_unary = true;
@@ -1134,7 +1134,7 @@
                                         * does not start the line */
 
     if (ps.in_decl && !ps.is_function_definition && !ps.block_init &&
-       !ps.decl_indent_done && ps.paren_level == 0) {
+           !ps.decl_indent_done && ps.line_start_nparen == 0) {
        /* indent leading commas and not the actual identifiers */
        code_add_decl_indent(decl_ind - 1, tabs_to_var);
        ps.decl_indent_done = true;
@@ -1142,7 +1142,7 @@
 
     *code.e++ = ',';
 
-    if (ps.p_l_follow == 0) {
+    if (ps.nparen == 0) {
        if (ps.block_init_level <= 0)
            ps.block_init = false;
        int varname_len = 8;    /* rough estimate for the length of a typical
@@ -1411,7 +1411,7 @@
            goto copy_token;
 
        case lsym_tag:
-           if (ps.p_l_follow > 0)
+           if (ps.nparen > 0)
                goto copy_token;
            /* FALLTHROUGH */
        case lsym_type_outside_parentheses:
diff -r bfff325d46d8 -r 9c06a36ca80f usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun Feb 13 12:24:24 2022 +0000
+++ b/usr.bin/indent/indent.h   Sun Feb 13 12:43:26 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.110 2022/02/13 12:20:09 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.111 2022/02/13 12:43:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -273,13 +273,16 @@
                                 * prefixed by a blank. (Said prefixing is
                                 * ignored in some cases.) */
 
-    int paren_level;           /* parenthesization level. used to indent
-                                * within statements, initializers and
-                                * declarations */
-    /* TODO: rename to next_line_paren_level */
-    int p_l_follow;            /* how to indent the remaining lines of the
-                                * statement or initializer or declaration */
+    int line_start_nparen;     /* the number of parentheses or brackets that
+                                * were already open at the beginning of the
+                                * current line; used to indent within
+                                * statements, initializers and declarations */
+    int nparen;                        /* the number of parentheses or brackets that
+                                * are currently open; used to indent the
+                                * remaining lines of the statement,
+                                * initializer or declaration */
     paren_level_props paren[20];
+
     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
diff -r bfff325d46d8 -r 9c06a36ca80f usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sun Feb 13 12:24:24 2022 +0000
+++ b/usr.bin/indent/io.c       Sun Feb 13 12:43:26 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.145 2022/02/13 12:20:09 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.146 2022/02/13 12:43:26 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.145 2022/02/13 12:20:09 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.146 2022/02/13 12:43:26 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -449,7 +449,7 @@
 {
 
     int target_ind = compute_code_indent();
-    for (int i = 0; i < ps.p_l_follow; i++) {
+    for (int i = 0; i < ps.nparen; i++) {
        if (ps.paren[i].indent >= 0) {
            int paren_ind = ps.paren[i].indent;
            ps.paren[i].indent = (short)(-1 - (paren_ind + target_ind));
@@ -571,11 +571,11 @@
     *(com.e = com.s = com.buf + 1) = '\0';
 
     ps.ind_level = ps.ind_level_follow;
-    ps.paren_level = ps.p_l_follow;
+    ps.line_start_nparen = ps.nparen;
 
-    if (ps.paren_level > 0) {
+    if (ps.line_start_nparen > 0) {
        /* TODO: explain what negative indentation means */
-       paren_indent = -1 - ps.paren[ps.paren_level - 1].indent;
+       paren_indent = -1 - ps.paren[ps.line_start_nparen - 1].indent;
        debug_println("paren_indent is now %d", paren_indent);
     }
 
@@ -617,7 +617,7 @@
 {
     int base_ind = ps.ind_level * opt.indent_size;
 
-    if (ps.paren_level == 0) {
+    if (ps.line_start_nparen == 0) {
        if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
            return base_ind + opt.continuation_indent;
        return base_ind;
@@ -632,7 +632,7 @@
     if (2 * opt.continuation_indent == opt.indent_size)
        return base_ind + opt.continuation_indent;



Home | Main Index | Thread Index | Old Index