fork of https://github.com/tree-sitter/tree-sitter-graph

Associate capture functions with the relevant types

Changed files
+57 -43
src
+44 -29
src/execution.rs
··· 7 7 8 8 use thiserror::Error; 9 9 use tree_sitter::CaptureQuantifier; 10 + use tree_sitter::Node; 10 11 use tree_sitter::QueryMatch; 11 12 use tree_sitter::Tree; 12 13 ··· 156 157 #[error("Cancelled at \"{0}\"")] 157 158 pub struct CancellationError(pub &'static str); 158 159 159 - /// Get the value for the given capture, considering the suffix 160 - pub(self) fn query_capture_value<'tree>( 161 - index: usize, 162 - quantifier: CaptureQuantifier, 163 - mat: &QueryMatch<'_, 'tree>, 164 - graph: &mut Graph<'tree>, 165 - ) -> Value { 166 - let mut nodes = mat 167 - .captures 168 - .iter() 169 - .filter(|c| c.index as usize == index) 170 - .map(|c| c.node); 171 - match quantifier { 172 - CaptureQuantifier::Zero => unreachable!(), 173 - CaptureQuantifier::One => { 174 - let syntax_node = graph.add_syntax_node(nodes.next().expect("missing capture")); 175 - syntax_node.into() 176 - } 177 - CaptureQuantifier::ZeroOrMore | CaptureQuantifier::OneOrMore => { 178 - let syntax_nodes = nodes 179 - .map(|n| graph.add_syntax_node(n.clone()).into()) 180 - .collect::<Vec<Value>>(); 181 - syntax_nodes.into() 182 - } 183 - CaptureQuantifier::ZeroOrOne => match nodes.next() { 184 - None => Value::Null.into(), 185 - Some(node) => { 186 - let syntax_node = graph.add_syntax_node(node); 160 + impl crate::ast::Stanza { 161 + /// Return the top-level matched node from a file query match result. 162 + pub fn full_capture_from_file_match<'a, 'cursor: 'a, 'tree: 'a>( 163 + &self, 164 + mat: &'a QueryMatch<'cursor, 'tree>, 165 + ) -> impl Iterator<Item = Node<'tree>> + 'a { 166 + mat.nodes_for_capture_index(self.full_match_file_capture_index as u32) 167 + } 168 + 169 + /// Return the top-level matched node from a stanza query match result. 170 + pub fn full_capture_from_stanza_match<'a, 'cursor: 'a, 'tree: 'a>( 171 + &self, 172 + mat: &'a QueryMatch<'cursor, 'tree>, 173 + ) -> impl Iterator<Item = Node<'tree>> + 'a { 174 + mat.nodes_for_capture_index(self.full_match_stanza_capture_index as u32) 175 + } 176 + } 177 + 178 + impl Value { 179 + pub fn from_nodes<'tree, NI: IntoIterator<Item = Node<'tree>>>( 180 + graph: &mut Graph<'tree>, 181 + nodes: NI, 182 + quantifier: CaptureQuantifier, 183 + ) -> Value { 184 + let mut nodes = nodes.into_iter(); 185 + match quantifier { 186 + CaptureQuantifier::Zero => unreachable!(), 187 + CaptureQuantifier::One => { 188 + let syntax_node = graph.add_syntax_node(nodes.next().expect("missing capture")); 187 189 syntax_node.into() 188 190 } 189 - }, 191 + CaptureQuantifier::ZeroOrMore | CaptureQuantifier::OneOrMore => { 192 + let syntax_nodes = nodes 193 + .map(|n| graph.add_syntax_node(n.clone()).into()) 194 + .collect::<Vec<Value>>(); 195 + syntax_nodes.into() 196 + } 197 + CaptureQuantifier::ZeroOrOne => match nodes.next() { 198 + None => Value::Null.into(), 199 + Some(node) => { 200 + let syntax_node = graph.add_syntax_node(node); 201 + syntax_node.into() 202 + } 203 + }, 204 + } 190 205 } 191 206 }
+6 -6
src/execution/lazy.rs
··· 22 22 use crate::execution::error::ExecutionError; 23 23 use crate::execution::error::ResultWithExecutionError; 24 24 use crate::execution::error::StatementContext; 25 - use crate::execution::query_capture_value; 26 25 use crate::execution::ExecutionConfig; 27 26 use crate::functions::Functions; 28 27 use crate::graph; 29 28 use crate::graph::Graph; 29 + use crate::graph::Value; 30 30 use crate::variables::Globals; 31 31 use crate::variables::MutVariables; 32 32 use crate::variables::VariableMap; ··· 167 167 ) -> Result<(), ExecutionError> { 168 168 let current_regex_captures = vec![]; 169 169 locals.clear(); 170 - let node = query_capture_value(self.full_match_file_capture_index, One, &mat, graph); 170 + let node = Value::from_nodes(graph, self.full_capture_from_file_match(mat), One); 171 171 debug!("match {} at {}", node, self.location); 172 172 trace!("{{"); 173 173 for statement in &self.statements { ··· 613 613 614 614 impl ast::Capture { 615 615 fn evaluate_lazy(&self, exec: &mut ExecutionContext) -> Result<LazyValue, ExecutionError> { 616 - Ok(query_capture_value( 617 - self.file_capture_index, 618 - self.quantifier, 619 - exec.mat, 616 + Ok(Value::from_nodes( 620 617 exec.graph, 618 + exec.mat 619 + .nodes_for_capture_index(self.file_capture_index as u32), 620 + self.quantifier, 621 621 ) 622 622 .into()) 623 623 }
+7 -8
src/execution/strict.rs
··· 44 44 use crate::ast::Variable; 45 45 use crate::execution::error::ExecutionError; 46 46 use crate::execution::error::ResultWithExecutionError; 47 - use crate::execution::query_capture_value; 47 + use crate::execution::error::StatementContext; 48 48 use crate::execution::CancellationFlag; 49 49 use crate::execution::ExecutionConfig; 50 50 use crate::graph::Attributes; ··· 57 57 use crate::variables::Variables; 58 58 use crate::Identifier; 59 59 use crate::Location; 60 - 61 - use super::error::StatementContext; 62 60 63 61 impl File { 64 62 /// Executes this graph DSL file against a source file, saving the results into an existing ··· 600 598 601 599 impl Capture { 602 600 fn evaluate(&self, exec: &mut ExecutionContext) -> Result<Value, ExecutionError> { 603 - Ok(query_capture_value( 604 - self.stanza_capture_index, 601 + Ok(Value::from_nodes( 602 + exec.graph, 603 + exec.mat 604 + .nodes_for_capture_index(self.stanza_capture_index as u32), 605 605 self.quantifier, 606 - exec.mat, 607 - exec.graph, 608 - )) 606 + ) 607 + .into()) 609 608 } 610 609 } 611 610