docs(lpeg): merge upstream changes

This commit is contained in:
Maria José Solano 2024-02-09 10:35:13 -08:00 committed by Lewis Russell
parent a7788c2e25
commit cb146cc4aa
2 changed files with 72 additions and 72 deletions

View File

@ -3035,10 +3035,10 @@ Pattern:match({subject}, {init}) *Pattern:match()*
pattern that matches anywhere. pattern that matches anywhere.
Example: >lua Example: >lua
local pattern = lpeg.R("az") ^ 1 * -1 local pattern = lpeg.R('az') ^ 1 * -1
assert(pattern:match("hello") == 6) assert(pattern:match('hello') == 6)
assert(lpeg.match(pattern, "hello") == 6) assert(lpeg.match(pattern, 'hello') == 6)
assert(pattern:match("1 hello") == nil) assert(pattern:match('1 hello') == nil)
< <
Parameters: ~ Parameters: ~
@ -3051,7 +3051,7 @@ Pattern:match({subject}, {init}) *Pattern:match()*
vim.lpeg.B({pattern}) *vim.lpeg.B()* vim.lpeg.B({pattern}) *vim.lpeg.B()*
Returns a pattern that matches only if the input string at the current Returns a pattern that matches only if the input string at the current
position is preceded by `patt`. Pattern `patt` must match only strings position is preceded by `patt`. Pattern `patt` must match only strings
with some fixed length, and it cannot contain captures. Like the and with some fixed length, and it cannot contain captures. Like the `and`
predicate, this pattern never consumes any input, independently of success predicate, this pattern never consumes any input, independently of success
or failure. or failure.
@ -3069,8 +3069,8 @@ vim.lpeg.C({patt}) *vim.lpeg.C()*
Example: >lua Example: >lua
local function split (s, sep) local function split (s, sep)
sep = lpeg.P(sep) sep = lpeg.P(sep)
local elem = lpeg.C((1 - sep)^0) local elem = lpeg.C((1 - sep) ^ 0)
local p = elem * (sep * elem)^0 local p = elem * (sep * elem) ^ 0
return lpeg.match(p, s) return lpeg.match(p, s)
end end
local a, b, c = split('a,b,c', ',') local a, b, c = split('a,b,c', ',')
@ -3137,11 +3137,11 @@ vim.lpeg.Cf({patt}, {func}) *vim.lpeg.Cf()*
final value of the accumulator becomes the captured value. final value of the accumulator becomes the captured value.
Example: >lua Example: >lua
local number = lpeg.R("09") ^ 1 / tonumber local number = lpeg.R('09') ^ 1 / tonumber
local list = number * ("," * number) ^ 0 local list = number * (',' * number) ^ 0
local function add(acc, newvalue) return acc + newvalue end local function add(acc, newvalue) return acc + newvalue end
local sum = lpeg.Cf(list, add) local sum = lpeg.Cf(list, add)
assert(sum:match("10,30,43") == 83) assert(sum:match('10,30,43') == 83)
< <
Parameters: ~ Parameters: ~
@ -3172,12 +3172,12 @@ vim.lpeg.Cmt({patt}, {fn}) *vim.lpeg.Cmt()*
`patt`), plus any capture values produced by `patt`. The first value `patt`), plus any capture values produced by `patt`. The first value
returned by `function` defines how the match happens. If the call returns returned by `function` defines how the match happens. If the call returns
a number, the match succeeds and the returned number becomes the new a number, the match succeeds and the returned number becomes the new
current position. (Assuming a subject sand current position i, the current position. (Assuming a subject sand current position `i`, the
returned number must be in the range [i, len(s) + 1].) If the call returns returned number must be in the range `[i, len(s) + 1]`.) If the call
true, the match succeeds without consuming any input (so, to return true returns `true`, the match succeeds without consuming any input (so, to
is equivalent to return i). If the call returns false, nil, or no value, return true is equivalent to return `i`). If the call returns `false`,
the match fails. Any extra values returned by the function become the `nil`, or no value, the match fails. Any extra values returned by the
values produced by the capture. function become the values produced by the capture.
Parameters: ~ Parameters: ~
• {patt} (`vim.lpeg.Pattern`) • {patt} (`vim.lpeg.Pattern`)
@ -3194,7 +3194,7 @@ vim.lpeg.Cp() *vim.lpeg.Cp()*
Example: >lua Example: >lua
local I = lpeg.Cp() local I = lpeg.Cp()
local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end
local match_start, match_end = anywhere("world"):match("hello world!") local match_start, match_end = anywhere('world'):match('hello world!')
assert(match_start == 7) assert(match_start == 7)
assert(match_end == 12) assert(match_end == 12)
< <
@ -3213,7 +3213,7 @@ vim.lpeg.Cs({patt}) *vim.lpeg.Cs()*
Example: >lua Example: >lua
local function gsub (s, patt, repl) local function gsub (s, patt, repl)
patt = lpeg.P(patt) patt = lpeg.P(patt)
patt = lpeg.Cs((patt / repl + 1)^0) patt = lpeg.Cs((patt / repl + 1) ^ 0)
return lpeg.match(patt, s) return lpeg.match(patt, s)
end end
assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!') assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!')
@ -3249,12 +3249,12 @@ vim.lpeg.locale({tab}) *vim.lpeg.locale()*
Example: >lua Example: >lua
lpeg.locale(lpeg) lpeg.locale(lpeg)
local space = lpeg.space^0 local space = lpeg.space ^ 0
local name = lpeg.C(lpeg.alpha^1) * space local name = lpeg.C(lpeg.alpha ^ 1) * space
local sep = lpeg.S(",;") * space local sep = lpeg.S(',;') * space
local pair = lpeg.Cg(name * "=" * space * name) * sep^-1 local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1
local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset) local list = lpeg.Cf(lpeg.Ct('') * pair ^ 0, rawset)
local t = list:match("a=b, c = hi; next = pi") local t = list:match('a=b, c = hi; next = pi')
assert(t.a == 'b') assert(t.a == 'b')
assert(t.c == 'hi') assert(t.c == 'hi')
assert(t.next == 'pi') assert(t.next == 'pi')
@ -3282,10 +3282,10 @@ vim.lpeg.match({pattern}, {subject}, {init}) *vim.lpeg.match()*
pattern that matches anywhere. pattern that matches anywhere.
Example: >lua Example: >lua
local pattern = lpeg.R("az") ^ 1 * -1 local pattern = lpeg.R('az') ^ 1 * -1
assert(pattern:match("hello") == 6) assert(pattern:match('hello') == 6)
assert(lpeg.match(pattern, "hello") == 6) assert(lpeg.match(pattern, 'hello') == 6)
assert(pattern:match("1 hello") == nil) assert(pattern:match('1 hello') == nil)
< <
Parameters: ~ Parameters: ~
@ -3297,7 +3297,7 @@ vim.lpeg.match({pattern}, {subject}, {init}) *vim.lpeg.match()*
(`integer|vim.lpeg.Capture?`) (`integer|vim.lpeg.Capture?`)
vim.lpeg.P({value}) *vim.lpeg.P()* vim.lpeg.P({value}) *vim.lpeg.P()*
Converts the given value into a proper pattern. This following rules are Converts the given value into a proper pattern. The following rules are
applied: applied:
• If the argument is a pattern, it is returned unmodified. • If the argument is a pattern, it is returned unmodified.
• If the argument is a string, it is translated to a pattern that matches • If the argument is a string, it is translated to a pattern that matches
@ -3314,7 +3314,7 @@ vim.lpeg.P({value}) *vim.lpeg.P()*
• If the argument is a table, it is interpreted as a grammar (see • If the argument is a table, it is interpreted as a grammar (see
Grammars). Grammars).
• If the argument is a function, returns a pattern equivalent to a • If the argument is a function, returns a pattern equivalent to a
match-time captureover the empty string. match-time capture over the empty string.
Parameters: ~ Parameters: ~
• {value} (`vim.lpeg.Pattern|string|integer|boolean|table|function`) • {value} (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
@ -3326,12 +3326,12 @@ vim.lpeg.R({...}) *vim.lpeg.R()*
Returns a pattern that matches any single character belonging to one of Returns a pattern that matches any single character belonging to one of
the given ranges. Each `range` is a string `xy` of length 2, representing the given ranges. Each `range` is a string `xy` of length 2, representing
all characters with code between the codes of `x` and `y` (both all characters with code between the codes of `x` and `y` (both
inclusive). As an example, the pattern `lpeg.R("09")` matches any digit, inclusive). As an example, the pattern ``lpeg.R('09')`` matches any digit,
and `lpeg.R("az", "AZ")` matches any ASCII letter. and ``lpeg.R('az', 'AZ')`` matches any ASCII letter.
Example: >lua Example: >lua
local pattern = lpeg.R("az") ^ 1 * -1 local pattern = lpeg.R('az') ^ 1 * -1
assert(pattern:match("hello") == 6) assert(pattern:match('hello') == 6)
< <
Parameters: ~ Parameters: ~
@ -3343,10 +3343,10 @@ vim.lpeg.R({...}) *vim.lpeg.R()*
vim.lpeg.S({string}) *vim.lpeg.S()* vim.lpeg.S({string}) *vim.lpeg.S()*
Returns a pattern that matches any single character that appears in the Returns a pattern that matches any single character that appears in the
given string (the `S` stands for Set). As an example, the pattern given string (the `S` stands for Set). As an example, the pattern
`lpeg.S("+-*/")` matches any arithmetic operator. Note that, if `s` is a ``lpeg.S('+-*/')`` matches any arithmetic operator. Note that, if `s` is a
character (that is, a string of length 1), then `lpeg.P(s)` is equivalent character (that is, a string of length 1), then `lpeg.P(s)` is equivalent
to `lpeg.S(s)` which is equivalent to `lpeg.R(s..s)`. Note also that both to `lpeg.S(s)` which is equivalent to `lpeg.R(s..s)`. Note also that both
`lpeg.S("")` and `lpeg.R()` are patterns that always fail. ``lpeg.S('')`` and `lpeg.R()` are patterns that always fail.
Parameters: ~ Parameters: ~
• {string} (`string`) • {string} (`string`)
@ -3382,7 +3382,7 @@ vim.lpeg.V({v}) *vim.lpeg.V()*
to the rule indexed by `v` in the enclosing grammar. to the rule indexed by `v` in the enclosing grammar.
Example: >lua Example: >lua
local b = lpeg.P({"(" * ((1 - lpeg.S "()") + lpeg.V(1)) ^ 0 * ")"}) local b = lpeg.P({'(' * ((1 - lpeg.S '()') + lpeg.V(1)) ^ 0 * ')'})
assert(b:match('((string))') == 11) assert(b:match('((string))') == 11)
assert(b:match('(') == nil) assert(b:match('(') == nil)
< <

View File

@ -2,7 +2,7 @@
error('Cannot require a meta file') error('Cannot require a meta file')
-- These types were taken from https://github.com/LuaCATS/lpeg -- These types were taken from https://github.com/LuaCATS/lpeg
-- (based on revision 4aded588f9531d89555566bb1de27490354b91c7) -- (based on revision e6789e28e5b91a4a277a2a03081d708c403a3e34)
-- with types being renamed to include the vim namespace and with some descriptions made less verbose. -- with types being renamed to include the vim namespace and with some descriptions made less verbose.
--- @brief <pre>help --- @brief <pre>help
@ -49,10 +49,10 @@ local Pattern = {}
--- Example: --- Example:
--- ---
--- ```lua --- ```lua
--- local pattern = lpeg.R("az") ^ 1 * -1 --- local pattern = lpeg.R('az') ^ 1 * -1
--- assert(pattern:match("hello") == 6) --- assert(pattern:match('hello') == 6)
--- assert(lpeg.match(pattern, "hello") == 6) --- assert(lpeg.match(pattern, 'hello') == 6)
--- assert(pattern:match("1 hello") == nil) --- assert(pattern:match('1 hello') == nil)
--- ``` --- ```
--- ---
--- @param pattern vim.lpeg.Pattern --- @param pattern vim.lpeg.Pattern
@ -73,10 +73,10 @@ function vim.lpeg.match(pattern, subject, init) end
--- Example: --- Example:
--- ---
--- ```lua --- ```lua
--- local pattern = lpeg.R("az") ^ 1 * -1 --- local pattern = lpeg.R('az') ^ 1 * -1
--- assert(pattern:match("hello") == 6) --- assert(pattern:match('hello') == 6)
--- assert(lpeg.match(pattern, "hello") == 6) --- assert(lpeg.match(pattern, 'hello') == 6)
--- assert(pattern:match("1 hello") == nil) --- assert(pattern:match('1 hello') == nil)
--- ``` --- ```
--- ---
--- @param subject string --- @param subject string
@ -103,7 +103,7 @@ function vim.lpeg.version() end
--- @param max integer --- @param max integer
function vim.lpeg.setmaxstack(max) end function vim.lpeg.setmaxstack(max) end
--- Converts the given value into a proper pattern. This following rules are applied: --- Converts the given value into a proper pattern. The following rules are applied:
--- * If the argument is a pattern, it is returned unmodified. --- * If the argument is a pattern, it is returned unmodified.
--- * If the argument is a string, it is translated to a pattern that matches the string literally. --- * If the argument is a string, it is translated to a pattern that matches the string literally.
--- * If the argument is a non-negative number `n`, the result is a pattern that matches exactly `n` characters. --- * If the argument is a non-negative number `n`, the result is a pattern that matches exactly `n` characters.
@ -113,7 +113,7 @@ function vim.lpeg.setmaxstack(max) end
--- * If the argument is a boolean, the result is a pattern that always succeeds or always fails --- * If the argument is a boolean, the result is a pattern that always succeeds or always fails
--- (according to the boolean value), without consuming any input. --- (according to the boolean value), without consuming any input.
--- * If the argument is a table, it is interpreted as a grammar (see Grammars). --- * If the argument is a table, it is interpreted as a grammar (see Grammars).
--- * If the argument is a function, returns a pattern equivalent to a match-time captureover the empty string. --- * If the argument is a function, returns a pattern equivalent to a match-time capture over the empty string.
--- ---
--- @param value vim.lpeg.Pattern|string|integer|boolean|table|function --- @param value vim.lpeg.Pattern|string|integer|boolean|table|function
--- @return vim.lpeg.Pattern --- @return vim.lpeg.Pattern
@ -121,7 +121,7 @@ function vim.lpeg.P(value) end
--- Returns a pattern that matches only if the input string at the current position is preceded by `patt`. --- Returns a pattern that matches only if the input string at the current position is preceded by `patt`.
--- Pattern `patt` must match only strings with some fixed length, and it cannot contain captures. --- Pattern `patt` must match only strings with some fixed length, and it cannot contain captures.
--- Like the and predicate, this pattern never consumes any input, independently of success or failure. --- Like the `and` predicate, this pattern never consumes any input, independently of success or failure.
--- ---
--- @param pattern vim.lpeg.Pattern --- @param pattern vim.lpeg.Pattern
--- @return vim.lpeg.Pattern --- @return vim.lpeg.Pattern
@ -129,14 +129,14 @@ function vim.lpeg.B(pattern) end
--- Returns a pattern that matches any single character belonging to one of the given ranges. --- Returns a pattern that matches any single character belonging to one of the given ranges.
--- Each `range` is a string `xy` of length 2, representing all characters with code between the codes of --- Each `range` is a string `xy` of length 2, representing all characters with code between the codes of
--- `x` and `y` (both inclusive). As an example, the pattern `lpeg.R("09")` matches any digit, and --- `x` and `y` (both inclusive). As an example, the pattern ``lpeg.R('09')`` matches any digit, and
--- `lpeg.R("az", "AZ")` matches any ASCII letter. --- ``lpeg.R('az', 'AZ')`` matches any ASCII letter.
--- ---
--- Example: --- Example:
--- ---
--- ```lua --- ```lua
--- local pattern = lpeg.R("az") ^ 1 * -1 --- local pattern = lpeg.R('az') ^ 1 * -1
--- assert(pattern:match("hello") == 6) --- assert(pattern:match('hello') == 6)
--- ``` --- ```
--- ---
--- @param ... string --- @param ... string
@ -144,9 +144,9 @@ function vim.lpeg.B(pattern) end
function vim.lpeg.R(...) end function vim.lpeg.R(...) end
--- Returns a pattern that matches any single character that appears in the given string (the `S` stands for Set). --- Returns a pattern that matches any single character that appears in the given string (the `S` stands for Set).
--- As an example, the pattern `lpeg.S("+-*/")` matches any arithmetic operator. Note that, if `s` is a character --- As an example, the pattern ``lpeg.S('+-*/')`` matches any arithmetic operator. Note that, if `s` is a character
--- (that is, a string of length 1), then `lpeg.P(s)` is equivalent to `lpeg.S(s)` which is equivalent to --- (that is, a string of length 1), then `lpeg.P(s)` is equivalent to `lpeg.S(s)` which is equivalent to
--- `lpeg.R(s..s)`. Note also that both `lpeg.S("")` and `lpeg.R()` are patterns that always fail. --- `lpeg.R(s..s)`. Note also that both ``lpeg.S('')`` and `lpeg.R()` are patterns that always fail.
--- ---
--- @param string string --- @param string string
--- @return vim.lpeg.Pattern --- @return vim.lpeg.Pattern
@ -158,7 +158,7 @@ function vim.lpeg.S(string) end
--- Example: --- Example:
--- ---
--- ```lua --- ```lua
--- local b = lpeg.P({"(" * ((1 - lpeg.S "()") + lpeg.V(1)) ^ 0 * ")"}) --- local b = lpeg.P({'(' * ((1 - lpeg.S '()') + lpeg.V(1)) ^ 0 * ')'})
--- assert(b:match('((string))') == 11) --- assert(b:match('((string))') == 11)
--- assert(b:match('(') == nil) --- assert(b:match('(') == nil)
--- ``` --- ```
@ -191,12 +191,12 @@ function vim.lpeg.V(v) end
--- ---
--- ```lua --- ```lua
--- lpeg.locale(lpeg) --- lpeg.locale(lpeg)
--- local space = lpeg.space^0 --- local space = lpeg.space ^ 0
--- local name = lpeg.C(lpeg.alpha^1) * space --- local name = lpeg.C(lpeg.alpha ^ 1) * space
--- local sep = lpeg.S(",;") * space --- local sep = lpeg.S(',;') * space
--- local pair = lpeg.Cg(name * "=" * space * name) * sep^-1 --- local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1
--- local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset) --- local list = lpeg.Cf(lpeg.Ct('') * pair ^ 0, rawset)
--- local t = list:match("a=b, c = hi; next = pi") --- local t = list:match('a=b, c = hi; next = pi')
--- assert(t.a == 'b') --- assert(t.a == 'b')
--- assert(t.c == 'hi') --- assert(t.c == 'hi')
--- assert(t.next == 'pi') --- assert(t.next == 'pi')
@ -216,8 +216,8 @@ function vim.lpeg.locale(tab) end
--- ```lua --- ```lua
--- local function split (s, sep) --- local function split (s, sep)
--- sep = lpeg.P(sep) --- sep = lpeg.P(sep)
--- local elem = lpeg.C((1 - sep)^0) --- local elem = lpeg.C((1 - sep) ^ 0)
--- local p = elem * (sep * elem)^0 --- local p = elem * (sep * elem) ^ 0
--- return lpeg.match(p, s) --- return lpeg.match(p, s)
--- end --- end
--- local a, b, c = split('a,b,c', ',') --- local a, b, c = split('a,b,c', ',')
@ -265,11 +265,11 @@ function vim.lpeg.Cc(...) end
--- Example: --- Example:
--- ---
--- ```lua --- ```lua
--- local number = lpeg.R("09") ^ 1 / tonumber --- local number = lpeg.R('09') ^ 1 / tonumber
--- local list = number * ("," * number) ^ 0 --- local list = number * (',' * number) ^ 0
--- local function add(acc, newvalue) return acc + newvalue end --- local function add(acc, newvalue) return acc + newvalue end
--- local sum = lpeg.Cf(list, add) --- local sum = lpeg.Cf(list, add)
--- assert(sum:match("10,30,43") == 83) --- assert(sum:match('10,30,43') == 83)
--- ``` --- ```
--- ---
--- @param patt vim.lpeg.Pattern --- @param patt vim.lpeg.Pattern
@ -294,7 +294,7 @@ function vim.lpeg.Cg(patt, name) end
--- ```lua --- ```lua
--- local I = lpeg.Cp() --- local I = lpeg.Cp()
--- local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end --- local function anywhere(p) return lpeg.P({I * p * I + 1 * lpeg.V(1)}) end
--- local match_start, match_end = anywhere("world"):match("hello world!") --- local match_start, match_end = anywhere('world'):match('hello world!')
--- assert(match_start == 7) --- assert(match_start == 7)
--- assert(match_end == 12) --- assert(match_end == 12)
--- ``` --- ```
@ -313,7 +313,7 @@ function vim.lpeg.Cp() end
--- ```lua --- ```lua
--- local function gsub (s, patt, repl) --- local function gsub (s, patt, repl)
--- patt = lpeg.P(patt) --- patt = lpeg.P(patt)
--- patt = lpeg.Cs((patt / repl + 1)^0) --- patt = lpeg.Cs((patt / repl + 1) ^ 0)
--- return lpeg.match(patt, s) --- return lpeg.match(patt, s)
--- end --- end
--- assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!') --- assert(gsub('Hello, xxx!', 'xxx', 'World') == 'Hello, World!')
@ -337,9 +337,9 @@ function vim.lpeg.Ct(patt) end
--- and then calls `function`. The given function gets as arguments the entire subject, the current position --- and then calls `function`. The given function gets as arguments the entire subject, the current position
--- (after the match of `patt`), plus any capture values produced by `patt`. The first value returned by `function` --- (after the match of `patt`), plus any capture values produced by `patt`. The first value returned by `function`
--- defines how the match happens. If the call returns a number, the match succeeds and the returned number --- defines how the match happens. If the call returns a number, the match succeeds and the returned number
--- becomes the new current position. (Assuming a subject sand current position i, the returned number must be --- becomes the new current position. (Assuming a subject sand current position `i`, the returned number must be
--- in the range [i, len(s) + 1].) If the call returns true, the match succeeds without consuming any input --- in the range `[i, len(s) + 1]`.) If the call returns `true`, the match succeeds without consuming any input
--- (so, to return true is equivalent to return i). If the call returns false, nil, or no value, the match fails. --- (so, to return true is equivalent to return `i`). If the call returns `false`, `nil`, or no value, the match fails.
--- Any extra values returned by the function become the values produced by the capture. --- Any extra values returned by the function become the values produced by the capture.
--- ---
--- @param patt vim.lpeg.Pattern --- @param patt vim.lpeg.Pattern