summaryrefslogtreecommitdiff
path: root/stag/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'stag/src/main.rs')
-rw-r--r--stag/src/main.rs72
1 files changed, 53 insertions, 19 deletions
diff --git a/stag/src/main.rs b/stag/src/main.rs
index eae6fe9..1945a03 100644
--- a/stag/src/main.rs
+++ b/stag/src/main.rs
@@ -1,5 +1,3 @@
1use std::collections::VecDeque;
2
3use serde::Deserialize; 1use serde::Deserialize;
4use serde::Serialize; 2use serde::Serialize;
5use tree_sitter::Parser; 3use tree_sitter::Parser;
@@ -19,11 +17,14 @@ use petgraph::{graph::NodeIndex, visit::EdgeRef, Direction, Graph};
19 17
20fn main() { 18fn main() {
21 let scopes = std::fs::read_to_string("src/stag.scm").unwrap(); 19 let scopes = std::fs::read_to_string("src/stag.scm").unwrap();
22 let src = r#"fn main() { 20 let src = r#"
23 let a = 3; 21 fn main() {
24 let b = 4; 22 let x = 2;
25 a + b 23 let a = 5;
26} 24 if let _ = z {
25 a[x];
26 }
27 }
27 "#; 28 "#;
28 29
29 let mut parser = Parser::new(); 30 let mut parser = Parser::new();
@@ -55,6 +56,15 @@ fn main() {
55 sg = build_scope_graph(graph, sg); 56 sg = build_scope_graph(graph, sg);
56 57
57 println!("{:#?}", sg); 58 println!("{:#?}", sg);
59
60 for e in sg
61 .graph
62 .raw_edges()
63 .iter()
64 .filter(|e| e.weight == EdgeKind::RefToDef)
65 {
66 println!("{:?} -> {:?}", e.source(), e.target());
67 }
58} 68}
59 69
60fn range_to_value(value: &tree_sitter::Range) -> Value { 70fn range_to_value(value: &tree_sitter::Range) -> Value {
@@ -111,11 +121,12 @@ fn is_ref(node: &tree_sitter_graph::graph::GraphNode) -> bool {
111 121
112pub struct ScopeShorthand; 122pub struct ScopeShorthand;
113 123
124#[allow(unused_must_use)]
114impl Function for ScopeShorthand { 125impl Function for ScopeShorthand {
115 fn call( 126 fn call(
116 &self, 127 &self,
117 graph: &mut tree_sitter_graph::graph::Graph, 128 graph: &mut tree_sitter_graph::graph::Graph,
118 source: &str, 129 _source: &str,
119 parameters: &mut dyn tree_sitter_graph::functions::Parameters, 130 parameters: &mut dyn tree_sitter_graph::functions::Parameters,
120 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> { 131 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> {
121 let target_range = parameters.param()?; 132 let target_range = parameters.param()?;
@@ -140,6 +151,7 @@ impl Function for ScopeShorthand {
140 151
141pub struct DefShorthand; 152pub struct DefShorthand;
142 153
154#[allow(unused_must_use)]
143impl Function for DefShorthand { 155impl Function for DefShorthand {
144 fn call( 156 fn call(
145 &self, 157 &self,
@@ -149,6 +161,10 @@ impl Function for DefShorthand {
149 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> { 161 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> {
150 let target_node = parameters.param()?.into_syntax_node_ref()?; 162 let target_node = parameters.param()?.into_syntax_node_ref()?;
151 let ts_node = graph[target_node]; 163 let ts_node = graph[target_node];
164 let symbol = parameters
165 .param()
166 .and_then(|p| p.as_str().map(ToOwned::to_owned))
167 .ok();
152 parameters.finish()?; 168 parameters.finish()?;
153 169
154 let graph_node = graph.add_graph_node(); 170 let graph_node = graph.add_graph_node();
@@ -158,6 +174,12 @@ impl Function for DefShorthand {
158 graph[graph_node] 174 graph[graph_node]
159 .attributes 175 .attributes
160 .add::<String>(Identifier::from("scope"), "local".into()); 176 .add::<String>(Identifier::from("scope"), "local".into());
177
178 if let Some(s) = symbol {
179 graph[graph_node]
180 .attributes
181 .add::<String>(Identifier::from("symbol"), s.into());
182 }
161 graph[graph_node].attributes.add::<String>( 183 graph[graph_node].attributes.add::<String>(
162 Identifier::from("text"), 184 Identifier::from("text"),
163 source[ts_node.byte_range()].to_string().into(), 185 source[ts_node.byte_range()].to_string().into(),
@@ -178,6 +200,7 @@ impl Function for DefShorthand {
178 200
179pub struct RefShortHand; 201pub struct RefShortHand;
180 202
203#[allow(unused_must_use)]
181impl Function for RefShortHand { 204impl Function for RefShortHand {
182 fn call( 205 fn call(
183 &self, 206 &self,
@@ -220,17 +243,29 @@ impl Function for CoverRanges {
220 _source: &str, 243 _source: &str,
221 parameters: &mut dyn tree_sitter_graph::functions::Parameters, 244 parameters: &mut dyn tree_sitter_graph::functions::Parameters,
222 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> { 245 ) -> Result<tree_sitter_graph::graph::Value, ExecutionError> {
223 let node_a = parameters.param()?.into_syntax_node_ref()?; 246 let p1 = parameters.param()?;
224 let node_b = parameters.param()?.into_syntax_node_ref()?; 247 let p2 = parameters.param()?;
225 let ts_node_a = graph[node_a]; 248
226 let ts_node_b = graph[node_b]; 249 match (p1.is_null(), p2.is_null()) {
227 250 (true, true) => panic!("all nulls"),
228 let mut range = cover(ts_node_a.range(), ts_node_b.range()); 251 (false, true) => return Ok(range_to_value(&graph[p1.into_syntax_node_ref()?].range())),
229 while let Ok(param) = parameters.param() { 252 (true, false) => return Ok(range_to_value(&graph[p2.into_syntax_node_ref()?].range())),
230 range = cover(range, graph[param.into_syntax_node_ref()?].range()) 253 (false, false) => {
231 } 254 let node_a = p1.into_syntax_node_ref()?;
255 let node_b = p2.into_syntax_node_ref()?;
256 let ts_node_a = graph[node_a];
257 let ts_node_b = graph[node_b];
258
259 let mut range = cover(ts_node_a.range(), ts_node_b.range());
260 while let Ok(param) = parameters.param() {
261 if !param.is_null() {
262 range = cover(range, graph[param.into_syntax_node_ref()?].range())
263 }
264 }
232 265
233 Ok(range_to_value(&range)) 266 Ok(range_to_value(&range))
267 }
268 }
234 } 269 }
235} 270}
236 271
@@ -739,7 +774,6 @@ fn build_scope_graph(
739 mut scope_graph: ScopeGraph, 774 mut scope_graph: ScopeGraph,
740) -> ScopeGraph { 775) -> ScopeGraph {
741 let nodes = tsg.iter_nodes().collect::<Vec<_>>(); 776 let nodes = tsg.iter_nodes().collect::<Vec<_>>();
742 // insert scopes first
743 for node in nodes 777 for node in nodes
744 .iter() 778 .iter()
745 .map(|node_ref| &tsg[*node_ref]) 779 .map(|node_ref| &tsg[*node_ref])