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: separate code for handling enums from...



details:   https://anonhg.NetBSD.org/src/rev/90292ddf1a03
branches:  trunk
changeset: 375969:90292ddf1a03
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue May 23 18:16:28 2023 +0000

description:
indent: separate code for handling enums from the lexer

The lexer's responsibility is to generate tokens, it's not supposed to
update the parser state.  Centralize the state transitions that control
indentation of enum constants to keep the lexer code clean.

Skip comments, newlines and preprocessing lines when updating the parser
state for enum constants and for '*' in declarations.

diffstat:

 usr.bin/indent/indent.c |  42 +++++++++++++++++++++++++++++++++++++-----
 usr.bin/indent/io.c     |   8 +++++---
 usr.bin/indent/lexi.c   |  16 +++-------------
 3 files changed, 45 insertions(+), 21 deletions(-)

diffs (160 lines):

diff -r 7dab0b461bce -r 90292ddf1a03 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue May 23 16:53:57 2023 +0000
+++ b/usr.bin/indent/indent.c   Tue May 23 18:16:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.309 2023/05/23 16:53:57 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.310 2023/05/23 18:16:28 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.309 2023/05/23 16:53:57 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.310 2023/05/23 18:16:28 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -372,6 +372,36 @@ update_ps_decl_ptr(lexer_symbol lsym)
        }
 }
 
+static void
+update_ps_in_enum(lexer_symbol lsym)
+{
+       switch (ps.in_enum) {
+       case in_enum_no:
+               if (lsym == lsym_tag && token.st[0] == 'e')
+                       ps.in_enum = in_enum_enum;
+               break;
+       case in_enum_enum:
+               if (lsym == lsym_type_outside_parentheses
+                   || lsym == lsym_type_in_parentheses)
+                       ps.in_enum = in_enum_type;
+               else if (lsym == lsym_lbrace)
+                       ps.in_enum = in_enum_brace;
+               else
+                       ps.in_enum = in_enum_no;
+               break;
+       case in_enum_type:
+               if (lsym == lsym_lbrace)
+                       ps.in_enum = in_enum_brace;
+               else
+                       ps.in_enum = in_enum_no;
+               break;
+       case in_enum_brace:
+               if (lsym == lsym_rbrace)
+                       ps.in_enum = in_enum_no;
+               break;
+       }
+}
+
 static int
 process_eof(void)
 {
@@ -1166,7 +1196,9 @@ indent(void)
 
                if (lsym == lsym_newline || lsym == lsym_preprocessing)
                        ps.force_nl = false;
-               else if (lsym != lsym_comment) {
+               else if (lsym == lsym_comment) {
+                       /* no special processing */
+               } else {
                        maybe_break_line(lsym);
                        /*
                         * Add an extra level of indentation; turned off again
@@ -1175,10 +1207,10 @@ indent(void)
                        ps.in_stmt_or_decl = true;
                        if (com.len > 0)
                                move_com_to_code(lsym);
+                       update_ps_decl_ptr(lsym);
+                       update_ps_in_enum(lsym);
                }
 
-               update_ps_decl_ptr(lsym);
-
                process_lsym(lsym);
 
                debug_parser_state();
diff -r 7dab0b461bce -r 90292ddf1a03 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Tue May 23 16:53:57 2023 +0000
+++ b/usr.bin/indent/io.c       Tue May 23 18:16:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.186 2023/05/23 12:12:29 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.187 2023/05/23 18:16:28 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.186 2023/05/23 12:12:29 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.187 2023/05/23 18:16:28 rillig Exp $");
 
 #include <stdio.h>
 
@@ -350,7 +350,9 @@ compute_code_indent(void)
        int base_ind = ps.ind_level * opt.indent_size;
 
        if (ps.line_start_nparen == 0) {
-               if (ps.in_stmt_cont && ps.in_enum != in_enum_brace)
+               if (ps.in_enum == in_enum_brace)
+                       return base_ind;
+               if (ps.in_stmt_cont)
                        return base_ind + opt.continuation_indent;
                return base_ind;
        }
diff -r 7dab0b461bce -r 90292ddf1a03 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue May 23 16:53:57 2023 +0000
+++ b/usr.bin/indent/lexi.c     Tue May 23 18:16:28 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.205 2023/05/23 12:12:29 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.206 2023/05/23 18:16:28 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.205 2023/05/23 12:12:29 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.206 2023/05/23 18:16:28 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -396,8 +396,6 @@ lexi_alnum(void)
        if (is_typename()) {
                lsym = lsym_type_in_parentheses;
                ps.next_unary = true;
-               if (ps.in_enum == in_enum_enum)
-                       ps.in_enum = in_enum_type;
 found_typename:
                if (ps.nparen > 0) {
                        /* inside parentheses: cast, param list, offsetof or
@@ -407,11 +405,8 @@ found_typename:
                }
                if (ps.prev_token != lsym_period
                    && ps.prev_token != lsym_unary_op) {
-                       if (kw != NULL && kw->lsym == lsym_tag) {
-                               if (token.st[0] == 'e' /* enum */)
-                                       ps.in_enum = in_enum_enum;
+                       if (kw != NULL && kw->lsym == lsym_tag)
                                return lsym_tag;
-                       }
                        if (ps.nparen == 0)
                                return lsym_type_outside_parentheses;
                }
@@ -673,11 +668,6 @@ lexi(void)
                next_unary = true;
        }
 
-       if (ps.in_enum == in_enum_enum || ps.in_enum == in_enum_type)
-               ps.in_enum = lsym == lsym_lbrace ? in_enum_brace : in_enum_no;
-       if (lsym == lsym_rbrace)
-               ps.in_enum = in_enum_no;
-
        ps.next_unary = next_unary;
 
        return lsym;



Home | Main Index | Thread Index | Old Index