[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: LPEG debugger conflicts with folding captures (was: LPEG-based relaxed parsing again)
- From: Paul K <paul@...>
- Date: Fri, 05 Sep 2014 23:31:22 +0000
It turned out that the lpeg debugger (and the proposed mechanism)
somehow conflicts with folding captures. I made a small fix to the
debugger, but even with the fix it produces the wrong result as it
only gets the first value from the fold:
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))
if k ~= 1 then
grammar[k] = lpeg.Cmt(enter * p + leave, function(s, p, ...)
level = level - 1; print((" "):rep(level).."=", k, p, s:sub(1, p-1),
...) return p, ... end)
end
end
return grammar
end
local grammar = lpeg_debug { "Sum";
Number = lpeg.R"09"^1 / tonumber;
List = lpeg.V("Number") * ("," * lpeg.V("Number"))^0;
Sum = lpeg.Cf(lpeg.V("List"), function(acc, newvalue)
print("folding", acc, newvalue)
return acc + newvalue end);
}
print(lpeg.match(lpeg.P(grammar),"10,30,43")) --> 83
If I remove lpeg_debug, I get the correct answer (83), but with the
debugger the folding callback is not even called and I only get 10
(the first value) with the following output:
+ Sum 1 1
+ List 1 1
+ Number 1 1
= Number 3 10 10
+ Number 4 3
= Number 6 10,30 30
+ Number 7 4
= Number 9 10,30,43 43
= List 9 10,30,43 10 30 43
= Sum 9 10,30,43 10
10
Any suggestions as to what I may be doing wrong here? Thank you.
Paul.