lua-users home
lua-l archive

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


Hi, Wesley,

This is a bug I only discovered recently. LPEG optimizes P(1)^n, with
n>=0, as a sequence of zero or more IAny instructions followed by an
ISpan instruction; this  this optimization does not work with list
subjects. I have added a IAnySpan instruction to replace the final
ISpan, the new instruction works correctly with both strings and
lists. Please pull the latest sources from github again.

--
Fabio Mascarenhas


On Thu, Apr 21, 2011 at 4:18 PM, Wesley Smith <wesley.hoke@gmail.com> wrote:
> There seems to be a bug in lpeglist whereby the repetition patterns
> patt^N when the subject is tables behaves differently than when the
> subject is strings.  A test script is below and produces the following
> output:
>
> 3
> 5
> 5
> -----------------------------------------
> 3
> 3  <---- should be 5
> 2  <---- should be 5
>
>
> Basically, the last P(1)^1 aborts after the first match instead of
> matching agains 1 or more elements when matching against tables.  I'm
> using the latest version from the git repo.
>
>
>
>
>
> local listlpeg = require("listlpeg")
> local P = listlpeg.P
> local L = listlpeg.L
> local C = listlpeg.C
> local Cmt = listlpeg.Cmt
>
> -----------------------------------------
> -- test string array
> local t1 = {
>        "A", "B", "C", "D"
> }
>
> print("-----------------------------------------")
> local patt = P"A" * P"B"
> print(patt:match(t1))
>
> local patt = P"A" * P(1)^1
> print(patt:match(t1))
>
> local patt = P(1)^1
> print(patt:match(t1))
>
>
> -----------------------------------------
> -- test table array
> local t2 = {
>        { val = "A"}, { val = "B" }, { val = "C" }, { val = "D" }
> }
>
> local
> function Pval(name)
>        return Cmt(C(1), function(s, i, t)
>                --print("Pval", i, t.val, name)
>                return t.val == name
>        end)
> end
>
> print("-----------------------------------------")
>
> local patt = Pval"A" * Pval"B"
> print(patt:match(t2))
>
> local patt = Pval"A" * P(1)^1
> print(patt:match(t2))
>
> local patt = P(1)^1
> print(patt:match(t2))
>
> --[[
> local patt = Pval"A" * Pval"B" * P(1)
> print(patt:match(t2))
>
> local patt = Pval"A" * P(1)^0
> print(patt:match(t2))
> --]]
>
>