| local lpeg = vim.lpeg |
| local P, S, V, R, B = lpeg.P, lpeg.S, lpeg.V, lpeg.R, lpeg.B |
| local C, Cc, Ct, Cf, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cf, lpeg.Cmt |
|
|
| local M = {} |
|
|
| local pathsep = P('/') |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.to_lpeg(pattern) |
| local function class(inv, ranges) |
| local patt = R(unpack(vim.tbl_map(table.concat, ranges))) |
| if inv == '!' then |
| patt = P(1) - patt |
| end |
| return patt |
| end |
|
|
| local function condlist(conds, after) |
| return vim.iter(conds):fold(P(false), function(acc, cond) |
| return acc + cond * after |
| end) |
| end |
|
|
| local function mul(acc, m) |
| return acc * m |
| end |
|
|
| local function star(stars, after) |
| return (-after * (P(1) - pathsep)) ^ #stars * after |
| end |
|
|
| local function dstar(after) |
| return (-after * P(1)) ^ 0 * after |
| end |
|
|
| |
| local function cut(_s, idx, match) |
| return idx, match |
| end |
| |
|
|
| |
| local p = P({ |
| 'Pattern', |
| Pattern = V('Elem') ^ -1 * V('End'), |
| Elem = Cmt( |
| Cf( |
| (V('DStar') + V('Star') + V('Ques') + V('Class') + V('CondList') + V('Literal')) |
| * (V('Elem') + V('End')), |
| mul |
| ), |
| cut |
| ), |
| DStar = (B(pathsep) + -B(P(1))) |
| * P('**') |
| * (pathsep * (V('Elem') + V('End')) + V('End')) |
| / dstar, |
| Star = C(P('*') ^ 1) * (V('Elem') + V('End')) / star, |
| Ques = P('?') * Cc(P(1) - pathsep), |
| Class = P('[') |
| * C(P('!') ^ -1) |
| * Ct(Ct(C(P(1)) * P('-') * C(P(1) - P(']'))) ^ 1 * P(']')) |
| / class, |
| CondList = P('{') * Ct(V('Cond') * (P(',') * V('Cond')) ^ 0) * P('}') * V('Pattern') / condlist, |
| |
| |
| |
| |
| |
| |
| Cond = Cmt(Cf((V('Ques') + V('Class') + V('Literal') - S(',}')) ^ 1, mul), cut) + Cc(P(0)), |
| Literal = P(1) / P, |
| End = P(-1) * Cc(P(-1)), |
| }) |
|
|
| local lpeg_pattern = p:match(pattern) |
| assert(lpeg_pattern, 'Invalid glob') |
| return lpeg_pattern |
| end |
|
|
| return M |
|
|