scan

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: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsExtglobType

func IsExtglobType(c byte) bool

IsExtglobType reports whether c is a valid extglob type character.

TypeScript: types = new Set(['!', '?', '+', '*', '@']); isExtglobType(c)

Types

type ExtglobType

type ExtglobType byte

ExtglobType is a TypeScript ExtglobType: '!' | '?' | '+' | '*' | '@'.

const (
	ExtglobNegate   ExtglobType = '!' // !(...)
	ExtglobOptional ExtglobType = '?' // ?(...)
	ExtglobPlus     ExtglobType = '+' // +(...)
	ExtglobStar     ExtglobType = '*' // *(...)
	ExtglobOne      ExtglobType = '@' // @(...)
)

Extglob type characters recognized by isExtglobType in ast.ts.

type Token

type Token struct {
	Kind  TokenKind
	Text  string
	Start int
	End   int
}

Token is one lexical unit of a path-segment pattern.

Start and End are byte offsets into the original segment string such that segment[Start:End] is the raw lexeme (except that TokenExtglobOpen.Text is only the type character, while Start:End still spans type + '(').

func Scan

func Scan(segment string, noExt bool, maxDepth int) []Token

Scan tokenizes a single path-segment pattern string.

This is the lexical counterpart of TypeScript AST.#parseAST: it walks the string character-by-character, tracks escape state and character-class regions, and recognizes extglob structure. It does not build an AST, does not expand braces, does not split on '/', and does not match paths.

The segment is typically one slash-separated piece of a full pattern (TypeScript parses each piece after slashSplit). Passing a string that contains '/' is allowed; '/' is ordinary TokenText (no path splitting).

Options that affect scanning:

  • NoExt: never emit TokenExtglobOpen; "*(" etc. stay in TokenText
  • MaxExtglobRecursion: depth at which nested extglobs stop being recognized (default DefaultMaxExtglobRecursion), matching maxExtglobRecursion ?? 2 and the adoption-aware depth rule from #parseAST / #canAdoptType

Other Options fields are ignored by Scan.

Unfinished extglobs (opening "*(" without a closing ")") produce an open without a matching TokenExtglobClose. TypeScript demotes those to literal text when building the AST; demotion is not applied here. Scan tokenizes a path-segment pattern. noExt disables extglobs; maxDepth is maxExtglobRecursion (use 2 for default).

type TokenKind

type TokenKind int

TokenKind is the kind of a lexical token produced by Scan.

These kinds correspond to what TypeScript AST.#parseAST recognizes while walking a path-segment string. They are structural / lexical only: leaf glob magic such as standalone "*" or "?" stays inside TokenText and is not re-tokenized here (that happens later, in the equivalent of #parseGlob).

const (
	// TokenText is a run of characters that are not extglob structure.
	//
	// In TypeScript #parseAST this is the "acc" string: escapes, character
	// classes, "*", "?", ordinary letters, and even "|" or ")" when not
	// inside an extglob are all accumulated as text.
	//
	// Text holds the raw substring, including backslashes and "["..."]"
	// class spans kept opaque (the class is not parsed into ranges here).
	TokenText TokenKind = iota

	// TokenExtglobOpen is an extglob introducer: one of ! ? + * @ followed
	// immediately by '('.
	//
	// TypeScript: isExtglobType(c) && str.charAt(i) === '(' with noext off
	// and depth limits allowing recursion.
	//
	// Text is the single type character ("!", "?", "+", "*", or "@").
	// The token span covers both the type character and the '('.
	TokenExtglobOpen

	// TokenPipe is "|" separating extglob alternatives.
	//
	// TypeScript: only recognized while parsing inside an extglob
	// (ast.type !== null). At root level, "|" is TokenText.
	TokenPipe

	// TokenExtglobClose is ")" closing the current extglob.
	//
	// TypeScript: only recognized inside an extglob. At root level, ")" is
	// TokenText.
	TokenExtglobClose
)

func (TokenKind) String

func (k TokenKind) String() string

String returns a stable name for diagnostics and tests.

Jump to

Keyboard shortcuts

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