Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: move parsing of 'INDENT OFF/ON' comme...



details:   https://anonhg.NetBSD.org/src/rev/bcd1294163c3
branches:  trunk
changeset: 375381:bcd1294163c3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue May 16 07:13:05 2023 +0000

description:
indent: move parsing of 'INDENT OFF/ON' comments to the lexer

No functional change.

diffstat:

 usr.bin/indent/indent.h |   3 +-
 usr.bin/indent/io.c     |  56 ++----------------------------------------------
 usr.bin/indent/lexi.c   |  54 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 57 insertions(+), 56 deletions(-)

diffs (174 lines):

diff -r c7899a4e1b9d -r bcd1294163c3 usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Mon May 15 22:52:21 2023 +0000
+++ b/usr.bin/indent/indent.h   Tue May 16 07:13:05 2023 +0000
@@ -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);
diff -r c7899a4e1b9d -r bcd1294163c3 usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Mon May 15 22:52:21 2023 +0000
+++ b/usr.bin/indent/io.c       Tue May 16 07:13:05 2023 +0000
@@ -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);
diff -r c7899a4e1b9d -r bcd1294163c3 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Mon May 15 22:52:21 2023 +0000
+++ b/usr.bin/indent/lexi.c     Tue May 16 07:13:05 2023 +0000
@@ -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)



Home | Main Index | Thread Index | Old Index