Module Name: src Committed By: rillig Date: Sun Oct 24 22:28:06 UTC 2021
Modified Files: src/tests/usr.bin/indent: token_lparen.c src/usr.bin/indent: indent.c indent.h lexi.c parse.c Log Message: indent: split kw_do_or_else into separate constants It was unnecessarily confusing to have the token types keyword_do_else, keyword_do and keyword_else at the same time, without any hint in what they differed. Some of the token types seem to be used by the lexer while others are used in the parse stack. Maybe all token types can be partitioned into these groups, which would suggest to use two different types for them. And if not, it's still clearer to have this distinction in the names of the constants. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/token_lparen.c cvs rdiff -u -r1.152 -r1.153 src/usr.bin/indent/indent.c cvs rdiff -u -r1.45 -r1.46 src/usr.bin/indent/indent.h cvs rdiff -u -r1.96 -r1.97 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.37 -r1.38 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/tests/usr.bin/indent/token_lparen.c diff -u src/tests/usr.bin/indent/token_lparen.c:1.4 src/tests/usr.bin/indent/token_lparen.c:1.5 --- src/tests/usr.bin/indent/token_lparen.c:1.4 Sun Oct 24 17:19:49 2021 +++ src/tests/usr.bin/indent/token_lparen.c Sun Oct 24 22:28:06 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: token_lparen.c,v 1.4 2021/10/24 17:19:49 rillig Exp $ */ +/* $NetBSD: token_lparen.c,v 1.5 2021/10/24 22:28:06 rillig Exp $ */ /* $FreeBSD$ */ /* @@ -133,7 +133,7 @@ void cover_want_blank_before_lparen(void /* $ XXX: form_feed should be skipped, just as newline. */ (form_feed)(); /* XXX: should be skipped */ for(;;); - do(keyword_do_else)=3;while(0); + do(tt_lex_do)=3;while(0); // $ TODO: is if_expr possible? if(cond)(if_expr)(); // $ TODO: is while_expr possible? @@ -144,10 +144,10 @@ void cover_want_blank_before_lparen(void (stmt); // $ TODO: is stmt_list possible? (stmt_list); - // $ TODO: is keyword_else possible? keyword_do_else is. - if(cond);else(keyword_else)(); - // $ TODO: is keyword_do possible? keyword_do_else is. - do(keyword_do);while(0); + // $ TODO: is tt_ps_else possible? tt_lex_else is. + if(cond);else(tt_ps_else)(); + // $ TODO: is tt_ps_do possible? tt_lex_do is. + do(tt_ps_do);while(0); // The following line would generate 'Statement nesting error'. // do stmt;(do_stmt());while(0); // $ TODO: is if_expr_stmt possible? @@ -200,7 +200,7 @@ cover_want_blank_before_lparen(void) (form_feed)(); /* XXX: should be skipped */ for (;;); do - (keyword_do_else) = 3; + (tt_lex_do) = 3; while (0); if (cond) (if_expr)(); @@ -212,9 +212,9 @@ cover_want_blank_before_lparen(void) (stmt_list); if (cond); else - (keyword_else)(); + (tt_ps_else)(); do - (keyword_do); + (tt_ps_do); while (0); // The following line would generate 'Statement nesting error'. // do stmt;(do_stmt());while(0); Index: src/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.152 src/usr.bin/indent/indent.c:1.153 --- src/usr.bin/indent/indent.c:1.152 Sun Oct 24 20:57:11 2021 +++ src/usr.bin/indent/indent.c Sun Oct 24 22:28:06 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.152 2021/10/24 20:57:11 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.153 2021/10/24 22:28:06 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.152 2021/10/24 20:57:11 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.153 2021/10/24 22:28:06 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -246,8 +246,7 @@ search_brace_other(token_type ttype, boo remove_newlines = /* "} else" */ - (ttype == keyword_do_else && *token.s == 'e' && - code.e != code.s && code.e[-1] == '}') + (ttype == tt_lex_else && code.e != code.s && code.e[-1] == '}') /* "else if" */ || (ttype == keyword_for_if_while && *token.s == 'i' && last_else && opt.else_if); @@ -1055,7 +1054,7 @@ process_keyword_do(bool *force_nl, bool *force_nl = true; /* following stuff must go onto new line */ *last_else = false; - parse(keyword_do); + parse(tt_ps_do); } static void @@ -1072,7 +1071,7 @@ process_keyword_else(bool *force_nl, boo *force_nl = true; /* following stuff must go onto new line */ *last_else = true; - parse(keyword_else); + parse(tt_ps_else); } static void @@ -1447,11 +1446,12 @@ main_loop(void) /* remember the type of header for later use by parser */ goto copy_token; - case keyword_do_else: - if (*token.s == 'd') - process_keyword_do(&force_nl, &last_else); - else - process_keyword_else(&force_nl, &last_else); + case tt_lex_do: + process_keyword_do(&force_nl, &last_else); + goto copy_token; + + case tt_lex_else: + process_keyword_else(&force_nl, &last_else); goto copy_token; case type_def: Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.45 src/usr.bin/indent/indent.h:1.46 --- src/usr.bin/indent/indent.h:1.45 Sun Oct 24 11:19:25 2021 +++ src/usr.bin/indent/indent.h Sun Oct 24 22:28:06 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.45 2021/10/24 11:19:25 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.46 2021/10/24 22:28:06 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -92,14 +92,15 @@ typedef enum token_type { form_feed, decl, keyword_for_if_while, /* 'for', 'if' or 'while' */ - keyword_do_else, /* 'do' or 'else' */ + tt_lex_do, + tt_lex_else, if_expr, /* 'if' '(' <expr> ')' */ while_expr, /* 'while' '(' <expr> ')' */ for_exprs, /* 'for' '(' ... ')' */ stmt, stmt_list, - keyword_else, /* 'else' */ - keyword_do, /* 'do' */ + tt_ps_else, + tt_ps_do, do_stmt, /* 'do' <stmt> */ if_expr_stmt, /* 'if' '(' <expr> ')' <stmt> */ if_expr_stmt_else, /* 'if' '(' <expr> ')' <stmt> 'else' */ @@ -225,7 +226,8 @@ enum keyword_kind { kw_struct_or_union_or_enum, kw_type, kw_for_or_if_or_while, - kw_do_or_else, + kw_do, + kw_else, kw_switch, kw_case_or_default, kw_jump, Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.96 src/usr.bin/indent/lexi.c:1.97 --- src/usr.bin/indent/lexi.c:1.96 Sun Oct 24 20:47:00 2021 +++ src/usr.bin/indent/lexi.c Sun Oct 24 22:28:06 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.96 2021/10/24 20:47:00 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.97 2021/10/24 22:28:06 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c 8.1 ( #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: lexi.c,v 1.96 2021/10/24 20:47:00 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.97 2021/10/24 22:28:06 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $"); #endif @@ -74,9 +74,9 @@ static const struct keyword { {"const", kw_type}, {"continue", kw_jump}, {"default", kw_case_or_default}, - {"do", kw_do_or_else}, + {"do", kw_do}, {"double", kw_type}, - {"else", kw_do_or_else}, + {"else", kw_else}, {"enum", kw_struct_or_union_or_enum}, {"extern", kw_storage_class}, {"float", kw_type}, @@ -227,14 +227,15 @@ token_type_name(token_type ttype) "case_label", "colon", "semicolon", "lbrace", "rbrace", "ident", "comma", "comment", "switch_expr", "preprocessing", "form_feed", "decl", - "keyword_for_if_while", "keyword_do_else", + "keyword_for_if_while", "tt_lex_do", "tt_lex_else", "if_expr", "while_expr", "for_exprs", - "stmt", "stmt_list", "keyword_else", "keyword_do", "do_stmt", + "stmt", "stmt_list", "tt_ps_else", "tt_ps_do", "do_stmt", "if_expr_stmt", "if_expr_stmt_else", "period", "string_prefix", "storage_class", "funcname", "type_def", "keyword_struct_union_enum" }; assert(0 <= ttype && ttype < array_length(name)); + assert(array_length(name) == (int)keyword_struct_union_enum + 1); return name[ttype]; } @@ -451,8 +452,11 @@ lexi_alnum(struct parser_state *state) case kw_for_or_if_or_while: return keyword_for_if_while; - case kw_do_or_else: - return keyword_do_else; + case kw_do: + return tt_lex_do; + + case kw_else: + return tt_lex_else; case kw_storage_class: return storage_class; Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.37 src/usr.bin/indent/parse.c:1.38 --- src/usr.bin/indent/parse.c:1.37 Sun Oct 24 19:14:33 2021 +++ src/usr.bin/indent/parse.c Sun Oct 24 22:28:06 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.37 2021/10/24 19:14:33 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.38 2021/10/24 22:28:06 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -65,7 +65,7 @@ parse(token_type ttype) debug_println("parse token: '%s' \"%s\"", token_type_name(ttype), token.s); - if (ttype != keyword_else) { + if (ttype != tt_ps_else) { while (ps.s_ttype[ps.tos] == if_expr_stmt) { ps.s_ttype[ps.tos] = stmt; reduce(); @@ -105,7 +105,7 @@ parse(token_type ttype) ps.ind_level_follow = ps.s_ind_level[ps.tos--]; } /* FALLTHROUGH */ - case keyword_do: + case tt_ps_do: case for_exprs: /* 'for' (...) */ ps.s_ttype[++ps.tos] = ttype; ps.s_ind_level[ps.tos] = ps.ind_level = ps.ind_level_follow; @@ -155,7 +155,7 @@ parse(token_type ttype) break; - case keyword_else: + case tt_ps_else: if (ps.s_ttype[ps.tos] != if_expr_stmt) diag(1, "Unmatched 'else'"); else { @@ -235,7 +235,7 @@ reduce_stmt(void) ps.s_ttype[--ps.tos] = stmt_list; return true; - case keyword_do: /* 'do' <stmt> */ + case tt_ps_do: /* 'do' <stmt> */ ps.s_ttype[--ps.tos] = do_stmt; ps.ind_level_follow = ps.s_ind_level[ps.tos]; return true;