Skip to content

Commit

Permalink
feat(merge): Add implementation for unordered tree merge (#17)
Browse files Browse the repository at this point in the history
Closes #11
  • Loading branch information
jpedroh authored Nov 23, 2023
1 parent 4461929 commit ddcf5be
Show file tree
Hide file tree
Showing 4 changed files with 748 additions and 9 deletions.
1 change: 1 addition & 0 deletions matching/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod unordered_tree_matching;
pub use matching_entry::MatchingEntry;
pub use matchings::Matchings;
pub use ordered_tree_matching::ordered_tree_matching;
pub use unordered_tree_matching::unordered_tree_matching;

pub fn calculate_matchings<'a>(
left: &'a model::CSTNode,
Expand Down
13 changes: 12 additions & 1 deletion matching/src/unordered_tree_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ pub fn unordered_tree_matching<'a>(left: &'a CSTNode, right: &'a CSTNode) -> cra

fn compute_matching_score<'a>(left: &'a CSTNode, right: &'a CSTNode) -> usize {
match (left, right) {
(CSTNode::Terminal { .. }, CSTNode::Terminal { .. }) => (left == right).into(),
(
CSTNode::Terminal {
kind: kind_left,
value: value_left,
..
},
CSTNode::Terminal {
kind: kind_right,
value: value_right,
..
},
) => (kind_left == kind_right && value_left == value_right).into(),
(
CSTNode::NonTerminal {
children: children_left,
Expand Down
29 changes: 21 additions & 8 deletions merge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod merged_cst_node;
mod ordered_merge;
mod unordered_merge;

use matching::Matchings;
use merged_cst_node::MergedCSTNode;
use model::CSTNode;
use ordered_merge::ordered_merge;
use unordered_merge::unordered_merge;

pub fn merge<'a>(
base: &'a CSTNode<'a>,
Expand All @@ -14,12 +16,23 @@ pub fn merge<'a>(
base_right_matchings: &'a Matchings<'a>,
left_right_matchings: &'a Matchings<'a>,
) -> MergedCSTNode<'a> {
return ordered_merge(
base,
left,
right,
base_left_matchings,
base_right_matchings,
left_right_matchings,
);
if right.are_children_unordered() && left.are_children_unordered() {
unordered_merge(
base,
left,
right,
base_left_matchings,
base_right_matchings,
left_right_matchings,
)
} else {
ordered_merge(
base,
left,
right,
base_left_matchings,
base_right_matchings,
left_right_matchings,
)
}
}
Loading

0 comments on commit ddcf5be

Please sign in to comment.