From 433b20ff8aab22623ddaa87ed273dfdb0f3b78f7 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Sat, 4 Sep 2021 11:04:30 -0700 Subject: [PATCH] Ensure that LHS and RHS syntax nodes have different IDs --- src/dijkstra.rs | 28 ++++++++++------------------ src/main.rs | 7 +++---- src/syntax.rs | 13 +++++++++++-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/dijkstra.rs b/src/dijkstra.rs index ea57f6370..cf061b0ef 100644 --- a/src/dijkstra.rs +++ b/src/dijkstra.rs @@ -609,7 +609,6 @@ mod tests { "]", pos_helper(2), )]; - init_info(&lhs); let rhs = vec![Syntax::new_list( &arena, @@ -619,7 +618,7 @@ mod tests { "]", pos_helper(2), )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -653,7 +652,6 @@ mod tests { "]", pos_helper(2), )]; - init_info(&lhs); let rhs = vec![Syntax::new_list( &arena, @@ -666,7 +664,7 @@ mod tests { "]", pos_helper(3), )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -704,7 +702,6 @@ mod tests { "]", pos_helper(4), )]; - init_info(&lhs); let rhs = vec![Syntax::new_list( &arena, @@ -717,7 +714,7 @@ mod tests { "}", pos_helper(4), )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -752,10 +749,9 @@ mod tests { Syntax::new_atom(&arena, col_helper(2, 0), "bar"), Syntax::new_atom(&arena, col_helper(2, 1), "foo"), ]; - init_info(&lhs); let rhs = vec![Syntax::new_atom(&arena, col_helper(1, 0), "foo")]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -790,9 +786,9 @@ mod tests { "]", pos_helper(2), )]; - init_info(&lhs); let rhs = vec![]; + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -827,9 +823,9 @@ mod tests { ), Syntax::new_atom(&arena, col_helper(2, 2), ";"), ]; - init_info(&lhs); let rhs = vec![]; + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -884,7 +880,6 @@ mod tests { "]", pos_helper(100), )]; - init_info(&lhs); let rhs = vec![Syntax::new_list( &arena, @@ -917,7 +912,7 @@ mod tests { "]", pos_helper(100), )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -950,14 +945,13 @@ mod tests { pos_helper(1), "the quick brown fox", )]; - init_info(&lhs); let rhs = vec![Syntax::new_comment( &arena, pos_helper(1), "the quick brown cat", )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -985,10 +979,9 @@ mod tests { pos_helper(1), "the quick brown fox", )]; - init_info(&lhs); let rhs = vec![Syntax::new_comment(&arena, pos_helper(1), "foo bar")]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), @@ -1015,14 +1008,13 @@ mod tests { Syntax::new_comment(&arena, pos_helper(1), "the quick brown fox"), Syntax::new_comment(&arena, pos_helper(2), "the quick brown thing"), ]; - init_info(&lhs); let rhs = vec![Syntax::new_comment( &arena, pos_helper(1), "the quick brown fox.", )]; - init_info(&rhs); + init_info(&lhs, &rhs); let start = Vertex { lhs_syntax: lhs.get(0).copied(), diff --git a/src/main.rs b/src/main.rs index 169e0b5c4..9aef319e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use typed_arena::Arena; use crate::dijkstra::mark_syntax; use crate::files::{is_probably_binary, read_or_die}; use crate::lines::{join_overlapping, visible_groups, MaxLine}; -use crate::syntax::{change_positions, init_info, matching_lines}; +use crate::syntax::{change_positions, init_info, init_info_single, matching_lines}; use crate::tree_sitter_parser as tsp; extern crate pretty_env_logger; @@ -183,7 +183,7 @@ fn main() { let bytes = read_or_die(&path); let src = String::from_utf8_lossy(&bytes).to_string(); let ast = tsp::parse(&arena, &src, &ts_lang); - init_info(&ast); + init_info_single(&ast, 0); println!("{:#?}", ast); } None => { @@ -249,8 +249,7 @@ fn main() { println!("{}", style::header(&display_path, &lang_name)); - init_info(&lhs); - init_info(&rhs); + init_info(&lhs, &rhs); mark_syntax(lhs.get(0).copied(), rhs.get(0).copied()); let lhs_positions = change_positions(&lhs_src, &rhs_src, &lhs); diff --git a/src/syntax.rs b/src/syntax.rs index f3463f14d..e0fa8698a 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -444,11 +444,20 @@ impl<'a> Syntax<'a> { } } -pub fn init_info<'a>(roots: &[&'a Syntax<'a>]) { - set_unique_id(roots, 0); +pub fn init_info<'a>(lhs_roots: &[&'a Syntax<'a>], rhs_roots: &[&'a Syntax<'a>]) { + let next_id = init_info_single(lhs_roots, 0); + init_info_single(rhs_roots, next_id); +} + +/// Initialise all the fields in SyntaxInfo. +/// +/// Return the next unique ID available, so we can ensure LHS and RHS +/// have different IDs. +pub fn init_info_single<'a>(roots: &[&'a Syntax<'a>], first_id: u64) -> u64 { set_next(roots, None); set_prev(roots, None); set_num_ancestors(roots, 0); + set_unique_id(roots, first_id) } fn set_unique_id<'a>(nodes: &[&'a Syntax<'a>], prev_id: u64) -> u64 {