• Subject: Re: Who could give me some comments on LPeg code snippets?
• From: Japa <greatjapa@...>
• Date: Thu, 11 Mar 2010 11:34:32 -0300

Hi Leeya,

In 1) I think that test just verifies if ## is working equivalent to #. In this test file, the asserts at lines 397 and 398 verify the same thing: just change # for ##.

In 2) you are right, the subpattern is enough for matching. I think that test verifies if the redundancy of # does not change the behavior of the match, as 1).

For 3) and 4): In LPeg documentation, p - q is equivalent to !pq in PEG notation. If you have -p in LPeg, the PEG equivalent is !p. By the way, - - p is !!p that is equal to &p; Finally in LPeg  &p is #p.

I hope I have helped you understand. Good luck

2010/3/11 leeya
Hi Japa,

Would you give me more commnents on code scripts below?

1)assert(m.match(m.Cs((##m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.")
2)assert(m.match(m.Cs((#((#m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.")
3)assert(m.match(m.Cs((- -m.P("a") * 1 + m.P(1)/".")^0), "aloal") == "a..a.")
4)assert(m.match(m.Cs((-((-m.P"a")/"") * 1 + m.P(1)/".")^0), "aloal") == "a..a.")

For the above four statements 1)2)3)4), I was not able to understand them completely.
- Regarding 1), why need consecutive token '#'?
- Regarding 2), I think sub-pattern ((#m.P"a")/"") should be enough for match, why need token '#' before it?
- Regarding 3), be confused with sub-pattern '- -m.P("a")', especially for two tokens '-'.
- Regarding 4), similar as 3)

Thanks again.

Each m.C is a single capture in LPeg. If you have nested captures, their values are returned after this one. The pseudocode below shows the order of captures, I named the first capture m.C as C1, second capture m.C as C2 and so on.

C1( C2('a') * C3( C4('b') * C5( C6('c') * C7( )  ) ) )

C1 -> abc
C2 -> a
C3 -> bc
C4 -> b
C5 -> c
C6 -> c
C7 -> ""

To understand the second example you just need to do the same thing.

2010/3/10 leeya
I read test.lua script from lpeg-0.9 and had a little confusion to code snippet below:

t = {m.match({[1] = m.C(m.C(1) * m.V(1) + -1)}, "abc")}
checkeq(t, {"abc", "a", "bc", "b", "c", "c", ""})

Who could give me some comments of above code? Why the output is {"abc", "a", "bc", "b", "c", "c", ""} sequence?
The same question was given as following too,
t = m.match(m.Ct(m.C(m.C(1) * 1 * m.C(1))), "alo")
checkeq(t, {"alo", "a", "o"})

Thanks a lot.

--
Marcelo Oikawa

--
Marcelo Oikawa

• References: