Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- brackets.lua, rewritten for blogme4. -- This file: -- http://angg.twu.net/blogme4/brackets.lua.html -- http://angg.twu.net/blogme4/brackets.lua -- (find-blogme4file "brackets.lua") -- Author: Eduardo Ochs <[email protected]> -- Version: 2011jan06 -- License: GPL3 -- -- See: (find-blogme3 "brackets.lua" "brackstructure") -- «.bracketstructure» (to "bracketstructure") -- «.test-brackets» (to "test-brackets") -- «bracketstructure» (to ".bracketstructure") -- Sometimes (actually, VERY often) we will have to jump over pairs of -- matching brackets in blogme source files... So let's implement a -- very efficient way to do that. -- -- Note that these functions are optional, and if they are loaded they -- are only used by the replacements for parse__block and parse_block, -- below. -- bracketstructure = function (subj) local pos2pos, stack = {}, {} local f = function (pos, c) if c == "[" then stack[#stack + 1] = pos else if #stack == 0 then error("Extra ']' at "..pos) end local openpos = stack[#stack] stack[#stack] = nil pos2pos[openpos], pos2pos[pos] = pos, openpos end end subj:gsub("()([%[%]])", f) if #stack > 0 then error("Extra '[' at "..stack[#stack]) end return pos2pos end bracketstructures = {} -- the cache afterclosing = function (subj, pos) local bs = bracketstructures[subj] or bracketstructure(subj) bracketstructures[subj] = bs -- store the resulting table into the cache if bs[pos] and bs[pos] > pos then return bs[pos] + 1 end end printbracketstructure = function (subj) local pos2pos = bracketstructure(subj) for _,pos in ipairs(sorted(keys(pos2pos))) do local otherpos = pos2pos[pos] if pos < otherpos then print(subj:sub(pos, otherpos)) end end end -- Override two functions from argparsers.lua with faster versions. -- The original parse__block and parse_block use string.match with the -- %b[]" pattern: -- (find-blogme4 "argparsers.lua" "parse_pattern" "parse__block") -- (find-blogme4 "argparsers.lua" "parse_pattern" "parse_block") -- (find-luamanualw3m "#5.4.1" "Patterns" "%b()") -- parse__block = function () oldpos, pos = pos, afterclosing(subj, pos) if not pos then pos = oldpos else return true end end parse_block = function () if parse__block() then result = subj:sub(oldpos, pos) return true end end -- dump-to: tests --[=[ -- «test-brackets» (to ".test-brackets") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) require "brackets" -- -- /----------\ -- | /-\ /-\ | str = "a[b[c]d[e]fg]h" -- ^ ^ ^ ^ ^ ^ -- 2 4 6 8 10 13 PP(bracketstructure(str)) --> {2=13, 4=6, 6=4, 8=10, 10=8, 13=2} printbracketstructure(str) --> [b[c]d[e]fg] -- [c] -- [e] print(afterclosing(str, 1)) --> nil print(afterclosing(str, 2)) --> 14 print(afterclosing(str, 6)) --> nil -- This very simple function always returns the same results as the -- complex one above. afterclosing2 = function (subj, pos) return subj:match("^%b[]()", pos) end print(afterclosing2(str, 1)) --> nil print(afterclosing2(str, 2)) --> 14 print(afterclosing2(str, 6)) --> nil --]=] -- Local Variables: -- coding: raw-text-unix -- ee-anchor-format: "«%s»" -- End: