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: clean up lexical analyzer



details:   https://anonhg.NetBSD.org/src/rev/a7a0442d4fc3
branches:  trunk
changeset: 990599:a7a0442d4fc3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Oct 30 22:15:51 2021 +0000

description:
indent: clean up lexical analyzer

Use traditional type for small unsigned numbers instead of uint8_t; the
required header was not included.

Remove assertion for debug mode; lint takes care of ensuring that the
enum constants match the length of the names array.

Constify a name array.

Move the comparison function for bsearch closer to its caller.

No functional change.

diffstat:

 usr.bin/indent/indent.h |   3 ++-
 usr.bin/indent/lexi.c   |  31 +++++++++++++------------------
 2 files changed, 15 insertions(+), 19 deletions(-)

diffs (110 lines):

diff -r 65785346496a -r a7a0442d4fc3 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat Oct 30 22:04:42 2021 +0000
+++ b/usr.bin/indent/indent.h   Sat Oct 30 22:15:51 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.64 2021/10/30 11:49:38 rillig Exp $       */
+/*     $NetBSD: indent.h,v 1.65 2021/10/30 22:15:51 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -69,6 +69,7 @@
 #endif
 
 #include <stdbool.h>
+#include <stdio.h>
 
 typedef enum lexer_symbol {
     lsym_eof,
diff -r 65785346496a -r a7a0442d4fc3 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sat Oct 30 22:04:42 2021 +0000
+++ b/usr.bin/indent/lexi.c     Sat Oct 30 22:15:51 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.114 2021/10/29 23:48:50 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.115 2021/10/30 22:15:51 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,14 +43,11 @@
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.114 2021/10/29 23:48:50 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.115 2021/10/30 22:15:51 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
 
-#include <sys/param.h>
-#include <assert.h>
-#include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
@@ -157,7 +154,7 @@
 };
 /* INDENT ON */
 
-static const uint8_t lex_number_row[] = {
+static const unsigned char lex_number_row[] = {
     ['0'] = 1,
     ['1'] = 2,
     ['2'] = 3, ['3'] = 3, ['4'] = 3, ['5'] = 3, ['6'] = 3, ['7'] = 3,
@@ -211,12 +208,6 @@
     *token.e++ = ch;
 }
 
-static int
-cmp_keyword_by_name(const void *key, const void *elem)
-{
-    return strcmp(key, ((const struct keyword *)elem)->name);
-}
-
 #ifdef debug
 static const char *
 lsym_name(lexer_symbol sym)
@@ -255,15 +246,13 @@
        "while",
     };
 
-    assert(array_length(name) == (int)lsym_while + 1);
-
     return name[sym];
 }
 
 static const char *
 kw_name(enum keyword_kind kw)
 {
-    static const char *name[] = {
+    static const char *const name[] = {
        "0",
        "offsetof",
        "sizeof",
@@ -376,12 +365,12 @@
 static void
 lex_number(void)
 {
-    for (uint8_t s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
-       uint8_t ch = (uint8_t)*inp.s;
+    for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
+       unsigned char ch = (unsigned char)*inp.s;
        if (ch >= array_length(lex_number_row) || lex_number_row[ch] == 0)
            break;
 
-       uint8_t row = lex_number_row[ch];
+       unsigned char row = lex_number_row[ch];
        if (lex_number_state[row][s - 'A'] == ' ') {
            /*-
             * lex_number_state[0][s - 'A'] now indicates the type:
@@ -485,6 +474,12 @@
     return bsearch_typenames(token.s) >= 0;
 }
 
+static int
+cmp_keyword_by_name(const void *key, const void *elem)
+{
+    return strcmp(key, ((const struct keyword *)elem)->name);
+}
+
 /* Read an alphanumeric token into 'token', or return end_of_file. */
 static lexer_symbol
 lexi_alnum(void)



Home | Main Index | Thread Index | Old Index