summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2023-05-23 18:16:28 +0000
committerrillig <rillig@NetBSD.org>2023-05-23 18:16:28 +0000
commit03d330c04bfd36feb00181a17bf9cf8bd2a07a29 (patch)
tree154dd7dc6f30b0d3a0544b96bb52b220da6c9be2
parent35cd7db685707d67cd6b0c21274728ef10285324 (diff)
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.
-rw-r--r--usr.bin/indent/indent.c42
-rw-r--r--usr.bin/indent/io.c8
-rw-r--r--usr.bin/indent/lexi.c16
3 files changed, 45 insertions, 21 deletions
diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c
index 3f2c1ae0adc..130677bc552 100644
--- a/usr.bin/indent/indent.c
+++ b/usr.bin/indent/indent.c
@@ -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 --git a/usr.bin/indent/io.c b/usr.bin/indent/io.c
index 6d42790b1f2..47ff818dd08 100644
--- a/usr.bin/indent/io.c
+++ b/usr.bin/indent/io.c
@@ -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 --git a/usr.bin/indent/lexi.c b/usr.bin/indent/lexi.c
index c121d3e1092..11f73147ef4 100644
--- a/usr.bin/indent/lexi.c
+++ b/usr.bin/indent/lexi.c
@@ -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;