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: directly access the input buffer



details:   https://anonhg.NetBSD.org/src/rev/43a82160e21b
branches:  trunk
changeset: 375842:43a82160e21b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue May 16 13:26:26 2023 +0000

description:
indent: directly access the input buffer

No functional change.

diffstat:

 usr.bin/indent/indent.c     |  20 ++++----
 usr.bin/indent/indent.h     |  15 ++++--
 usr.bin/indent/io.c         |  36 +--------------
 usr.bin/indent/lexi.c       |  98 ++++++++++++++++++++++----------------------
 usr.bin/indent/pr_comment.c |  44 ++++++++++----------
 5 files changed, 95 insertions(+), 118 deletions(-)

diffs (truncated from 561 to 300 lines):

diff -r b5d0d4d00d32 -r 43a82160e21b usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue May 16 13:20:38 2023 +0000
+++ b/usr.bin/indent/indent.c   Tue May 16 13:26:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.289 2023/05/16 11:32:01 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.290 2023/05/16 13:26:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.289 2023/05/16 11:32:01 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.290 2023/05/16 13:26:26 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -291,7 +291,7 @@ main_prepare_parsing(void)
     inp_read_line();
 
     int ind = 0;
-    for (const char *p = inp_p();; p++) {
+    for (const char *p = inp.st;; p++) {
        if (*p == ' ')
            ind++;
        else if (*p == '\t')
@@ -886,10 +886,10 @@ read_preprocessing_line(void)
 
     buf_add_char(&lab, '#');
 
-    while (ch_isblank(inp_peek()))
-       buf_add_char(&lab, inp_next());
+    while (ch_isblank(inp.st[0]))
+       buf_add_char(&lab, *inp.st++);
 
-    while (inp_peek() != '\n' || (state == COMM && !had_eof)) {
+    while (inp.st[0] != '\n' || (state == COMM && !had_eof)) {
        buf_add_char(&lab, inp_next());
        switch (lab.mem[lab.len - 1]) {
        case '\\':
@@ -897,9 +897,9 @@ read_preprocessing_line(void)
                buf_add_char(&lab, inp_next());
            break;
        case '/':
-           if (inp_peek() == '*' && state == PLAIN) {
+           if (inp.st[0] == '*' && state == PLAIN) {
                state = COMM;
-               buf_add_char(&lab, inp_next());
+               buf_add_char(&lab, *inp.st++);
            }
            break;
        case '"':
@@ -915,9 +915,9 @@ read_preprocessing_line(void)
                state = CHR;
            break;
        case '*':
-           if (inp_peek() == '/' && state == COMM) {
+           if (inp.st[0] == '/' && state == COMM) {
                state = PLAIN;
-               buf_add_char(&lab, inp_next());
+               buf_add_char(&lab, *inp.st++);
            }
            break;
        }
diff -r b5d0d4d00d32 -r 43a82160e21b usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Tue May 16 13:20:38 2023 +0000
+++ b/usr.bin/indent/indent.h   Tue May 16 13:26:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.145 2023/05/16 11:32:01 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.146 2023/05/16 13:26:26 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -133,6 +133,15 @@ struct buffer {
 extern FILE *input;
 extern FILE *output;
 
+/*
+ * The current line from the input file, used by the lexer to generate tokens.
+ * To read from the line, start at inp.st and continue up to and including the
+ * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
+ * make the next line available, invalidating any pointers into the previous
+ * line.
+ */
+extern struct buffer inp;
+
 extern struct buffer token;    /* the current token to be processed, is
                                 * typically copied to the buffer 'code', or
                                 * in some cases to 'lab'. */
@@ -409,10 +418,6 @@ int compute_code_indent(void);
 int compute_label_indent(void);
 int ind_add(int, const char *, size_t);
 
-const char *inp_p(void);
-const char *inp_line_start(void);
-char inp_peek(void);
-char inp_lookahead(size_t);
 void inp_skip(void);
 char inp_next(void);
 void clear_indent_off_text(void);
diff -r b5d0d4d00d32 -r 43a82160e21b usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Tue May 16 13:20:38 2023 +0000
+++ b/usr.bin/indent/io.c       Tue May 16 13:26:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.174 2023/05/16 11:32:01 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.175 2023/05/16 13:26:26 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,47 +38,19 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.174 2023/05/16 11:32:01 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.175 2023/05/16 13:26:26 rillig Exp $");
 
 #include <stdio.h>
 #include <string.h>
 
 #include "indent.h"
 
-/*
- * The current line, ready to be split into tokens, terminated with '\n'. The
- * current read position is inp.s, and the invariant inp.s < inp.e holds.
- */
-static struct buffer inp;
+struct buffer inp;
 static struct buffer indent_off_text;
 
 static int paren_indent;
 
 
-const char *
-inp_p(void)
-{
-    return inp.st;
-}
-
-const char *
-inp_line_start(void)
-{
-    return inp.mem;
-}
-
-char
-inp_peek(void)
-{
-    return *inp.st;
-}
-
-char
-inp_lookahead(size_t i)
-{
-    return inp.st[i];
-}
-
 void
 inp_skip(void)
 {
@@ -90,7 +62,7 @@ inp_skip(void)
 char
 inp_next(void)
 {
-    char ch = inp_peek();
+    char ch = inp.st[0];
     inp_skip();
     return ch;
 }
diff -r b5d0d4d00d32 -r 43a82160e21b usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Tue May 16 13:20:38 2023 +0000
+++ b/usr.bin/indent/lexi.c     Tue May 16 13:26:26 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.196 2023/05/16 12:46:43 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.197 2023/05/16 13:26:26 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.196 2023/05/16 12:46:43 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.197 2023/05/16 13:26:26 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -178,9 +178,9 @@ static void
 lex_number(void)
 {
     for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
-       unsigned char ch = (unsigned char)inp_peek();
-       if (ch == '\\' && inp_lookahead(1) == '\n') {
-           inp_skip();
+       unsigned char ch = (unsigned char)inp.st[0];
+       if (ch == '\\' && inp.st[1] == '\n') {
+           inp.st++;
            inp_skip();
            line_no++;
            continue;
@@ -218,10 +218,10 @@ static void
 lex_word(void)
 {
     for (;;) {
-       if (is_identifier_part(inp_peek()))
-           token_add_char(inp_next());
-       else if (inp_peek() == '\\' && inp_lookahead(1) == '\n') {
-           inp_skip();
+       if (is_identifier_part(inp.st[0]))
+           token_add_char(*inp.st++);
+       else if (inp.st[0] == '\\' && inp.st[1] == '\n') {
+           inp.st++;
            inp_skip();
            line_no++;
        } else
@@ -233,17 +233,17 @@ static void
 lex_char_or_string(void)
 {
     for (char delim = token.mem[token.len - 1];;) {
-       if (inp_peek() == '\n') {
+       if (inp.st[0] == '\n') {
            diag(1, "Unterminated literal");
            return;
        }
 
-       token_add_char(inp_next());
+       token_add_char(*inp.st++);
        if (token.mem[token.len - 1] == delim)
            return;
 
        if (token.mem[token.len - 1] == '\\') {
-           if (inp_peek() == '\n')
+           if (inp.st[0] == '\n')
                ++line_no;
            token_add_char(inp_next());
        }
@@ -260,10 +260,10 @@ probably_typename(void)
        return false;
     if (ps.in_stmt_or_decl)    /* XXX: this condition looks incorrect */
        return false;
-    if (inp_peek() == '*' && inp_lookahead(1) != '=')
+    if (inp.st[0] == '*' && inp.st[1] != '=')
        goto maybe;
     /* XXX: is_identifier_start */
-    if (ch_isalpha(inp_peek()))
+    if (ch_isalpha(inp.st[0]))
        goto maybe;
     return false;
 maybe:
@@ -316,7 +316,7 @@ static bool
 probably_looking_at_definition(void)
 {
     int paren_level = 0;
-    for (const char *p = inp_p(); *p != '\n'; p++) {
+    for (const char *p = inp.st; *p != '\n'; p++) {
        if (*p == '(')
            paren_level++;
        if (*p == ')' && --paren_level == 0) {
@@ -353,15 +353,15 @@ probably_looking_at_definition(void)
 static lexer_symbol
 lexi_alnum(void)
 {
-    if (ch_isdigit(inp_peek()) ||
-           (inp_peek() == '.' && ch_isdigit(inp_lookahead(1)))) {
+    if (ch_isdigit(inp.st[0]) ||
+           (inp.st[0] == '.' && ch_isdigit(inp.st[1]))) {
        lex_number();
-    } else if (is_identifier_start(inp_peek())) {
+    } else if (is_identifier_start(inp.st[0])) {
        lex_word();
 
        if (token.len == 1 && token.st[0] == 'L' &&
-               (inp_peek() == '"' || inp_peek() == '\'')) {
-           token_add_char(inp_next());
+               (inp.st[0] == '"' || inp.st[0] == '\'')) {
+           token_add_char(*inp.st++);
            lex_char_or_string();
            ps.next_unary = false;
            return lsym_word;
@@ -369,8 +369,8 @@ lexi_alnum(void)
     } else
        return lsym_eof;        /* just as a placeholder */
 
-    while (ch_isblank(inp_peek()))
-       inp_skip();
+    while (ch_isblank(inp.st[0]))
+       inp.st++;
 
     ps.next_unary = ps.prev_token == lsym_tag
        || ps.prev_token == lsym_typedef;
@@ -415,7 +415,7 @@ found_typename:
        }
     }
 
-    if (inp_peek() == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
+    if (inp.st[0] == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
            !ps.in_func_def_params && !ps.block_init) {
 
        if (ps.nparen == 0 && probably_looking_at_definition()) {
@@ -447,14 +447,14 @@ is_asterisk_unary(void)



Home | Main Index | Thread Index | Old Index