[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Match-time captures in lpeg
- From: Geoff Richards <Ni1phahs@...>
- Date: Tue, 2 Dec 2008 13:54:07 +0000
On Mon, Dec 01, 2008 at 07:20:20PM +0000, Paul Moore wrote:
> However, I'm still a little unclear on the reasoning behind match-time
> captures. There is only one example (Lua long strings) in the
Just looked through an unfinished parser I've been working on for a while
(a templating language which includes Lua syntax with other stuff). I've
got a Cmt for the Lua long string/comment syntax which I probably copied
from Roberto's examples.
The only other use I've made of Cmt (in several places) was for catching
errors. So I might have an ordered choice where I match several valid
things and use normal captures to get the interesting bits and function
captures to build the AST, but in some cases the last choice uses Cmt
to match something that's not quite valid and throw an exception giving
a more precise error message than just 'it didn't match'. I can also
have a generic 'syntax error' Cmt which matches any character (having
failed to match anything reasonable).
For example, if your normal matching finds pairs of opening and closing
brackets, you can have a pattern which only gets checked later, which
matches just a closing bracket. Use Cp in front of the closing bracket
to get its position, and a function which throws an exception saying
'unexpected closing bracket without matching opening bracket' or whatever.
The position capture allows you to calculate the line and column number to
include in the message.
Hope that helps.