ast

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2026 License: BlueOak-1.0.0 Imports: 4 Imported by: 0

Documentation

Index

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

func ParseGlob(pattern string, opts Config) *AST

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

func ParseTokens(src string, tokens []scan.Token, opts Config) *AST

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) Depth

func (n *AST) Depth() int

Depth returns the depth of this node (root is 0), matching TS AST.depth.

func (*AST) FillNegs

func (n *AST) FillNegs() *AST

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

func (n *AST) FilledNegs() bool

FilledNegs reports whether FillNegs has been applied on this tree's root.

func (*AST) Flatten

func (n *AST) Flatten() *AST

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):

  1. Adopt — pull a nested extglob’s alternatives into the parent when quantifier semantics allow (adoptionMap).
  2. Adopt with space — same, but add an empty alternative so optional / star-zero semantics stay correct (adoptionWithSpaceMap).
  3. 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) ID

func (n *AST) ID() int

ID returns a stable per-tree identity for debugging (like TS AST.id).

func (*AST) IsExtglob

func (n *AST) IsExtglob() bool

IsExtglob reports whether n is an extglob node.

func (*AST) IsList

func (n *AST) IsList() bool

IsList reports whether n is a list node (TypeScript type === null).

func (*AST) Parent

func (n *AST) Parent() *AST

Parent returns the parent node, if any.

func (*AST) Root

func (n *AST) Root() *AST

Root returns the root of the tree.

func (*AST) String

func (n *AST) String() 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

func (n *AST) ToJSON() any

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

func (n *AST) ToMMPattern() (MMPattern, error)

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

type ASTPart struct {
	Text string
	Node *AST
}

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

func (ASTPart) IsText

func (p ASTPart) IsText() bool

IsText reports whether p is a raw text part.

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).

func (MMPattern) Match

func (p MMPattern) Match(s string) (bool, error)

Match reports whether s matches this pattern (full segment).

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL