Documentation
¶
Index ¶
- type Arg
- type Context
- type DiffExpr
- type DiffTail
- type Evaluator
- type Expr
- type FuncArg
- type FuncCall
- type IntersectExpr
- type IntersectTail
- type MapContext
- type Node
- type Parser
- type Primary
- type Set
- func (s *Set[T]) Add(item T)
- func (s *Set[T]) Clone() *Set[T]
- func (s *Set[T]) Diff(other *Set[T]) *Set[T]
- func (s *Set[T]) Has(item T) bool
- func (s *Set[T]) Intersect(other *Set[T]) *Set[T]
- func (s *Set[T]) IsEmpty() bool
- func (s *Set[T]) Items() []T
- func (s *Set[T]) Remove(item T)
- func (s *Set[T]) Size() int
- func (s *Set[T]) Union(other *Set[T]) *Set[T]
- type UnionExpr
- type UnionTail
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Arg ¶
Arg represents a function argument, which can be either: - A nested expression (for functions that take set expressions as arguments) - A string literal (for functions that take string parameters) - An identifier (for simple arguments)
type Context ¶
type Context[T comparable] interface { // LookupIdent resolves an identifier to a set. // Returns an error if the identifier is unknown. LookupIdent(name string) (*Set[T], error) // CallFunc calls a function with the given arguments. // Args can be either string literals or evaluated sets. // Returns an error if the function is unknown or arguments are invalid. CallFunc(name string, args []FuncArg[T]) (*Set[T], error) }
Context provides the evaluation context for set expressions. Consumers of the library implement this interface to provide application-specific behavior for identifier lookup and function calls.
type Evaluator ¶
type Evaluator[T comparable] struct { // contains filtered or unexported fields }
Evaluator evaluates set expressions using a provided context.
func NewEvaluator ¶
func NewEvaluator[T comparable](ctx Context[T]) *Evaluator[T]
NewEvaluator creates a new evaluator with the given context.
type Expr ¶
type Expr struct {
Union *UnionExpr `@@`
}
Expr is the top-level expression node. Grammar: UnionExpr
func MustParse ¶
MustParse is a convenience function that creates a new parser and parses the input, panicking on error.
type FuncArg ¶
type FuncArg[T comparable] struct { // StrVal is non-nil if the argument is a string literal StrVal *string // Ident is non-nil if the argument is an identifier Ident *string // Set is non-nil if the argument is an evaluated set expression Set *Set[T] }
FuncArg represents an argument to a function call. It can be either a string literal or an evaluated set expression.
func (FuncArg[T]) GetIdent ¶
GetIdent returns the identifier value, or an error if not an identifier.
type FuncCall ¶
FuncCall represents a function call with arguments. Grammar: Ident "(" (Arg ("," Arg)*)? ")"
type IntersectExpr ¶
type IntersectExpr struct {
Left *DiffExpr `@@`
Right []*IntersectTail `@@*`
}
IntersectExpr represents intersection operations (&). Grammar: DiffExpr ("&" DiffExpr)*
func (*IntersectExpr) String ¶
func (i *IntersectExpr) String() string
type IntersectTail ¶
IntersectTail represents the right-hand side of an intersection operation.
type MapContext ¶
type MapContext[T comparable] struct { Idents map[string]*Set[T] Funcs map[string]func([]FuncArg[T]) (*Set[T], error) }
MapContext is a simple implementation of Context that uses maps for lookups. This is useful for testing and simple use cases.
func NewMapContext ¶
func NewMapContext[T comparable]() *MapContext[T]
NewMapContext creates a new MapContext.
func (*MapContext[T]) CallFunc ¶
func (m *MapContext[T]) CallFunc(name string, args []FuncArg[T]) (*Set[T], error)
CallFunc implements Context.CallFunc.
func (*MapContext[T]) LookupIdent ¶
func (m *MapContext[T]) LookupIdent(name string) (*Set[T], error)
LookupIdent implements Context.LookupIdent.
func (*MapContext[T]) SetFunc ¶
func (m *MapContext[T]) SetFunc(name string, fn func([]FuncArg[T]) (*Set[T], error))
SetFunc registers a function.
func (*MapContext[T]) SetIdent ¶
func (m *MapContext[T]) SetIdent(name string, set *Set[T])
SetIdent registers an identifier with a set.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is an interface that all AST nodes implement. This allows for uniform handling of different node types during evaluation.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser for the set language.
type Primary ¶
type Primary struct {
FuncCall *FuncCall ` @@`
Ident *string `| @Ident`
SubExpr *Expr `| "(" @@ ")"`
}
Primary represents atomic expressions: function calls, identifiers, or parenthesized expressions.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set represents a set of items of type T. It uses a map for O(1) membership testing and efficient set operations.
func Eval ¶
func Eval[T comparable](ctx Context[T], input string) (*Set[T], error)
Eval is a convenience function that parses and evaluates an expression.
func NewSetFrom ¶
func NewSetFrom[T comparable](items ...T) *Set[T]
NewSetFrom creates a new set containing the given items.
type UnionExpr ¶
type UnionExpr struct {
Left *IntersectExpr `@@`
Right []*UnionTail `@@*`
}
UnionExpr represents union operations (|). Grammar: IntersectExpr ("|" IntersectExpr)*
type UnionTail ¶
type UnionTail struct {
Op string `@"|"`
Right *IntersectExpr `@@`
}
UnionTail represents the right-hand side of a union operation.