Hi Sean !
Thanks for your patience, really appreciate it.
1 list = lpeg.P {
2 'list',
3 list = lpeg.P'['
4 * lpeg.Cf(lpeg.Ct"" * lpeg.V"data"^0,function(a,b) table.insert(a,b) return a end)
5 * lpeg.P']',
6 data = "" * C(number)
7 + lpeg.V"list"
8 }
In line 4 are you passing lpeg.Ct"" to lpeg.Cf , that is key I think.
I was under the impression this was an anti-pattern, that LPEG operations should be stateless ( you should not maintain local state )
Yes, Cf should be used when you are constructing something that is NOT a table.
I was so confused because this is what I had in mind:
Cn(.....Cf(Cf(Cf(Cf(Cf(C1,C2),C3),C4),C5),C6),....Cn)
If you change the notation to:
T1 -> C1 C2
T2 -> T1 C3
T3 -> T2 C4
T4 -> T3 C5
...........
...........
...........
You can see the algorithm moves linearly like a snake through every capture.
Why is this important ? Errors Handling !
You can terminate the entire algorithm at ANY wrong capture regardless of how deeply its nested.
However it gets problematic when you also nest Cf, since you cannot create LOCAL STATE in your nest ( without passing Ct '')
I was trying really hard to solve the problem using Carg(1) which is the global state.
I was a fool in making such useless assumptions.
best wishes,
Joy