indent: extract the output state from the parser state
The parser state depends on the preprocessing lines, the output state shouldn't.
This commit is contained in:
parent
5a9a628018
commit
4fb425a29f
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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") &&
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 *p_delim, int *p_line_length)
|
|||
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();
|
||||
|
|
Loading…
Reference in New Issue