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

Record TSG location for edges

Add the TSG location for edge statements in the debug information,
similarly to what already happened for nodes.

Changed files
+80 -44
src
+43
src/execution.rs
··· 11 11 use tree_sitter::QueryMatch; 12 12 use tree_sitter::Tree; 13 13 14 + use crate::ast::CreateEdge; 14 15 use crate::ast::File; 15 16 use crate::ast::Stanza; 17 + use crate::ast::Variable; 16 18 use crate::execution::error::ExecutionError; 17 19 use crate::functions::Functions; 20 + use crate::graph::Attributes; 18 21 use crate::graph::Graph; 19 22 use crate::graph::Value; 20 23 use crate::variables::Globals; ··· 336 339 } 337 340 } 338 341 } 342 + 343 + impl CreateEdge { 344 + pub(crate) fn add_debug_attrs( 345 + &self, 346 + attributes: &mut Attributes, 347 + config: &ExecutionConfig, 348 + ) -> Result<(), ExecutionError> { 349 + if let Some(location_attr) = &config.location_attr { 350 + attributes 351 + .add(location_attr.clone(), format!("{}", self.location)) 352 + .map_err(|_| ExecutionError::DuplicateAttribute(location_attr.as_str().into()))?; 353 + } 354 + Ok(()) 355 + } 356 + } 357 + impl Variable { 358 + pub(crate) fn add_debug_attrs( 359 + &self, 360 + attributes: &mut Attributes, 361 + config: &ExecutionConfig, 362 + ) -> Result<(), ExecutionError> { 363 + if let Some(variable_name_attr) = &config.variable_name_attr { 364 + attributes 365 + .add(variable_name_attr.clone(), format!("{}", self)) 366 + .map_err(|_| { 367 + ExecutionError::DuplicateAttribute(variable_name_attr.as_str().into()) 368 + })?; 369 + } 370 + if let Some(location_attr) = &config.location_attr { 371 + let location = match &self { 372 + Variable::Scoped(v) => v.location, 373 + Variable::Unscoped(v) => v.location, 374 + }; 375 + attributes 376 + .add(location_attr.clone(), format!("{}", location)) 377 + .map_err(|_| ExecutionError::DuplicateAttribute(location_attr.as_str().into()))?; 378 + } 379 + Ok(()) 380 + } 381 + }
+4 -1
src/execution/lazy.rs
··· 24 24 use crate::execution::ExecutionConfig; 25 25 use crate::functions::Functions; 26 26 use crate::graph; 27 + use crate::graph::Attributes; 27 28 use crate::graph::Graph; 28 29 use crate::graph::Value; 29 30 use crate::variables::Globals; ··· 282 283 fn execute_lazy(&self, exec: &mut ExecutionContext) -> Result<(), ExecutionError> { 283 284 let source = self.source.evaluate_lazy(exec)?; 284 285 let sink = self.sink.evaluate_lazy(exec)?; 285 - let stmt = LazyCreateEdge::new(source, sink, exec.error_context.clone().into()); 286 + let mut attributes = Attributes::new(); 287 + self.add_debug_attrs(&mut attributes, exec.config)?; 288 + let stmt = LazyCreateEdge::new(source, sink, attributes, exec.error_context.clone().into()); 286 289 exec.lazy_graph.push(stmt.into()); 287 290 Ok(()) 288 291 }
+22 -10
src/execution/lazy/statements.rs
··· 14 14 15 15 use crate::execution::error::ExecutionError; 16 16 use crate::execution::error::ResultWithExecutionError; 17 + use crate::graph::Attributes; 17 18 use crate::Identifier; 18 19 19 20 use super::store::DebugInfo; ··· 150 151 pub(super) struct LazyCreateEdge { 151 152 source: LazyValue, 152 153 sink: LazyValue, 154 + attributes: Attributes, 153 155 debug_info: DebugInfo, 154 156 } 155 157 156 158 impl LazyCreateEdge { 157 - pub(super) fn new(source: LazyValue, sink: LazyValue, debug_info: DebugInfo) -> Self { 159 + pub(super) fn new( 160 + source: LazyValue, 161 + sink: LazyValue, 162 + attributes: Attributes, 163 + debug_info: DebugInfo, 164 + ) -> Self { 158 165 Self { 159 166 source, 160 167 sink, 168 + attributes, 161 169 debug_info, 162 170 } 163 171 } ··· 168 176 let prev_debug_info = exec 169 177 .prev_element_debug_info 170 178 .insert(GraphElementKey::Edge(source, sink), self.debug_info.clone()); 171 - if let Err(_) = exec.graph[source].add_edge(sink) { 172 - Err(ExecutionError::DuplicateEdge(format!( 173 - "({} -> {}) at {} and {}", 174 - source, 175 - sink, 176 - prev_debug_info.unwrap(), 177 - self.debug_info, 178 - )))?; 179 - } 179 + let edge = match exec.graph[source].add_edge(sink) { 180 + Ok(edge) => edge, 181 + Err(_) => { 182 + return Err(ExecutionError::DuplicateEdge(format!( 183 + "({} -> {}) at {} and {}", 184 + source, 185 + sink, 186 + prev_debug_info.unwrap(), 187 + self.debug_info, 188 + )))? 189 + } 190 + }; 191 + edge.attributes = self.attributes.clone(); 180 192 Ok(()) 181 193 } 182 194 }
+10 -33
src/execution/strict.rs
··· 47 47 use crate::execution::error::StatementContext; 48 48 use crate::execution::CancellationFlag; 49 49 use crate::execution::ExecutionConfig; 50 - use crate::graph::Attributes; 51 50 use crate::graph::Graph; 52 51 use crate::graph::SyntaxNodeRef; 53 52 use crate::graph::Value; ··· 304 303 fn execute(&self, exec: &mut ExecutionContext) -> Result<(), ExecutionError> { 305 304 let source = self.source.evaluate(exec)?.into_graph_node_ref()?; 306 305 let sink = self.sink.evaluate(exec)?.into_graph_node_ref()?; 307 - if let Err(_) = exec.graph[source].add_edge(sink) { 308 - Err(ExecutionError::DuplicateEdge(format!( 309 - "({} -> {}) in {}", 310 - source, sink, self, 311 - )))?; 312 - } 306 + let edge = match exec.graph[source].add_edge(sink) { 307 + Ok(edge) => edge, 308 + Err(_) => { 309 + return Err(ExecutionError::DuplicateEdge(format!( 310 + "({} -> {}) in {}", 311 + source, sink, self, 312 + )))? 313 + } 314 + }; 315 + self.add_debug_attrs(&mut edge.attributes, exec.config)?; 313 316 Ok(()) 314 317 } 315 318 } ··· 798 801 ExecutionError::UndefinedVariable(format!("{}", self)) 799 802 } 800 803 }) 801 - } 802 - } 803 - 804 - impl Variable { 805 - pub(crate) fn add_debug_attrs( 806 - &self, 807 - attributes: &mut Attributes, 808 - config: &ExecutionConfig, 809 - ) -> Result<(), ExecutionError> { 810 - if let Some(variable_name_attr) = &config.variable_name_attr { 811 - attributes 812 - .add(variable_name_attr.clone(), format!("{}", self)) 813 - .map_err(|_| { 814 - ExecutionError::DuplicateAttribute(variable_name_attr.as_str().into()) 815 - })?; 816 - } 817 - if let Some(location_attr) = &config.location_attr { 818 - let location = match &self { 819 - Variable::Scoped(v) => v.location, 820 - Variable::Unscoped(v) => v.location, 821 - }; 822 - attributes 823 - .add(location_attr.clone(), format!("{}", location)) 824 - .map_err(|_| ExecutionError::DuplicateAttribute(location_attr.as_str().into()))?; 825 - } 826 - Ok(()) 827 804 } 828 805 } 829 806
+1
src/graph.rs
··· 260 260 } 261 261 262 262 /// A set of attributes associated with a graph node or edge 263 + #[derive(Clone, Debug)] 263 264 pub struct Attributes { 264 265 values: HashMap<Identifier, Value>, 265 266 }