]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Handle newlines in Lua long bracket literal strings correctly.
authorBruno Haible <bruno@clisp.org>
Thu, 22 Aug 2019 14:36:59 +0000 (16:36 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 13 Apr 2020 11:09:24 +0000 (13:09 +0200)
Reported by Frans de Jonge <fransdejonge@gmail.com>
at <https://savannah.gnu.org/bugs/?56794>.

* gettext-tools/src/x-lua.c (phase3_get): Test for EOF first. After an opening
long bracket, swallow one newline.
* gettext-tools/tests/xgettext-lua-1: Add tests for newlines in long brackets.
* gettext-tools/tests/xgettext-lua-2: Update comment.

gettext-tools/src/x-lua.c
gettext-tools/tests/xgettext-lua-1
gettext-tools/tests/xgettext-lua-2

index 3a2296f3649080df5f18c856109c98d6febf0dc4..8db7561ba1b7dd5c42dc88e18b34bd6191c136d6 100644 (file)
@@ -1,5 +1,5 @@
 /* xgettext Lua backend.
-   Copyright (C) 2012-2013, 2016, 2018 Free Software Foundation, Inc.
+   Copyright (C) 2012-2013, 2016, 2018-2019 Free Software Foundation, Inc.
 
    This file was written by Ľubomír Remák <lubomirr@lubomirr.eu>, 2012.
 
@@ -46,8 +46,9 @@
 
 #define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
 
-/* The Lua syntax is defined in the Lua manual section 9,
+/* The Lua syntax is defined in the Lua manual sections 3.1 and 9,
    which can be found at
+   https://www.lua.org/manual/5.2/manual.html#3.1
    https://www.lua.org/manual/5.2/manual.html#9  */
 
 /* If true extract all strings.  */
@@ -599,6 +600,16 @@ phase3_get (token_ty *tp)
               /* We need unprocessed characters from phase 1.  */
               c = phase1_getc ();
 
+              if (c == EOF || c == c_start || c == '\n')
+                {
+                  /* End of string.  */
+                  string_end ();
+                  tp->string = xstrdup (string_buf);
+                  tp->comment = add_reference (savable_comment);
+                  tp->type = token_type_string;
+                  return;
+                }
+
               /* We got '\', this is probably an escape sequence.  */
               if (c == '\\')
                 {
@@ -696,15 +707,6 @@ phase3_get (token_ty *tp)
                         string_add (c);
                     }
                 }
-              else if (c == c_start || c == EOF || c == '\n')
-                {
-                  /* End of string.  */
-                  string_end ();
-                  tp->string = xstrdup (string_buf);
-                  tp->comment = add_reference (savable_comment);
-                  tp->type = token_type_string;
-                  return;
-                }
               else
                 string_add (c);
             }
@@ -738,12 +740,26 @@ phase3_get (token_ty *tp)
                 continue;
             }
 
+          /* Found an opening long bracket.  */
           string_start ();
 
+          /* See if it is immediately followed by a newline.  */
+          c = phase1_getc ();
+          if (c != '\n')
+            phase1_ungetc (c);
+
           for (;;)
             {
               c = phase1_getc ();
 
+              if (c == EOF)
+                {
+                  string_end ();
+                  tp->string = xstrdup (string_buf);
+                  tp->comment = add_reference (savable_comment);
+                  tp->type = token_type_string;
+                  return;
+                }
               if (c == ']')
                 {
                   c = phase1_getc ();
@@ -785,18 +801,7 @@ phase3_get (token_ty *tp)
                     }
                 }
               else
-                {
-                  if (c == EOF)
-                    {
-                      string_end ();
-                      tp->string = xstrdup (string_buf);
-                      tp->comment = add_reference (savable_comment);
-                      tp->type = token_type_string;
-                      return;
-                    }
-                  else
-                    string_add (c);
-                }
+                string_add (c);
             }
           break;
 
index a41c313e1704f1357f9d567128c663b43d152053..7e4715fbcbc4708c1969a158459a417092f14a84 100755 (executable)
@@ -14,6 +14,15 @@ print(_([[I like brackets.]]))
 print(_([===[Brackets are awesome!]===]))
 print(_([===[==[Even nested brackets]==]===]))
 print(_([===[Or even unmached number of '=' signs]==]===]))
+print(_([=[
+First newline in long bracket is ignored.]=]))
+print(_([=[
+
+Second newline in long bracket is extracted.]=]))
+print(_([=[
+Newlines inside a long bracket string
+and at the end are not special.
+]=]))
 print(_(hmm["nope"]))
 print({_"yep"})
 print(_["nope"])
@@ -60,6 +69,19 @@ msgstr ""
 msgid "Or even unmached number of '=' signs]=="
 msgstr ""
 
+msgid "First newline in long bracket is ignored."
+msgstr ""
+
+msgid ""
+"\n"
+"Second newline in long bracket is extracted."
+msgstr ""
+
+msgid ""
+"Newlines inside a long bracket string\n"
+"and at the end are not special.\n"
+msgstr ""
+
 msgid "yep"
 msgstr ""
 
index 26eac14ed3d2847bbcc79170a18a714136b586d5..c92cad4e3c7d3f461d4405971b8465eeb1f75821 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/sh
 . "${srcdir=.}/init.sh"; path_prepend_ . ../src
 
-# Test Lua support
+# Test Lua comment syntax
 
 cat <<\EOF > xg-lu-2.lua
 -- This comment won't be extracted.