From 78dca0dae51ff358f43a05765fa70d76f55b0bbd Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 22 Aug 2019 16:36:59 +0200 Subject: [PATCH] xgettext: Handle newlines in Lua long bracket literal strings correctly. Reported by Frans de Jonge at . * 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 | 51 ++++++++++++++++-------------- gettext-tools/tests/xgettext-lua-1 | 22 +++++++++++++ gettext-tools/tests/xgettext-lua-2 | 2 +- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/gettext-tools/src/x-lua.c b/gettext-tools/src/x-lua.c index 3a2296f36..8db7561ba 100644 --- a/gettext-tools/src/x-lua.c +++ b/gettext-tools/src/x-lua.c @@ -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 , 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; diff --git a/gettext-tools/tests/xgettext-lua-1 b/gettext-tools/tests/xgettext-lua-1 index a41c313e1..7e4715fbc 100755 --- a/gettext-tools/tests/xgettext-lua-1 +++ b/gettext-tools/tests/xgettext-lua-1 @@ -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 "" diff --git a/gettext-tools/tests/xgettext-lua-2 b/gettext-tools/tests/xgettext-lua-2 index 26eac14ed..c92cad4e3 100755 --- a/gettext-tools/tests/xgettext-lua-2 +++ b/gettext-tools/tests/xgettext-lua-2 @@ -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. -- 2.47.2