Module Name: src
Committed By: rillig
Date: Tue May 16 07:13:05 UTC 2023
Modified Files:
src/usr.bin/indent: indent.h io.c lexi.c
Log Message:
indent: move parsing of 'INDENT OFF/ON' comments to the lexer
No functional change.
To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.171 -r1.172 src/usr.bin/indent/io.c
cvs rdiff -u -r1.192 -r1.193 src/usr.bin/indent/lexi.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/indent.h
diff -u src/usr.bin/indent/indent.h:1.142 src/usr.bin/indent/indent.h:1.143
--- src/usr.bin/indent/indent.h:1.142 Mon May 15 22:52:21 2023
+++ src/usr.bin/indent/indent.h Tue May 16 07:13:05 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.142 2023/05/15 22:52:21 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.143 2023/05/16 07:13:05 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -414,6 +414,7 @@ void inp_skip(void);
char inp_next(void);
lexer_symbol lexi(void);
+void lex_indent_comment(void);
void diag(int, const char *, ...) __printflike(2, 3);
void output_line(void);
void output_line_ff(void);
Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.171 src/usr.bin/indent/io.c:1.172
--- src/usr.bin/indent/io.c:1.171 Mon May 15 22:35:41 2023
+++ src/usr.bin/indent/io.c Tue May 16 07:13:05 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.171 2023/05/15 22:35:41 rillig Exp $ */
+/* $NetBSD: io.c,v 1.172 2023/05/16 07:13:05 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.171 2023/05/15 22:35:41 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.172 2023/05/16 07:13:05 rillig Exp $");
#include <stdio.h>
#include <string.h>
@@ -359,62 +359,12 @@ compute_label_indent(void)
return opt.indent_size * (ps.ind_level - 2);
}
-static void
-skip_blank(const char **pp)
-{
- while (ch_isblank(**pp))
- (*pp)++;
-}
-
-static bool
-skip_string(const char **pp, const char *s)
-{
- size_t len = strlen(s);
- if (strncmp(*pp, s, len) == 0) {
- *pp += len;
- return true;
- }
- return false;
-}
-
-static void
-parse_indent_comment(void)
-{
- bool on;
-
- const char *p = inp.mem;
-
- skip_blank(&p);
- if (!skip_string(&p, "/*"))
- return;
- skip_blank(&p);
- if (!skip_string(&p, "INDENT"))
- return;
-
- skip_blank(&p);
- if (*p == '*' || skip_string(&p, "ON"))
- on = true;
- else if (skip_string(&p, "OFF"))
- on = false;
- else
- return;
-
- skip_blank(&p);
- if (!skip_string(&p, "*/\n"))
- return;
-
- if (lab.len > 0 || code.len > 0 || com.len > 0)
- output_line();
-
- inhibit_formatting = !on;
-}
-
void
inp_read_line(void)
{
inp_read_next_line(input);
- parse_indent_comment();
+ lex_indent_comment();
if (inhibit_formatting)
output_range(inp.st, inp.len);
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.192 src/usr.bin/indent/lexi.c:1.193
--- src/usr.bin/indent/lexi.c:1.192 Mon May 15 22:52:21 2023
+++ src/usr.bin/indent/lexi.c Tue May 16 07:13:05 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.192 2023/05/15 22:52:21 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.193 2023/05/16 07:13:05 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.192 2023/05/15 22:52:21 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.193 2023/05/16 07:13:05 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -470,6 +470,56 @@ lex_asterisk_unary(void)
}
}
+static void
+skip_blank(const char **pp)
+{
+ while (ch_isblank(**pp))
+ (*pp)++;
+}
+
+static bool
+skip_string(const char **pp, const char *s)
+{
+ size_t len = strlen(s);
+ if (strncmp(*pp, s, len) == 0) {
+ *pp += len;
+ return true;
+ }
+ return false;
+}
+
+void
+lex_indent_comment(void)
+{
+ bool on;
+
+ const char *p = inp_line_start();
+
+ skip_blank(&p);
+ if (!skip_string(&p, "/*"))
+ return;
+ skip_blank(&p);
+ if (!skip_string(&p, "INDENT"))
+ return;
+
+ skip_blank(&p);
+ if (*p == '*' || skip_string(&p, "ON"))
+ on = true;
+ else if (skip_string(&p, "OFF"))
+ on = false;
+ else
+ return;
+
+ skip_blank(&p);
+ if (!skip_string(&p, "*/\n"))
+ return;
+
+ if (lab.len > 0 || code.len > 0 || com.len > 0)
+ output_line();
+
+ inhibit_formatting = !on;
+}
+
/* Reads the next token, placing it in the global variable "token". */
lexer_symbol
lexi(void)