Skip to content

Commit

Permalink
Merge pull request #140 from tree-sitter/match_node_attr
Browse files Browse the repository at this point in the history
Add `match_node_attr`
  • Loading branch information
BekaValentine authored Jul 17, 2023
2 parents 86aa672 + 42b8027 commit 70ba65c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.11.0 -- 2023-07-17

### Library

#### Added

- Store a debug attribute for the matched syntax node, providing information about the syntactic category of the match that gave rise to the graph node.

## v0.10.5 -- 2023-06-26

#### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tree-sitter-graph"
version = "0.10.5"
version = "0.11.0"
description = "Construct graphs from parsed source code"
homepage = "https://github.com/tree-sitter/tree-sitter-graph/"
repository = "https://github.com/tree-sitter/tree-sitter-graph/"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To use it as a library, add the following to your `Cargo.toml`:

``` toml
[dependencies]
tree-sitter-graph = "0.10"
tree-sitter-graph = "0.11"
```

To use it as a program, install it via `cargo install`:
Expand Down
5 changes: 5 additions & 0 deletions src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub struct ExecutionConfig<'a, 'g> {
pub(crate) lazy: bool,
pub(crate) location_attr: Option<Identifier>,
pub(crate) variable_name_attr: Option<Identifier>,
pub(crate) match_node_attr: Option<Identifier>,
}

impl<'a, 'g> ExecutionConfig<'a, 'g> {
Expand All @@ -266,20 +267,23 @@ impl<'a, 'g> ExecutionConfig<'a, 'g> {
lazy: false,
location_attr: None,
variable_name_attr: None,
match_node_attr: None,
}
}

pub fn debug_attributes(
self,
location_attr: Identifier,
variable_name_attr: Identifier,
match_node_attr: Identifier,
) -> Self {
Self {
functions: self.functions,
globals: self.globals,
lazy: self.lazy,
location_attr: location_attr.into(),
variable_name_attr: variable_name_attr.into(),
match_node_attr: match_node_attr.into(),
}
}

Expand All @@ -290,6 +294,7 @@ impl<'a, 'g> ExecutionConfig<'a, 'g> {
lazy,
location_attr: self.location_attr,
variable_name_attr: self.variable_name_attr,
match_node_attr: self.match_node_attr,
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/execution/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl ast::File {
lazy: config.lazy,
location_attr: config.location_attr.clone(),
variable_name_attr: config.variable_name_attr.clone(),
match_node_attr: config.match_node_attr.clone(),
};

let mut locals = VariableMap::new();
Expand Down Expand Up @@ -136,6 +137,7 @@ struct ExecutionContext<'a, 'c, 'g, 'tree> {
locals: &'a mut dyn MutVariables<LazyValue>,
current_regex_captures: &'a Vec<String>,
mat: &'a QueryMatch<'a, 'tree>,
full_match_file_capture_index: usize,
store: &'a mut LazyStore,
scoped_store: &'a mut LazyScopedVariables,
lazy_graph: &'a mut LazyGraph,
Expand Down Expand Up @@ -201,6 +203,7 @@ impl ast::Stanza {
locals,
current_regex_captures: &current_regex_captures,
mat,
full_match_file_capture_index: self.full_match_file_capture_index,
store,
scoped_store,
lazy_graph,
Expand Down Expand Up @@ -265,6 +268,23 @@ impl ast::CreateGraphNode {
let graph_node = exec.graph.add_graph_node();
self.node
.add_debug_attrs(&mut exec.graph[graph_node].attributes, exec.config)?;
if let Some(match_node_attr) = &exec.config.match_node_attr {
let match_node = exec
.mat
.nodes_for_capture_index(exec.full_match_file_capture_index as u32)
.next()
.expect("missing capture for full match");
let syn_node = exec.graph.add_syntax_node(match_node);
exec.graph[graph_node]
.attributes
.add(match_node_attr.clone(), syn_node)
.map_err(|_| {
ExecutionError::DuplicateAttribute(format!(
" {} on graph node ({}) in {}",
match_node_attr, graph_node, self,
))
})?;
}
self.node.add_lazy(exec, graph_node.into(), false)
}
}
Expand Down Expand Up @@ -365,6 +385,7 @@ impl ast::Scan {
locals: &mut arm_locals,
current_regex_captures: &current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down Expand Up @@ -431,6 +452,7 @@ impl ast::If {
locals: &mut arm_locals,
current_regex_captures: exec.current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down Expand Up @@ -478,6 +500,7 @@ impl ast::ForIn {
locals: &mut loop_locals,
current_regex_captures: exec.current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down Expand Up @@ -572,6 +595,7 @@ impl ast::ListComprehension {
locals: &mut loop_locals,
current_regex_captures: exec.current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down Expand Up @@ -615,6 +639,7 @@ impl ast::SetComprehension {
locals: &mut loop_locals,
current_regex_captures: exec.current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down Expand Up @@ -830,6 +855,7 @@ impl ast::AttributeShorthand {
locals: &mut shorthand_locals,
current_regex_captures: exec.current_regex_captures,
mat: exec.mat,
full_match_file_capture_index: exec.full_match_file_capture_index,
store: exec.store,
scoped_store: exec.scoped_store,
lazy_graph: exec.lazy_graph,
Expand Down
26 changes: 26 additions & 0 deletions src/execution/strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl File {
lazy: config.lazy,
location_attr: config.location_attr.clone(),
variable_name_attr: config.variable_name_attr.clone(),
match_node_attr: config.match_node_attr.clone(),
};

let mut locals = VariableMap::new();
Expand Down Expand Up @@ -133,6 +134,7 @@ struct ExecutionContext<'a, 'c, 'g, 's, 'tree> {
current_regex_captures: &'a Vec<String>,
function_parameters: &'a mut Vec<Value>,
mat: &'a QueryMatch<'a, 'tree>,
full_match_stanza_capture_index: usize,
error_context: StatementContext,
inherited_variables: &'a HashSet<Identifier>,
shorthands: &'a AttributeShorthands,
Expand Down Expand Up @@ -192,6 +194,7 @@ impl Stanza {
current_regex_captures,
function_parameters,
mat: &mat,
full_match_stanza_capture_index: self.full_match_stanza_capture_index,
error_context,
inherited_variables,
shorthands,
Expand Down Expand Up @@ -283,6 +286,23 @@ impl CreateGraphNode {
let graph_node = exec.graph.add_graph_node();
self.node
.add_debug_attrs(&mut exec.graph[graph_node].attributes, exec.config)?;
if let Some(match_node_attr) = &exec.config.match_node_attr {
let match_node = exec
.mat
.nodes_for_capture_index(exec.full_match_stanza_capture_index as u32)
.next()
.expect("missing capture for full match");
let syn_node = exec.graph.add_syntax_node(match_node);
exec.graph[graph_node]
.attributes
.add(match_node_attr.clone(), syn_node)
.map_err(|_| {
ExecutionError::DuplicateAttribute(format!(
" {} on graph node ({}) in {}",
match_node_attr, graph_node, self,
))
})?;
}
let value = Value::GraphNode(graph_node);
self.node.add(exec, value, false)
}
Expand Down Expand Up @@ -408,6 +428,7 @@ impl Scan {
current_regex_captures: &current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down Expand Up @@ -468,6 +489,7 @@ impl If {
current_regex_captures: exec.current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down Expand Up @@ -510,6 +532,7 @@ impl ForIn {
current_regex_captures: exec.current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down Expand Up @@ -585,6 +608,7 @@ impl ListComprehension {
current_regex_captures: exec.current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down Expand Up @@ -625,6 +649,7 @@ impl SetComprehension {
current_regex_captures: exec.current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down Expand Up @@ -882,6 +907,7 @@ impl AttributeShorthand {
current_regex_captures: exec.current_regex_captures,
function_parameters: exec.function_parameters,
mat: exec.mat,
full_match_stanza_capture_index: exec.full_match_stanza_capture_index,
error_context: exec.error_context.clone(),
inherited_variables: exec.inherited_variables,
shorthands: exec.shorthands,
Expand Down

0 comments on commit 70ba65c

Please sign in to comment.