Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: tree-sitter like traverse API #2

Merged
merged 24 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
66718dd
reorder methods
lemonadern Dec 11, 2024
58304db
feat: implement Display for Range
lemonadern Dec 11, 2024
4ab1be4
feat: implement child_count
lemonadern Dec 11, 2024
eb926f4
feat: implement tree-sitter like traverse API
lemonadern Dec 11, 2024
f316107
feat: implement relationship between Nodes and their Range
lemonadern Dec 18, 2024
1353b71
checkpoint: range conversion worked
lemonadern Dec 18, 2024
a5c9c8d
implement Display trait for Range
lemonadern Dec 18, 2024
37a3a08
comment out legacy node lists
lemonadern Dec 18, 2024
5ce503c
wip: implement tree conversion (includes lifetime error)
lemonadern Dec 18, 2024
210cc1a
remove comments that is not needed
lemonadern Dec 20, 2024
01edb95
Change the interface of the function that gets the Cursor
lemonadern Dec 20, 2024
f1b54cf
Change the structure of Range
lemonadern Dec 20, 2024
5c10dc8
style: fmt
lemonadern Dec 20, 2024
86f9997
add tests
lemonadern Dec 20, 2024
1a3e55d
add flatten nodes
lemonadern Dec 20, 2024
16d1fed
fix test failure
lemonadern Dec 20, 2024
8f93acb
move traverse_pre_order to convert.rs
lemonadern Dec 20, 2024
d8b9272
apply clippy
lemonadern Dec 20, 2024
5b4bb41
remove TreeCursor::goto_direct_prev_sibling method
lemonadern Dec 25, 2024
34457f0
Delete unnecessary comment, method, function, and test
lemonadern Dec 25, 2024
1d688ac
Move the tree_sitter_like_traverse test to examples dir
lemonadern Dec 25, 2024
cf0bb49
Change to use the built-in traverse_pre_order of cstree
lemonadern Dec 25, 2024
1983b1b
Add select_clause to the node to be removed
lemonadern Dec 25, 2024
8dce661
implement Node::walk()
lemonadern Dec 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/postgresql-cst-parser/examples/tree_sitter_like.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use postgresql_cst_parser::{
parse,
tree_sitter::{as_tree_sitter_cursor, get_ts_tree_and_range_map, TreeCursor},
};

fn main() {
let src = r#"
-- comment
SELECT
1 as X
, 2 -- comment
, 3
FROM
A
, B
;
select
1
, 2
;

"#;

let node = parse(&src).unwrap();
let (node, range_map) = get_ts_tree_and_range_map(&src, &node);
let mut cursor = as_tree_sitter_cursor(src, &node, range_map);

visit(&mut cursor, 0, &src);
}

const UNIT: usize = 2;
fn visit(cursor: &mut TreeCursor, depth: usize, src: &str) {
(0..(depth * UNIT)).for_each(|_| print!("-"));

print!("{}", cursor.node().kind());

if cursor.node().child_count() == 0 {
print!(" \"{}\"", cursor.node().text().escape_debug());
}
println!(" {}", cursor.node().range());

if cursor.goto_first_child() {
visit(cursor, depth + 1, src);
while cursor.goto_next_sibling() {
visit(cursor, depth + 1, src);
}
cursor.goto_parent();
}
}
Loading