Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I am not expert in making grammars in LPeg, I found this exercise interesting to do...</p> <p>I couldn't manage to use your grammar, so I went ahead and made my own, with smaller chunks easier to understand and where I could put the captures I needed.</p> <p>I think I got a decent empirical result. It works on your test case, I don't know if groups can be more deeply nested, etc. The post-processing of the capture is a bit ad hoc...</p> <pre><code>require"lpeg" -- Guesswork... lpeg.T_WSpace = lpeg.P" "^0 lpeg.T_LGroup = lpeg.P"[" lpeg.T_RGroup = lpeg.P"]" lpeg.T_Group = lpeg.S"[]" function lpeg.commaSplit(arg) local P, C, Ct, V, sep = lpeg.P, lpeg.C, lpeg.Ct, lpeg.V, lpeg.P"," local grammar = { "S"; S = lpeg.T_WSpace * V"Group" * lpeg.T_WSpace, Group = Ct(lpeg.T_LGroup * C(V"Units") * lpeg.T_RGroup), Units = V"Unit" * (lpeg.T_WSpace * sep * lpeg.T_WSpace * V"Unit")^0, Unit = V"Element" + V"Group", Element = (1 - sep - lpeg.T_Group)^1, } return lpeg.match(Ct(P(grammar)^-1), arg) end local test = "[[a,b,[c,d]],[e,[f,g]]]" local res = lpeg.commaSplit(test) print(dumpObject(res)) print(res[1], res[1][1], res[1][2]) local groups = res[1] local finalResult = {} for n, v in ipairs(groups) do if type(v) == 'table' then finalResult[#finalResult+1] = "[" .. v[1] .. "]" end end print(dumpObject(finalResult)) </code></pre> <p>dumpObject is just a table dump of my own. The output of this code is as follows:</p> <pre><code>local T = { { "[a,b,[c,d]],[e,[f,g]]", { "a,b,[c,d]", { "c,d" } }, { "e,[f,g]", { "f,g" } } } } table: 0037ED48 [a,b,[c,d]],[e,[f,g]] table: 0037ED70 local T = { "[a,b,[c,d]]", "[e,[f,g]]" } </code></pre> <p>Personally, I wouldn't pollute the lpeg table with my stuff, but I kept your style here.</p> <p>I hope this will be useful (or will be a starting point to make you to advance).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload