更改 LPeg 模式中捕获的返回顺序



(我使用的是Lua 5.2和LPeg 0.12(

假设我有一个模式P,它会产生一些不确定数量的捕获(如果有的话(,我想编写创建一个模式Q来捕获P以及P后的位置 - 但要在捕获P之前返回该位置。从本质上讲,如果lpeg.match(P * lpeg.Cp(), str, i)导致v1, v2, ..., j,那么我希望lpeg.match(Q, str, i)导致j, v1, v2, ...

这是否可以实现,而不必在每次匹配P时都创建新表?

大多数情况下,我想这样做是为了简化一些生成迭代器的函数。Lua 的无状态迭代器函数只获取一个控制变量,它必须是迭代器函数返回的第一个值。

在一个让人们命名可变参数函数的最后一个参数的世界里,我可以写:

function pos_then_captures(pattern)
    local function roll(..., pos)
        return pos, (...)
    end
    return (pattern * lpeg.Cp()) / roll
end

唉。简单的解决方案是明智地使用lpeg.Ct()

function pos_then_captures(pattern)
    -- exchange the order of two values and unpack the first parameter
    local function exch(a, b)
        return b, unpack(a)
    end
    return (lpeg.Ct(pattern) * lpeg.Cp()) / exch
end

或者让呼叫者lpeg.match做一个打包/删除/插入/解包的舞蹈。尽管后者听起来很糟糕,但我可能会这样做,因为lpeg.Ct()可能会对病态但"正确"pos_then_captures的论点产生一些意想不到的后果。

每次成功匹配pattern时,这两种方法中的任何一个都会创建一个新表,诚然,这在我的应用程序中并不重要,但是有没有办法在没有任何打包魔法的情况下做到这一点?

我对 Lua 的内部不太熟悉,但感觉我真正想做的是从 Lua 的堆栈中弹出一些东西并将其放回其他地方,这似乎不是一个直接或有效支持的操作,但也许 LPeg 可以在这种特定情况下做一些事情。

比赛时间捕获和上值可以完成工作。此函数使用Cmt来确保在将其粘贴到pattern的捕获pattern / prepend前面之前设置pos

Cmt = lpeg.Cmt
Cp  = lpeg.Cp
function prepend_final_pos(pattern)
    -- Upvalues are dynamic, so this variable belongs to a
    -- new environment for each call to prepend_final_pos.
    local pos
    -- lpeg.Cmt(patt, func) passes the entire text being
    -- searched to `function` as the first parameter, then
    -- any captures. Ignore the first parameter.
    local function setpos(_, x)
      pos = x
      -- If we return nothing, Cmt will fail every time
      return true
    end
    -- Keep the varargs safe!
    local function prepend(...)
      return pos, ...
    end
    -- The `/ 0` in `Cmt(etc etc) / 0` is to get rid of that
    -- captured `true` that we picked up from setpos.
    return (pattern / prepend) * (Cmt(Cp(), setpos) / 0)
end

示例会话:

> bar = lpeg.C "bar"
> Pbar = prepend_final_pos(bar)
> print(lpeg.match(Pbar, "foobarzok", 4))
7       bar
> foo = lpeg.C "foo" / "zokzokzok"
> Pfoobar = prepend_final_pos(foo * bar)
> print(lpeg.match(Pfoobar, "foobarzok"))
7       zokzokzok       bar

正如预期的那样,实际捕获对新模式返回的位置没有影响;只有与原始模式匹配的文本长度。

您可以使用原始解决方案来执行此操作,而无需捕获表,也可以像这样进行比赛时间捕获

function pos_then_captures(pattern)
    local function exch(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, ...)
        if a1 == nil then return end
        if a2 == nil then return a1 end
        if a3 == nil then return a2, a1 end
        if a4 == nil then return a3, a1, a2 end
        if a5 == nil then return a4, a1, a2, a3 end
        if a6 == nil then return a5, a1, a2, a3, a4 end
        if a7 == nil then return a6, a1, a2, a3, a4, a5 end
        if a8 == nil then return a7, a1, a2, a3, a4, a5, a6 end
        if a9 == nil then return a8, a1, a2, a3, a4, a5, a6, a7 end
        if a10 == nil then return a9, a1, a2, a3, a4, a5, a6, a7, a8 end
        local t = { a10, ... }
        return t[#t], a1, a2, a3, a4, a5, a6, a7, a8, a9, unpack(t, 1, #t-1)
    end
    return (pattern * lpeg.Cp()) / exch
end

以下示例用法返回每个匹配的"a",其前面是匹配的结尾

local p = lpeg.P{ (pos_then_captures(lpeg.C'a') + 1) * lpeg.V(1) + -1 }
print(p:match('abababcd'))
-- output: 2       a       4       a       6       a

最新更新