---@meta lpeglabel

--------------------------------------------------------------------------------
-- LPegLabel 1.6.1 pattern matching module
--------------------------------------------------------------------------------

---@alias lpeglabel_label string|integer
---@alias lpeglabel_match_time_function fun(subject:string, position:integer, ...:any):integer|boolean|nil, ...
---@alias lpeglabel_fold_function fun(acc:any, value:any, ...:any):any, ...
---@alias lpeglabel_capture_function fun(...:any):any, ...
---@alias lpeglabel_capture_replacement string|integer|table<any, any>|lpeglabel_capture_function
---@alias lpeglabel_pattern_convertible lpeglabel_pattern|string|number|boolean|table<any, any>|lpeglabel_match_time_function

---@class lpeglabel_pattern*
---@operator add(lpeglabel_pattern_convertible): lpeglabel_pattern
---@operator sub(lpeglabel_pattern_convertible): lpeglabel_pattern
---@operator mul(lpeglabel_pattern_convertible): lpeglabel_pattern
---@operator div(lpeglabel_capture_replacement): lpeglabel_pattern
---@operator pow(integer): lpeglabel_pattern
---@operator unm: lpeglabel_pattern
---@operator len: lpeglabel_pattern
local pattern = {}

--- Try to match subject from the current position.
---
--- Returns `nil, label, errpos` when matching fails; the label for ordinary failure is `"fail"`.
---@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 result Capture value or next position; nil on failure
---@return lpeglabel_label? label failure label
---@return integer? errpos failed position
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 lpeglabel_locale_table*
---@field alnum lpeglabel_pattern letters or numbers
---@field alpha lpeglabel_pattern letters
---@field cntrl lpeglabel_pattern control characters
---@field digit lpeglabel_pattern numbers
---@field graph lpeglabel_pattern graphic characters
---@field lower lpeglabel_pattern lowercase letters
---@field print lpeglabel_pattern Printable characters
---@field punct lpeglabel_pattern Punctuation characters
---@field space lpeglabel_pattern White space characters
---@field upper lpeglabel_pattern capital letters
---@field xdigit lpeglabel_pattern hexadecimal number

---@class lpeglabel*
---@field version string LPegLabel version string
---@type lpeglabel*
local M = {}

M.version = "LPegLabel 1.6.1"

--- Try to match subject from the current position.
---
--- Returns `nil, label, errpos` when matching fails; the label for ordinary failure is `"fail"`.
---@param patt lpeglabel_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 result Capture value or next position; nil on failure
---@return lpeglabel_label? label failure label
---@return integer? errpos failed position
function M.match(patt, subject, init, ...) end

--- If value is an LPegLabel 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 the LPegLabel 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 lpeglabel_pattern_convertible A value convertible to pattern
---@return lpeglabel_pattern pattern pattern
function M.P(value) end

--- Create a pattern that throws tags.
---
--- If a rule with the same name exists in the grammar, this rule will be used as a recovery rule; otherwise, the entire matching will fail.
---@param label lpeglabel_label label
---@return lpeglabel_pattern pattern pattern
function M.T(label) end

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

--- Create grammar variable reference.
---@param v any grammar Rule index
---@return lpeglabel_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 lpeglabel_pattern pattern pattern
function M.R(...) end

--- Create a character set pattern.
---@param chars string character set
---@return lpeglabel_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 lpeglabel_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|lpeglabel_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 lpeglabel_pattern_convertible pattern
---@return lpeglabel_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 lpeglabel_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 lpeglabel_pattern pattern pattern
function M.Cb(key) end

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

--- Create a fold capture.
---@param patt lpeglabel_pattern_convertible pattern
---@param func lpeglabel_fold_function fold function
---@return lpeglabel_pattern pattern pattern
function M.Cf(patt, func) end

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

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

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

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

--- Create a match-time capture.
---@param patt lpeglabel_pattern_convertible pattern
---@param func lpeglabel_match_time_function match function
---@return lpeglabel_pattern pattern pattern
function M.Cmt(patt, func) end

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

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

return M
