[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: help with lpeg
- From: Wesley Smith <wesley.hoke@...>
- Date: Wed, 26 Dec 2012 22:08:44 -0800
> I want both <length> and <name> to conform to kv as above, but I also
> want the captured value of <length> to conform to <length_value> and
> the captured value of <name> to conform to <name_value>. Basically I
> want to be able to do more parsing on the captures before succeeding
> on a match. Can I express something like that? I know I can do element
> <- kv -> parse_kv and do furtehr matching inside the parse_kv
> function, but I wanted to avoid fragmenting the parser in multiple
> stages like that.
You can use the '#' operator on a pattern in order to allow to to
match the input against a second pattern. Here's a really simple
example:
local lpeg = require"lpeg"
local P = lpeg.P
local S = lpeg.S
local R = lpeg.R
local C = lpeg.C
local input1 = "aabbcc"
local input2 = "aabbccd"
local patt1 = S"abc"^2 * P(-1)
local patt2 = R"az"^2 * P(-1)
local test_both = #patt1 * patt2
local test_both_rev = #patt2 * patt1
local function test(input)
print"*********************"
print("testing:", input)
print(patt1:match(input))
print(patt2:match(input))
print(test_both:match(input))
print(test_both_rev:match(input))
print""
end
test(input1)
test(input2)