codegen

package
v0.0.0-...-98aa140 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(w io.Writer, d *dfa.DFA, opts Options) error

Generate writes Go source code implementing a DFA matcher to w.

func HasNamedGroups

func HasNamedGroups(names []string) bool

HasNamedGroups reports whether any capture group (1..N) carries a name.

Types

type MatchMode

type MatchMode int

MatchMode controls how the generated function matches input.

const (
	// MatchFull requires the entire input to match (default). Equivalent to ^pattern$.
	MatchFull MatchMode = iota
	// MatchPrefix matches if a prefix of the input matches the pattern. Equivalent to ^pattern.
	MatchPrefix
	// MatchContains matches if any substring of the input matches. Equivalent to unanchored pattern.
	MatchContains
)

type Options

type Options struct {
	PackageName string           // Go package name
	FuncName    string           // Name of the generated match function
	Regex       string           // Original regex (for comment)
	Mode        MatchMode        // Match mode (default: MatchFull)
	Submatch    *SubmatchOptions // If non-nil, also generate a FindSubmatch function

	// LiteralPrefix/LiteralComplete mirror syntax.Prog.Prefix() for the
	// pattern: when LiteralComplete is true the pattern matches exactly the
	// literal LiteralPrefix, and contains mode compiles to a single
	// strings.Contains call instead of a DFA scan.
	LiteralPrefix   string
	LiteralComplete bool
}

Options controls the generated code.

type SubmatchOptions

type SubmatchOptions struct {
	PackageName string
	FuncName    string // e.g. "FindSubmatch" — positional []string API
	MatchFunc   string // name of the bool match function to call first
	Regex       string
	Prog        *syntax.Prog
	NumGroups   int      // number of capture groups (not counting group 0)
	GroupNames  []string // names per group index (len NumGroups+1); index 0 == ""

	// NamesFuncName is the name of the generated accessor that returns the
	// group-name slice (parity with regexp.Regexp.SubexpNames). Always emitted.
	NamesFuncName string

	// Struct controls generation of the typed capture struct. It is only
	// emitted when StructEnabled is true AND at least one named group exists.
	StructEnabled bool
	StructType    string // type name, e.g. "Captures"
	StructFunc    string // constructor func name, e.g. "FindCaptures"
}

SubmatchOptions controls generation of the submatch function family.

Jump to

Keyboard shortcuts

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