Skip to content

Commit

Permalink
chore: use NodeWithContainer in ResolvedPattern::from_node() + mo…
Browse files Browse the repository at this point in the history
…re `Binding::list_items()` usage (#103)
  • Loading branch information
arendjr authored Mar 28, 2024
1 parent c15e9ad commit 542cca3
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 74 deletions.
2 changes: 1 addition & 1 deletion crates/core/src/pattern/after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Matcher for After {
};
let prev_node = resolve!(node.previous_non_trivia_node());
if !self.after.execute(
&ResolvedPattern::from_node(prev_node.source, prev_node.node),
&ResolvedPattern::from_node(prev_node),
&mut cur_state,
context,
logs,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Matcher for ASTNode {
)
} else if let Some(child) = node.child_by_field_id(*field_id) {
pattern.execute(
&ResolvedPattern::from_node(source, child),
&ResolvedPattern::from_node(NodeWithSource::new(child, source)),
&mut cur_state,
context,
logs,
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/before.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Matcher for Before {
};
let next_node = resolve!(node.next_non_trivia_node());
if !self.before.execute(
&ResolvedPattern::from_node(next_node.source, next_node.node),
&ResolvedPattern::from_node(next_node),
&mut cur_state,
context,
logs,
Expand Down
42 changes: 16 additions & 26 deletions crates/core/src/pattern/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,28 +402,20 @@ fn distinct_fn<'a>(
Ok(ResolvedPattern::List(unique_list))
}
Some(ResolvedPattern::Binding(binding)) => match binding.last() {
Some(b) => match b {
Binding::List(src, parent_node, field_id) => {
Some(b) => {
if let Some(list_items) = b.list_items() {
let mut unique_list = Vector::new();
for child in parent_node
.children_by_field_id(*field_id, &mut parent_node.walk())
.filter(|child| child.is_named())
{
let resolved = ResolvedPattern::from_node(src, child);
for item in list_items {
let resolved = ResolvedPattern::from_node(item);
if !unique_list.contains(&resolved) {
unique_list.push_back(resolved);
}
}
Ok(ResolvedPattern::List(unique_list))
}
Binding::String(..)
| Binding::FileName(_)
| Binding::Node(..)
| Binding::Empty(..)
| Binding::ConstantRef(_) => {
} else {
bail!("distinct takes a list as the first argument")
}
},
}
None => Ok(ResolvedPattern::Binding(binding)),
},
_ => Err(anyhow!("distinct takes a list as the first argument")),
Expand Down Expand Up @@ -454,19 +446,17 @@ fn shuffle_fn<'a>(
Ok(ResolvedPattern::List(shuffled_list.into()))
}
ResolvedPattern::Binding(binding) => match binding.last() {
Some(Binding::List(src, parent_node, field_id)) => {
let mut list = parent_node
.children_by_field_id(*field_id, &mut parent_node.walk())
.filter(|child| child.is_named())
.collect::<Vec<_>>();
list.shuffle(state.get_rng());
let list = list
.into_iter()
.map(|child| ResolvedPattern::from_node(src, child))
.collect::<Vector<_>>();
Ok(ResolvedPattern::List(list))
Some(b) => {
if let Some(list_items) = b.list_items() {
let mut list: Vec<_> = list_items.collect();
list.shuffle(state.get_rng());
let list: Vector<_> =
list.into_iter().map(ResolvedPattern::from_node).collect();
Ok(ResolvedPattern::List(list))
} else {
Err(anyhow!("shuffle takes a list as the first argument"))
}
}
Some(_) => Err(anyhow!("shuffle takes a list as the first argument")),
None => Err(anyhow!("shuffle argument must be bound")),
},
ResolvedPattern::Snippets(_)
Expand Down
67 changes: 30 additions & 37 deletions crates/core/src/pattern/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
use anyhow::{anyhow, Result};
use core::fmt::Debug;
use im::vector;
use marzano_util::analysis_logs::AnalysisLogs;
use marzano_util::{analysis_logs::AnalysisLogs, node_with_source::NodeWithSource};

use std::collections::BTreeMap;

Expand Down Expand Up @@ -86,7 +86,7 @@ fn execute_until<'a>(
let mut still_computing = true;
while still_computing {
let node = cursor.node();
let node_lhs = ResolvedPattern::from_node(src, node);
let node_lhs = ResolvedPattern::from_node(NodeWithSource::new(node, src));

let state = cur_state.clone();
if the_contained.execute(&node_lhs, &mut cur_state, context, logs)? {
Expand Down Expand Up @@ -137,50 +137,43 @@ impl Matcher for Contains {
match resolved_pattern {
ResolvedPattern::Binding(bindings) => {
let binding = resolve!(bindings.last());
let mut did_match = false;
let mut cur_state = init_state.clone();
let mut cursor; // needed for scope in case of list.
match binding {
Binding::Empty(_, _, _) => Ok(false),
Binding::String(_, _) => Ok(false),
Binding::Node(src, node) => execute_until(
if let Some(node) = binding.as_node() {
execute_until(
init_state,
node,
src,
&node.node,
node.source,
context,
logs,
&self.contains,
&self.until,
),
Binding::List(src, node, field_id) => {
cursor = node.walk();
let children = node.children_by_field_id(*field_id, &mut cursor);

for child in children {
let state = cur_state.clone();
if self.execute(
&ResolvedPattern::from_node(src, child),
&mut cur_state,
context,
logs,
)? {
did_match = true;
} else {
cur_state = state;
}
)
} else if let Some(list_items) = binding.list_items() {
let mut did_match = false;
let mut cur_state = init_state.clone();
for item in list_items {
let state = cur_state.clone();
if self.execute(
&ResolvedPattern::from_node(item),
&mut cur_state,
context,
logs,
)? {
did_match = true;
} else {
cur_state = state;
}
}

if did_match {
*init_state = cur_state;
}
Ok(did_match)
if did_match {
*init_state = cur_state;
}
Binding::FileName(_) => Ok(false),
Ok(did_match)
} else if let Some(_c) = binding.as_constant() {
// this seems like an infinite loop, todo return false?
Binding::ConstantRef(_c) => {
self.contains
.execute(resolved_pattern, init_state, context, logs)
}
self.contains
.execute(resolved_pattern, init_state, context, logs)
} else {
Ok(false)
}
}
ResolvedPattern::List(elements) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/every.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Matcher for Every {

for item in list_items {
if !self.pattern.execute(
&ResolvedPattern::from_node(item.source, item.node),
&ResolvedPattern::from_node(item),
init_state,
context,
logs,
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/pattern/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ impl Matcher for List {
};

let children: Vec<Cow<ResolvedPattern>> = list_items
.map(|node| Cow::Owned(ResolvedPattern::from_node(node.source, node.node)))
.map(ResolvedPattern::from_node)
.map(Cow::Owned)
.collect();

execute_assoc(&self.patterns, &children, state, context, logs)
Expand Down
8 changes: 5 additions & 3 deletions crates/core/src/pattern/resolved_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use crate::{
use anyhow::{anyhow, bail, Result};
use im::{vector, Vector};
use marzano_language::{language::FieldId, target_language::TargetLanguage};
use marzano_util::{analysis_logs::AnalysisLogs, position::Range};
use marzano_util::{
analysis_logs::AnalysisLogs, node_with_source::NodeWithSource, position::Range,
};
use std::{
borrow::Cow,
collections::{BTreeMap, HashMap},
Expand Down Expand Up @@ -478,8 +480,8 @@ impl<'a> ResolvedPattern<'a> {
Self::Binding(vector![Binding::ConstantRef(constant)])
}

pub(crate) fn from_node(src: &'a str, node: Node<'a>) -> Self {
Self::Binding(vector![Binding::Node(src, node)])
pub(crate) fn from_node(node: NodeWithSource<'a>) -> Self {
Self::Binding(vector![Binding::Node(node.source, node.node)])
}

pub(crate) fn from_list(src: &'a str, node: Node<'a>, field_id: FieldId) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern/some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Matcher for Some {
for item in list_items {
let state = cur_state.clone();
if self.pattern.execute(
&ResolvedPattern::from_node(item.source, item.node),
&ResolvedPattern::from_node(item),
&mut cur_state,
context,
logs,
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/pattern/within.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{context::Context, resolve};
use anyhow::{anyhow, Result};
use core::fmt::Debug;
use marzano_language::parent_traverse::{ParentTraverse, TreeSitterParentCursor};
use marzano_util::analysis_logs::AnalysisLogs;
use marzano_util::{analysis_logs::AnalysisLogs, node_with_source::NodeWithSource};
use std::collections::BTreeMap;
use tree_sitter::Node;

Expand Down Expand Up @@ -88,7 +88,7 @@ impl Matcher for Within {
for n in ParentTraverse::new(TreeSitterParentCursor::new(node.node)) {
let state = cur_state.clone();
if self.pattern.execute(
&ResolvedPattern::from_node(node.source, n),
&ResolvedPattern::from_node(NodeWithSource::new(n, node.source)),
&mut cur_state,
context,
logs,
Expand Down

0 comments on commit 542cca3

Please sign in to comment.