Module Name: src Committed By: rillig Date: Tue Jun 6 05:11:11 UTC 2023
Modified Files: src/usr.bin/indent: debug.c io.c lexi.c parse.c Log Message: indent: sort functions in call order No functional change. To generate a diff of this commit: cvs rdiff -u -r1.36 -r1.37 src/usr.bin/indent/debug.c cvs rdiff -u -r1.199 -r1.200 src/usr.bin/indent/io.c cvs rdiff -u -r1.214 -r1.215 src/usr.bin/indent/lexi.c cvs rdiff -u -r1.67 -r1.68 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.36 src/usr.bin/indent/debug.c:1.37 --- src/usr.bin/indent/debug.c:1.36 Mon Jun 5 14:40:13 2023 +++ src/usr.bin/indent/debug.c Tue Jun 6 05:11:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.36 2023/06/05 14:40:13 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.37 2023/06/06 05:11:11 rillig Exp $"); #include <stdarg.h> @@ -144,6 +144,7 @@ static const char *const decl_ptr_name[] static unsigned wrote_newlines = 1; + void debug_printf(const char *fmt, ...) { Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.199 src/usr.bin/indent/io.c:1.200 --- src/usr.bin/indent/io.c:1.199 Tue Jun 6 04:37:26 2023 +++ src/usr.bin/indent/io.c Tue Jun 6 05:11:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $ */ +/* $NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.199 2023/06/06 04:37:26 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.200 2023/06/06 05:11:11 rillig Exp $"); #include <stdio.h> @@ -55,22 +55,6 @@ static unsigned wrote_newlines = 2; /* 0 static int paren_indent; -void -inp_skip(void) -{ - inp_p++; - if ((size_t)(inp_p - inp.s) >= inp.len) - inp_read_line(); -} - -char -inp_next(void) -{ - char ch = inp_p[0]; - inp_skip(); - return ch; -} - static void inp_read_next_line(FILE *f) { @@ -95,6 +79,32 @@ inp_read_next_line(FILE *f) inp_p = inp.s; } +void +inp_read_line(void) +{ + if (indent_enabled == indent_on) + out.indent_off_text.len = 0; + buf_add_chars(&out.indent_off_text, inp.s, inp.len); + inp_read_next_line(input); +} + +void +inp_skip(void) +{ + inp_p++; + if ((size_t)(inp_p - inp.s) >= inp.len) + inp_read_line(); +} + +char +inp_next(void) +{ + char ch = inp_p[0]; + inp_skip(); + return ch; +} + + static void output_newline(void) { @@ -176,6 +186,26 @@ is_blank_line_optional(void) return wrote_newlines >= 3; } +static int +compute_case_label_indent(void) +{ + int i = ps.tos; + while (i > 0 && ps.s_sym[i] != psym_switch_expr) + i--; + float case_ind = (float)ps.s_ind_level[i] + opt.case_indent; + return (int)(case_ind * (float)opt.indent_size); +} + +int +compute_label_indent(void) +{ + if (out.line_kind == lk_case_or_default) + return compute_case_label_indent(); + if (lab.s[0] == '#') + return 0; + return opt.indent_size * (ps.ind_level - 2); +} + static void output_line_label(void) { @@ -183,6 +213,53 @@ output_line_label(void) output_range(lab.s, lab.len); } +static int +compute_code_indent_lineup(int base_ind) +{ + int ind = paren_indent; + int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length; + if (overflow < 0) + return ind; + + if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) { + ind -= overflow + 2; + if (ind > base_ind) + return ind; + return base_ind; + } + + return ind; +} + +int +compute_code_indent(void) +{ + int base_ind = ps.ind_level * opt.indent_size; + + if (ps.line_start_nparen == 0) { + 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; + return base_ind; + } + + if (opt.lineup_to_parens) { + if (opt.lineup_to_parens_always) + return paren_indent; + return compute_code_indent_lineup(base_ind); + } + + if (ps.extra_expr_indent != eei_no) + return base_ind + 2 * opt.continuation_indent; + + if (2 * opt.continuation_indent == opt.indent_size) + return base_ind + opt.continuation_indent; + else + return base_ind + + opt.continuation_indent * ps.line_start_nparen; +} + static void output_line_code(void) { @@ -314,79 +391,3 @@ dont_write_line: ps.want_blank = false; out.line_kind = lk_other; } - -static int -compute_code_indent_lineup(int base_ind) -{ - int ind = paren_indent; - int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length; - if (overflow < 0) - return ind; - - if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) { - ind -= overflow + 2; - if (ind > base_ind) - return ind; - return base_ind; - } - - return ind; -} - -int -compute_code_indent(void) -{ - int base_ind = ps.ind_level * opt.indent_size; - - if (ps.line_start_nparen == 0) { - 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; - return base_ind; - } - - if (opt.lineup_to_parens) { - if (opt.lineup_to_parens_always) - return paren_indent; - return compute_code_indent_lineup(base_ind); - } - - if (ps.extra_expr_indent != eei_no) - return base_ind + 2 * opt.continuation_indent; - - if (2 * opt.continuation_indent == opt.indent_size) - return base_ind + opt.continuation_indent; - else - return base_ind + - opt.continuation_indent * ps.line_start_nparen; -} - -static int -compute_case_label_indent(void) -{ - int i = ps.tos; - while (i > 0 && ps.s_sym[i] != psym_switch_expr) - i--; - float case_ind = (float)ps.s_ind_level[i] + opt.case_indent; - return (int)(case_ind * (float)opt.indent_size); -} - -int -compute_label_indent(void) -{ - if (out.line_kind == lk_case_or_default) - return compute_case_label_indent(); - if (lab.s[0] == '#') - return 0; - return opt.indent_size * (ps.ind_level - 2); -} - -void -inp_read_line(void) -{ - if (indent_enabled == indent_on) - out.indent_off_text.len = 0; - buf_add_chars(&out.indent_off_text, inp.s, inp.len); - inp_read_next_line(input); -} Index: src/usr.bin/indent/lexi.c diff -u src/usr.bin/indent/lexi.c:1.214 src/usr.bin/indent/lexi.c:1.215 --- src/usr.bin/indent/lexi.c:1.214 Sun Jun 4 22:57:18 2023 +++ src/usr.bin/indent/lexi.c Tue Jun 6 05:11:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: lexi.c,v 1.214 2023/06/04 22:57:18 rillig Exp $ */ +/* $NetBSD: lexi.c,v 1.215 2023/06/06 05:11:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: lexi.c,v 1.214 2023/06/04 22:57:18 rillig Exp $"); +__RCSID("$NetBSD: lexi.c,v 1.215 2023/06/06 05:11:11 rillig Exp $"); #include <stdlib.h> #include <string.h> @@ -166,6 +166,7 @@ static const unsigned char lex_number_ro ['.'] = 15, }; + static void token_add_char(char ch) { Index: src/usr.bin/indent/parse.c diff -u src/usr.bin/indent/parse.c:1.67 src/usr.bin/indent/parse.c:1.68 --- src/usr.bin/indent/parse.c:1.67 Tue Jun 6 04:37:26 2023 +++ src/usr.bin/indent/parse.c Tue Jun 6 05:11:11 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $ */ +/* $NetBSD: parse.c,v 1.68 2023/06/06 05:11:11 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,13 +38,57 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: parse.c,v 1.67 2023/06/06 04:37:26 rillig Exp $"); +__RCSID("$NetBSD: parse.c,v 1.68 2023/06/06 05:11:11 rillig Exp $"); #include <err.h> #include "indent.h" -static void reduce(void); +/* + * Try to combine the statement on the top of the parse stack with the symbol + * directly below it, replacing these two symbols with a single symbol. + */ +static bool +reduce_stmt(void) +{ + switch (ps.s_sym[ps.tos - 1]) { + + case psym_stmt: + case psym_stmt_list: + ps.s_sym[--ps.tos] = psym_stmt_list; + return true; + + case psym_do: + ps.s_sym[--ps.tos] = psym_do_stmt; + ps.ind_level_follow = ps.s_ind_level[ps.tos]; + return true; + + case psym_if_expr: + ps.s_sym[--ps.tos] = psym_if_expr_stmt; + 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_block) + --i; + ps.ind_level_follow = ps.s_ind_level[i]; + /* For the time being, assume that there is no 'else' on this + * 'if', and set the indentation level accordingly. If an + * 'else' is scanned, it will be fixed up later. */ + return true; + + case psym_switch_expr: + case psym_decl: + case psym_if_expr_stmt_else: + case psym_for_exprs: + case psym_while_expr: + ps.s_sym[--ps.tos] = psym_stmt; + ps.ind_level_follow = ps.s_ind_level[ps.tos]; + return true; + + default: + return false; + } +} static int decl_level(void) @@ -70,6 +114,23 @@ ps_push_follow(parser_symbol psym) ps.s_ind_level[ps.tos] = ps.ind_level_follow; } +/* + * Repeatedly try to reduce the top two symbols on the parse stack to a single + * symbol, until no more reductions are possible. + */ +static void +reduce(void) +{ +again: + if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt()) + goto again; + if (ps.s_sym[ps.tos] == psym_while_expr && + ps.s_sym[ps.tos - 1] == psym_do_stmt) { + ps.tos -= 2; + goto again; + } +} + static bool is_lbrace(parser_symbol psym) { @@ -203,66 +264,3 @@ parse(parser_symbol psym) reduce(); /* see if any reduction can be done */ debug_parse_stack("after reduction"); } - -/* - * Try to combine the statement on the top of the parse stack with the symbol - * directly below it, replacing these two symbols with a single symbol. - */ -static bool -reduce_stmt(void) -{ - switch (ps.s_sym[ps.tos - 1]) { - - case psym_stmt: - case psym_stmt_list: - ps.s_sym[--ps.tos] = psym_stmt_list; - return true; - - case psym_do: - ps.s_sym[--ps.tos] = psym_do_stmt; - ps.ind_level_follow = ps.s_ind_level[ps.tos]; - return true; - - case psym_if_expr: - ps.s_sym[--ps.tos] = psym_if_expr_stmt; - 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_block) - --i; - ps.ind_level_follow = ps.s_ind_level[i]; - /* For the time being, assume that there is no 'else' on this - * 'if', and set the indentation level accordingly. If an - * 'else' is scanned, it will be fixed up later. */ - return true; - - case psym_switch_expr: - case psym_decl: - case psym_if_expr_stmt_else: - case psym_for_exprs: - case psym_while_expr: - ps.s_sym[--ps.tos] = psym_stmt; - ps.ind_level_follow = ps.s_ind_level[ps.tos]; - return true; - - default: - return false; - } -} - -/* - * Repeatedly try to reduce the top two symbols on the parse stack to a single - * symbol, until no more reductions are possible. - */ -static void -reduce(void) -{ -again: - if (ps.s_sym[ps.tos] == psym_stmt && reduce_stmt()) - goto again; - if (ps.s_sym[ps.tos] == psym_while_expr && - ps.s_sym[ps.tos - 1] == psym_do_stmt) { - ps.tos -= 2; - goto again; - } -}