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 token names more precise



details:   https://anonhg.NetBSD.org/src/rev/75899bab9b79
branches:  trunk
changeset: 953467:75899bab9b79
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 09 19:14:39 2021 +0000

description:
indent: make token names more precise

The previous 'casestmt' was wrong since a case label is not a statement
at all.

The previous 'swstmt' was overly short, and wrong as well, since it
represents only the 'switch (expr)' part, which is not a complete switch
statement.  Same for 'ifstmt', 'whilestmt', 'forstmt'.

The previous word 'head' was not precise enough since it didn't specify
exactly where the head ends and the body starts.  Especially for
handling the dangling else, this distinction is important.

No functional change.

diffstat:

 usr.bin/indent/indent.c       |  23 ++++++------
 usr.bin/indent/indent_codes.h |  28 +++++++-------
 usr.bin/indent/lexi.c         |  18 ++++----
 usr.bin/indent/parse.c        |  78 +++++++++++++++++++++---------------------
 4 files changed, 74 insertions(+), 73 deletions(-)

diffs (truncated from 369 to 300 lines):

diff -r fc2dc4822088 -r 75899bab9b79 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue Mar 09 18:28:10 2021 +0000
+++ b/usr.bin/indent/indent.c   Tue Mar 09 19:14:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.37 2021/03/09 18:28:10 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.38 2021/03/09 19:14:39 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.37 2021/03/09 18:28:10 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.38 2021/03/09 19:14:39 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -786,7 +786,7 @@
            ps.want_blank = true;
            break;
 
-       case casestmt:          /* got word 'case' or 'default' */
+       case case_label:        /* got word 'case' or 'default' */
            scase = true;       /* so we can process the later colon properly */
            goto copy_id;
 
@@ -853,7 +853,7 @@
                                                 * structure declaration, we
                                                 * arent any more */
 
-           if ((!sp_sw || hd_type != forstmt) && ps.p_l_follow > 0) {
+           if ((!sp_sw || hd_type != for_exprs) && ps.p_l_follow > 0) {
 
                /*
                 * This should be true iff there were unbalanced parens in the
@@ -981,23 +981,24 @@
            }
            prefix_blankline_requested = 0;
            parse(rbrace);      /* let parser know about this */
-           ps.search_brace = opt.cuddle_else && ps.p_stack[ps.tos] == ifhead
+           ps.search_brace = opt.cuddle_else
+               && ps.p_stack[ps.tos] == if_expr_stmt
                && ps.il[ps.tos] >= ps.ind_level;
            if (ps.tos <= 1 && opt.blanklines_after_procs && ps.dec_nest <= 0)
                postfix_blankline_requested = 1;
            break;
 
-       case swstmt:            /* got keyword "switch" */
+       case switch_expr:       /* got keyword "switch" */
            sp_sw = true;
-           hd_type = swstmt;   /* keep this for when we have seen the
+           hd_type = switch_expr; /* keep this for when we have seen the
                                 * expression */
            goto copy_id;       /* go move the token into buffer */
 
        case keyword_for_if_while:
            sp_sw = true;       /* the interesting stuff is done after the
                                 * expression is scanned */
-           hd_type = (*token == 'i' ? ifstmt :
-                      (*token == 'w' ? whilestmt : forstmt));
+           hd_type = (*token == 'i' ? if_expr :
+                      (*token == 'w' ? while_expr : for_exprs));
 
            /*
             * remember the type of header for later use by parser
@@ -1015,7 +1016,7 @@
                }
                force_nl = true;/* also, following stuff must go onto new line */
                last_else = 1;
-               parse(elselit);
+               parse(keyword_else);
            }
            else {
                if (e_code != s_code) { /* make sure this starts a line */
@@ -1026,7 +1027,7 @@
                }
                force_nl = true;/* also, following stuff must go onto new line */
                last_else = 0;
-               parse(dolit);
+               parse(keyword_do);
            }
            goto copy_id;       /* move the token into line */
 
diff -r fc2dc4822088 -r 75899bab9b79 usr.bin/indent/indent_codes.h
--- a/usr.bin/indent/indent_codes.h     Tue Mar 09 18:28:10 2021 +0000
+++ b/usr.bin/indent/indent_codes.h     Tue Mar 09 19:14:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent_codes.h,v 1.9 2021/03/09 18:28:10 rillig Exp $  */
+/*     $NetBSD: indent_codes.h,v 1.10 2021/03/09 19:14:39 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -49,7 +49,7 @@
     binary_op,                 /* e.g. '<<' or '+' or '&&' or '/=' */
     postop,                    /* trailing '++' or '--' */
     question,                  /* the '?' from a '?:' expression */
-    casestmt,
+    case_label,
     colon,
     semicolon,
     lbrace,
@@ -57,22 +57,22 @@
     ident,
     comma,
     comment,
-    swstmt,
+    switch_expr,               /* 'switch' '(' <expr> ')' */
     preesc,
     form_feed,
     decl,
-    keyword_for_if_while,
-    keyword_do_else,
-    ifstmt,
-    whilestmt,
-    forstmt,
+    keyword_for_if_while,      /* 'for', 'if' or 'while' */
+    keyword_do_else,           /* 'do' or 'else' */
+    if_expr,                   /* 'if' '(' <expr> ')' */
+    while_expr,                        /* 'while' '(' <expr> ')' */
+    for_exprs,                 /* 'for' '(' ... ')' */
     stmt,
-    stmtl,
-    elselit,
-    dolit,
-    dohead,
-    ifhead,
-    elsehead,
+    stmt_list,
+    keyword_else,              /* 'else' */
+    keyword_do,                        /* 'do' */
+    do_stmt,                   /* 'do' <stmt> */
+    if_expr_stmt,              /* 'if' '(' <expr> ')' <stmt> */
+    if_expr_stmt_else,         /* 'if' '(' <expr> ')' <stmt> 'else' */
     period,
     strpfx,
     storage,
diff -r fc2dc4822088 -r 75899bab9b79 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue Mar 09 18:28:10 2021 +0000
+++ b/usr.bin/indent/lexi.c     Tue Mar 09 19:14:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.29 2021/03/09 18:28:10 rillig Exp $ */
+/*     $NetBSD: lexi.c,v 1.30 2021/03/09 19:14:39 rillig Exp $ */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.29 2021/03/09 18:28:10 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.30 2021/03/09 19:14:39 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -204,13 +204,13 @@
 {
     static const char *const name[] = {
        "end_of_file", "newline", "lparen", "rparen", "unary_op",
-       "binary_op", "postop", "question", "casestmt", "colon",
+       "binary_op", "postop", "question", "case_label", "colon",
        "semicolon", "lbrace", "rbrace", "ident", "comma",
-       "comment", "swstmt", "preesc", "form_feed", "decl",
+       "comment", "switch_expr", "preesc", "form_feed", "decl",
        "keyword_for_if_while", "keyword_do_else",
-       "ifstmt", "whilestmt", "forstmt",
-       "stmt", "stmtl", "elselit", "dolit", "dohead",
-       "ifhead", "elsehead", "period", "strpfx", "storage",
+       "if_expr", "while_expr", "for_exprs",
+       "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt",
+       "if_expr_stmt", "if_expr_stmt_else", "period", "strpfx", "storage",
        "funcname", "type_def", "structure"
     };
 
@@ -367,9 +367,9 @@
            state->last_u_d = true;
            switch (p->rwcode) {
            case rw_switch:
-               return lexi_end(swstmt);
+               return lexi_end(switch_expr);
            case rw_case_or_default:
-               return lexi_end(casestmt);
+               return lexi_end(case_label);
            case rw_struct_or_union_or_enum:
            case rw_type:
            found_typename:
diff -r fc2dc4822088 -r 75899bab9b79 usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Tue Mar 09 18:28:10 2021 +0000
+++ b/usr.bin/indent/parse.c    Tue Mar 09 19:14:39 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.16 2021/03/09 18:21:01 rillig Exp $        */
+/*     $NetBSD: parse.c,v 1.17 2021/03/09 19:14:39 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -68,7 +68,7 @@
     printf("parse token: '%s' \"%s\"\n", token_type_name(tk), token);
 #endif
 
-    while (ps.p_stack[ps.tos] == ifhead && tk != elselit) {
+    while (ps.p_stack[ps.tos] == if_expr_stmt && tk != keyword_else) {
        /* true if we have an if without an else */
        ps.p_stack[ps.tos] = stmt;      /* apply the if(..) stmt ::= stmt
                                         * reduction */
@@ -101,18 +101,19 @@
        }
        break;
 
-    case ifstmt:               /* scanned if (...) */
-       if (ps.p_stack[ps.tos] == elsehead && opt.else_if) /* "else if ..." */
-               /*
-                * Note that the stack pointer here is decremented, effectively
-                * reducing "else if" to "if". This saves a lot of stack space
-                * in case of a long "if-else-if ... else-if" sequence.
-                */
-               ps.i_l_follow = ps.il[ps.tos--];
-       /* the rest is the same as for dolit and forstmt */
+    case if_expr:              /* 'if' '(' <expr> ')' */
+       if (ps.p_stack[ps.tos] == if_expr_stmt_else && opt.else_if) {
+           /*
+            * Note that the stack pointer here is decremented, effectively
+            * reducing "else if" to "if". This saves a lot of stack space
+            * in case of a long "if-else-if ... else-if" sequence.
+            */
+           ps.i_l_follow = ps.il[ps.tos--];
+       }
+       /* the rest is the same as for keyword_do and for_exprs */
        /* FALLTHROUGH */
-    case dolit:                        /* 'do' */
-    case forstmt:              /* for (...) */
+    case keyword_do:           /* 'do' */
+    case for_exprs:            /* 'for' (...) */
        ps.p_stack[++ps.tos] = tk;
        ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
        ++ps.i_l_follow;        /* subsequent statements should be indented 1 */
@@ -122,7 +123,7 @@
     case lbrace:               /* scanned { */
        break_comma = false;    /* don't break comma in an initial list */
        if (ps.p_stack[ps.tos] == stmt || ps.p_stack[ps.tos] == decl
-               || ps.p_stack[ps.tos] == stmtl)
+               || ps.p_stack[ps.tos] == stmt_list)
            ++ps.i_l_follow;    /* it is a random, isolated stmt group or a
                                 * declaration */
        else {
@@ -134,7 +135,7 @@
                /*
                 * it is a group as part of a while, for, etc.
                 */
-               if (ps.p_stack[ps.tos] == swstmt && opt.case_indent >= 1)
+               if (ps.p_stack[ps.tos] == switch_expr && opt.case_indent >= 1)
                    --ps.ind_level;
                /*
                 * for a switch, brace should be two levels out from the code
@@ -149,15 +150,15 @@
        ps.il[ps.tos] = ps.i_l_follow;
        break;
 
-    case whilestmt:            /* scanned while (...) */
-       if (ps.p_stack[ps.tos] == dohead) {
+    case while_expr:           /* 'while' '(' <expr> ')' */
+       if (ps.p_stack[ps.tos] == do_stmt) {
            /* it is matched with do stmt */
            ps.ind_level = ps.i_l_follow = ps.il[ps.tos];
-           ps.p_stack[++ps.tos] = whilestmt;
+           ps.p_stack[++ps.tos] = while_expr;
            ps.il[ps.tos] = ps.ind_level = ps.i_l_follow;
        }
        else {                  /* it is a while loop */
-           ps.p_stack[++ps.tos] = whilestmt;
+           ps.p_stack[++ps.tos] = while_expr;
            ps.il[ps.tos] = ps.i_l_follow;
            ++ps.i_l_follow;
            ps.search_brace = opt.btype_2;
@@ -165,23 +166,22 @@
 
        break;
 
-    case elselit:              /* scanned an else */
-
-       if (ps.p_stack[ps.tos] != ifhead)
+    case keyword_else:
+       if (ps.p_stack[ps.tos] != if_expr_stmt)
            diag(1, "Unmatched 'else'");
        else {
            ps.ind_level = ps.il[ps.tos];       /* indentation for else should
                                                 * be same as for if */
            ps.i_l_follow = ps.ind_level + 1;   /* everything following should
                                                 * be in 1 level */
-           ps.p_stack[ps.tos] = elsehead;
+           ps.p_stack[ps.tos] = if_expr_stmt_else;
            /* remember if with else */
            ps.search_brace = opt.btype_2 | opt.else_if;
        }
        break;
 
     case rbrace:               /* scanned a } */
-       /* stack should have <lbrace> <stmt> or <lbrace> <stmtl> */
+       /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
        if (ps.tos > 0 && ps.p_stack[ps.tos - 1] == lbrace) {
            ps.ind_level = ps.i_l_follow = ps.il[--ps.tos];



Home | Main Index | Thread Index | Old Index