From 08420f8a29c9d53494d1d70154e96cb732658ba7 Mon Sep 17 00:00:00 2001 From: Georgi Kirilov <> Date: Sun, 22 Nov 2020 15:10:49 +0200 Subject: Use lpeg.Cc() instead of lpeg.Carg() `pos` doesn't change across lpeg.match() invocations. --- pairs.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'pairs.lua') diff --git a/pairs.lua b/pairs.lua index 7bca6ed..81dd847 100644 --- a/pairs.lua +++ b/pairs.lua @@ -38,7 +38,11 @@ local function get_pair(key) or not key:match("%w") and {key, key} end -local function asymmetric(d, at_pos) +local function at_pos(t, pos) + if pos >= t[1] and pos < t[#t] then return t end +end + +local function asymmetric(d, pos) local p local I = l.Cp() if #d == 1 then @@ -46,13 +50,13 @@ local function asymmetric(d, at_pos) else p = d * I * (l.P(1) - d)^0 * I * d end - return l.Ct(I * p * I) * l.Carg(1) / at_pos + return l.Ct(I * p * I) * l.Cc(pos) / at_pos end -local function symmetric(d1, d2, escaped, at_pos) +local function symmetric(d1, d2, escaped, pos) local I = l.Cp() local skip = escaped and escaped + l.P(1) or l.P(1) - return l.P{l.Ct(I * d1 * I * ((skip - d1 - d2) + l.V(1))^0 * I * d2 * I) * l.Carg(1) / at_pos} + return l.P{l.Ct(I * d1 * I * ((skip - d1 - d2) + l.V(1))^0 * I * d2 * I) * l.Cc(pos) / at_pos} end local function nth(t) @@ -117,10 +121,6 @@ local function escaping_context(file, range, syntax) end end -local function at_pos(t, pos) - if pos >= t[1] and pos < t[#t] then return t end -end - local function past(_, position, pos) return position > pos end @@ -139,14 +139,14 @@ local function get_range(key, file, pos) pos = pos - range.start end correction = range.start - local p = d[1] ~= d[2] and symmetric(d[1], d[2], c.escape, at_pos) or asymmetric(d[1], at_pos) + local p = d[1] ~= d[2] and symmetric(d[1], d[2], c.escape, pos + 1) or asymmetric(d[1], pos + 1) local skip = c.escape and c.escape + 1 or 1 local data = file:content(range) - local pattern = l.P{p + skip * (l.V(1) - l.Cmt(l.Carg(1), past))} + local pattern = l.P{p + skip * (l.V(1) - l.Cmt(l.Cc(pos + 1), past))} local result local start = 1 repeat - result = pattern:match(data, start, pos + 1) + result = pattern:match(data, start) if not result then break end start = result until type(result) == "table" -- cgit