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: make ps.keyword easier to understand



details:   https://anonhg.NetBSD.org/src/rev/7d997d3e35bb
branches:  trunk
changeset: 1024498:7d997d3e35bb
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Oct 26 20:43:35 2021 +0000

description:
indent: make ps.keyword easier to understand

Previously, ps.keyword did not have any documentation and was not
straight-forward. In some cases it was reset to kw_0, in others it was
set to an interesting value. The idea behind it was to remember the kind
of word of the previous token, to decide whether to have a space between
sizeof or offsetof and a following '('.

No functional change.

diffstat:

 usr.bin/indent/indent.c |  13 ++++++-------
 usr.bin/indent/indent.h |   5 +++--
 usr.bin/indent/lexi.c   |  23 ++++++++++++-----------
 3 files changed, 21 insertions(+), 20 deletions(-)

diffs (153 lines):

diff -r af83b9397c65 -r 7d997d3e35bb usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue Oct 26 20:37:26 2021 +0000
+++ b/usr.bin/indent/indent.c   Tue Oct 26 20:43:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.160 2021/10/26 19:36:30 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.161 2021/10/26 20:43:35 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.160 2021/10/26 19:36:30 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.161 2021/10/26 20:43:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -333,8 +333,7 @@
            inbuf_read_line();
     }
 
-    struct parser_state transient_state;
-    transient_state = ps;
+    struct parser_state transient_state = ps;
     *lsym = lexi(&transient_state);    /* read another token */
     if (*lsym != lsym_newline && *lsym != lsym_form_feed &&
        *lsym != lsym_comment && !transient_state.search_stmt) {
@@ -707,9 +706,9 @@
        return true;
     if (opt.proc_calls_space)
        return true;
-    if (ps.keyword == kw_sizeof)
+    if (ps.prev_keyword == kw_sizeof)
        return opt.blank_after_sizeof;
-    return ps.keyword != kw_0 && ps.keyword != kw_offsetof;
+    return ps.prev_keyword != kw_0 && ps.prev_keyword != kw_offsetof;
 }
 
 static void
@@ -753,7 +752,7 @@
     }
 
     /* parenthesized type following sizeof or offsetof is not a cast */
-    if (ps.keyword == kw_offsetof || ps.keyword == kw_sizeof)
+    if (ps.prev_keyword == kw_offsetof || ps.prev_keyword == kw_sizeof)
        ps.not_cast_mask |= 1 << ps.p_l_follow;
 }
 
diff -r af83b9397c65 -r 7d997d3e35bb usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Tue Oct 26 20:37:26 2021 +0000
+++ b/usr.bin/indent/indent.h   Tue Oct 26 20:43:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.51 2021/10/26 19:36:30 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.52 2021/10/26 20:43:35 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -332,7 +332,8 @@
     bool want_blank;           /* whether the following token should be
                                 * prefixed by a blank. (Said prefixing is
                                 * ignored in some cases.) */
-    enum keyword_kind keyword;
+    enum keyword_kind prev_keyword;
+    enum keyword_kind curr_keyword;
     bool dumped_decl_indent;
     bool in_parameter_declaration;
     char procname[100];                /* The name of the current procedure */
diff -r af83b9397c65 -r 7d997d3e35bb usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue Oct 26 20:37:26 2021 +0000
+++ b/usr.bin/indent/lexi.c     Tue Oct 26 20:43:35 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.104 2021/10/26 20:17:42 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.105 2021/10/26 20:43:35 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.104 2021/10/26 20:17:42 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.105 2021/10/26 20:43:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -305,8 +305,10 @@
     debug_print_buf("code", &code);
     debug_print_buf("comment", &com);
     debug_printf("lexi returns '%s'", lsym_name(lsym));
-    if (state->keyword != kw_0)
-       debug_printf(" keyword '%s'", kw_name(state->keyword));
+    if (state->curr_keyword != kw_0)
+       debug_printf(" keyword '%s'", kw_name(state->curr_keyword));
+    if (state->prev_keyword != kw_0)
+       debug_printf(" previous keyword '%s'", kw_name(state->prev_keyword));
     debug_println("");
     debug_print_buf("token", &token);
 }
@@ -455,7 +457,6 @@
 
     while (is_hspace(inbuf_peek()))
        inbuf_skip();
-    state->keyword = kw_0;
 
     if (state->last_token == lsym_tag && state->p_l_follow == 0) {
        state->next_unary = true;
@@ -469,13 +470,13 @@
        array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
     if (kw == NULL) {
        if (is_typename()) {
-           state->keyword = kw_type;
+           state->curr_keyword = kw_type;
            state->next_unary = true;
            goto found_typename;
        }
 
     } else {                   /* we have a keyword */
-       state->keyword = kw->kind;
+       state->curr_keyword = kw->kind;
        state->next_unary = true;
 
        switch (kw->kind) {
@@ -493,10 +494,8 @@
                state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
            }
            if (state->last_token == lsym_period ||
-                   state->last_token == lsym_unary_op) {
-               state->keyword = kw_0;
+                   state->last_token == lsym_unary_op)
                break;
-           }
            if (kw != NULL && kw->kind == kw_struct_or_union_or_enum)
                return lsym_tag;
            if (state->p_l_follow != 0)
@@ -544,7 +543,7 @@
 not_proc:;
 
     } else if (probably_typename(state)) {
-       state->keyword = kw_type;
+       state->curr_keyword = kw_type;
        state->next_unary = true;
        return lsym_type;
     }
@@ -563,6 +562,8 @@
     token.e = token.s;
     state->col_1 = state->last_nl;
     state->last_nl = false;
+    state->prev_keyword = state->curr_keyword;
+    state->curr_keyword = kw_0;
 
     while (is_hspace(*inp.s)) {
        state->col_1 = false;



Home | Main Index | Thread Index | Old Index