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 "<flagcomment>", cmt
+ end
local lcmt = ls.lex_cmt
if not lcmt then return nil end
if type(lcmt) ~= "string" then
return "<comment>", 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 "<comment>", cmt
end
return nil
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 == "<comment>"
+ yield(t, v)
+ end
else
+ last_comment = false
yield(lex_default(ls))
end
end
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 = {
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
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
local tok = ls.token
while tok.name ~= "<eof>" do
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()
elseif tok.name == "<name>" and keywords[tok.value] then
local line = ls.line_number
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()
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)