[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Trace debugger for lpeg
- From: Patrick Donnelly <batrick@...>
- Date: Wed, 21 Oct 2009 07:00:02 -0400
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
For my Lua parser, it looks something like this:
--- space ---
--- space ---
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)
(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!
"Let all men know thee, but no man know thee thoroughly: Men freely
ford that see the shallows."
- Benjamin Franklin