Documentation
¶
Index ¶
- type AST
- func (n *AST) Depth() int
- func (n *AST) FillNegs() *AST
- func (n *AST) FilledNegs() bool
- func (n *AST) Flatten() *AST
- func (n *AST) ID() int
- func (n *AST) IsExtglob() bool
- func (n *AST) IsList() bool
- func (n *AST) Parent() *AST
- func (n *AST) Root() *AST
- func (n *AST) String() string
- func (n *AST) ToJSON() any
- func (n *AST) ToMMPattern() (MMPattern, error)
- func (n *AST) ToRegExpSource(allowDot *bool) RegExpSource
- type ASTPart
- type Config
- type MMPattern
- type RegExpSource
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AST ¶
type AST struct {
// Type is the extglob operator, or 0 for a list node (TS type === null).
Type scan.ExtglobType
// Parts is the ordered content of this node (see package comment).
Parts []ASTPart
// EmptyExt marks an extglob that closed with an empty body (e.g. "*()").
EmptyExt bool
// Options are the parse options from the root (shared by the tree).
Options Config
// contains filtered or unexported fields
}
AST is the extglob syntax tree for a single path-segment pattern.
It corresponds to the TypeScript minimatch AST class after #parseAST / fromGlob. Call Flatten to apply adopt/usurp rewrites (#flatten) before fillNegs and toRegExpSource. Construction and Flatten do not match paths or compile regular expressions.
Node kinds ¶
There is one Go type for all nodes. Kind is determined by Type:
List node (Type == 0): an ordered sequence of text runs and/or child AST nodes. The root is always a list node. Each extglob alternative body is also a list node. TypeScript: type === null.
Extglob node (Type is !, ?, +, *, or @): a choice among alternatives. Parts are only list-node children (one per alternative), never raw text. TypeScript: type === '!' | '?' | '+' | '*' | '@'.
Part entries ¶
List-node Parts alternate freely:
Text parts: raw pattern text (may contain "*", "?", character classes, escapes). Empty strings are never stored (TypeScript push skips ”).
Child AST parts: nested extglob nodes or (after unfinished demotion) list nodes holding demoted literal text.
Extglob-node Parts are always child list nodes (alternatives), including empty alternatives.
EmptyExt ¶
EmptyExt is true when the extglob closed with an empty first body and no prior alternatives were flushed yet (TypeScript #emptyExt for patterns like "*()"). Used later for regexp emission of empty !() / *().
Call Flatten then FillNegs on the root before compiling to a regular expression (same order as TypeScript toRegExpSource).
func ParseGlob ¶
ParseGlob parses a path-segment pattern into an AST.
Corresponds to TypeScript AST.fromGlob(pattern, options): scan structure with Scan, then build the tree. Does not flatten, fill negative tails, or compile to a regular expression.
func ParseTokens ¶
ParseTokens builds an AST from tokens previously produced by Scan on src.
src must be the same string that was scanned (used for unfinished-extglob demotion spans). tokens should be Scan(src, opts).
func (*AST) FillNegs ¶
FillNegs copies pattern tails into negative extglob alternatives.
Corresponds to TypeScript AST.#fillNegs(). It must run on the root (or any node — work is always applied via the root). Typical order matches toRegExpSource:
ast.Flatten() ast.FillNegs()
For each !(...) node registered at parse time, every following sibling in each list ancestor is copyIn'd into each alternative of that !. That way a pattern like "!(a)b" negates "ab", not just "a".
FillNegs does not match paths or emit regular expressions. It mutates the tree in place and returns the receiver for chaining. Calling it twice is a no-op (TypeScript #filledNegs).
func (*AST) FilledNegs ¶
FilledNegs reports whether FillNegs has been applied on this tree's root.
func (*AST) Flatten ¶
Flatten rewrites nested extglobs into equivalent shallower forms.
Corresponds to TypeScript AST.#flatten(), which runs before fillNegs and toRegExpSource. It does not match paths or emit regular expressions.
Three rewrites (checked in this order per child, up to 10 passes):
- Adopt — pull a nested extglob’s alternatives into the parent when quantifier semantics allow (adoptionMap).
- Adopt with space — same, but add an empty alternative so optional / star-zero semantics stay correct (adoptionWithSpaceMap).
- Usurp — when the parent has a single nested extglob child, replace the parent’s type with a mapped type (usurpMap).
List nodes only recurse into children. Flatten mutates the tree in place and returns the receiver for chaining.
func (*AST) String ¶
String reconstructs a pattern string from the tree (TypeScript toString before flatten). List nodes concatenate parts; extglob nodes emit type(alt|alt|...).
func (*AST) ToJSON ¶
ToJSON returns a JSON-like structure comparable to TypeScript AST.toJSON() before fillNegs (no start/end markers beyond root list markers when isStart/isEnd apply without filled negs).
List: [ [], ...parts..., {}? ] when isStart / isEnd on root-like nodes. Extglob: [ type, ...alternative JSONs ]
This is for inspection and tests only; it is not a stable wire format.
func (*AST) ToMMPattern ¶
ToMMPattern compiles this AST (root) to a literal or regular expression.
Corresponds to TypeScript AST.toMMPattern(). Calls Flatten and FillNegs via ToRegExpSource. Does not perform multi-segment path matching.
func (*AST) ToRegExpSource ¶
func (n *AST) ToRegExpSource(allowDot *bool) RegExpSource
ToRegExpSource returns regexp source for this node.
Corresponds to TypeScript AST.toRegExpSource(allowDot?). On the root, runs Flatten and FillNegs first.
allowDot nil means use Options.Dot; non-nil overrides (extglob dual-body).
type ASTPart ¶
ASTPart is one entry in an AST node’s Parts slice.
Exactly one of Text or Node is used:
- Node == nil: Text is a non-empty raw string (except demotion may use any string)
- Node != nil: child AST; Text is ignored
type Config ¶
type Config struct {
Dot bool
NoCase bool
NoCaseMagicOnly bool
NoExt bool
MaxExtglobRecursion int // effective value; default 2 supplied by caller
}
Config is the subset of minimatch.Options needed by the AST compiler.
type MMPattern ¶
type MMPattern struct {
// Literal is the non-magic match string (TypeScript string return).
Literal string
// RE is the compiled regular expression (nil if literal).
// Uses regexp2 so lookarounds from minimatch sources work.
RE *regexp2.Regexp
// Src is the unanchored source (_src in TypeScript).
Src string
// Glob is the reconstructed glob string (_glob in TypeScript).
Glob string
// IsRE is true when RE should be used instead of Literal.
IsRE bool
}
MMPattern is a compiled path-segment pattern (TypeScript toMMPattern).
If IsRE is false, match with exact equality to Literal (unescaped body). If IsRE is true, use RE (anchored ^…$ as in TypeScript).
type RegExpSource ¶
type RegExpSource struct {
// Re is the regular-expression source for this node (may include
// lookarounds for dots / negations).
Re string
// Body is the unescaped pattern body used for non-magic comparison.
Body string
// HasMagic is true when a RegExp is required to match.
HasMagic bool
// UFlag is true when Unicode property escapes are present (JS /u).
UFlag bool
}
RegExpSource is the result of ToRegExpSource (TypeScript tuple return).