lua-users home
lua-l archive

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


Hi Leeya,

pcall is a auxiliary Lua function that encapsulate your code and execute in protected mode. For instance, the call m.Carg(0) will produce an error because the 0 isn't a valid argument for m.Carg, to test if m.Carg is produce an error with the argument 0 without stop the execution of test file, we execute a protected call with pcall as pcall(m.Carg, 0) that returns false because occours an error.

In the LPeg documentation, the function match has 3 arguments. A pattern, the entry and a optional index. The Carg function matchs the empty string and produce the value given as the n extra argument given in the call to lpeg.match. So, m.match(m.Carg(1), 'a', 1, print) will return the 1 first extra argument given to match function that is print.

I recommend you to look http://lua-users.org/wiki/LpegRecipes
In this page you will see more examples with LPeg. It's better than learn LPeg with test file. Try to make a little user case with LPeg.

Good luck

2010/3/12 YLee <leeyacn@126.com>
Thank you, Japa, I got it. Actually, I did ignore the fact that -patt and #patt did not consume any character input. Could you confirm the priority sequence +,-,*, i.e. (patt1 - patt2 * patt3 ) ?
 
Next, I read 'tests for argument captures' [lpeg.Carg(n)] section and  'tests for Lua functions' [lpeg.C(patt)] section from test.lua. I did not understand this two sections.
1) lpeg.Carg(n)]
-- tests for argument captures
assert(not pcall(m.Carg, 0))
assert(not pcall(m.Carg, -1))
assert(not pcall(m.Carg, 2^18))
assert(not pcall(m.match, m.Carg(1), 'a', 1))
assert(m.match(m.Carg(1), 'a', 1, print) == print)
 
first, I did not understand Roberto's intention for why test m.Carg with pcall?
second, did not understand statement - assert(m.match(m.Carg(1), 'a', 1, print) == print)
Anyways, I did not catch the Carg(pattern), which scenario did we need it at ?
 
2) lpeg.C(patt), patt is function
t = {}
s = ""
p = function (s1, i) assert(s == s1); t[#t + 1] = i; return nil end
s = "hi, this is a test"
assert(m.match(((p - m.P(-1)) + 2)^0, s) == string.len(s) + 1)
assert(#t == string.len(s)/2 and t[1] == 1 and t[2] == 3)
 
I did not understand that how did s1, i bypass, and process procedure.
 
 
I was not able to get enough information from http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html to help me learn how to use LPeg, if I missed others, please point out, I appreciated that.
So test.lua was my start.
 
Thanks again.
在2010-03-11 22:34:32,Japa <greatjapa@gmail.com> 写道:
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 <leeyacn@126.com>
Hi Japa,
 
Many thanks for your comments, which is so clear.
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.

在2010-03-11 03:33:05,Japa <greatjapa@gmail.com> 写道:

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 <leeyacn@126.com>
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





--
Marcelo Oikawa