elua: prepare xgettext for flag comments and try to emulate xgettext's silly behavior when it comes to concatenating comments

This commit is contained in:
Daniel Kolesa 2014-06-02 16:42:27 +01:00
parent d8caf323d8
commit 9d5b2d433e
3 changed files with 32 additions and 12 deletions

View File

@ -14,7 +14,7 @@ local input_sources = {}
local search_dirs = {} local search_dirs = {}
local excluded_files = {} local excluded_files = {}
local keywords = {} local keywords = {}
local flags = {} local flags = { valid = {} }
local opts_final = {} local opts_final = {}
local opts_nolua = {} local opts_nolua = {}

View File

@ -144,14 +144,20 @@ local read_string = function(ls)
return tconc(buf) return tconc(buf)
end end
local last_comment = false
local match_comment = function(ls, cmt) local match_comment = function(ls, cmt)
cmt = cmt:match("^%s*(.+)%s*$")
if ls.flags[cmt] then
return "<flagcomment>", cmt
end
local lcmt = ls.lex_cmt local lcmt = ls.lex_cmt
if not lcmt then return nil end if not lcmt then return nil end
if type(lcmt) ~= "string" then if type(lcmt) ~= "string" then
return "<comment>", cmt return "<comment>", cmt
end end
lcmt = lcmt:match("^%s*(.+)$") lcmt = lcmt:match("^%s*(.+)%s*$")
if cmt:match("^%s*(.+)$"):sub(1, #lcmt) == lcmt then if last_comment or cmt:sub(1, #lcmt) == lcmt then
return "<comment>", cmt return "<comment>", cmt
end end
return nil return nil
@ -249,8 +255,12 @@ local lex_main = function(ls)
local opt = lex_tbl[c] local opt = lex_tbl[c]
if opt then if opt then
local t, v = opt(ls) local t, v = opt(ls)
if t then yield(t, v) end if t then
last_comment = t == "<comment>"
yield(t, v)
end
else else
last_comment = false
yield(lex_default(ls)) yield(lex_default(ls))
end end
end end
@ -291,7 +301,7 @@ local ls_get = function(self)
return tok return tok
end 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 reader = type(input) == "string" and strstream(input) or input
local current = skip_shebang(reader) local current = skip_shebang(reader)
local ls = { local ls = {
@ -301,7 +311,8 @@ return { init = function(chunkname, input, opts)
current = current, current = current,
line_number = 1, line_number = 1,
get = ls_get, get = ls_get,
lex_cmt = opts["c"] lex_cmt = opts["c"],
flags = flags.valid
} }
local coro = coroutine.wrap(lex_main, ls) local coro = coroutine.wrap(lex_main, ls)
ls.coro = coro ls.coro = coro

View File

@ -5,8 +5,10 @@ local lexer = require("xgettext.lexer")
local syntax_error = lexer.syntax_error local syntax_error = lexer.syntax_error
local yield = coroutine.yield 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) local check_match = function(ls, a, b, line)
if ls.token.name ~= a then if ls.token.name ~= a then
@ -100,7 +102,10 @@ local parse = function(ls, keywords)
local tok = ls.token local tok = ls.token
while tok.name ~= "<eof>" do while tok.name ~= "<eof>" do
if tok.name == "<comment>" then if tok.name == "<comment>" then
saved_comment = tok.value saved_comments[#saved_comments + 1] = tok.value
ls:get()
elseif tok.name == "<flagcomment>" then
saved_flagcomments[#saved_flagcomments + 1] = tok.value
ls:get() ls:get()
elseif tok.name == "<name>" and keywords[tok.value] then elseif tok.name == "<name>" and keywords[tok.value] then
local line = ls.line_number local line = ls.line_number
@ -120,11 +125,15 @@ local parse = function(ls, keywords)
if not n1argt then goto skip end if not n1argt then goto skip end
if n2 and not n2argt then goto skip end if n2 and not n2argt then goto skip end
if cx and not cxargt then goto skip end if cx and not cxargt then goto skip end
local sc = saved_comment local sc = saved_comments
saved_comment = nil saved_comments = {}
sc = tconc(sc, "\n")
local fsc = saved_flagcomments
saved_flagcomments = {}
yield { yield {
n1arg[1], n2 and n2arg[1], context = cx and cxarg[1], 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 else
ls:get() ls:get()
@ -154,7 +163,7 @@ local parse_all = function(ls)
end end
return { init = function (chunkname, input, keywords, flags, opts) 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() ls:get()
local coro = coroutine.wrap(opts["a"] and parse_all or parse, ls, keywords) local coro = coroutine.wrap(opts["a"] and parse_all or parse, ls, keywords)
coro(ls, keywords) coro(ls, keywords)