lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I'm trying to parse http headers with lpeg.re as an exercise for
learning lpeg and because http has a few cases that need recursive
parsing. There's a few things I don't know how to express in lpeg.re
(or lpeg) yet.

For instance, I have this syntax: k1=v1,k2=v2,... then v1 and v2 have
themselves a different syntax depending on the keys.

Consider this:

list <- element (',' element)*
element <- length / name
length <- kv -- but I also want k <- 'length' and v <- length_value in
order to succeed
name <- kv -- but I also want k <- 'name' and v <- name_value in order
to succeed
kv <- k '=' v
k <- {[^=]+}
v <- {[^,]*}
length_value <- [0-9]+
name_value <- [a-z]+

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.

Any hints appreciated. Thanks.