Hi List,
I thought I would share a debugger I created for tracing the path lpeg
takes in matching a pattern using a large/complex grammar. I've gone
through a lot of headache debugging a parser I made for Lua so I
thought I would share this bit of code which made it much easier.
The code changes the grammar like so:
(1) Changes each rule in the grammar to have a prefix pattern that is
always true and run time captures to print "ENTER <rule>"
(2) Changes each rule in the grammar to have a suffix pattern that is
composed of a pattern always true that run time captures to print
"LEAVE <rule>". The end of the suffix pattern is always false so it
does not change the actual pattern when it fails.
(3) If the rule in the grammar matches, (2) does not happen and
instead the current position and subject up to the position is
printed.
For my Lua parser, it looks something like this:
ENTER 1
ENTER space
ENTER comment
LEAVE comment
--- space ---
2
ENTER chunk
ENTER space
ENTER comment
LEAVE comment
--- space ---
2
ENTER stat
ENTER varlist
ENTER var
ENTER prefix
ENTER Name
The actual code to make this happen is quite simple:
for k, p in pairs(grammar) do
local enter = lpeg.Cmt(lpeg.P(true), function(s, p, ...)
print("ENTER", k) return p end);
local leave = lpeg.Cmt(lpeg.P(true), function(s, p, ...)
print("LEAVE", k) return p end) * (lpeg.P("k") - lpeg.P "k");
grammar[k] = lpeg.Cmt(enter * p + leave, function(s, p, ...)
print("---", k, "---") print(p, s:sub(1, p-1)) return p end)
end
(Notice that there is a trick in leave, we can't use lpeg.P(false)
because LPeg optimizes it out.)
Hope someone out there finds it useful!
--
-Patrick Donnelly
"Let all men know thee, but no man know thee thoroughly: Men freely
ford that see the shallows."
- Benjamin Franklin