lua-users home
lua-l archive

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


I have a relatively simple contrived grammar (below) which works great
with small files.
When I try to feed it a large file though I get an error:

    stack overflow (too many runtime captures)

This happens when it tries to read over about 8000 lines. Is there any
way to get around this limitation with match time captures? I am
considering making the grammar only process one line at a time and
handling the looping from Lua. Is this the best solution?

    require "lpeg"

    local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P
    local C, Ct, Cmt = lpeg.C, lpeg.Ct, lpeg.Cmt

    local Break = S"\n"
    local Name = R("az", "AZ", "__") * R("az", "AZ", "09", "__")^0

    local Block, Line = V"Block", V"Line"

    local count = 0
    Name = Cmt(Name, function(str, pos, name)
    	count = count + 1
    	return true, name
    end)

    local g = lpeg.P{
    	Block,
    	Block = Ct(Line * (Break * Line)^0),
    	Line = Name,
    }

    local success, err = pcall(function()
    	local out = g:match(io.open("big.txt"):read("*a"))
    	print(#out)
    end)

    if not success then
    	print(err)
    	print("got", count)
    end


Thanks, Leaf