groff-commit
[Top][All Lists]
Advanced

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

[groff] 03/25: [troff]: Refactor has_arg() to support copy mode.


From: G. Branden Robinson
Subject: [groff] 03/25: [troff]: Refactor has_arg() to support copy mode.
Date: Fri, 19 Jul 2024 15:12:41 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit aefbf42b59e19f923c300d534a611dc3138ce954
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Tue Jul 16 11:21:09 2024 -0500

    [troff]: Refactor has_arg() to support copy mode.
    
    [troff]: Refactor `has_arg()` to optionally work in copy mode.
    
    * src/roff/troff/token.h (has_arg): Add Boolean parameter, defaulting to
      `false`, indicating that "peeking" ahead in the input stream is
      desired.  This is for use by requests that need to read their
      argument(s) in copy mode.
    
    * src/roff/troff/input.cpp (has_arg): Implement, moving code duplicated
      in `device_request` and `output_request`.
    
      (device_request, output_request): Drop duplicated code in favor of
      calling `has_arg()` with `true` argument.
---
 ChangeLog                | 13 +++++++++++++
 src/roff/troff/input.cpp | 46 ++++++++++++++++++++++------------------------
 src/roff/troff/token.h   |  2 +-
 3 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5c2bc1927..2c8245897 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-07-16  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       [troff]: Refactor `has_arg()` to optionally work in copy mode.
+
+       * src/roff/troff/token.h (has_arg): Add Boolean parameter,
+       defaulting to `false`, indicating that "peeking" ahead in the
+       input stream is desired.  This is for use by requests that need
+       to read their argument(s) in copy mode.
+       * src/roff/troff/input.cpp (has_arg): Implement, moving code
+       duplicated in `device_request` and `output_request`.
+       (device_request, output_request): Drop duplicated code in favor
+       of calling `has_arg()` with `true` argument.
+
 2024-07-16  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/input.cpp (device_request): Honor a `device`
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 80968a207..ae833c6f5 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -1853,11 +1853,25 @@ void token::skip()
     next();
 }
 
-bool has_arg()
+// Specify `want_peek` if request reads the next argument in copy mode.
+bool has_arg(bool want_peek)
 {
-  while (tok.is_space())
-    tok.next();
-  return !tok.is_newline();
+  if (want_peek) {
+    int c;
+    for (;;) {
+      c = input_stack::peek();
+      if (' ' == c)
+       (void) get_copy(0 /* nullptr */);
+      else
+       break;
+    }
+    return !(('\n' == c) || (EOF == c));
+  }
+  else {
+    while (tok.is_space())
+      tok.next();
+    return !tok.is_newline();
+  }
 }
 
 void token::make_space()
@@ -5672,21 +5686,13 @@ static node *do_device_control() // \X
 
 static void device_request()
 {
-  // We can't use `has_arg()` here because we want to read in copy mode.
-  int c;
-  for (;;) {
-    c = input_stack::peek();
-    if (' ' == c)
-      (void) get_copy(0 /* nullptr */);
-    else
-      break;
-  }
-  if (('\n' == c) || (EOF == c)) {
+  if (!has_arg(true /* peek; we want to read in copy mode */)) {
     warning(WARN_MISSING, "device control request expects arguments");
     skip_line();
     return;
   }
   macro mac;
+  int c;
   for (;;) {
     c = get_copy(0 /* nullptr */);
     if ('"' == c) {
@@ -5721,20 +5727,12 @@ static void device_macro_request()
 
 static void output_request()
 {
-  // We can't use `has_arg()` here because we want to read in copy mode.
-  int c;
-  for (;;) {
-    c = input_stack::peek();
-    if (' ' == c)
-      (void) get_copy(0 /* nullptr */);
-    else
-      break;
-  }
-  if (('\n' == c) || (EOF == c)) {
+  if (!has_arg(true /* peek; we want to read in copy mode */)) {
     warning(WARN_MISSING, "output request expects arguments");
     skip_line();
     return;
   }
+  int c;
   for (;;) {
     c = get_copy(0 /* nullptr */);
     if ('"' == c) {
diff --git a/src/roff/troff/token.h b/src/roff/troff/token.h
index f9e2e6ec0..eb94873ae 100644
--- a/src/roff/troff/token.h
+++ b/src/roff/troff/token.h
@@ -240,7 +240,7 @@ inline bool token::is_zero_width_break()
   return type == TOKEN_ZERO_WIDTH_BREAK;
 }
 
-bool has_arg();
+bool has_arg(bool = false /* want_peek */);
 
 // Local Variables:
 // fill-column: 72



reply via email to

[Prev in Thread] Current Thread [Next in Thread]