local assert, loadstring, string_find, string_format, string_gfind, string_sub, table_concat, table_insert = assert, loadstring, string.find, string.format, string.gfind, string.sub, table.concat, table.insert module(arg and arg[1] or "slslpp") -- Source: http://lua-users.org/wiki/SlightlyLessSimpleLuaPreprocessor local function parseDollarParen(pieces, chunk, s, e) local s = 1 for term, executed, e in string_gfind(chunk, "()$(%b())()") do table_insert(pieces, string_format("%q..(%s or '')..", string_sub(chunk, s, term - 1), executed)) s = e end table_insert(pieces, string_format("%q", string_sub(chunk, s))) end ------------------------------------------------------------------------------- local function parseHashLines(chunk) local pieces, s = {"return function(_put)", n=1}, 1 while true do local ss, e, lua = string_find(chunk, "^#+([^\n]*\n?)", s) if not e then ss, e, lua = string_find(chunk, "\n#+([^\n]*\n?)", s) table_insert(pieces, "_put(") parseDollarParen(pieces, string_sub(chunk, s, ss)) table_insert(pieces, ")") if not e then break end end table_insert(pieces, lua) s = e + 1 end table_insert(pieces, " end") return table_concat(pieces) end ------------------------------------------------------------------------------- function preprocess(chunk, name) return assert(loadstring(parseHashLines(chunk), name))() end