Module Name:    src
Committed By:   rillig
Date:           Sat May 20 11:53:53 UTC 2023

Modified Files:
        src/usr.bin/indent: debug.c indent.c indent.h io.c lexi.c pr_comment.c

Log Message:
indent: extract the output state from the parser state

The parser state depends on the preprocessing lines, the output state
shouldn't.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.298 -r1.299 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.155 -r1.156 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.182 -r1.183 src/usr.bin/indent/io.c
cvs rdiff -u -r1.201 -r1.202 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.147 -r1.148 src/usr.bin/indent/pr_comment.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.18 src/usr.bin/indent/debug.c:1.19
--- src/usr.bin/indent/debug.c:1.18	Sat May 20 11:19:17 2023
+++ src/usr.bin/indent/debug.c	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: debug.c,v 1.18 2023/05/20 11:19:17 rillig Exp $	*/
+/*	$NetBSD: debug.c,v 1.19 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.18 2023/05/20 11:19:17 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.19 2023/05/20 11:53:53 rillig Exp $");
 
 #include <stdarg.h>
 
@@ -117,7 +117,7 @@ const char *const paren_level_cast_name[
 	"(no cast)",
 };
 
-static const char *const line_kind_name[] = {
+const char *const line_kind_name[] = {
 	"other",
 	"#if",
 	"#endif",
@@ -328,9 +328,6 @@ debug_parser_state(lexer_symbol lsym)
 	debug_ps_enum(spaced_expr_psym, psym_name);
 	debug_ps_int(quest_level);
 
-	debug_ps_enum(line_kind, line_kind_name);
-	debug_ps_enum(prev_line_kind, line_kind_name);
-
 	prev_ps = ps;
 }
 

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.298 src/usr.bin/indent/indent.c:1.299
--- src/usr.bin/indent/indent.c:1.298	Sat May 20 10:46:21 2023
+++ src/usr.bin/indent/indent.c	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.298 2023/05/20 10:46:21 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.299 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.298 2023/05/20 10:46:21 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.299 2023/05/20 11:53:53 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -751,7 +751,7 @@ process_rbrace(void)
 	}
 
 	if (ps.tos == 2 && code.len == 1 && code.st[0] == '}')
-		ps.line_kind = lk_func_end;
+		out.line_kind = lk_func_end;
 
 	parse(psym_rbrace);
 }
@@ -978,7 +978,7 @@ process_preprocessing(void)
 			state_stack[ifdef_level++] = ps;
 		else
 			diag(1, "#if stack overflow");
-		ps.line_kind = lk_if;
+		out.line_kind = lk_if;
 
 	} else if (substring_starts_with(dir, "el")) {	/* else, elif */
 		if (ifdef_level <= 0)
@@ -992,7 +992,7 @@ process_preprocessing(void)
 			diag(1, "Unmatched #endif");
 		else
 			ifdef_level--;
-		ps.line_kind = lk_endif;
+		out.line_kind = lk_endif;
 
 	} else {
 		if (!substring_equals(dir, "pragma") &&

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.155 src/usr.bin/indent/indent.h:1.156
--- src/usr.bin/indent/indent.h:1.155	Sat May 20 11:19:17 2023
+++ src/usr.bin/indent/indent.h	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.155 2023/05/20 11:19:17 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.156 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -264,6 +264,9 @@ typedef struct paren_level_props {
 /*
  * The parser state determines the layout of the formatted text.
  *
+ * At each '#if', the parser state is copied so that the corresponding '#else'
+ * lines start in the same state.
+ *
  * In a function body, the number of block braces determines the indentation
  * of statements and declarations.
  *
@@ -394,6 +397,14 @@ extern struct parser_state {
 	} declaration;
 	bool blank_line_after_decl;
 
+	/* Comments */
+
+	bool curr_col_1;	/* whether the current token started in column
+				 * 1 of the original input */
+	bool next_col_1;
+} ps;
+
+extern struct output_state {
 	enum line_kind {
 		lk_other,
 		lk_if,		/* #if, #ifdef, #ifndef */
@@ -405,12 +416,9 @@ extern struct parser_state {
 				 * line; used for inserting blank lines */
 	enum line_kind prev_line_kind;
 
-	/* Comments */
-
-	bool curr_col_1;	/* whether the current token started in column
-				 * 1 of the original input */
-	bool next_col_1;
-} ps;
+	struct buffer indent_off_text;	/* text from between 'INDENT OFF' and
+					 * 'INDENT ON', both inclusive */
+} out;
 
 
 #define array_length(array) (sizeof(array) / sizeof((array)[0]))
@@ -425,6 +433,7 @@ void debug_buffers(void);
 extern const char *const lsym_name[];
 extern const char *const psym_name[];
 extern const char *const paren_level_cast_name[];
+extern const char *const line_kind_name[];
 #else
 #define debug_noop() do { } while (false)
 #define	debug_printf(fmt, ...) debug_noop()
@@ -442,7 +451,6 @@ int ind_add(int, const char *, size_t);
 
 void inp_skip(void);
 char inp_next(void);
-void clear_indent_off_text(void);
 
 lexer_symbol lexi(void);
 void diag(int, const char *, ...) __printflike(2, 3);

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.182 src/usr.bin/indent/io.c:1.183
--- src/usr.bin/indent/io.c:1.182	Sat May 20 11:19:17 2023
+++ src/usr.bin/indent/io.c	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: io.c,v 1.182 2023/05/20 11:19:17 rillig Exp $	*/
+/*	$NetBSD: io.c,v 1.183 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,18 +38,17 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.182 2023/05/20 11:19:17 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.183 2023/05/20 11:53:53 rillig Exp $");
 
 #include <stdio.h>
 
 #include "indent.h"
 
 struct buffer inp;
-static struct buffer indent_off_text;	/* text from between 'INDENT OFF' and
-					 * 'INDENT ON', both inclusive */
-static unsigned wrote_newlines = 2;	/* 0 in the middle of a line, 1 after
-					 * a single '\n', > 1 means there were
-					 * (n - 1) blank lines above */
+struct output_state out;
+static unsigned wrote_newlines = 2;	/* 0 in the middle of a line, 1 after a
+					 * single '\n', > 1 means there were (n
+					 * - 1) blank lines above */
 static int paren_indent;
 
 
@@ -139,19 +138,24 @@ output_indent(int old_ind, int new_ind)
 static bool
 want_blank_line(void)
 {
+	debug_println("%s: %s -> %s", __func__,
+	    line_kind_name[out.prev_line_kind], line_kind_name[out.line_kind]);
+
 	if (ps.blank_line_after_decl && ps.declaration == decl_no) {
 		ps.blank_line_after_decl = false;
 		return true;
 	}
 	if (opt.blanklines_around_conditional_compilation) {
-		if (ps.prev_line_kind != lk_if && ps.line_kind == lk_if)
+		if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
 			return true;
-		if (ps.prev_line_kind == lk_endif && ps.line_kind != lk_endif)
+		if (out.prev_line_kind == lk_endif
+		    && out.line_kind != lk_endif)
 			return true;
 	}
-	if (opt.blanklines_after_procs && ps.prev_line_kind == lk_func_end)
+	if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end)
 		return true;
-	if (opt.blanklines_before_block_comments && ps.line_kind == lk_block_comment)
+	if (opt.blanklines_before_block_comments
+	    && out.line_kind == lk_block_comment)
 		return true;
 	return false;
 }
@@ -274,8 +278,8 @@ output_line(void)
 
 	if (indent_enabled == indent_last_off_line) {
 		indent_enabled = indent_on;
-		output_range(indent_off_text.st, indent_off_text.len);
-		indent_off_text.len = 0;
+		output_range(out.indent_off_text.st, out.indent_off_text.len);
+		out.indent_off_text.len = 0;
 	}
 
 	ps.decl_on_line = ps.in_decl;	/* for proper comment indentation */
@@ -298,8 +302,8 @@ output_line(void)
 	}
 
 	ps.want_blank = false;
-	ps.prev_line_kind = ps.line_kind;
-	ps.line_kind = lk_other;
+	out.prev_line_kind = out.line_kind;
+	out.line_kind = lk_other;
 }
 
 static int
@@ -361,13 +365,7 @@ void
 inp_read_line(void)
 {
 	if (indent_enabled == indent_on)
-		indent_off_text.len = 0;
-	buf_add_chars(&indent_off_text, inp.mem, inp.len);
+		out.indent_off_text.len = 0;
+	buf_add_chars(&out.indent_off_text, inp.mem, inp.len);
 	inp_read_next_line(input);
 }
-
-void
-clear_indent_off_text(void)
-{
-	indent_off_text.len = 0;
-}

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.201 src/usr.bin/indent/lexi.c:1.202
--- src/usr.bin/indent/lexi.c:1.201	Sat May 20 01:28:14 2023
+++ src/usr.bin/indent/lexi.c	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.201 2023/05/20 01:28:14 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.202 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.201 2023/05/20 01:28:14 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.202 2023/05/20 11:53:53 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -657,7 +657,7 @@ lexi(void)
 			enum indent_enabled prev = indent_enabled;
 			lex_indent_comment();
 			if (prev == indent_on && indent_enabled == indent_off)
-				clear_indent_off_text();
+				out.indent_off_text.len = 0;
 			token_add_char(*inp.st++);
 			lsym = lsym_comment;
 			next_unary = ps.next_unary;

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.147 src/usr.bin/indent/pr_comment.c:1.148
--- src/usr.bin/indent/pr_comment.c:1.147	Sat May 20 11:19:17 2023
+++ src/usr.bin/indent/pr_comment.c	Sat May 20 11:53:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: pr_comment.c,v 1.147 2023/05/20 11:19:17 rillig Exp $	*/
+/*	$NetBSD: pr_comment.c,v 1.148 2023/05/20 11:53:53 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.147 2023/05/20 11:19:17 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.148 2023/05/20 11:53:53 rillig Exp $");
 
 #include <string.h>
 
@@ -93,7 +93,7 @@ analyze_comment(bool *p_may_wrap, bool *
 			delim = false;
 		}
 		if (code.len == 0 && inp.st[strspn(inp.st, "*")] == '\n')
-			ps.line_kind = lk_block_comment;
+			out.line_kind = lk_block_comment;
 
 		if (com.len > 0)
 			output_line();

Reply via email to