lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Paul K <paul <at> zerobrane.com> writes:
> 
> Is there any way to "unpack" it? Essentially, I would like to be able
> to use run-time processing (don't need run-time capture per se) on
> patterns that may be folded. It would be something like Cmt in terms
> of run-time processing, but without the capture part.
> 
Is possible modify lpeg_debug function - but this not show captures.
Solution (without waranty): 

local function lpeg_debug(grammar)
    local level = 0
    for k, p in pairs(grammar) do
        local enter = lpeg.Cmt(lpeg.P(true),
            function(s, p, ...) print((" "):rep(level) .. "+", k, p,
                s:sub(p, p)); level = level + 1 return p
            end)
        local leave = lpeg.Cmt(lpeg.P(true),
            function(s, p, ...) level = level - 1; print(("                
"):rep(level) .. "-", k, p) return p end) * (lpeg.P(1) - lpeg.P(1))
        local eq = lpeg.Cmt(lpeg.P(true), function(s, p, ...)
            level = level - 1; print((" "):rep(level) .. "=", k, p, 
s:sub(1, p - 1))
            return p
        end)
        if k ~= 1 then
            grammar[k] = enter * p * eq + leave
        end
    end
    return grammar
end