diff --git a/src/bin/elua/apps/xgettext.lua b/src/bin/elua/apps/xgettext.lua index b85eb06ec3..18e3315fa5 100644 --- a/src/bin/elua/apps/xgettext.lua +++ b/src/bin/elua/apps/xgettext.lua @@ -14,7 +14,7 @@ local input_sources = {} local search_dirs = {} local excluded_files = {} local keywords = {} -local flags = {} +local flags = { valid = {} } local opts_final = {} local opts_nolua = {} diff --git a/src/bin/elua/modules/xgettext/lexer.lua b/src/bin/elua/modules/xgettext/lexer.lua index d980d7afdf..54c67ad0e5 100644 --- a/src/bin/elua/modules/xgettext/lexer.lua +++ b/src/bin/elua/modules/xgettext/lexer.lua @@ -144,14 +144,20 @@ local read_string = function(ls) return tconc(buf) end +local last_comment = false + local match_comment = function(ls, cmt) + cmt = cmt:match("^%s*(.+)%s*$") + if ls.flags[cmt] then + return "", cmt + end local lcmt = ls.lex_cmt if not lcmt then return nil end if type(lcmt) ~= "string" then return "", cmt end - lcmt = lcmt:match("^%s*(.+)$") - if cmt:match("^%s*(.+)$"):sub(1, #lcmt) == lcmt then + lcmt = lcmt:match("^%s*(.+)%s*$") + if last_comment or cmt:sub(1, #lcmt) == lcmt then return "", cmt end return nil @@ -249,8 +255,12 @@ local lex_main = function(ls) local opt = lex_tbl[c] if opt then local t, v = opt(ls) - if t then yield(t, v) end + if t then + last_comment = t == "" + yield(t, v) + end else + last_comment = false yield(lex_default(ls)) end end @@ -291,7 +301,7 @@ local ls_get = function(self) return tok end -return { init = function(chunkname, input, opts) +return { init = function(chunkname, input, flags, opts) local reader = type(input) == "string" and strstream(input) or input local current = skip_shebang(reader) local ls = { @@ -301,7 +311,8 @@ return { init = function(chunkname, input, opts) current = current, line_number = 1, get = ls_get, - lex_cmt = opts["c"] + lex_cmt = opts["c"], + flags = flags.valid } local coro = coroutine.wrap(lex_main, ls) ls.coro = coro diff --git a/src/bin/elua/modules/xgettext/parser.lua b/src/bin/elua/modules/xgettext/parser.lua index 878597ff76..78ea604942 100644 --- a/src/bin/elua/modules/xgettext/parser.lua +++ b/src/bin/elua/modules/xgettext/parser.lua @@ -5,8 +5,10 @@ local lexer = require("xgettext.lexer") local syntax_error = lexer.syntax_error local yield = coroutine.yield +local tconc = table.concat -local saved_comment +local saved_flagcomments = {} +local saved_comments = {} local check_match = function(ls, a, b, line) if ls.token.name ~= a then @@ -100,7 +102,10 @@ local parse = function(ls, keywords) local tok = ls.token while tok.name ~= "" do if tok.name == "" then - saved_comment = tok.value + saved_comments[#saved_comments + 1] = tok.value + ls:get() + elseif tok.name == "" then + saved_flagcomments[#saved_flagcomments + 1] = tok.value ls:get() elseif tok.name == "" and keywords[tok.value] then local line = ls.line_number @@ -120,11 +125,15 @@ local parse = function(ls, keywords) if not n1argt then goto skip end if n2 and not n2argt then goto skip end if cx and not cxargt then goto skip end - local sc = saved_comment - saved_comment = nil + local sc = saved_comments + saved_comments = {} + sc = tconc(sc, "\n") + local fsc = saved_flagcomments + saved_flagcomments = {} yield { n1arg[1], n2 and n2arg[1], context = cx and cxarg[1], - xcomment = kw.xcomment, comment = sc, line = line + xcomment = kw.xcomment, comment = sc, line = line, + flags = fsc } else ls:get() @@ -154,7 +163,7 @@ local parse_all = function(ls) end return { init = function (chunkname, input, keywords, flags, opts) - local ls = lexer.init(chunkname, input, opts) + local ls = lexer.init(chunkname, input, flags, opts) ls:get() local coro = coroutine.wrap(opts["a"] and parse_all or parse, ls, keywords) coro(ls, keywords)