Documentation
¶
Overview ¶
Package declarationfacts compiles grammar-owned declaration rules and projects their facts without depending on the parser or its public facade.
Index ¶
- func ChildByField[N comparable](r Reader[N], n N, field uint16) N
- func HasFieldChild[N comparable](r Reader[N], n N, field uint16, symbols SymbolSet) bool
- func HasSymbol(set SymbolSet, symbol uint16) bool
- func MatchesAncestors[N comparable](r Reader[N], n N, path []SymbolSet) bool
- func SingleChild[N comparable](r Reader[N], n N) N
- func ValidRule(g Grammar, rule Rule) bool
- type Container
- type ContainerRule
- type Fact
- type Grammar
- type Names
- type Program
- type Reader
- type Rule
- type SymbolSet
- type TypeNameRule
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 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.
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 ¶
Containers finds exact owners of body and expands grouped names as siblings.
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.
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 ¶
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 ¶
ResolveSymbolSet combines node names; empty optional names are skipped.
type TypeNameRule ¶
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).