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: move cast detection from the lexer to...



details:   https://anonhg.NetBSD.org/src/rev/c8a96fe5ea05
branches:  trunk
changeset: 376624:c8a96fe5ea05
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 25 19:35:45 2023 +0000

description:
indent: move cast detection from the lexer to the main processor

It is not the job of the lexer to modify the parser state.

diffstat:

 usr.bin/indent/indent.c |   8 ++++++--
 usr.bin/indent/lexi.c   |  24 +++++++++---------------
 2 files changed, 15 insertions(+), 17 deletions(-)

diffs (98 lines):

diff -r e7e5dfee6795 -r c8a96fe5ea05 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Jun 25 19:29:57 2023 +0000
+++ b/usr.bin/indent/indent.c   Sun Jun 25 19:35:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -958,6 +958,10 @@ process_type_outside_parentheses(void)
 static void
 process_word(lexer_symbol lsym)
 {
+       if (lsym == lsym_type   /* in parentheses */
+           && ps.paren.item[ps.paren.len - 1].cast == cast_unknown)
+               ps.paren.item[ps.paren.len - 1].cast = cast_maybe;
+
        if (ps.in_decl) {
                if (lsym == lsym_funcname) {
                        ps.in_decl = false;
diff -r e7e5dfee6795 -r c8a96fe5ea05 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Jun 25 19:29:57 2023 +0000
+++ b/usr.bin/indent/lexi.c     Sun Jun 25 19:35:45 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -296,6 +296,8 @@ bsearch_typenames(const char *key)
 static bool
 is_typename(void)
 {
+       if (ps.prev_lsym == lsym_tag)
+               return true;
        if (opt.auto_typedefs &&
            token.len >= 2 && memcmp(token.s + token.len - 2, "_t", 2) == 0)
                return true;
@@ -335,6 +337,7 @@ cmp_keyword_by_name(const void *key, con
 static bool
 probably_function_definition(const char *p)
 {
+       // TODO: Don't look at characters in comments, see lsym_funcname.c.
        int paren_level = 0;
        for (; *p != '\n'; p++) {
                if (*p == '(')
@@ -409,29 +412,20 @@ lexi_alnum(void)
            array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
        lexer_symbol lsym = lsym_word;
        if (kw != NULL) {
-               if (kw->lsym == lsym_type)
-                       lsym = lsym_type;
+               lsym = kw->lsym;
                ps.next_unary = true;
-               if (kw->lsym == lsym_tag || kw->lsym == lsym_type)
+               if (lsym == lsym_tag || lsym == lsym_type)
                        goto found_typename;
-               return kw->lsym;
+               return lsym;
        }
 
        if (is_typename()) {
                lsym = lsym_type;
                ps.next_unary = true;
 found_typename:
-               if (ps.paren.len > 0) {
-                       /* inside parentheses: cast, param list, offsetof or
-                        * sizeof */
-                       struct paren_level *paren_level =
-                           ps.paren.item + ps.paren.len - 1;
-                       if (paren_level->cast == cast_unknown)
-                               paren_level->cast = cast_maybe;
-               }
                if (ps.prev_lsym != lsym_period
                    && ps.prev_lsym != lsym_unary_op) {
-                       if (kw != NULL && kw->lsym == lsym_tag)
+                       if (lsym == lsym_tag)
                                return lsym_tag;
                        if (ps.paren.len == 0)
                                return lsym_type;



Home | Main Index | Thread Index | Old Index