Home Explore Blog CI



neovim

43th chunk of `runtime/doc/lua.txt`
daacffd0a1e3b3cfaec9febe1407db1a2b3169d1e6d612100000000100000fd0

==============================================================================
Lua module: vim.glob                                                *vim.glob*

Glob-to-LPeg Converter (Peglob) This module converts glob patterns to LPeg
patterns according to the LSP 3.17 specification:
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#pattern

Glob grammar overview:
• `*` to match zero or more characters in a path segment
• `?` to match on one character in a path segment
• `**` to match any number of path segments, including none
• `{}` to group conditions (e.g. `*.{ts,js}` matches TypeScript and JavaScript
  files)
• `[]` to declare a range of characters to match in a path segment (e.g.,
  `example.[0-9]` to match on `example.0`, `example.1`, …)
• `[!...]` to negate a range of characters to match in a path segment (e.g.,
  `example.[!0-9]` to match on `example.a`, `example.b`, but not `example.0`)

Additional constraints:
• A Glob pattern must match an entire path, with partial matches considered
  failures.
• The pattern only determines success or failure, without specifying which
  parts correspond to which characters.
• A path segment is the portion of a path between two adjacent path separators
  (`/`), or between the start/end of the path and the nearest separator.
• The `**` (globstar) pattern matches zero or more path segments, including
  intervening separators (`/`). Within pattern strings, `**` must be delimited
  by path separators (`/`) or pattern boundaries and cannot be adjacent to any
  characters other than `/`. If `**` is not the final element, it must be
  followed by `/`.
• `{}` (braced conditions) contains valid Glob patterns as branches, separated
  by commas. Commas are exclusively used for separating branches and cannot
  appear within a branch for any other purpose. Nested `{}` structures are
  allowed, but `{}` must contain at least two branches—zero or one branch is
  not permitted.
• In `[]` or `[!...]`, a character range consists of character intervals
  (e.g., `a-z`) or individual characters (e.g., `w`). A range including `/`
  won’t match that character.


vim.glob.to_lpeg({pattern})                               *vim.glob.to_lpeg()*
    Parses a raw glob into an |lua-lpeg| pattern.

    Parameters: ~
      • {pattern}  (`string`) The raw glob pattern

    Return: ~
        (`vim.lpeg.Pattern`) An |lua-lpeg| representation of the pattern


==============================================================================
VIM.LPEG                                                            *vim.lpeg*


LPeg is a pattern-matching library for Lua, based on Parsing Expression
Grammars (PEGs). https://bford.info/packrat/

                                                   *lua-lpeg* *vim.lpeg.Pattern*
The LPeg library for parsing expression grammars is included as `vim.lpeg`
(https://www.inf.puc-rio.br/~roberto/lpeg/).

In addition, its regex-like interface is available as |vim.re|
(https://www.inf.puc-rio.br/~roberto/lpeg/re.html).



Pattern:match({subject}, {init}, {...})                      *Pattern: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')

Title: Lua API: vim.glob (continued) and vim.lpeg
Summary
This section continues the description of `vim.glob` and its `to_lpeg` function, which parses a raw glob into an LPeg pattern. It then introduces `vim.lpeg`, a pattern-matching library based on Parsing Expression Grammars (PEGs), and its `Pattern:match()` function, which matches a given pattern against a subject string.