lpeg.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. --- @meta
  2. error('Cannot require a meta file')
  3. -- These types were taken from https://github.com/LuaCATS/lpeg
  4. -- (based on revision 33f4ff5343a64cf613a0634d70092fbc2b64291b)
  5. -- with types being renamed to include the vim namespace and with some descriptions made less verbose.
  6. --- @brief <pre>help
  7. --- LPeg is a pattern-matching library for Lua, based on Parsing Expression
  8. --- Grammars (PEGs). https://bford.info/packrat/
  9. ---
  10. --- *lua-lpeg* *vim.lpeg.Pattern*
  11. --- The LPeg library for parsing expression grammars is included as `vim.lpeg`
  12. --- (https://www.inf.puc-rio.br/~roberto/lpeg/).
  13. ---
  14. --- In addition, its regex-like interface is available as |vim.re|
  15. --- (https://www.inf.puc-rio.br/~roberto/lpeg/re.html).
  16. ---
  17. --- </pre>
  18. vim.lpeg = {}
  19. --- @nodoc
  20. --- @class vim.lpeg.Pattern
  21. --- @operator len: vim.lpeg.Pattern
  22. --- @operator unm: vim.lpeg.Pattern
  23. --- @operator add(vim.lpeg.Pattern): vim.lpeg.Pattern
  24. --- @operator sub(vim.lpeg.Pattern): vim.lpeg.Pattern
  25. --- @operator mul(vim.lpeg.Pattern): vim.lpeg.Pattern
  26. --- @operator mul(vim.lpeg.Capture): vim.lpeg.Pattern
  27. --- @operator div(string): vim.lpeg.Capture
  28. --- @operator div(integer): vim.lpeg.Capture
  29. --- @operator div(table): vim.lpeg.Capture
  30. --- @operator div(function): vim.lpeg.Capture
  31. --- @operator pow(integer): vim.lpeg.Pattern
  32. --- @operator mod(function): vim.lpeg.Capture
  33. local Pattern = {}
  34. --- @alias vim.lpeg.Capture vim.lpeg.Pattern
  35. --- Matches the given `pattern` against the `subject` string. If the match succeeds, returns the index in the
  36. --- subject of the first character after the match, or the captured values (if the pattern captured any value).
  37. --- An optional numeric argument `init` makes the match start at that position in the subject string. As usual
  38. --- in Lua libraries, a negative value counts from the end. Unlike typical pattern-matching functions, `match`
  39. --- works only in anchored mode; that is, it tries to match the pattern with a prefix of the given subject
  40. --- string (at position `init`), not with an arbitrary substring of the subject. So, if we want to find a
  41. --- pattern anywhere in a string, we must either write a loop in Lua or write a pattern that
  42. --- matches anywhere.
  43. ---
  44. --- Example:
  45. ---
  46. --- ```lua
  47. --- local pattern = lpeg.R('az') ^ 1 * -1
  48. --- assert(pattern:match('hello') == 6)
  49. --- assert(lpeg.match(pattern, 'hello') == 6)
  50. --- assert(pattern:match('1 hello') == nil)
  51. --- ```
  52. ---
  53. --- @param pattern vim.lpeg.Pattern|string|integer|boolean|table|function
  54. --- @param subject string
  55. --- @param init? integer
  56. --- @param ... any
  57. --- @return any ...
  58. function vim.lpeg.match(pattern, subject, init, ...) end
  59. --- Matches the given `pattern` against the `subject` string. If the match succeeds, returns the
  60. --- index in the subject of the first character after the match, or the captured values (if the
  61. --- pattern captured any value). An optional numeric argument `init` makes the match start at
  62. --- that position in the subject string. As usual in Lua libraries, a negative value counts from the end.
  63. --- Unlike typical pattern-matching functions, `match` works only in anchored mode; that is, it tries
  64. --- to match the pattern with a prefix of the given subject string (at position `init`), not with
  65. --- an arbitrary substring of the subject. So, if we want to find a pattern anywhere in a string,
  66. --- we must either write a loop in Lua or write a pattern that matches anywhere.
  67. ---
  68. --- Example:
  69. ---
  70. --- ```lua
  71. --- local pattern = lpeg.R('az') ^ 1 * -1
  72. --- assert(pattern:match('hello') == 6)
  73. --- assert(lpeg.match(pattern, 'hello') == 6)
  74. --- assert(pattern:match('1 hello') == nil)
  75. --- ```
  76. ---
  77. --- @param subject string
  78. --- @param init? integer
  79. --- @param ... any
  80. --- @return any ...
  81. function Pattern:match(subject, init, ...) end
  82. --- Returns the string `"pattern"` if the given value is a pattern, otherwise `nil`.
  83. ---
  84. --- @param value vim.lpeg.Pattern|string|integer|boolean|table|function
  85. --- @return "pattern"|nil
  86. function vim.lpeg.type(value) end
  87. --- Returns a string with the running version of LPeg.
  88. --- @return string
  89. function vim.lpeg.version() end
  90. --- Sets a limit for the size of the backtrack stack used by LPeg to track calls and choices.
  91. --- The default limit is `400`. Most well-written patterns need little backtrack levels and
  92. --- therefore you seldom need to change this limit; before changing it you should try to rewrite
  93. --- your pattern to avoid the need for extra space. Nevertheless, a few useful patterns may overflow.
  94. --- Also, with recursive grammars, subjects with deep recursion may also need larger limits.
  95. ---
  96. --- @param max integer
  97. function vim.lpeg.setmaxstack(max) end
  98. --- Converts the given value into a proper pattern. The following rules are applied:
  99. --- * If the argument is a pattern, it is returned unmodified.
  100. --- * If the argument is a string, it is translated to a pattern that matches the string literally.
  101. --- * If the argument is a non-negative number `n`, the result is a pattern that matches exactly `n` characters.
  102. --- * If the argument is a negative number `-n`, the result is a pattern that succeeds only if
  103. --- the input string has less than `n` characters left: `lpeg.P(-n)` is equivalent to `-lpeg.P(n)`
  104. --- (see the unary minus operation).
  105. --- * If the argument is a boolean, the result is a pattern that always succeeds or always fails
  106. --- (according to the boolean value), without consuming any input.
  107. --- * If the argument is a table, it is interpreted as a grammar (see Grammars).
  108. --- * If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.
  109. ---
  110. --- @param value vim.lpeg.Pattern|string|integer|boolean|table|function
  111. --- @return vim.lpeg.Pattern
  112. function vim.lpeg.P(value) end
  113. --- Returns a pattern that matches only if the input string at the current position is preceded by `patt`.
  114. --- Pattern `patt` must match only strings with some fixed length, and it cannot contain captures.
  115. --- Like the `and` predicate, this pattern never consumes any input, independently of success or failure.
  116. ---
  117. --- @param pattern vim.lpeg.Pattern|string|integer|boolean|table
  118. --- @return vim.lpeg.Pattern
  119. function vim.lpeg.B(pattern) end
  120. --- Returns a pattern that matches any single character belonging to one of the given ranges.
  121. --- Each `range` is a string `xy` of length 2, representing all characters with code between the codes of
  122. --- `x` and `y` (both inclusive). As an example, the pattern `lpeg.R('09')` matches any digit, and
  123. --- `lpeg.R('az', 'AZ')` matches any ASCII letter.
  124. ---
  125. --- Example:
  126. ---
  127. --- ```lua
  128. --- local pattern = lpeg.R('az') ^ 1 * -1
  129. --- assert(pattern:match('hello') == 6)
  130. --- ```
  131. ---
  132. --- @param ... string
  133. --- @return vim.lpeg.Pattern
  134. function vim.lpeg.R(...) end
  135. --- Returns a pattern that matches any single character that appears in the given string (the `S` stands for Set).
  136. --- As an example, the pattern `lpeg.S('+-*/')` matches any arithmetic operator. Note that, if `s` is a character
  137. --- (that is, a string of length 1), then `lpeg.P(s)` is equivalent to `lpeg.S(s)` which is equivalent to
  138. --- `lpeg.R(s..s)`. Note also that both `lpeg.S('')` and `lpeg.R()` are patterns that always fail.
  139. ---
  140. --- @param string string
  141. --- @return vim.lpeg.Pattern
  142. function vim.lpeg.S(string) end
  143. --- Creates a non-terminal (a variable) for a grammar. This operation creates a non-terminal (a variable)
  144. --- for a grammar. The created non-terminal refers to the rule indexed by `v` in the enclosing grammar.
  145. ---
  146. --- Example:
  147. ---
  148. --- ```lua
  149. --- local b = lpeg.P({'(' * ((1 - lpeg.S '()') + lpeg.V(1)) ^ 0 * ')'})
  150. --- assert(b:match('((string))') == 11)
  151. --- assert(b:match('(') == nil)
  152. --- ```
  153. ---
  154. --- @param v boolean|string|number|function|table|thread|userdata|lightuserdata
  155. --- @return vim.lpeg.Pattern
  156. function vim.lpeg.V(v) end
  157. --- @nodoc
  158. --- @class vim.lpeg.Locale
  159. --- @field alnum userdata
  160. --- @field alpha userdata
  161. --- @field cntrl userdata
  162. --- @field digit userdata
  163. --- @field graph userdata
  164. --- @field lower userdata
  165. --- @field print userdata
  166. --- @field punct userdata
  167. --- @field space userdata
  168. --- @field upper userdata
  169. --- @field xdigit userdata
  170. --- Returns a table with patterns for matching some character classes according to the current locale.
  171. --- The table has fields named `alnum`, `alpha`, `cntrl`, `digit`, `graph`, `lower`, `print`, `punct`,
  172. --- `space`, `upper`, and `xdigit`, each one containing a correspondent pattern. Each pattern matches
  173. --- any single character that belongs to its class.
  174. --- If called with an argument `table`, then it creates those fields inside the given table and returns
  175. --- that table.
  176. ---
  177. --- Example:
  178. ---
  179. --- ```lua
  180. --- lpeg.locale(lpeg)
  181. --- local space = lpeg.space ^ 0
  182. --- local name = lpeg.C(lpeg.alpha ^ 1) * space
  183. --- local sep = lpeg.S(',;') * space
  184. --- local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1
  185. --- local list = lpeg.Cf(lpeg.Ct('') * pair ^ 0, rawset)
  186. --- local t = list:match('a=b, c = hi; next = pi')
  187. --- assert(t.a == 'b')
  188. --- assert(t.c == 'hi')
  189. --- assert(t.next == 'pi')
  190. --- local locale = lpeg.locale()
  191. --- assert(type(locale.digit) == 'userdata')
  192. --- ```
  193. ---
  194. --- @param tab? table
  195. --- @return vim.lpeg.Locale
  196. function vim.lpeg.locale(tab) end
  197. --- Creates a simple capture, which captures the substring of the subject that matches `patt`.
  198. --- The captured value is a string. If `patt` has other captures, their values are returned after this one.
  199. ---
  200. --- Example:
  201. ---
  202. --- ```lua
  203. --- local function split (s, sep)
  204. --- sep = lpeg.P(sep)
  205. --- local elem = lpeg.C((1 - sep) ^ 0)
  206. --- local p = elem * (sep * elem) ^ 0
  207. --- return lpeg.match(p, s)
  208. --- end
  209. --- local a, b, c = split('a,b,c', ',')
  210. --- assert(a == 'a')
  211. --- assert(b == 'b')
  212. --- assert(c == 'c')
  213. --- ```
  214. ---
  215. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  216. --- @return vim.lpeg.Capture
  217. function vim.lpeg.C(patt) end
  218. --- Creates an argument capture. This pattern matches the empty string and produces the value given as the
  219. --- nth extra argument given in the call to `lpeg.match`.
  220. --- @param n integer
  221. --- @return vim.lpeg.Capture
  222. function vim.lpeg.Carg(n) end
  223. --- Creates a back capture. This pattern matches the empty string and produces the values produced by the most recent
  224. --- group capture named `name` (where `name` can be any Lua value). Most recent means the last complete outermost
  225. --- group capture with the given name. A Complete capture means that the entire pattern corresponding to the capture
  226. --- has matched. An Outermost capture means that the capture is not inside another complete capture.
  227. --- In the same way that LPeg does not specify when it evaluates captures, it does not specify whether it reuses
  228. --- values previously produced by the group or re-evaluates them.
  229. ---
  230. --- @param name any
  231. --- @return vim.lpeg.Capture
  232. function vim.lpeg.Cb(name) end
  233. --- Creates a constant capture. This pattern matches the empty string and produces all given values as its captured values.
  234. ---
  235. --- @param ... any
  236. --- @return vim.lpeg.Capture
  237. function vim.lpeg.Cc(...) end
  238. --- Creates a fold capture. If `patt` produces a list of captures C1 C2 ... Cn, this capture will produce the value
  239. --- `func(...func(func(C1, C2), C3)...,Cn)`, that is, it will fold (or accumulate, or reduce) the captures from
  240. --- `patt` using function `func`. This capture assumes that `patt` should produce at least one capture with at
  241. --- least one value (of any type), which becomes the initial value of an accumulator. (If you need a specific
  242. --- initial value, you may prefix a constant capture to `patt`.) For each subsequent capture, LPeg calls `func`
  243. --- with this accumulator as the first argument and all values produced by the capture as extra arguments;
  244. --- the first result from this call becomes the new value for the accumulator. The final value of the accumulator
  245. --- becomes the captured value.
  246. ---
  247. --- Example:
  248. ---
  249. --- ```lua
  250. --- local number = lpeg.R('09') ^ 1 / tonumber
  251. --- local list = number * (',' * number) ^ 0
  252. --- local function add(acc, newvalue) return acc + newvalue end
  253. --- local sum = lpeg.Cf(list, add)
  254. --- assert(sum:match('10,30,43') == 83)
  255. --- ```
  256. ---
  257. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  258. --- @param func fun(acc, newvalue)
  259. --- @return vim.lpeg.Capture
  260. function vim.lpeg.Cf(patt, func) end
  261. --- Creates a group capture. It groups all values returned by `patt` into a single capture.
  262. --- The group may be anonymous (if no name is given) or named with the given name (which
  263. --- can be any non-nil Lua value).
  264. ---
  265. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  266. --- @param name? string
  267. --- @return vim.lpeg.Capture
  268. function vim.lpeg.Cg(patt, name) end
  269. --- Creates a position capture. It matches the empty string and captures the position in the
  270. --- subject where the match occurs. The captured value is a number.
  271. ---
  272. --- Example:
  273. ---
  274. --- ```lua
  275. --- local I = lpeg.Cp()
  276. --- local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end
  277. --- local match_start, match_end = anywhere('world'):match('hello world!')
  278. --- assert(match_start == 7)
  279. --- assert(match_end == 12)
  280. --- ```
  281. ---
  282. --- @return vim.lpeg.Capture
  283. function vim.lpeg.Cp() end
  284. --- Creates a substitution capture. This function creates a substitution capture, which
  285. --- captures the substring of the subject that matches `patt`, with substitutions.
  286. --- For any capture inside `patt` with a value, the substring that matched the capture
  287. --- is replaced by the capture value (which should be a string). The final captured
  288. --- value is the string resulting from all replacements.
  289. ---
  290. --- Example:
  291. ---
  292. --- ```lua
  293. --- local function gsub (s, patt, repl)
  294. --- patt = lpeg.P(patt)
  295. --- patt = lpeg.Cs((patt / repl + 1) ^ 0)
  296. --- return lpeg.match(patt, s)
  297. --- end
  298. --- assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!')
  299. --- ```
  300. ---
  301. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  302. --- @return vim.lpeg.Capture
  303. function vim.lpeg.Cs(patt) end
  304. --- Creates a table capture. This capture returns a table with all values from all anonymous captures
  305. --- made by `patt` inside this table in successive integer keys, starting at 1.
  306. --- Moreover, for each named capture group created by `patt`, the first value of the group is put into
  307. --- the table with the group name as its key. The captured value is only the table.
  308. ---
  309. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  310. --- @return vim.lpeg.Capture
  311. function vim.lpeg.Ct(patt) end
  312. --- Creates a match-time capture. Unlike all other captures, this one is evaluated immediately when a match occurs
  313. --- (even if it is part of a larger pattern that fails later). It forces the immediate evaluation of all its nested captures
  314. --- and then calls `function`. The given function gets as arguments the entire subject, the current position
  315. --- (after the match of `patt`), plus any capture values produced by `patt`. The first value returned by `function`
  316. --- defines how the match happens. If the call returns a number, the match succeeds and the returned number
  317. --- becomes the new current position. (Assuming a subject sand current position `i`, the returned number must be
  318. --- in the range `[i, len(s) + 1]`.) If the call returns `true`, the match succeeds without consuming any input
  319. --- (so, to return true is equivalent to return `i`). If the call returns `false`, `nil`, or no value, the match fails.
  320. --- Any extra values returned by the function become the values produced by the capture.
  321. ---
  322. --- @param patt vim.lpeg.Pattern|string|integer|boolean|table|function
  323. --- @param fn fun(s: string, i: integer, ...: any): (position: boolean|integer, ...: any)
  324. --- @return vim.lpeg.Capture
  325. function vim.lpeg.Cmt(patt, fn) end