mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
fix(glob): avoid subcapture nesting too deep
error (#29520)
Use Cmt to evaluate Cond and Elem during match to avoid building the nested capture structure later.
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
local lpeg = vim.lpeg
|
local lpeg = vim.lpeg
|
||||||
local P, S, V, R, B = lpeg.P, lpeg.S, lpeg.V, lpeg.R, lpeg.B
|
local P, S, V, R, B = lpeg.P, lpeg.S, lpeg.V, lpeg.R, lpeg.B
|
||||||
local C, Cc, Ct, Cf = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf
|
local C, Cc, Ct, Cf, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf, lpeg.Cmt
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -47,13 +47,22 @@ function M.to_lpeg(pattern)
|
|||||||
return (-after * P(1)) ^ 0 * after
|
return (-after * P(1)) ^ 0 * after
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- luacheck: push ignore s
|
||||||
|
local function cut(s, idx, match)
|
||||||
|
return idx, match
|
||||||
|
end
|
||||||
|
-- luacheck: pop
|
||||||
|
|
||||||
local p = P({
|
local p = P({
|
||||||
'Pattern',
|
'Pattern',
|
||||||
Pattern = V('Elem') ^ -1 * V('End'),
|
Pattern = V('Elem') ^ -1 * V('End'),
|
||||||
Elem = Cf(
|
Elem = Cmt(
|
||||||
(V('DStar') + V('Star') + V('Ques') + V('Class') + V('CondList') + V('Literal'))
|
Cf(
|
||||||
* (V('Elem') + V('End')),
|
(V('DStar') + V('Star') + V('Ques') + V('Class') + V('CondList') + V('Literal'))
|
||||||
mul
|
* (V('Elem') + V('End')),
|
||||||
|
mul
|
||||||
|
),
|
||||||
|
cut
|
||||||
),
|
),
|
||||||
DStar = (B(pathsep) + -B(P(1)))
|
DStar = (B(pathsep) + -B(P(1)))
|
||||||
* P('**')
|
* P('**')
|
||||||
@ -72,7 +81,7 @@ function M.to_lpeg(pattern)
|
|||||||
-- pattern" which in all other cases is the entire succeeding part of the pattern, but at the end of a {}
|
-- pattern" which in all other cases is the entire succeeding part of the pattern, but at the end of a {}
|
||||||
-- condition means "everything after the {}" where several other options separated by ',' may
|
-- condition means "everything after the {}" where several other options separated by ',' may
|
||||||
-- exist in between that should not be matched by '*'.
|
-- exist in between that should not be matched by '*'.
|
||||||
Cond = Cf((V('Ques') + V('Class') + V('Literal') - S(',}')) ^ 1, mul) + Cc(P(0)),
|
Cond = Cmt(Cf((V('Ques') + V('Class') + V('Literal') - S(',}')) ^ 1, mul), cut) + Cc(P(0)),
|
||||||
Literal = P(1) / P,
|
Literal = P(1) / P,
|
||||||
End = P(-1) * Cc(P(-1)),
|
End = P(-1) * Cc(P(-1)),
|
||||||
})
|
})
|
||||||
|
@ -205,6 +205,19 @@ describe('glob', function()
|
|||||||
eq(true, match('[!a-zA-Z0-9]', '!'))
|
eq(true, match('[!a-zA-Z0-9]', '!'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('should handle long patterns', function()
|
||||||
|
-- lpeg has a recursion limit of 200 by default, make sure the grammar does trigger it on
|
||||||
|
-- strings longer than that
|
||||||
|
local fill_200 =
|
||||||
|
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
||||||
|
eq(200, fill_200:len())
|
||||||
|
local long_lit = fill_200 .. 'a'
|
||||||
|
eq(false, match(long_lit, 'b'))
|
||||||
|
eq(true, match(long_lit, long_lit))
|
||||||
|
local long_pat = fill_200 .. 'a/**/*.c'
|
||||||
|
eq(true, match(long_pat, fill_200 .. 'a/b/c/d.c'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('should match complex patterns', function()
|
it('should match complex patterns', function()
|
||||||
eq(false, match('**/*.{c,h}', ''))
|
eq(false, match('**/*.{c,h}', ''))
|
||||||
eq(false, match('**/*.{c,h}', 'c'))
|
eq(false, match('**/*.{c,h}', 'c'))
|
||||||
|
Reference in New Issue
Block a user