From 76756d8abc735a214a060d6c08ba4e7d6c0545b1 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Sat, 20 Jul 2024 14:52:39 -0300 Subject: [PATCH] feat: move behavior to simplify code --- matching/src/lib.rs | 30 ++++++++++++++++++++++-------- matching/src/matches.rs | 25 +++++++++++++++++++++++++ matching/src/ordered/mod.rs | 7 +++---- 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 matching/src/matches.rs diff --git a/matching/src/lib.rs b/matching/src/lib.rs index a67ce0a..1c240b3 100644 --- a/matching/src/lib.rs +++ b/matching/src/lib.rs @@ -1,3 +1,4 @@ +mod matches; mod matching; pub mod matching_configuration; mod matching_entry; @@ -11,24 +12,37 @@ pub use matchings::Matchings; use model::{cst_node::Terminal, CSTNode}; use unordered_pair::UnorderedPair; -trait MatchingRepresentation { - fn get_matching_representation(&self) -> Vec<&str>; +pub trait Matches { + fn matches(&self, right: &CSTNode) -> bool; } -impl MatchingRepresentation for CSTNode<'_> { - fn get_matching_representation(&self) -> Vec<&str> { - self.get_identifier() - .map(|f| f.to_owned()) - .unwrap_or(vec![self.kind()]) +impl Matches for CSTNode<'_> { + fn matches(&self, right: &CSTNode) -> bool { + match (self, right) { + (CSTNode::Terminal(left), CSTNode::Terminal(right)) => { + left.get_identifier() == right.get_identifier() + } + (CSTNode::NonTerminal(left), CSTNode::NonTerminal(right)) => { + if let (Some(left_identifier), Some(right_identifier)) = + (left.get_identifier(), right.get_identifier()) + { + left_identifier == right_identifier + } else { + left.kind == right.kind + } + } + (_, _) => false, + } } } + pub fn calculate_matchings<'a>( left: &'a model::CSTNode, right: &'a model::CSTNode, config: &'a MatchingConfiguration<'a>, ) -> Matchings<'a> { - if left.get_matching_representation() != right.get_matching_representation() { + if !left.matches(&right) { return Matchings::empty(); } diff --git a/matching/src/matches.rs b/matching/src/matches.rs new file mode 100644 index 0000000..ec27414 --- /dev/null +++ b/matching/src/matches.rs @@ -0,0 +1,25 @@ +use model::CSTNode; + +pub trait Matches { + fn matches(&self, right: &CSTNode) -> bool; +} + +impl Matches for CSTNode<'_> { + fn matches(&self, right: &CSTNode) -> bool { + match (self, right) { + (CSTNode::Terminal(left), CSTNode::Terminal(right)) => { + left.get_identifier() == right.get_identifier() + } + (CSTNode::NonTerminal(left), CSTNode::NonTerminal(right)) => { + if let (Some(left_identifier), Some(right_identifier)) = + (left.get_identifier(), right.get_identifier()) + { + left_identifier == right_identifier + } else { + left.kind == right.kind + } + } + (_, _) => false, + } + } +} diff --git a/matching/src/ordered/mod.rs b/matching/src/ordered/mod.rs index 1b8f093..01dd64b 100644 --- a/matching/src/ordered/mod.rs +++ b/matching/src/ordered/mod.rs @@ -1,6 +1,6 @@ use crate::{ - matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, - MatchingRepresentation, Matchings, + matching_configuration::MatchingConfiguration, matching_entry::MatchingEntry, Matches, + Matchings, }; use model::{cst_node::NonTerminal, CSTNode}; use unordered_pair::UnorderedPair; @@ -37,8 +37,7 @@ pub fn calculate_matchings<'a>( .. }), ) => { - let root_matching: usize = - (left.get_matching_representation() == right.get_matching_representation()).into(); + let root_matching: usize = (left.matches(right)).into(); let m = children_left.len(); let n = children_right.len();