engine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AncestorNameSimilarity

func AncestorNameSimilarity(t1, t2 *treesitter.ASTNode) int

AncestorNameSimilarity calculates the number of matching identifier labels among the ancestors of t1 and t2. This helps break ties in top-down matching by preferring pairs located in similarly named functions or classes.

func BottomUp

func BottomUp(
	t1Root, t2Root *treesitter.ASTNode,
	m *Mapping,
	minDice float64,
)

BottomUp implements the GumTree bottom-up matching phase.

It walks T1 in post-order and for each unmatched node that has at least one matched child, it finds the best candidate in T2 (same type, highest dice) and adds the pair to the mapping. Then it runs SimpleRecovery to map descendants inside the newly matched container.

Parameters:

t1Root  - root of source tree T1
t2Root  - root of destination tree T2
m       - existing mapping from the top-down phase (modified in place)
minDice - minimum dice coefficient to accept a candidate

func ChawatheSimilarity

func ChawatheSimilarity(t1, t2 *treesitter.ASTNode, m map[*treesitter.ASTNode]*treesitter.ASTNode) float64

ChawatheSimilarity computes the Chawathe similarity coefficient between two subtrees given the current mapping set m (maps T1 nodes -> T2 nodes).

chawathe(t1, t2, m) = |{t ∈ s(t1) | (t, t2') ∈ m for some t2'}| / max(|s(t1)|, |s(t2)|)

func Descendants

func Descendants(n *treesitter.ASTNode) []*treesitter.ASTNode

Descendants returns all nodes in the subtree rooted at n, excluding n itself.

func Dice

Dice computes the Dice similarity coefficient between two subtrees given the current mapping set m (maps T1 nodes -> T2 nodes).

dice(t1, t2, m) = 2 × |{t ∈ s(t1) | (t, t2') ∈ m for some t2'}| / (|s(t1)| + |s(t2)|)

func FprintMappings

func FprintMappings(w io.Writer, r *MatchResult) error

func Height

func Height(n *treesitter.ASTNode) int

Height returns the height of a subtree rooted at n. A leaf has height 1.

func Isomorphic

func Isomorphic(a, b *treesitter.ASTNode) bool

TODO: replace the O(n) algorithm with O(1) hash comparison from M. Chilowicz, E. Duris, and G. Roussel. Syntax tree fingerprinting for source code similarity detection.

Isomorphic returns true when two subtrees are structurally and label-wise identical.

func LCSLabel

func LCSLabel(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode

isomorphic equality (type + label + structure).

func LCSStructure

func LCSStructure(seq1, seq2 []*treesitter.ASTNode) [][2]*treesitter.ASTNode

structure-only isomorphism (type + shape, ignoring leaf labels).

This variant resolves ambiguous structural matches (one src node structurally equal to several dst nodes) with a positional + label-similarity tie-break. It first computes the plain structural LCS, then for each matched src node that is structurally equal to more than one matched/unmatched dst node, re-selects the best partner: same child index within the parent first, then the most label-similar candidate. This keeps a loop variable like "import_from_child" paired with "child" instead of the rightmost "children".

func MatchUnmatchedLeaves

func MatchUnmatchedLeaves(t1Root, t2Root *treesitter.ASTNode, m *Mapping)

MatchUnmatchedLeaves greedily maps remaining leaf nodes with matching type and label. Dice of parents picks the best candidate; ties are broken by a positional score (parent mapped > parent same slot in matched ancestor > same child index > matched ancestor pair). Leaves with an unmatched parent are skipped — they belong to deleted or inserted subtrees and have no real counterpart.

func NearestMatchedAncestor

func NearestMatchedAncestor(n *treesitter.ASTNode, m *Mapping, isDst bool) *treesitter.ASTNode

NearestMatchedAncestor finds the closest ancestor of n that is present in the mapping. If isDst is true, it checks m.HasDst; otherwise it checks m.Has.

func Open

func Open(t *treesitter.ASTNode, l *PriorityList)

Open expansd node t into its childrens and pushes each into list.

func PeekMax

func PeekMax(l *PriorityList) int

PeekMax returns the maximum height currently in the list.

func Pop

func Pop(l *PriorityList) []*treesitter.ASTNode

Pop removes and returns all nodes whose height equals PeekMax.

func PostOrder

func PostOrder(n *treesitter.ASTNode) []*treesitter.ASTNode

PostOrder returns all nodes in the subtree rooted at n in post-order (children before parent).

func PreOrder

func PreOrder(n *treesitter.ASTNode) []*treesitter.ASTNode

PreOrder returns all nodes in the subtree rooted at n in pre-order (parent before children). Used for deterministic mapping output.

func PrintMappings

func PrintMappings(r *MatchResult)

func Push

func Push(n *treesitter.ASTNode, l *PriorityList)

Push inserts node n (keyed by its height) into the list.

func SimpleRecovery

func SimpleRecovery(t1, t2 *treesitter.ASTNode, m *Mapping)

SimpleRecovery recovers additional mappings inside a pair of matched container nodes (t1, t2). It is called by BottomUp after a new container mapping is established.

Three steps:

  1. Label-based LCS on unmatched children → map isomorphic subtrees
  2. Structure-based LCS on remaining unmatched children → map structurally isomorphic subtrees (labels may differ)
  3. Unique-type pairing on remaining unmatched children → map nodes whose type appears exactly once on each side, then recurse

func StructureIsomorphic

func StructureIsomorphic(a, b *treesitter.ASTNode) bool

StructureIsomorphic returns true when two subtrees are structurally identical (same Type, same shape) but ignores leaf labels.

TODO: replace with O(1) hash comparison alongside Isomorphic.

Types

type Mapping

type Mapping struct {
	Pairs []MappingPair
	// contains filtered or unexported fields
}

Mapping is a 1-1 node mapping between T1 nodes and T2 nodes. Pairs preserves insertion order for deterministic iteration.

func NewMapping

func NewMapping() *Mapping

func TopDown

func TopDown(
	t1Root, t2Root *treesitter.ASTNode,
	minHeight int,
) *Mapping

TopDown imploments the Gumtree Top-Down matching phase.

It takes:

t1Root - root of source tree T1
t2Root - root of destination tree T2
minHeight - minimum subtree height (Will be removed after adding the Simple recovery function)

Returns the set of mappings m and the candidate list A

func (*Mapping) Add

func (m *Mapping) Add(t1, t2 *treesitter.ASTNode)

func (*Mapping) DiceSrc

func (m *Mapping) DiceSrc(t1, t2 *treesitter.ASTNode) float64

DiceSrc computes dice(t1, t2) using the internal src mapping.

func (*Mapping) Dst

func (m *Mapping) Dst() map[*treesitter.ASTNode]*treesitter.ASTNode

Dst exposes the T2->T1 map.

func (*Mapping) Has

func (m *Mapping) Has(t1 *treesitter.ASTNode) bool

func (*Mapping) HasDst

func (m *Mapping) HasDst(t2 *treesitter.ASTNode) bool

func (*Mapping) Remove

func (m *Mapping) Remove(t1 *treesitter.ASTNode)

func (*Mapping) Src

func (m *Mapping) Src() map[*treesitter.ASTNode]*treesitter.ASTNode

Src exposes the T1->T2 map for use in Dice calculations.

type MappingPair

type MappingPair struct {
	Src *treesitter.ASTNode
	Dst *treesitter.ASTNode
}

MappingPair is a single src→dst node mapping, kept for deterministic ordering.

type MatchResult

type MatchResult struct {
	Mappings *Mapping
}

func Match

func Match(t1, t2 *treesitter.ASTNode) *MatchResult

type PriorityList

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

PriorityList is a height-indexed max-priority list. Internally we store buckets keyed by height so PeekMax / Pop are O(1) and Push is O(log n) via a sorted key list.

func NewPriorityList

func NewPriorityList() *PriorityList

Jump to

Keyboard shortcuts

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