gbranden pushed a commit to branch master
in repository groff.

commit dd7bf97938aa0c22b0528da85b2076a8f1e20b7b
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun Nov 23 16:31:23 2025 -0600

    [troff]: Refactor new token description logic.
    
    ...into its own function, since there is a second place it can be used.
    
    * src/roff/troff/token.h (class token): Declare new `describe_node()`
      member function.
    
    * src/roff/troff/input.cpp (token::description): Move the logic from
      here...
      (token::describe_node): ...to this new member function.
    
      (token::description): Call the new member function.
    
    Also annotate null pointers with `nullptr` comment to ease any future
    transition to C++11, which defines it as a keyword.
---
 ChangeLog                | 12 ++++++++++++
 src/roff/troff/input.cpp | 45 +++++++++++++++++++++++++++++----------------
 src/roff/troff/token.h   |  1 +
 3 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9d6f99894..a143ac0d8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2025-11-23  G. Branden Robinson <[email protected]>
+
+       [troff]: Refactor new token description logic into its own
+       function, since there is a second place it can be used.
+
+       * src/roff/troff/token.h (class token): Declare new
+       `describe_node()` member function.
+       * src/roff/troff/input.cpp (token::description): Move the logic
+       from here...
+       (token::describe_node): ...to this new member function.
+       (token::description): Call the new member function.
+
 2025-11-23  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/input.cpp
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index aa06eee1b..384171204 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -2795,6 +2795,29 @@ static bool is_char_usable_as_delimiter(int c)
   }
 }
 
+void token::describe_node(char *buf, size_t bufsz)
+{
+  assert(nd != 0 /* nullptr */);
+  if (0 /* nullptr */ == nd) {
+    (void) snprintf(buf, bufsz, "a null(!) node");
+    return;
+  }
+  // Ah, the joys of computational natural language grammar.
+  const char *ndtype = nd->type();
+  const char initial_letter = ndtype[0];
+  bool is_vowelly = false;
+  // I wonder if Kernighan thought that the presence of set types and an
+  // "in" operator was one of Pascal's great blunders.  --GBR
+  if (('a' == initial_letter)
+      || ('e' == initial_letter)
+      || ('i' == initial_letter)
+      || ('o' == initial_letter)
+      || ('u' == initial_letter))
+    is_vowelly = true;
+  (void) memset(buf, 0, bufsz);
+  (void) snprintf(buf, bufsz, "a%s %s", is_vowelly ? "n" : "", ndtype);
+}
+
 // Is the token a valid delimiter (like `'`)?
 bool token::is_usable_as_delimiter(bool report_error,
                                   enum delimiter_context context)
@@ -2897,9 +2920,11 @@ const char *token::description()
   //   "character code XXX"
   //   "special character 'bracketrighttp'"
   //   "indexed character -2147483648"
+  //   "space character horizontal motion node token"
   // Future:
   //   "character code XXX (U+XXXX)" or similar
-  const size_t maxstr = sizeof "special character 'bracketrighttp'";
+  const size_t maxstr
+    = sizeof "space character horizontal motion node token";
   const size_t bufsz = maxstr + 2; // for trailing '"' and null
   static char buf[bufsz];
   (void) memset(buf, 0, bufsz);
@@ -2940,21 +2965,9 @@ const char *token::description()
   case TOKEN_NEWLINE:
     return "a newline";
   case TOKEN_NODE:
-    // Ah, the joys of computational natural language grammar.
     {
-      const char *ndtype = nd->type();
-      const char initial_letter = ndtype[0];
-      bool is_vowelly = false;
-      // I wonder if Kernighan thought that the presence of set types
-      // and an "in" operator was one of Pascal's great blunders.  --GBR
-      if (('a' == initial_letter)
-         || ('e' == initial_letter)
-         || ('i' == initial_letter)
-         || ('o' == initial_letter)
-         || ('u' == initial_letter))
-       is_vowelly = true;
-      (void) snprintf(buf, bufsz, "a%s %s token", is_vowelly ? "n" : "",
-                     ndtype);
+      static char nodebuf[bufsz];
+      describe_node(nodebuf, bufsz);
       return buf;
     }
   case TOKEN_INDEXED_CHAR:
@@ -8891,7 +8904,7 @@ void token::process()
   case TOKEN_NODE:
   case TOKEN_HORIZONTAL_SPACE:
     curenv->add_node(nd);
-    nd = 0;
+    nd = 0 /* nullptr */;
     break;
   case TOKEN_INDEXED_CHAR:
     // Optimize `curenv->add_char(get_charinfo())` for token type.
diff --git a/src/roff/troff/token.h b/src/roff/troff/token.h
index 3435cc5e6..1a5f8b4d4 100644
--- a/src/roff/troff/token.h
+++ b/src/roff/troff/token.h
@@ -115,6 +115,7 @@ public:
   bool add_to_zero_width_node_list(node **);
   void make_space();
   void make_newline();
+  void describe_node(char * /* buf */, size_t /* bufsz */);
   const char *description();
 
   friend void process_input_stack();

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to