robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
6.6 KiB275 linesraw
1--- @diagnostic disable: no-unknown
2--
3-- Copyright 2007-2023, Lua.org & PUC-Rio  (see 'lpeg.html' for license)
4-- written by Roberto Ierusalimschy
5--
6--- vendored from lpeg-1.1.0
7--- documentation available at runtime/lua/vim/_meta/re.lua
8
9-- imported functions and modules
10local tonumber, type, error = tonumber, type, error
11local setmetatable = setmetatable
12local m = require"lpeg"
13
14-- 'm' will be used to parse expressions, and 'mm' will be used to
15-- create expressions; that is, 're' runs on 'm', creating patterns
16-- on 'mm'
17local mm = m
18
19-- patterns' metatable
20local mt = getmetatable(mm.P(0))
21
22
23local version = _VERSION
24
25-- No more global accesses after this point
26_ENV = nil     -- does no harm in Lua 5.1
27
28
29local any = m.P(1)
30
31
32-- Pre-defined names
33local Predef = { nl = m.P"\n" }
34
35
36local mem
37local fmem
38local gmem
39
40
41local function updatelocale ()
42  mm.locale(Predef)
43  Predef.a = Predef.alpha
44  Predef.c = Predef.cntrl
45  Predef.d = Predef.digit
46  Predef.g = Predef.graph
47  Predef.l = Predef.lower
48  Predef.p = Predef.punct
49  Predef.s = Predef.space
50  Predef.u = Predef.upper
51  Predef.w = Predef.alnum
52  Predef.x = Predef.xdigit
53  Predef.A = any - Predef.a
54  Predef.C = any - Predef.c
55  Predef.D = any - Predef.d
56  Predef.G = any - Predef.g
57  Predef.L = any - Predef.l
58  Predef.P = any - Predef.p
59  Predef.S = any - Predef.s
60  Predef.U = any - Predef.u
61  Predef.W = any - Predef.w
62  Predef.X = any - Predef.x
63  mem = {}    -- restart memoization
64  fmem = {}
65  gmem = {}
66  local mt0 = {__mode = "v"}
67  setmetatable(mem, mt0)
68  setmetatable(fmem, mt0)
69  setmetatable(gmem, mt0)
70end
71
72
73updatelocale()
74
75
76
77-- local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end)
78
79
80local function patt_error (s, i)
81  local msg = (#s < i + 20) and s:sub(i)
82                             or s:sub(i,i+20) .. "..."
83  msg = ("pattern error near '%s'"):format(msg)
84  error(msg, 2)
85end
86
87local function mult (p, n)
88  local np = mm.P(true)
89  while n >= 1 do
90    if n%2 >= 1 then np = np * p end
91    p = p * p
92    n = n/2
93  end
94  return np
95end
96
97local function equalcap (s, i, c)
98  if type(c) ~= "string" then return nil end
99  local e = #c + i
100  if s:sub(i, e - 1) == c then return e else return nil end
101end
102
103
104local S = (Predef.space + "--" * (any - Predef.nl)^0)^0
105
106local name = m.R("AZ", "az", "__") * m.R("AZ", "az", "__", "09")^0
107
108local arrow = S * "<-"
109
110local seq_follow = m.P"/" + ")" + "}" + ":}" + "~}" + "|}" + (name * arrow) + -1
111
112name = m.C(name)
113
114
115-- a defined name only have meaning in a given environment
116local Def = name * m.Carg(1)
117
118
119local function getdef (id, defs)
120  local c = defs and defs[id]
121  if not c then error("undefined name: " .. id) end
122  return c
123end
124
125-- match a name and return a group of its corresponding definition
126-- and 'f' (to be folded in 'Suffix')
127local function defwithfunc (f)
128  return m.Cg(Def / getdef * m.Cc(f))
129end
130
131
132local num = m.C(m.R"09"^1) * S / tonumber
133
134local String = "'" * m.C((any - "'")^0) * "'" +
135               '"' * m.C((any - '"')^0) * '"'
136
137
138local defined = "%" * Def / function (c,Defs)
139  local cat =  Defs and Defs[c] or Predef[c]
140  if not cat then error ("name '" .. c .. "' undefined") end
141  return cat
142end
143
144local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R
145
146local item = (defined + Range + m.C(any)) / m.P
147
148local Class =
149    "["
150  * (m.C(m.P"^"^-1))    -- optional complement symbol
151  * (item * ((item % mt.__add) - "]")^0) /
152                          function (c, p) return c == "^" and any - p or p end
153  * "]"
154
155local function adddef (t, k, exp)
156  if t[k] then
157    error("'"..k.."' already defined as a rule")
158  else
159    t[k] = exp
160  end
161  return t
162end
163
164local function firstdef (n, r) return adddef({n}, n, r) end
165
166
167local function NT (n, b)
168  if not b then
169    error("rule '"..n.."' used outside a grammar")
170  else return mm.V(n)
171  end
172end
173
174
175local exp = m.P{ "Exp",
176  Exp = S * ( m.V"Grammar"
177            + m.V"Seq" * ("/" * S * m.V"Seq" % mt.__add)^0 );
178  Seq = (m.Cc(m.P"") * (m.V"Prefix" % mt.__mul)^0)
179        * (#seq_follow + patt_error);
180  Prefix = "&" * S * m.V"Prefix" / mt.__len
181         + "!" * S * m.V"Prefix" / mt.__unm
182         + m.V"Suffix";
183  Suffix = m.V"Primary" * S *
184          ( ( m.P"+" * m.Cc(1, mt.__pow)
185            + m.P"*" * m.Cc(0, mt.__pow)
186            + m.P"?" * m.Cc(-1, mt.__pow)
187            + "^" * ( m.Cg(num * m.Cc(mult))
188                    + m.Cg(m.C(m.S"+-" * m.R"09"^1) * m.Cc(mt.__pow))
189                    )
190            + "->" * S * ( m.Cg((String + num) * m.Cc(mt.__div))
191                         + m.P"{}" * m.Cc(nil, m.Ct)
192                         + defwithfunc(mt.__div)
193                         )
194            + "=>" * S * defwithfunc(mm.Cmt)
195            + ">>" * S * defwithfunc(mt.__mod)
196            + "~>" * S * defwithfunc(mm.Cf)
197            ) % function (a,b,f) return f(a,b) end * S
198          )^0;
199  Primary = "(" * m.V"Exp" * ")"
200            + String / mm.P
201            + Class
202            + defined
203            + "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" /
204                     function (n, p) return mm.Cg(p, n) end
205            + "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end
206            + m.P"{}" / mm.Cp
207            + "{~" * m.V"Exp" * "~}" / mm.Cs
208            + "{|" * m.V"Exp" * "|}" / mm.Ct
209            + "{" * m.V"Exp" * "}" / mm.C
210            + m.P"." * m.Cc(any)
211            + (name * -arrow + "<" * name * ">") * m.Cb("G") / NT;
212  Definition = name * arrow * m.V"Exp";
213  Grammar = m.Cg(m.Cc(true), "G") *
214            ((m.V"Definition" / firstdef) * (m.V"Definition" % adddef)^0) / mm.P
215}
216
217--- @type vim.lpeg.Pattern
218local pattern = S * m.Cg(m.Cc(false), "G") * exp / mm.P * (-any + patt_error)
219
220
221local function compile (p, defs)
222  if mm.type(p) == "pattern" then return p end   -- already compiled
223  local cp = pattern:match(p, 1, defs)
224  if not cp then error("incorrect pattern", 3) end
225  return cp
226end
227
228local function match (s, p, i)
229  local cp = mem[p]
230  if not cp then
231    cp = compile(p)
232    mem[p] = cp
233  end
234  return cp:match(s, i or 1)
235end
236
237local function find (s, p, i)
238  local cp = fmem[p]
239  if not cp then
240    cp = compile(p) / 0
241    cp = mm.P{ mm.Cp() * cp * mm.Cp() + 1 * mm.V(1) }
242    fmem[p] = cp
243  end
244  local e
245  i, e = cp:match(s, i or 1)
246  if i then return i, e - 1
247  else return i
248  end
249end
250
251local function gsub (s, p, rep)
252  local g = gmem[p] or {}   -- ensure gmem[p] is not collected while here
253  gmem[p] = g
254  local cp = g[rep]
255  if not cp then
256    cp = compile(p)
257    cp = mm.Cs((cp / rep + 1)^0)
258    g[rep] = cp
259  end
260  return cp:match(s)
261end
262
263
264-- exported names
265local re = {
266  compile = compile,
267  match = match,
268  find = find,
269  gsub = gsub,
270  updatelocale = updatelocale,
271}
272
273if version == "Lua 5.1" then _G.re = re end
274
275return re