re.lua 6.5 KB

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