Home Explore Blog CI



neovim

47th chunk of `runtime/doc/lua.txt`
9d1791f76780b2230e0913ca3f12b4436c0a42c972fe4fa10000000100000fc3
 group name as its key. The captured value is only the table.

    Parameters: ~
      • {patt}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)

    Return: ~
        (`vim.lpeg.Capture`)

vim.lpeg.locale({tab})                                     *vim.lpeg.locale()*
    Returns a table with patterns for matching some character classes
    according to the current locale. The table has fields named `alnum`,
    `alpha`, `cntrl`, `digit`, `graph`, `lower`, `print`, `punct`, `space`,
    `upper`, and `xdigit`, each one containing a correspondent pattern. Each
    pattern matches any single character that belongs to its class. If called
    with an argument `table`, then it creates those fields inside the given
    table and returns that table.

    Example: >lua
        lpeg.locale(lpeg)
        local space = lpeg.space ^ 0
        local name = lpeg.C(lpeg.alpha ^ 1) * space
        local sep = lpeg.S(',;') * space
        local pair = lpeg.Cg(name * '=' * space * name) * sep ^ -1
        local list = lpeg.Cf(lpeg.Ct('') * pair ^ 0, rawset)
        local t = list:match('a=b, c = hi; next = pi')
        assert(t.a == 'b')
        assert(t.c == 'hi')
        assert(t.next == 'pi')
        local locale = lpeg.locale()
        assert(type(locale.digit) == 'userdata')
<

    Parameters: ~
      • {tab}  (`table?`)

    Return: ~
        (`vim.lpeg.Locale`)

vim.lpeg.match({pattern}, {subject}, {init}, {...})         *vim.lpeg.match()*
    Matches the given `pattern` against the `subject` string. If the match
    succeeds, returns the index in the subject of the first character after
    the match, or the captured values (if the pattern captured any value). An
    optional numeric argument `init` makes the match start at that position in
    the subject string. As usual in Lua libraries, a negative value counts
    from the end. Unlike typical pattern-matching functions, `match` works
    only in anchored mode; that is, it tries to match the pattern with a
    prefix of the given subject string (at position `init`), not with an
    arbitrary substring of the subject. So, if we want to find a pattern
    anywhere in a string, we must either write a loop in Lua or write a
    pattern that matches anywhere.

    Example: >lua
        local pattern = lpeg.R('az') ^ 1 * -1
        assert(pattern:match('hello') == 6)
        assert(lpeg.match(pattern, 'hello') == 6)
        assert(pattern:match('1 hello') == nil)
<

    Parameters: ~
      • {pattern}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)
      • {subject}  (`string`)
      • {init}     (`integer?`)
      • {...}      (`any`)

    Return: ~
        (`any`) ...

vim.lpeg.P({value})                                             *vim.lpeg.P()*
    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 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 negative number `-n`, the result is a pattern that
      succeeds only if the input string has less than `n` characters left:
      `lpeg.P(-n)` is equivalent to `-lpeg.P(n)` (see the unary minus
      operation).
    • 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.
    • 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 capture over the empty string.

    Parameters: ~
      • {value}  (`vim.lpeg.Pattern|string|integer|boolean|table|function`)

    Return: ~
        (`vim.lpeg.Pattern`)

vim.lpeg.R({...})                                               *vim.lpeg.R()*
    Returns a pattern that matches

Title: Lua API: vim.lpeg.locale(), vim.lpeg.match(), and vim.lpeg.P()
Summary
This section describes the `vim.lpeg.locale()` function, which provides patterns for matching locale-specific character classes. It also explains the `vim.lpeg.match()` function, which matches a pattern against a subject string, returning the index of the character after the match or captured values. Finally, it details the `vim.lpeg.P()` function, which converts various data types (patterns, strings, numbers, booleans, tables, functions) into LPeg patterns.