From 67c978b75b551f25ac01022e979d67589f8ae4d0 Mon Sep 17 00:00:00 2001 From: Georgi Kirilov Date: Sat, 27 Jun 2020 02:38:03 +0300 Subject: fix wrong words order in pattern Since + is _ordered_ choice in LPeg, if two words start with the same string, and the shorter one comes first in the pattern, the longer one will never match. Fix this by sorting all the words before making a pattern out of them. --- init.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 817279c..4bac6b0 100644 --- a/init.lua +++ b/init.lua @@ -102,12 +102,20 @@ end local function preprocess(tbl) local cfg, ord = {}, P(false) + local longer_first = {} for _, options in ipairs(tbl) do for i, key in ipairs(options) do cfg[key] = {i, options} - ord = ord + key + table.insert(longer_first, key) end end + table.sort(longer_first, function(f, s) + local flen, slen = #f, #s + return flen > slen or flen == slen and f < s + end) + for _, key in ipairs(longer_first) do + ord = ord + key + end return cfg, ord end -- cgit