declarationfacts

package
v0.56.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package declarationfacts compiles grammar-owned declaration rules and projects their facts without depending on the parser or its public facade.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChildByField

func ChildByField[N comparable](r Reader[N], n N, field uint16) N

ChildByField returns the first direct child with this field.

func HasFieldChild

func HasFieldChild[N comparable](r Reader[N], n N, field uint16, symbols SymbolSet) bool

HasFieldChild reports whether n has a direct child in field whose symbol is one of symbols.

func HasSymbol

func HasSymbol(set SymbolSet, symbol uint16) bool

HasSymbol reports whether set contains symbol.

func MatchesAncestors

func MatchesAncestors[N comparable](r Reader[N], n N, path []SymbolSet) bool

MatchesAncestors checks an exact parent chain, nearest first.

func SingleChild

func SingleChild[N comparable](r Reader[N], n N) N

SingleChild returns the sole named, present, non-comment direct child.

func ValidRule

func ValidRule(g Grammar, rule Rule) bool

ValidRule reports whether all of a rule's grammar references resolve.

Types

type Container

type Container[N comparable] struct {
	Name string
	Node N
}

Container is a named syntax owner and its range-bearing node.

type ContainerRule

type ContainerRule struct {
	NodeType, NameField, NameNodeType, TypeField string
	Ancestors, ParentPath                        []string
	BodyWrappers                                 []TypeNameRule
	// Self makes the node passed to Containers the owner itself, rather than a
	// body owned by it. TypeField is then optional and unused.
	Self bool
}

ContainerRule names the direct owner of a type body. Ancestors restrict its scope. ParentPath lists the exact path from a nested owner to its parent body. An empty ParentPath starts a container; otherwise names extend its dotted path. BodyWrappers allows exact type wrappers between body and owner.

type Fact

type Fact struct {
	Lang, Kind, Name, NodeType                     string
	StartByte, EndByte, NameStartByte, NameEndByte uint32
	Shape                                          string `json:",omitempty"`
	Container                                      string `json:",omitempty"`
	ContainerStartByte                             uint32 `json:",omitempty"`
	ContainerEndByte                               uint32 `json:",omitempty"`
	Embedded                                       bool   `json:",omitempty"`
}

Fact describes one declared name. Equal construct ranges mean siblings; each name retains its own name range. Metadata is zero when not applicable.

type Grammar

type Grammar struct {
	Names  []string
	Symbol func(string) (uint16, bool)
	Field  func(string) (uint16, bool)
}

Grammar resolves the symbol and field data needed to compile rules.

type Names

type Names[N comparable] struct {
	// contains filtered or unexported fields
}

Names holds compiled base-name and container paths shared by fact projections.

func CompileNames

func CompileNames[N comparable](g Grammar, r Reader[N], types []TypeNameRule, containers []ContainerRule) (*Names[N], bool)

CompileNames validates every referenced symbol and field, copying all paths.

func (*Names[N]) Base

func (p *Names[N]) Base(n N) N

Base selects an identifier through only the compiled grammar paths.

func (*Names[N]) Containers

func (p *Names[N]) Containers(body N, source []byte) []Container[N]

Containers finds exact owners of body and expands grouped names as siblings.

func (*Names[N]) HasType

func (p *Names[N]) HasType(n N, symbol uint16) bool

HasType checks a node and its grammar-owned type path for a symbol. Wrappers may occur at any depth; traversal stops at a leaf or an unknown type.

type Program

type Program[N comparable] struct {
	// contains filtered or unexported fields
}

Program holds immutable compiled rules. Concurrent extractions need separate destination slices. Compile returns nil when no rule can be resolved.

func Compile

func Compile[N comparable](grammar Grammar, reader Reader[N], rules []Rule) *Program[N]

Compile resolves grammar data once and owns all retained ancestor storage.

func (*Program[N]) Extract

func (p *Program[N]) Extract(root N, source []byte, dst *[]Fact)

Extract appends facts in source-tree order. A nil program or root does nothing.

type Reader

type Reader[N comparable] struct {
	Language           string
	Symbol             func(N) uint16
	Parent             func(N) N
	ChildCount         func(N) int
	Child              func(N, int) N
	Field              func(N, int) uint16
	Missing            func(N) bool
	Named              func(N) bool
	Text               func(N, []byte) string
	NodeType           func(N) string
	StartByte, EndByte func(N) uint32
}

Reader adapts the parser's node representation without materializing wrappers.

type Rule

type Rule struct {
	NodeType, Kind, NameField, NameNodeType                       string
	Ancestors                                                     []string
	TypeField, TypeNodeType, Shape                                string
	ContainerNameField, ContainerNameNodeType, ContainerTypeField string
	// BaseName selects an embedded type through TypeNames; BaseNameField is
	// empty for a node with one named child. A non-empty BaseNameField emits one
	// fact for every direct child in that field. ExcludeNames filters bare constraint terms.
	BaseName      bool
	BaseNameField string
	Embedded      bool
	ExcludeNames  []string
	TypeNames     []TypeNameRule
	// ContainerPath replaces fixed container ownership with recursive exact paths.
	ContainerPath []ContainerRule
	// ParentChildField and ParentChildNodeType, set together, require the direct
	// parent to have a direct child in that field with that node type. They
	// separate declarations that share a parent node type, such as a keyword token.
	ParentChildField, ParentChildNodeType string
	// NameSelf makes the matched node its own name; NameNodeType must equal
	// NodeType and NameField stays empty. Its name range is the node range.
	NameSelf bool
	// NameWrappers holds binding patterns; only names reached through these wrappers emit facts.
	NameWrappers []string
}

Rule describes a declaration using direct name fields and exact parent paths. Ancestors lists parents nearest first; the last is the optional container. ContainerTypeField must link the container to the preceding ancestor. TypeField/TypeNodeType optionally constrain the node's direct type child. The first applicable rule wins; missing grammar data fails closed.

type SymbolSet

type SymbolSet map[uint16]bool

SymbolSet includes every grammar symbol for one or more node names. Grammar names are not unique: named nodes and anonymous tokens can share them.

func ResolveSymbolSet

func ResolveSymbolSet(g Grammar, names ...string) (SymbolSet, bool)

ResolveSymbolSet combines node names; empty optional names are skipped.

func ResolveSymbols

func ResolveSymbols(g Grammar, name string) (SymbolSet, bool)

ResolveSymbols returns all symbols with name, including duplicate names. The resolver must accept the name; missing grammar data fails closed.

type TypeNameRule

type TypeNameRule struct {
	NodeType, Field string
	Leaf            bool
}

TypeNameRule selects a base identifier through a grammar-owned type path. Leaf returns this node. Field follows one direct field; an empty Field on a non-leaf follows exactly one named child (excluding comments).

Jump to

Keyboard shortcuts

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