---@meta lpeg

--------------------------------------------------------------------------------
-- LPeg 1.1.0 pattern matching module
--------------------------------------------------------------------------------

---@alias lpeg_match_time_function fun(subject:string, position:integer, ...:any):integer|boolean|nil, ...
---@alias lpeg_fold_function fun(acc:any, value:any, ...:any):any, ...
---@alias lpeg_capture_function fun(...:any):any, ...
---@alias lpeg_capture_replacement string|integer|table<any, any>|lpeg_capture_function
---@alias lpeg_pattern_convertible lpeg_pattern|string|number|boolean|table<any, any>|lpeg_match_time_function

---@class lpeg_pattern*
---@operator add(lpeg_pattern_convertible): lpeg_pattern
---@operator sub(lpeg_pattern_convertible): lpeg_pattern
---@operator mul(lpeg_pattern_convertible): lpeg_pattern
---@operator div(lpeg_capture_replacement): lpeg_pattern
---@operator mod(lpeg_fold_function): lpeg_pattern
---@operator pow(integer): lpeg_pattern
---@operator unm: lpeg_pattern
---@operator len: lpeg_pattern
local pattern = {}

--- Try to match subject from the current position.
---@param subject string input string
---@param init? integer Starting position, negative numbers are counted from the end
---@param ... any Additional parameters passed to `Carg`
---@return any ... Capture value; returns next position if no capture occurs; returns nil on failure
function pattern:match(subject, init, ...) end

--- Output the tree structure of pattern for debugging.
function pattern:ptree() end

--- Output the bytecode of pattern for debugging.
function pattern:pcode() end

---@class lpeg_locale_table*
---@field alnum lpeg_pattern letters or numbers
---@field alpha lpeg_pattern letters
---@field cntrl lpeg_pattern control characters
---@field digit lpeg_pattern numbers
---@field graph lpeg_pattern graphic characters
---@field lower lpeg_pattern lowercase letters
---@field print lpeg_pattern Printable characters
---@field punct lpeg_pattern Punctuation characters
---@field space lpeg_pattern White space characters
---@field upper lpeg_pattern capital letters
---@field xdigit lpeg_pattern hexadecimal number

---@class lpeg*
---@field version string LPeg version string
---@type lpeg*
local M = {}

M.version = "LPeg 1.1.0"

--- Try to match subject from the current position.
---@param patt lpeg_pattern pattern
---@param subject string input string
---@param init? integer Starting position, negative numbers are counted from the end
---@param ... any Additional parameters passed to `Carg`
---@return any ... Capture value; returns next position if no capture occurs; returns nil on failure
function M.match(patt, subject, init, ...) end

--- If value is an LPeg pattern, returns the string `"pattern"`; otherwise returns nil.
---@param value any Value to be checked
---@return string? type Type name
function M.type(value) end

--- Set the upper limit of LPeg backtrace stack.
---@param max integer Maximum number of backtracking levels
function M.setmaxstack(max) end

--- Convert value to pattern.
---
--- Strings are matched literally; non-negative numbers match a fixed number of characters; negative numbers represent negative fixed-length matches;
--- Boolean values generate an always true/always false pattern; tables are interpreted as grammar; functions are interpreted as capturing on match.
---@param value lpeg_pattern_convertible A value convertible to pattern
---@return lpeg_pattern pattern pattern
function M.P(value) end

--- Create behind predicate.
---@param patt lpeg_pattern_convertible pattern
---@return lpeg_pattern pattern pattern
function M.B(patt) end

--- Create grammar variable reference.
---@param v any grammar Rule index
---@return lpeg_pattern pattern pattern
function M.V(v) end

--- Create a character range pattern.
---@param ... string Range strings, each string length is 2, such as `"09"`, `"az"`
---@return lpeg_pattern pattern pattern
function M.R(...) end

--- Create a character set pattern.
---@param chars string character set
---@return lpeg_pattern pattern pattern
function M.S(chars) end

--- Create a UTF-8 code point range pattern.
---@param cp1 integer Starting code point
---@param cp2 integer end code point
---@return lpeg_pattern pattern pattern
function M.utfR(cp1, cp2) end

--- Create or populate the character class pattern table for the current locale.
---@generic T: table
---@param table? T The table to populate; creates a new table when omitted
---@return T|lpeg_locale_table table locale Character class table
function M.locale(table) end

--- Create a simple capture to capture the substring matched by patt.
---@param patt lpeg_pattern_convertible pattern
---@return lpeg_pattern pattern pattern
function M.C(patt) end

--- Creates a parameter capture that captures the nth extra parameter in a `match` call.
---@param n integer Additional parameter number
---@return lpeg_pattern pattern pattern
function M.Carg(n) end

--- Create a back capture that captures the most recent group capture with the same name.
---@param key any group capture Name
---@return lpeg_pattern pattern pattern
function M.Cb(key) end

--- Create a constant capture.
---@param ... any constant value
---@return lpeg_pattern pattern pattern
function M.Cc(...) end

--- Create a fold capture.
---@param patt lpeg_pattern_convertible pattern
---@param func lpeg_fold_function fold function
---@return lpeg_pattern pattern pattern
function M.Cf(patt, func) end

--- Create group capture.
---@param patt lpeg_pattern_convertible pattern
---@param key? any group Name
---@return lpeg_pattern pattern pattern
function M.Cg(patt, key) end

--- Create a location capture.
---@return lpeg_pattern pattern pattern
function M.Cp() end

--- Create a substitution capture.
---@param patt lpeg_pattern_convertible pattern
---@return lpeg_pattern pattern pattern
function M.Cs(patt) end

--- Create table capture.
---@param patt lpeg_pattern_convertible pattern
---@return lpeg_pattern pattern pattern
function M.Ct(patt) end

--- Create a match-time capture.
---@param patt lpeg_pattern_convertible pattern
---@param func lpeg_match_time_function match function
---@return lpeg_pattern pattern pattern
function M.Cmt(patt, func) end

--- Output the tree structure of pattern for debugging.
---@param patt lpeg_pattern pattern
function M.ptree(patt) end

--- Output the bytecode of pattern for debugging.
---@param patt lpeg_pattern pattern
function M.pcode(patt) end

return M
