]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: D: Allow comments between the parts of concatenated string literals.
authorBruno Haible <bruno@clisp.org>
Wed, 23 Apr 2025 17:50:04 +0000 (19:50 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 23 Apr 2025 17:50:04 +0000 (19:50 +0200)
* gettext-tools/src/x-d.c (is_add_expression_with_tilde): New function.
(is_string_literal, string_literal_accumulate_pieces): Use it.
* gettext-tools/tests/xgettext-d-2: Add another test case regarding string
concatenation.

gettext-tools/src/x-d.c
gettext-tools/tests/xgettext-d-2

index 844d632c5297a5978a24ed655760c089a2232ac8..142ac62d439bf21433f9c14622cf2c0e1d21289f 100644 (file)
@@ -380,8 +380,48 @@ static void handle_comments (TSNode node)
 
 /* ---------------------------- String literals ---------------------------- */
 
+/* Determines whether NODE is an 'add_expression' with a '~' operator between
+   two operands.  If so, it returns the indices of the two operands.  */
+static bool
+is_add_expression_with_tilde (TSNode node,
+                              uint32_t *left_operand_index,
+                              uint32_t *right_operand_index)
+{
+  if (ts_node_symbol (node) == ts_symbol_add_expression)
+    {
+      uint32_t count = ts_node_child_count (node);
+      uint32_t other_subnodes = 0;
+      uint32_t i;
+      for (i = 0; i < count; i++)
+        {
+          TSNode subnode = ts_node_child (node, i);
+          if (!(ts_node_symbol (subnode) == ts_symbol_comment
+                || (ts_node_symbol (subnode) == ts_symbol_tilde
+                    && other_subnodes == 1)))
+            {
+              switch (other_subnodes)
+                {
+                case 0:
+                  *left_operand_index = i;
+                  break;
+                case 1:
+                  *right_operand_index = i;
+                  break;
+                case 2:
+                default:
+                  return false;
+                }
+              other_subnodes++;
+            }
+        }
+      return other_subnodes == 2;
+    }
+  else
+    return false;
+}
+
 /* Determines whether NODE represents a string literal or the concatenation
-   of string literals (via the '+' operator).  */
+   of string literals (via the '~' operator).  */
 static bool
 is_string_literal (TSNode node)
 {
@@ -430,14 +470,14 @@ is_string_literal (TSNode node)
       && ts_node_child_count (node) == 1)
     {
       TSNode subnode = ts_node_child (node, 0);
-      if (ts_node_symbol (subnode) == ts_symbol_add_expression
-          && ts_node_child_count (subnode) == 3
-          && ts_node_symbol (ts_node_child (subnode, 1)) == ts_symbol_tilde
+      uint32_t left_index;
+      uint32_t right_index;
+      if (is_add_expression_with_tilde (subnode, &left_index, &right_index)
           /* Recurse into the left and right subnodes.  */
-          && is_string_literal (ts_node_child (subnode, 2)))
+          && is_string_literal (ts_node_child (subnode, right_index)))
         {
-          /*return is_string_literal (ts_node_child (subnode, 0));*/
-          node = ts_node_child (subnode, 0);
+          /*return is_string_literal (ts_node_child (subnode, left_index));*/
+          node = ts_node_child (subnode, left_index);
           goto start;
         }
     }
@@ -750,14 +790,14 @@ string_literal_accumulate_pieces (TSNode node,
            && ts_node_child_count (node) == 1)
     {
       TSNode subnode = ts_node_child (node, 0);
-      if (ts_node_symbol (subnode) == ts_symbol_add_expression
-          && ts_node_child_count (subnode) == 3
-          && ts_node_symbol (ts_node_child (subnode, 1)) == ts_symbol_tilde)
+      uint32_t left_index;
+      uint32_t right_index;
+      if (is_add_expression_with_tilde (subnode, &left_index, &right_index))
         {
           /* Recurse into the left and right subnodes.  */
-          string_literal_accumulate_pieces (ts_node_child (subnode, 2), buffer);
-          /*string_literal_accumulate_pieces (ts_node_child (subnode, 0), buffer);*/
-          node = ts_node_child (subnode, 0);
+          string_literal_accumulate_pieces (ts_node_child (subnode, right_index), buffer);
+          /*string_literal_accumulate_pieces (ts_node_child (subnode, left_index), buffer);*/
+          node = ts_node_child (subnode, left_index);
           goto start;
         }
       else
index 3b09bf987b37c0438152eb38f5c2ce61b5c24f16..b302c6fd516b91c6be9d8a1ed9258858db0b9dc5 100755 (executable)
@@ -29,8 +29,12 @@ void main ()
   string test7 = gettext("Test " ~
   "String "
   ~ "7");
+  // a
+  string test8 = /* b */ gettext /* c */ ( /* d */ "Test " /* e */ ~ /* f */
+  /* g */ "String " /* h */
+  ~ "8");
   // Empty string.
-  string test8 = gettext("");
+  string test9 = gettext("");
 
   // printf expects a C format string.
   printf(gettext("depth %f"), 3.4);
@@ -103,6 +107,13 @@ msgstr ""
 msgid "Test String 7"
 msgstr ""
 
+#. a
+#. b
+#. c
+#. d
+msgid "Test String 8"
+msgstr ""
+
 #. Empty string.
 msgid ""
 msgstr ""