Module Name: src Committed By: rillig Date: Sun Jun 4 17:54:11 UTC 2023
Modified Files: src/usr.bin/indent: debug.c indent.c indent.h io.c parse.c Log Message: indent: track the kind of '{' on the parser stack To generate a diff of this commit: cvs rdiff -u -r1.32 -r1.33 src/usr.bin/indent/debug.c cvs rdiff -u -r1.325 -r1.326 src/usr.bin/indent/indent.c cvs rdiff -u -r1.169 -r1.170 src/usr.bin/indent/indent.h cvs rdiff -u -r1.190 -r1.191 src/usr.bin/indent/io.c cvs rdiff -u -r1.64 -r1.65 src/usr.bin/indent/parse.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/debug.c diff -u src/usr.bin/indent/debug.c:1.32 src/usr.bin/indent/debug.c:1.33 --- src/usr.bin/indent/debug.c:1.32 Sun Jun 4 17:02:06 2023 +++ src/usr.bin/indent/debug.c Sun Jun 4 17:54:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.32 2023/06/04 17:02:06 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.33 2023/06/04 17:54:11 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.32 2023/06/04 17:02:06 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.33 2023/06/04 17:54:11 rillig Exp $"); #include <stdarg.h> @@ -86,8 +86,11 @@ const char *const lsym_name[] = { }; const char *const psym_name[] = { - "0", - "lbrace", + "-", + "lbrace_block", + "lbrace_struct", + "lbrace_union", + "lbrace_enum", "rbrace", "decl", "stmt", @@ -109,13 +112,6 @@ static const char *const declaration_nam "end", }; -static const char *const in_enum_name[] = { - "no", - "enum", - "type", - "brace", -}; - const char *const paren_level_cast_name[] = { "(unknown cast)", "(maybe cast)", @@ -325,7 +321,7 @@ debug_parser_state(void) debug_ps_enum(declaration, declaration_name); debug_ps_bool(blank_line_after_decl); debug_ps_bool(in_func_def_params); - debug_ps_enum(in_enum, in_enum_name); + debug_ps_enum(lbrace_kind, psym_name); debug_ps_enum(decl_ptr, decl_ptr_name); debug_ps_bool(decl_indent_done); debug_ps_int(decl_ind); Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.325 src/usr.bin/indent/indent.c:1.326 --- src/usr.bin/indent/indent.c:1.325 Sun Jun 4 14:38:15 2023 +++ src/usr.bin/indent/indent.c Sun Jun 4 17:54:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.325 2023/06/04 14:38:15 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.326 2023/06/04 17:54:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: indent.c,v 1.325 2023/06/04 14:38:15 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.326 2023/06/04 17:54:11 rillig Exp $"); #include <sys/param.h> #include <err.h> @@ -181,6 +181,7 @@ init_globals(void) ps.s_sym[0] = psym_stmt_list; ps.prev_lsym = lsym_semicolon; ps.next_col_1 = true; + ps.lbrace_kind = psym_lbrace_block; const char *suffix = getenv("SIMPLE_BACKUP_SUFFIX"); if (suffix != NULL) @@ -371,33 +372,16 @@ update_ps_decl_ptr(lexer_symbol lsym) } static void -update_ps_in_enum(lexer_symbol lsym) +update_ps_prev_tag(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; - } + if (lsym == lsym_tag) { + ps.lbrace_kind = token.mem[0] == 's' ? psym_lbrace_struct : + token.mem[0] == 'u' ? psym_lbrace_union : + psym_lbrace_enum; + } else if (lsym != lsym_type_outside_parentheses + && lsym != lsym_word + && lsym != lsym_lbrace) + ps.lbrace_kind = psym_lbrace_block; } static int @@ -828,7 +812,7 @@ process_lbrace(void) } ps.decl_ind = 0; - parse(psym_lbrace); + parse(ps.lbrace_kind); if (ps.want_blank) buf_add_char(&code, ' '); ps.want_blank = false; @@ -1278,7 +1262,7 @@ indent(void) if (com.len > 0) move_com_to_code(lsym); update_ps_decl_ptr(lsym); - update_ps_in_enum(lsym); + update_ps_prev_tag(lsym); } process_lsym(lsym); Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.169 src/usr.bin/indent/indent.h:1.170 --- src/usr.bin/indent/indent.h:1.169 Sun Jun 4 17:02:06 2023 +++ src/usr.bin/indent/indent.h Sun Jun 4 17:54:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.169 2023/06/04 17:02:06 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.170 2023/06/04 17:54:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -118,7 +118,10 @@ typedef enum lexer_symbol { */ typedef enum parser_symbol { psym_0, /* a placeholder; not stored on the stack */ - psym_lbrace, + psym_lbrace_block, /* '{' for a block of code */ + psym_lbrace_struct, /* '{' in 'struct ... { ... }' */ + psym_lbrace_union, /* '{' in 'union ... { ... }' */ + psym_lbrace_enum, /* '{' in 'enum ... { ... }' */ psym_rbrace, /* not stored on the stack */ psym_decl, psym_stmt, @@ -326,6 +329,8 @@ extern struct parser_state { * after the parenthesized expression * from a 'for', 'if', 'switch' or * 'while'; or psym_0 */ + parser_symbol lbrace_kind; /* the kind of brace to be pushed to + * the parser symbol stack next */ /* Indentation of statements and declarations */ @@ -350,13 +355,6 @@ extern struct parser_state { eei_last } extra_expr_indent; - enum { - in_enum_no, /* outside any 'enum { ... }' */ - in_enum_enum, /* after keyword 'enum' */ - in_enum_type, /* after 'enum' or 'enum tag' */ - in_enum_brace /* between '{' and '}' */ - } in_enum; /* enum { . } */ - int tos; /* pointer to top of stack */ parser_symbol s_sym[STACKSIZE]; int s_ind_level[STACKSIZE]; Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.190 src/usr.bin/indent/io.c:1.191 --- src/usr.bin/indent/io.c:1.190 Sun Jun 4 14:38:15 2023 +++ src/usr.bin/indent/io.c Sun Jun 4 17:54:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.190 2023/06/04 14:38:15 rillig Exp $ */ +/* $NetBSD: io.c,v 1.191 2023/06/04 17:54:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.190 2023/06/04 14:38:15 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.191 2023/06/04 17:54:11 rillig Exp $"); #include <stdio.h> @@ -347,7 +347,7 @@ compute_code_indent(void) int base_ind = ps.ind_level * opt.indent_size; if (ps.line_start_nparen == 0) { - if (ps.in_enum == in_enum_brace) + if (ps.tos >= 1 && ps.s_sym[ps.tos - 1] == psym_lbrace_enum) return base_ind; if (ps.in_stmt_cont) return base_ind + opt.continuation_indent; Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.64 src/usr.bin/indent/parse.c:1.65 --- src/usr.bin/indent/parse.c:1.64 Sat Jun 3 21:24:26 2023 +++ src/usr.bin/indent/parse.c Sun Jun 4 17:54:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.64 2023/06/03 21:24:26 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.65 2023/06/04 17:54:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: parse.c,v 1.64 2023/06/03 21:24:26 rillig Exp $"); +__RCSID("$NetBSD: parse.c,v 1.65 2023/06/04 17:54:11 rillig Exp $"); #include <err.h> @@ -70,6 +70,15 @@ ps_push_follow(parser_symbol psym) ps.s_ind_level[ps.tos] = ps.ind_level_follow; } +static bool +is_lbrace(parser_symbol psym) +{ + return psym == psym_lbrace_block + || psym == psym_lbrace_struct + || psym == psym_lbrace_union + || psym == psym_lbrace_enum; +} + /* * Shift the token onto the parser stack, or reduce it by combining it with * previous tokens. @@ -110,7 +119,10 @@ parse(parser_symbol psym) ps_push(psym); break; - case psym_lbrace: + case psym_lbrace_block: + case psym_lbrace_struct: + case psym_lbrace_union: + case psym_lbrace_enum: ps.break_after_comma = false; if (ps.s_sym[ps.tos] == psym_stmt || ps.s_sym[ps.tos] == psym_decl @@ -132,7 +144,7 @@ parse(parser_symbol psym) } } - ps_push(psym_lbrace); + ps_push(psym); ps_push_follow(psym_stmt); break; @@ -160,7 +172,7 @@ parse(parser_symbol psym) case psym_rbrace: /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */ - if (!(ps.tos > 0 && ps.s_sym[ps.tos - 1] == psym_lbrace)) { + if (!(ps.tos > 0 && is_lbrace(ps.s_sym[ps.tos - 1]))) { diag(1, "Statement nesting error"); break; } @@ -217,7 +229,7 @@ reduce_stmt(void) int i = ps.tos - 1; while (ps.s_sym[i] != psym_stmt && ps.s_sym[i] != psym_stmt_list && - ps.s_sym[i] != psym_lbrace) + ps.s_sym[i] != psym_lbrace_block) --i; ps.ind_level_follow = ps.s_ind_level[i]; /* For the time being, assume that there is no 'else' on this