Module Name: src
Committed By: rillig
Date: Sat Oct 30 22:15:51 UTC 2021
Modified Files:
src/usr.bin/indent: indent.h lexi.c
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.114 -r1.115 src/usr.bin/indent/lexi.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.64 src/usr.bin/indent/indent.h:1.65
--- src/usr.bin/indent/indent.h:1.64 Sat Oct 30 11:49:38 2021
+++ src/usr.bin/indent/indent.h Sat Oct 30 22:15:51 2021
@@ -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 @@ __FBSDID("$FreeBSD: head/usr.bin/indent/
#endif
#include <stdbool.h>
+#include <stdio.h>
typedef enum lexer_symbol {
lsym_eof,
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.114 src/usr.bin/indent/lexi.c:1.115
--- src/usr.bin/indent/lexi.c:1.114 Fri Oct 29 23:48:50 2021
+++ src/usr.bin/indent/lexi.c Sat Oct 30 22:15:51 2021
@@ -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 @@ static char sccsid[] = "@(#)lexi.c 8.1 (
#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 @@ static const unsigned char lex_number_st
};
/* 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_add_char(char ch)
*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 @@ lsym_name(lexer_symbol sym)
"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 @@ lexi_end(lexer_symbol lsym)
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 @@ is_typename(void)
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)