|
| 1 | +extern crate tree_sitter_nu; |
| 2 | +use tree_sitter::{Language, Parser, TreeCursor}; |
| 3 | + |
| 4 | +extern "C" { |
| 5 | + fn tree_sitter_nu() -> Language; |
| 6 | +} |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let src = include_str!("../example-file.nu"); |
| 10 | + let nu_lang = unsafe { tree_sitter_nu() }; |
| 11 | + let mut parser = Parser::new(); |
| 12 | + parser.set_language(&nu_lang).unwrap(); |
| 13 | + let parse_tree = parser.parse(src, None).unwrap(); |
| 14 | + println!("\nTree Walk 1"); |
| 15 | + print_tree(&parse_tree); |
| 16 | + println!("\nTree Walk 2"); |
| 17 | + let mut tree_cursor = parse_tree.walk(); |
| 18 | + walk_tree(&mut tree_cursor, src); |
| 19 | + println!("\n{}", parse_tree.root_node().to_sexp()); |
| 20 | + |
| 21 | + // let query = Query::new( |
| 22 | + // nu_lang, |
| 23 | + // r#" |
| 24 | + // ((custom_command |
| 25 | + // custom_command_name: (identifier) |
| 26 | + // )) |
| 27 | + // "# |
| 28 | + // ).unwrap(); |
| 29 | + |
| 30 | + // let mut query_cursor = QueryCursor::new(); |
| 31 | + // let all_matches = query_cursor.matches(&query, parse_tree.root_node(), src.as_bytes()); |
| 32 | + // println!("matches: {:?}", all_matches.count()); |
| 33 | +} |
| 34 | + |
| 35 | +pub fn print_tree(tree: &tree_sitter::Tree) { |
| 36 | + let mut cursor = tree.walk(); |
| 37 | + print_cursor(&mut cursor, 0); |
| 38 | +} |
| 39 | + |
| 40 | +fn print_cursor(cursor: &mut TreeCursor, depth: usize) { |
| 41 | + loop { |
| 42 | + let node = cursor.node(); |
| 43 | + println!("{}{:#?}", " ".repeat(depth), node); |
| 44 | + |
| 45 | + if cursor.goto_first_child() { |
| 46 | + print_cursor(cursor, depth + 1); |
| 47 | + cursor.goto_parent(); |
| 48 | + } |
| 49 | + |
| 50 | + if !cursor.goto_next_sibling() { |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn walk_tree(cursor: &mut TreeCursor, source: &str) { |
| 57 | + // let node = cursor.node(); |
| 58 | + // let start_pos = node.start_position(); |
| 59 | + // let end_pos = node.end_position(); |
| 60 | + // let kind = node.kind().trim(); |
| 61 | + // let field_id = cursor.field_id().unwrap_or(0); |
| 62 | + // let field_name = cursor.field_name().unwrap_or(""); |
| 63 | + // let node_string = node.utf8_text(source.as_bytes()).unwrap(); |
| 64 | + // println!( |
| 65 | + // "id: [{}], name: [{}], kind: [{}], start_pos: [{} {}] end_pos: [{} {}], node_text: [{}]", |
| 66 | + // field_id, |
| 67 | + // field_name, |
| 68 | + // kind, |
| 69 | + // start_pos.row, |
| 70 | + // start_pos.column, |
| 71 | + // end_pos.row, |
| 72 | + // end_pos.column, |
| 73 | + // node_string |
| 74 | + // ); |
| 75 | + |
| 76 | + if cursor.goto_first_child() { |
| 77 | + loop { |
| 78 | + walk_tree(cursor, source); |
| 79 | + let node = cursor.node(); |
| 80 | + let start_pos = node.start_position(); |
| 81 | + let end_pos = node.end_position(); |
| 82 | + let kind = node.kind().trim(); |
| 83 | + let field_id = cursor |
| 84 | + .field_id() |
| 85 | + .unwrap_or(core::num::NonZero::new(1).unwrap()); |
| 86 | + let field_name = cursor.field_name().unwrap_or(""); |
| 87 | + let node_string = node.utf8_text(source.as_bytes()).unwrap(); |
| 88 | + println!( |
| 89 | + "id: [{}], name: [{}], kind: [{}], start_pos: [{} {}] end_pos: [{} {}], node_text: [{}]", |
| 90 | + field_id, |
| 91 | + field_name, |
| 92 | + kind, |
| 93 | + start_pos.row, |
| 94 | + start_pos.column, |
| 95 | + end_pos.row, |
| 96 | + end_pos.column, |
| 97 | + node_string |
| 98 | + ); |
| 99 | + |
| 100 | + if !cursor.goto_next_sibling() { |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + cursor.goto_parent(); |
| 105 | + } |
| 106 | +} |
0 commit comments