[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: yet another pattern-matching library for Lua
- From: roberto@... (Roberto Ierusalimschy)
- Date: Wed, 3 Jan 2007 10:40:00 -0200
> - I wondered how to emulate {n,m} syntax of REs. It seems that patt^n is
> close of that. Perhaps for newbies like me, you should provide more
> examples, something with both bounds, if possible. Classical example in
> REs is [a-z]{2,6} for TLDs (perhaps more on the upper bound).
> I understand that, per the doc, we have:
> ^n -> {n,}
> ^-n -> {0,n}
> ^0 -> {0,} -> *
> ^1 -> {1,} -> +
> ^-1 -> {0,1} -> ?
> so, to have patt{n,m}, can we write
> patt^n * patt^(n - m)
This is not correct. Remember that patt^n is actually "at least n". So,
patt^n will always consume as many patt as possible, leaving no one to
patt^(n - m) (which will always fail).
> ? Is it possible to express this without writing the pattern twice? (or
> is it a non-issue?)
I am not sure whether {n,m} is that common to deserve primitive support.
But it should be easy to write a function to build that kind of patterns.
Something like this (untested!):
function rep (p, n, m)
local r = lpeg.P""
for i = 1, n do r = r * p end -- at least n
if not m then m = 0
else m = n - m -- assuming that n > m
end
return r * p^m
end
That's the point of composability. (We could have a library of these
auxiliary functions.)
> - Looking at the CSV example, is (lpeg.P(1) - '"') the same as the (more
> used elsewhere) (1 - lpeg.P"'")? I suppose the rule here is to have at
> least one pattern in the expression to trigger metamethods.
Sure. This has nothing to do with Lpeg. It is how Lua chooses metamethods.
> I suppose your library (parser) uses the Lua license, like most others.
Yes.
-- Roberto