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: split kw_do_or_else into separate con...



details:   https://anonhg.NetBSD.org/src/rev/662be84f918b
branches:  trunk
changeset: 990051:662be84f918b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Oct 24 22:28:06 2021 +0000

description:
indent: split kw_do_or_else into separate constants

It was unnecessarily confusing to have the token types keyword_do_else,
keyword_do and keyword_else at the same time, without any hint in what
they differed.

Some of the token types seem to be used by the lexer while others are
used in the parse stack. Maybe all token types can be partitioned into
these groups, which would suggest to use two different types for them.
And if not, it's still clearer to have this distinction in the names of
the constants.

No functional change.

diffstat:

 tests/usr.bin/indent/token_lparen.c |  18 +++++++++---------
 usr.bin/indent/indent.c             |  22 +++++++++++-----------
 usr.bin/indent/indent.h             |  12 +++++++-----
 usr.bin/indent/lexi.c               |  20 ++++++++++++--------
 usr.bin/indent/parse.c              |  10 +++++-----
 5 files changed, 44 insertions(+), 38 deletions(-)

diffs (263 lines):

diff -r 81fba96e0d2c -r 662be84f918b tests/usr.bin/indent/token_lparen.c
--- a/tests/usr.bin/indent/token_lparen.c       Sun Oct 24 21:55:07 2021 +0000
+++ b/tests/usr.bin/indent/token_lparen.c       Sun Oct 24 22:28:06 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: token_lparen.c,v 1.4 2021/10/24 17:19:49 rillig Exp $ */
+/* $NetBSD: token_lparen.c,v 1.5 2021/10/24 22:28:06 rillig Exp $ */
 /* $FreeBSD$ */
 
 /*
@@ -133,7 +133,7 @@
        /* $ XXX: form_feed should be skipped, just as newline. */
        (form_feed)();         /* XXX: should be skipped */
        for(;;);
-       do(keyword_do_else)=3;while(0);
+       do(tt_lex_do)=3;while(0);
        // $ TODO: is if_expr possible?
        if(cond)(if_expr)();
        // $ TODO: is while_expr possible?
@@ -144,10 +144,10 @@
        (stmt);
        // $ TODO: is stmt_list possible?
        (stmt_list);
-       // $ TODO: is keyword_else possible? keyword_do_else is.
-       if(cond);else(keyword_else)();
-       // $ TODO: is keyword_do possible? keyword_do_else is.
-       do(keyword_do);while(0);
+       // $ TODO: is tt_ps_else possible? tt_lex_else is.
+       if(cond);else(tt_ps_else)();
+       // $ TODO: is tt_ps_do possible? tt_lex_do is.
+       do(tt_ps_do);while(0);
        // The following line would generate 'Statement nesting error'.
        // do stmt;(do_stmt());while(0);
        // $ TODO: is if_expr_stmt possible?
@@ -200,7 +200,7 @@
        (form_feed)();          /* XXX: should be skipped */
        for (;;);
        do
-               (keyword_do_else) = 3;
+               (tt_lex_do) = 3;
        while (0);
        if (cond)
                (if_expr)();
@@ -212,9 +212,9 @@
        (stmt_list);
        if (cond);
        else
-               (keyword_else)();
+               (tt_ps_else)();
        do
-               (keyword_do);
+               (tt_ps_do);
        while (0);
        // The following line would generate 'Statement nesting error'.
        // do stmt;(do_stmt());while(0);
diff -r 81fba96e0d2c -r 662be84f918b usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Oct 24 21:55:07 2021 +0000
+++ b/usr.bin/indent/indent.c   Sun Oct 24 22:28:06 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.152 2021/10/24 20:57:11 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.153 2021/10/24 22:28:06 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.152 2021/10/24 20:57:11 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.153 2021/10/24 22:28:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -246,8 +246,7 @@
 
     remove_newlines =
            /* "} else" */
-           (ttype == keyword_do_else && *token.s == 'e' &&
-            code.e != code.s && code.e[-1] == '}')
+           (ttype == tt_lex_else && code.e != code.s && code.e[-1] == '}')
            /* "else if" */
            || (ttype == keyword_for_if_while &&
                *token.s == 'i' && last_else && opt.else_if);
@@ -1055,7 +1054,7 @@
 
     *force_nl = true;          /* following stuff must go onto new line */
     *last_else = false;
-    parse(keyword_do);
+    parse(tt_ps_do);
 }
 
 static void
@@ -1072,7 +1071,7 @@
 
     *force_nl = true;          /* following stuff must go onto new line */
     *last_else = true;
-    parse(keyword_else);
+    parse(tt_ps_else);
 }
 
 static void
@@ -1447,11 +1446,12 @@
            /* remember the type of header for later use by parser */
            goto copy_token;
 
-       case keyword_do_else:
-           if (*token.s == 'd')
-               process_keyword_do(&force_nl, &last_else);
-           else
-               process_keyword_else(&force_nl, &last_else);
+       case tt_lex_do:
+           process_keyword_do(&force_nl, &last_else);
+           goto copy_token;
+
+       case tt_lex_else:
+           process_keyword_else(&force_nl, &last_else);
            goto copy_token;
 
        case type_def:
diff -r 81fba96e0d2c -r 662be84f918b usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sun Oct 24 21:55:07 2021 +0000
+++ b/usr.bin/indent/indent.h   Sun Oct 24 22:28:06 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.45 2021/10/24 11:19:25 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.46 2021/10/24 22:28:06 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -92,14 +92,15 @@
     form_feed,
     decl,
     keyword_for_if_while,      /* 'for', 'if' or 'while' */
-    keyword_do_else,           /* 'do' or 'else' */
+    tt_lex_do,
+    tt_lex_else,
     if_expr,                   /* 'if' '(' <expr> ')' */
     while_expr,                        /* 'while' '(' <expr> ')' */
     for_exprs,                 /* 'for' '(' ... ')' */
     stmt,
     stmt_list,
-    keyword_else,              /* 'else' */
-    keyword_do,                        /* 'do' */
+    tt_ps_else,
+    tt_ps_do,
     do_stmt,                   /* 'do' <stmt> */
     if_expr_stmt,              /* 'if' '(' <expr> ')' <stmt> */
     if_expr_stmt_else,         /* 'if' '(' <expr> ')' <stmt> 'else' */
@@ -225,7 +226,8 @@
     kw_struct_or_union_or_enum,
     kw_type,
     kw_for_or_if_or_while,
-    kw_do_or_else,
+    kw_do,
+    kw_else,
     kw_switch,
     kw_case_or_default,
     kw_jump,
diff -r 81fba96e0d2c -r 662be84f918b usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Oct 24 21:55:07 2021 +0000
+++ b/usr.bin/indent/lexi.c     Sun Oct 24 22:28:06 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.96 2021/10/24 20:47:00 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.97 2021/10/24 22:28:06 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.96 2021/10/24 20:47:00 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.97 2021/10/24 22:28:06 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -74,9 +74,9 @@
     {"const", kw_type},
     {"continue", kw_jump},
     {"default", kw_case_or_default},
-    {"do", kw_do_or_else},
+    {"do", kw_do},
     {"double", kw_type},
-    {"else", kw_do_or_else},
+    {"else", kw_else},
     {"enum", kw_struct_or_union_or_enum},
     {"extern", kw_storage_class},
     {"float", kw_type},
@@ -227,14 +227,15 @@
        "case_label", "colon",
        "semicolon", "lbrace", "rbrace", "ident", "comma",
        "comment", "switch_expr", "preprocessing", "form_feed", "decl",
-       "keyword_for_if_while", "keyword_do_else",
+       "keyword_for_if_while", "tt_lex_do", "tt_lex_else",
        "if_expr", "while_expr", "for_exprs",
-       "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt",
+       "stmt", "stmt_list", "tt_ps_else", "tt_ps_do", "do_stmt",
        "if_expr_stmt", "if_expr_stmt_else", "period", "string_prefix",
        "storage_class", "funcname", "type_def", "keyword_struct_union_enum"
     };
 
     assert(0 <= ttype && ttype < array_length(name));
+    assert(array_length(name) == (int)keyword_struct_union_enum + 1);
 
     return name[ttype];
 }
@@ -451,8 +452,11 @@
        case kw_for_or_if_or_while:
            return keyword_for_if_while;
 
-       case kw_do_or_else:
-           return keyword_do_else;
+       case kw_do:
+           return tt_lex_do;
+
+       case kw_else:
+           return tt_lex_else;
 
        case kw_storage_class:
            return storage_class;
diff -r 81fba96e0d2c -r 662be84f918b usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Sun Oct 24 21:55:07 2021 +0000
+++ b/usr.bin/indent/parse.c    Sun Oct 24 22:28:06 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.37 2021/10/24 19:14:33 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.38 2021/10/24 22:28:06 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -65,7 +65,7 @@
     debug_println("parse token: '%s' \"%s\"",
        token_type_name(ttype), token.s);
 
-    if (ttype != keyword_else) {
+    if (ttype != tt_ps_else) {
        while (ps.s_ttype[ps.tos] == if_expr_stmt) {
            ps.s_ttype[ps.tos] = stmt;
            reduce();
@@ -105,7 +105,7 @@
            ps.ind_level_follow = ps.s_ind_level[ps.tos--];
        }
        /* FALLTHROUGH */
-    case keyword_do:
+    case tt_ps_do:
     case for_exprs:            /* 'for' (...) */
        ps.s_ttype[++ps.tos] = ttype;
        ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow;
@@ -155,7 +155,7 @@
 
        break;
 
-    case keyword_else:
+    case tt_ps_else:
        if (ps.s_ttype[ps.tos] != if_expr_stmt)
            diag(1, "Unmatched 'else'");
        else {
@@ -235,7 +235,7 @@
        ps.s_ttype[--ps.tos] = stmt_list;
        return true;
 
-    case keyword_do:           /* 'do' <stmt> */
+    case tt_ps_do:             /* 'do' <stmt> */
        ps.s_ttype[--ps.tos] = do_stmt;
        ps.ind_level_follow = ps.s_ind_level[ps.tos];
        return true;



Home | Main Index | Thread Index | Old Index