parser

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package parser provides tools for parsing Go source files to extract adapter configurations.

Index

Constants

View Source
const DirectivePrefix = "//go:adapter:"

DirectivePrefix is the prefix used to identify adapter directives in Go source code comments

Variables

This section is empty.

Functions

func NewParserError

func NewParserError(format string, args ...any) error

NewParserError creates a new parser error instance with a formatted message. It captures the current stack trace. This is for general parser errors not directly tied to a specific directive.

func NewParserErrorWithCause

func NewParserErrorWithCause(cause error, format string, args ...any) error

func NewParserErrorWithCauseAndContext

func NewParserErrorWithCauseAndContext(cause error, context any, format string, args ...any) error

func NewParserErrorWithContext

func NewParserErrorWithContext(context any, format string, args ...any) error

NewParserErrorWithContext creates a new parser error instance with a formatted message and an arbitrary context object. It captures the current stack trace.

func ParseDirective

func ParseDirective(parentCtx *Context, ruleType interfaces.RuleType, directive *Directive) error

func ParseFileDirectives

func ParseFileDirectives(cfg *config.Config, file *goast.File, fset *gotoken.FileSet) (*config.Config, error)

ParseFileDirectives parses a Go source file and returns the built configuration. This is the exported entry point.

func RegisterContainer

func RegisterContainer(rt interfaces.RuleType, factoryFunc ContainerFactory)

RegisterContainer registers a factory function for a given interfaces.RuleType. It will resize the registry slice if necessary.

Types

type ConstRule

type ConstRule struct {
	*config.ConstRule
}

ConstRule is a wrapper around config.ConstRule to implement the Container interface.

func (*ConstRule) AddConstRule

func (r *ConstRule) AddConstRule(rule *ConstRule) error

func (*ConstRule) AddFieldRule

func (r *ConstRule) AddFieldRule(rule *FieldRule) error

func (*ConstRule) AddFuncRule

func (r *ConstRule) AddFuncRule(rule *FuncRule) error

func (*ConstRule) AddMethodRule

func (r *ConstRule) AddMethodRule(rule *MethodRule) error

func (*ConstRule) AddPackage

func (r *ConstRule) AddPackage(pkg *PackageRule) error

func (*ConstRule) AddRule

func (r *ConstRule) AddRule(rule any) error

func (*ConstRule) AddTypeRule

func (r *ConstRule) AddTypeRule(rule *TypeRule) error

func (*ConstRule) AddVarRule

func (r *ConstRule) AddVarRule(rule *VarRule) error

func (*ConstRule) Finalize

func (r *ConstRule) Finalize(parent Container) error

func (*ConstRule) ParseDirective

func (r *ConstRule) ParseDirective(directive *Directive) error

func (*ConstRule) Type

func (r *ConstRule) Type() interfaces.RuleType

type Container

type Container interface {
	// Type returns the type of this container.
	Type() interfaces.RuleType
	// ParseDirective applies a sub-command (e.g., ":rename", ":disabled") to the rule.
	// It takes the builder to interact with the broader parsing state if necessary (e.g., to set an active member).
	ParseDirective(directive *Directive) error

	// AddRule adds a child rule to this container. This is the generic method.
	AddRule(rule any) error

	// AddPackage adds a PackageConfig package configuration to this container.
	AddPackage(pkg *PackageRule) error
	// AddTypeRule adds a TypeRule to this container.
	AddTypeRule(rule *TypeRule) error
	// AddFuncRule adds a FuncRule to this container.
	AddFuncRule(rule *FuncRule) error
	// AddVarRule adds a VarRule to this container.
	AddVarRule(rule *VarRule) error
	// AddConstRule adds a ConstRule to this container.
	AddConstRule(rule *ConstRule) error
	// AddMethodRule adds a MethodRule to this container.
	AddMethodRule(rule *MethodRule) error
	// AddFieldRule adds a FieldRule to this container.
	AddFieldRule(rule *FieldRule) error
	// Finalize performs any post-processing or validation for this container
	// after all its direct rules have been added.
	Finalize(parent Container) error
}

Container defines the interface for any object that can hold parsed rules and participate in the hierarchical configuration structure.

func NewContainer

func NewContainer(ruleType interfaces.RuleType) Container

NewContainer creates a new Container instance for a given interfaces.RuleType. It returns nil if the type is not registered or invalid.

type ContainerFactory

type ContainerFactory func() Container

ContainerFactory defines a function that creates a new instance of a Container.

func NewContainerFactory

func NewContainerFactory(ruleType interfaces.RuleType) ContainerFactory

NewContainerFactory resolves a command string (including abbreviations) and returns the corresponding interfaces.RuleType constant.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context represents a node in the parsing state hierarchy. It holds the configuration for a specific scope (e.g., a file or a specific struct) and maintains links to its parent and children contexts, forming a tree structure.

func NewContext

func NewContext(container Container, explicit bool) *Context

NewContext creates a new root Context node. It takes the data container (Container) for this scope and whether it was explicitly created.

func (*Context) ActiveContext

func (c *Context) ActiveContext() *Context

ActiveContext finds and returns the currently active child context from the activeContexts. It returns nil if no child context is active.

func (*Context) Container

func (c *Context) Container() Container

Container returns the data container associated with this context.

func (*Context) EndContext

func (c *Context) EndContext() error

EndContext deactivates the current context, finalizes its container, and returns its parent context. This process is recursive, ensuring that all active child contexts are also ended and finalized from the bottom up.

func (*Context) IsActive

func (c *Context) IsActive() bool

IsActive returns true if the context is currently active.

func (*Context) IsExplicit

func (c *Context) IsExplicit() bool

IsExplicit returns true if this context was created by an explicit directive.

func (*Context) Parent

func (c *Context) Parent() *Context

Parent returns the parent of the current context.

func (*Context) SetActivate

func (c *Context) SetActivate(active bool)

SetActivate sets the active status of the context.

func (*Context) SetExplicit

func (c *Context) SetExplicit(explicit bool) *Context

SetExplicit sets the explicit flag for the context and returns the context.

func (*Context) StartContext

func (c *Context) StartContext(container Container) (*Context, error)

StartContext creates a new child context, makes it the sole active context among its siblings, and returns it. Before starting the new context, it ensures any previously active sibling context is properly ended by calling EndContext.

func (*Context) StartOrActiveContext

func (c *Context) StartOrActiveContext(ruleType interfaces.RuleType) (*Context, error)

StartOrActiveContext gets an active child context or creates a new one. It first checks if an active child context already exists and returns it. If not, it creates a new one by calling the provided factory function.

type Directive

type Directive struct {
	Line     int    // Line number in the source file.
	Command  string // The full command string (e.g., "type:struct"). Note: :json suffix is removed here.
	Argument string // The raw argument string.

	// Parsed components of the command.
	BaseCmd string   // The base command (e.g., "type").
	SubCmds []string // Sub-commands (e.g., ["struct"]).
	IsJSON  bool     // True if the original command had a ":json" suffix.
}

Directive represents a parsed adptool directive from a Go comment. It is immutable after creation.

func (Directive) HasSub

func (d Directive) HasSub() bool

func (Directive) Root

func (d Directive) Root() *Directive

func (Directive) ShouldUnmarshal

func (d Directive) ShouldUnmarshal() bool

func (Directive) Sub

func (d Directive) Sub() *Directive

type DirectiveIterator

type DirectiveIterator iter.Seq[*Directive]

func NewDirectiveIterator

func NewDirectiveIterator(file *goast.File, fset *gotoken.FileSet) DirectiveIterator

NewDirectiveIterator creates a new directiveIterator.

type FieldRule

type FieldRule struct {
	*config.MemberRule
}

FieldRule is a wrapper around config.MemberRule to implement the Container interface.

func (*FieldRule) AddConstRule

func (f *FieldRule) AddConstRule(rule *ConstRule) error

func (*FieldRule) AddFieldRule

func (f *FieldRule) AddFieldRule(rule *FieldRule) error

func (*FieldRule) AddFuncRule

func (f *FieldRule) AddFuncRule(rule *FuncRule) error

func (*FieldRule) AddMethodRule

func (f *FieldRule) AddMethodRule(rule *MethodRule) error

func (*FieldRule) AddPackage

func (f *FieldRule) AddPackage(pkg *PackageRule) error

func (*FieldRule) AddRule

func (f *FieldRule) AddRule(rule any) error

func (*FieldRule) AddTypeRule

func (f *FieldRule) AddTypeRule(rule *TypeRule) error

func (*FieldRule) AddVarRule

func (f *FieldRule) AddVarRule(rule *VarRule) error

func (*FieldRule) Finalize

func (f *FieldRule) Finalize(parent Container) error

func (*FieldRule) ParseDirective

func (f *FieldRule) ParseDirective(directive *Directive) error

func (*FieldRule) Type

func (f *FieldRule) Type() interfaces.RuleType

type FuncRule

type FuncRule struct {
	*config.FuncRule
}

FuncRule is a wrapper around config.FuncRule to implement the Container interface.

func (*FuncRule) AddConstRule

func (r *FuncRule) AddConstRule(rule *ConstRule) error

func (*FuncRule) AddFieldRule

func (r *FuncRule) AddFieldRule(rule *FieldRule) error

func (*FuncRule) AddFuncRule

func (r *FuncRule) AddFuncRule(rule *FuncRule) error

func (*FuncRule) AddMethodRule

func (r *FuncRule) AddMethodRule(rule *MethodRule) error

func (*FuncRule) AddPackage

func (r *FuncRule) AddPackage(pkg *PackageRule) error

func (*FuncRule) AddRule

func (r *FuncRule) AddRule(rule any) error

func (*FuncRule) AddTypeRule

func (r *FuncRule) AddTypeRule(rule *TypeRule) error

func (*FuncRule) AddVarRule

func (r *FuncRule) AddVarRule(rule *VarRule) error

func (*FuncRule) Finalize

func (r *FuncRule) Finalize(parent Container) error

func (*FuncRule) ParseDirective

func (r *FuncRule) ParseDirective(directive *Directive) error

func (*FuncRule) Type

func (r *FuncRule) Type() interfaces.RuleType

type InvalidRule

type InvalidRule struct{}

InvalidRule is a singleton container returned by the factory when a type is not found. Its methods always return an error, allowing for deferred error handling at the call site.

func (*InvalidRule) AddConstRule

func (i *InvalidRule) AddConstRule(rule *ConstRule) error

func (*InvalidRule) AddFieldRule

func (i *InvalidRule) AddFieldRule(rule *FieldRule) error

func (*InvalidRule) AddFuncRule

func (i *InvalidRule) AddFuncRule(rule *FuncRule) error

func (*InvalidRule) AddMethodRule

func (i *InvalidRule) AddMethodRule(rule *MethodRule) error

func (*InvalidRule) AddPackage

func (i *InvalidRule) AddPackage(pkg *PackageRule) error

func (*InvalidRule) AddRule

func (i *InvalidRule) AddRule(rule any) error

func (*InvalidRule) AddTypeRule

func (i *InvalidRule) AddTypeRule(rule *TypeRule) error

func (*InvalidRule) AddVarRule

func (i *InvalidRule) AddVarRule(rule *VarRule) error

func (*InvalidRule) Finalize

func (i *InvalidRule) Finalize(parent Container) error

Finalize for an invalid rule is a no-op.

func (*InvalidRule) ParseDirective

func (i *InvalidRule) ParseDirective(directive *Directive) error

ParseDirective for an invalid rule always returns an error.

func (*InvalidRule) Type

func (i *InvalidRule) Type() interfaces.RuleType

type MethodRule

type MethodRule struct {
	*config.MemberRule
}

MethodRule is a wrapper around config.MemberRule to implement the Container interface.

func (*MethodRule) AddConstRule

func (m *MethodRule) AddConstRule(rule *ConstRule) error

func (*MethodRule) AddFieldRule

func (m *MethodRule) AddFieldRule(rule *FieldRule) error

func (*MethodRule) AddFuncRule

func (m *MethodRule) AddFuncRule(rule *FuncRule) error

func (*MethodRule) AddMethodRule

func (m *MethodRule) AddMethodRule(rule *MethodRule) error

func (*MethodRule) AddPackage

func (m *MethodRule) AddPackage(pkg *PackageRule) error

func (*MethodRule) AddRule

func (m *MethodRule) AddRule(rule any) error

func (*MethodRule) AddTypeRule

func (m *MethodRule) AddTypeRule(rule *TypeRule) error

func (*MethodRule) AddVarRule

func (m *MethodRule) AddVarRule(rule *VarRule) error

func (*MethodRule) Finalize

func (m *MethodRule) Finalize(parent Container) error

func (*MethodRule) ParseDirective

func (m *MethodRule) ParseDirective(directive *Directive) error

func (*MethodRule) Type

func (m *MethodRule) Type() interfaces.RuleType

type PackageRule

type PackageRule struct {
	*config.Package
}

PackageRule is a wrapper around config.Package to implement the Container interface. (Previously PackageConfig)

func (*PackageRule) AddConstRule

func (p *PackageRule) AddConstRule(rule *ConstRule) error

func (*PackageRule) AddFieldRule

func (p *PackageRule) AddFieldRule(rule *FieldRule) error

func (*PackageRule) AddFuncRule

func (p *PackageRule) AddFuncRule(rule *FuncRule) error

func (*PackageRule) AddMethodRule

func (p *PackageRule) AddMethodRule(rule *MethodRule) error

func (*PackageRule) AddPackage

func (p *PackageRule) AddPackage(pkg *PackageRule) error

func (*PackageRule) AddRule

func (p *PackageRule) AddRule(rule any) error

func (*PackageRule) AddTypeRule

func (p *PackageRule) AddTypeRule(rule *TypeRule) error

func (*PackageRule) AddVarRule

func (p *PackageRule) AddVarRule(rule *VarRule) error

func (*PackageRule) Finalize

func (p *PackageRule) Finalize(parent Container) error

func (*PackageRule) ParseDirective

func (p *PackageRule) ParseDirective(directive *Directive) error

func (*PackageRule) Type

func (p *PackageRule) Type() interfaces.RuleType

type RootConfig

type RootConfig struct {
	*config.Config
}

RootConfig is a wrapper around config.Config to implement the Container interface.

func (*RootConfig) AddConstRule

func (r *RootConfig) AddConstRule(rule *ConstRule) error

func (*RootConfig) AddFieldRule

func (r *RootConfig) AddFieldRule(rule *FieldRule) error

func (*RootConfig) AddFuncRule

func (r *RootConfig) AddFuncRule(rule *FuncRule) error

func (*RootConfig) AddMethodRule

func (r *RootConfig) AddMethodRule(rule *MethodRule) error

func (*RootConfig) AddPackage

func (r *RootConfig) AddPackage(pkg *PackageRule) error

func (*RootConfig) AddRule

func (r *RootConfig) AddRule(rule any) error

func (*RootConfig) AddTypeRule

func (r *RootConfig) AddTypeRule(rule *TypeRule) error

func (*RootConfig) AddVarRule

func (r *RootConfig) AddVarRule(rule *VarRule) error

func (*RootConfig) Finalize

func (r *RootConfig) Finalize(parent Container) error

func (*RootConfig) ParseDirective

func (r *RootConfig) ParseDirective(directive *Directive) error

func (*RootConfig) Type

func (r *RootConfig) Type() interfaces.RuleType

type TypeRule

type TypeRule struct {
	*config.TypeRule
}

TypeRule is a wrapper around config.TypeRule to implement the Container interface.

func (*TypeRule) AddConstRule

func (r *TypeRule) AddConstRule(rule *ConstRule) error

func (*TypeRule) AddFieldRule

func (r *TypeRule) AddFieldRule(rule *FieldRule) error

func (*TypeRule) AddFuncRule

func (r *TypeRule) AddFuncRule(rule *FuncRule) error

func (*TypeRule) AddMethodRule

func (r *TypeRule) AddMethodRule(rule *MethodRule) error

func (*TypeRule) AddPackage

func (r *TypeRule) AddPackage(pkg *PackageRule) error

func (*TypeRule) AddRule

func (r *TypeRule) AddRule(rule any) error

func (*TypeRule) AddTypeRule

func (r *TypeRule) AddTypeRule(rule *TypeRule) error

func (*TypeRule) AddVarRule

func (r *TypeRule) AddVarRule(rule *VarRule) error

func (*TypeRule) Finalize

func (r *TypeRule) Finalize(parent Container) error

func (*TypeRule) ParseDirective

func (r *TypeRule) ParseDirective(directive *Directive) error

func (*TypeRule) Type

func (r *TypeRule) Type() interfaces.RuleType

type VarRule

type VarRule struct {
	*config.VarRule
}

VarRule is a wrapper around config.VarRule to implement the Container interface.

func (*VarRule) AddConstRule

func (r *VarRule) AddConstRule(rule *ConstRule) error

func (*VarRule) AddFieldRule

func (r *VarRule) AddFieldRule(rule *FieldRule) error

func (*VarRule) AddFuncRule

func (r *VarRule) AddFuncRule(rule *FuncRule) error

func (*VarRule) AddMethodRule

func (r *VarRule) AddMethodRule(rule *MethodRule) error

func (*VarRule) AddPackage

func (r *VarRule) AddPackage(pkg *PackageRule) error

func (*VarRule) AddRule

func (r *VarRule) AddRule(rule any) error

func (*VarRule) AddTypeRule

func (r *VarRule) AddTypeRule(rule *TypeRule) error

func (*VarRule) AddVarRule

func (r *VarRule) AddVarRule(rule *VarRule) error

func (*VarRule) Finalize

func (r *VarRule) Finalize(parent Container) error

func (*VarRule) ParseDirective

func (r *VarRule) ParseDirective(directive *Directive) error

func (*VarRule) Type

func (r *VarRule) Type() interfaces.RuleType

Jump to

Keyboard shortcuts

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