Skip to content

Commit

Permalink
update rust example (#124)
Browse files Browse the repository at this point in the history
Just a quick update to update the rust example.
  • Loading branch information
fdncred authored Oct 17, 2024
1 parent e3b4c96 commit 082a7c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
58 changes: 44 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ path = "bindings/rust/lib.rs"
# path = "src/main.rs"

[dev-dependencies]
cc = "1.0"
cc = "1.1.30"

[dependencies]
tree-sitter-language = "0.1.0"
tree-sitter = "~0.20.3"
tree-sitter-language = "0.1.2"
tree-sitter = "0.24.3"

[build-dependencies]
cc = "1.0"
cc = "1.1.30"
2 changes: 1 addition & 1 deletion bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
let src_dir = std::path::Path::new("src");

let mut c_config = cc::Build::new();
c_config.include(&src_dir);
c_config.include(src_dir);
c_config
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
Expand Down
14 changes: 10 additions & 4 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
extern crate tree_sitter_nu;
use tree_sitter::{Language, Parser, TreeCursor};

extern "C" {
fn tree_sitter_nu() -> Language;
}

fn main() {
let src = include_str!("../example-file.nu");
let nu_lang: Language = tree_sitter_nu::language();
let nu_lang = unsafe { tree_sitter_nu() };
let mut parser = Parser::new();
parser.set_language(nu_lang).unwrap();
let parse_tree = parser.parse(&src, None).unwrap();
parser.set_language(&nu_lang).unwrap();
let parse_tree = parser.parse(src, None).unwrap();
println!("\nTree Walk 1");
print_tree(&parse_tree);
println!("\nTree Walk 2");
Expand Down Expand Up @@ -76,7 +80,9 @@ fn walk_tree(cursor: &mut TreeCursor, source: &str) {
let start_pos = node.start_position();
let end_pos = node.end_position();
let kind = node.kind().trim();
let field_id = cursor.field_id().unwrap_or(0);
let field_id = cursor
.field_id()
.unwrap_or(core::num::NonZero::new(1).unwrap());
let field_name = cursor.field_name().unwrap_or("");
let node_string = node.utf8_text(source.as_bytes()).unwrap();
println!(
Expand Down

0 comments on commit 082a7c7

Please sign in to comment.