1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
mod ffi; use std::fmt; use std::ffi::CStr; use std::marker::PhantomData; use std::os::raw::{c_char, c_int, c_void}; use std::ptr; pub type Language = *const ffi::TSLanguage; pub trait Utf16Input { fn read(&mut self) -> &[u16]; fn seek(&mut self, u32, Point); } pub trait Utf8Input { fn read(&mut self) -> &[u8]; fn seek(&mut self, u32, Point); } #[derive(Debug, PartialEq, Eq)] pub enum LogType { Parse, Lex, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Point { pub row: u32, pub column: u32, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct InputEdit { pub start_byte: u32, pub old_end_byte: u32, pub new_end_byte: u32, pub start_position: Point, pub old_end_position: Point, pub new_end_position: Point, } pub struct Node<'a>(ffi::TSNode, PhantomData<&'a ()>); pub struct Parser(*mut ffi::TSParser); pub struct Tree(*mut ffi::TSTree, ffi::TSInputEncoding); pub struct TreeCursor<'a>(ffi::TSTreeCursor, PhantomData<&'a ()>); struct FlatInput<'a> { bytes: &'a [u8], offset: usize, } impl Parser { pub fn new() -> Parser { unsafe { let parser = ffi::ts_parser_new(); Parser(parser) } } pub fn set_language(&mut self, language: Language) -> Result<(), String> { unsafe { let version = ffi::ts_language_version(language) as usize; if version == ffi::TREE_SITTER_LANGUAGE_VERSION { ffi::ts_parser_set_language(self.0, language); Ok(()) } else { Err(format!( "Incompatible language version {}. Expected {}.", version, ffi::TREE_SITTER_LANGUAGE_VERSION )) } } } pub fn set_logger<F: FnMut(LogType, &str) -> ()>(&mut self, logger: Option<&mut F>) { unsafe extern "C" fn log<F: FnMut(LogType, &str) -> ()>( payload: *mut c_void, c_log_type: ffi::TSLogType, c_message: *const c_char, ) { let callback = (payload as *mut F).as_mut().unwrap(); if let Ok(message) = CStr::from_ptr(c_message).to_str() { let log_type = if c_log_type == ffi::TSLogType_TSLogTypeParse { LogType::Parse } else { LogType::Lex }; callback(log_type, message); } }; let c_logger; if let Some(logger) = logger { c_logger = ffi::TSLogger { payload: logger as *mut F as *mut c_void, log: Some(log::<F>), }; } else { c_logger = ffi::TSLogger { payload: ptr::null_mut(), log: None }; } unsafe { ffi::ts_parser_set_logger(self.0, c_logger) }; } pub fn parse_str(&mut self, input: &str, old_tree: Option<Tree>) -> Option<Tree> { let mut input = FlatInput { bytes: input.as_bytes(), offset: 0}; self.parse_utf8(&mut input, old_tree) } pub fn parse_utf8<T: Utf8Input>( &mut self, input: &mut T, old_tree: Option<Tree>, ) -> Option<Tree> { unsafe extern "C" fn read<T: Utf8Input>( payload: *mut c_void, bytes_read: *mut u32, ) -> *const c_char { let input = (payload as *mut T).as_mut().unwrap(); let result = input.read(); *bytes_read = result.len() as u32; return result.as_ptr() as *const c_char; }; unsafe extern "C" fn seek<T: Utf8Input>( payload: *mut c_void, byte: u32, position: ffi::TSPoint, ) -> c_int { let input = (payload as *mut T).as_mut().unwrap(); input.seek( byte, Point { row: position.row, column: position.column, }, ); return 1; }; let c_input = ffi::TSInput { payload: input as *mut T as *mut c_void, read: Some(read::<T>), seek: Some(seek::<T>), encoding: ffi::TSInputEncoding_TSInputEncodingUTF8, }; let old_tree_ptr = old_tree.map_or(ptr::null_mut(), |t| t.0); let new_tree_ptr = unsafe { ffi::ts_parser_parse(self.0, old_tree_ptr, c_input) }; if new_tree_ptr.is_null() { None } else { Some(Tree(new_tree_ptr, ffi::TSInputEncoding_TSInputEncodingUTF8)) } } pub fn parse_utf16<T: Utf16Input>( &mut self, input: &mut T, old_tree: Option<Tree>, ) -> Option<Tree> { unsafe extern "C" fn read<T: Utf16Input>( payload: *mut c_void, bytes_read: *mut u32, ) -> *const c_char { let input = (payload as *mut T).as_mut().unwrap(); let result = input.read(); *bytes_read = result.len() as u32 * 2; return result.as_ptr() as *const c_char; }; unsafe extern "C" fn seek<T: Utf16Input>( payload: *mut c_void, byte: u32, position: ffi::TSPoint, ) -> c_int { let input = (payload as *mut T).as_mut().unwrap(); input.seek( byte / 2, Point { row: position.row, column: position.column / 2, }, ); return 1; }; let c_input = ffi::TSInput { payload: input as *mut T as *mut c_void, read: Some(read::<T>), seek: Some(seek::<T>), encoding: ffi::TSInputEncoding_TSInputEncodingUTF8, }; let old_tree_ptr = old_tree.map_or(ptr::null_mut(), |t| t.0); let new_tree_ptr = unsafe { ffi::ts_parser_parse(self.0, old_tree_ptr, c_input) }; if new_tree_ptr.is_null() { None } else { Some(Tree( new_tree_ptr, ffi::TSInputEncoding_TSInputEncodingUTF16, )) } } } impl Drop for Parser { fn drop(&mut self) { unsafe { ffi::ts_parser_delete(self.0) } } } impl Tree { pub fn root_node(&self) -> Node { Node::new(unsafe { ffi::ts_tree_root_node(self.0) }).unwrap() } pub fn edit(&mut self, edit: &InputEdit) { let edit = ffi::TSInputEdit { start_byte: edit.start_byte, old_end_byte: edit.old_end_byte, new_end_byte: edit.new_end_byte, start_point: edit.start_position.into(), old_end_point: edit.old_end_position.into(), new_end_point: edit.new_end_position.into(), }; unsafe { ffi::ts_tree_edit(self.0, &edit) }; } pub fn walk(&self) -> TreeCursor { TreeCursor(unsafe { ffi::ts_tree_cursor_new(self.0) }, PhantomData) } } impl Drop for Tree { fn drop(&mut self) { unsafe { ffi::ts_tree_delete(self.0) } } } impl Clone for Tree { fn clone(&self) -> Tree { unsafe { Tree(ffi::ts_tree_copy(self.0), self.1) } } } impl<'a> Node<'a> { fn new(node: ffi::TSNode) -> Option<Self> { if node.id.is_null() { None } else { Some(Node(node, PhantomData)) } } pub fn kind_id(&self) -> u16 { unsafe { ffi::ts_node_symbol(self.0) } } pub fn kind(&self) -> &'static str { unsafe { CStr::from_ptr(ffi::ts_node_type(self.0)) }.to_str().unwrap() } pub fn is_named(&self) -> bool { unsafe { ffi::ts_node_is_named(self.0) } } pub fn has_changes(&self) -> bool { unsafe { ffi::ts_node_has_changes(self.0) } } pub fn has_error(&self) -> bool { unsafe { ffi::ts_node_has_error(self.0) } } pub fn start_byte(&self) -> u32 { unsafe { ffi::ts_node_start_byte(self.0) } } pub fn end_byte(&self) -> u32 { unsafe { ffi::ts_node_end_byte(self.0) } } pub fn start_position(&self) -> Point { let result = unsafe { ffi::ts_node_start_point(self.0) }; Point { row: result.row, column: result.column, } } pub fn end_position(&self) -> Point { let result = unsafe { ffi::ts_node_end_point(self.0) }; Point { row: result.row, column: result.column, } } pub fn child(&self, i: u32) -> Option<Node> { Self::new(unsafe { ffi::ts_node_child(self.0, i) }) } pub fn child_count(&self) -> u32 { unsafe { ffi::ts_node_child_count(self.0) } } pub fn named_child(&self, i: u32) -> Option<Node> { Self::new(unsafe { ffi::ts_node_named_child(self.0, i) }) } pub fn named_child_count(&self) -> u32 { unsafe { ffi::ts_node_named_child_count(self.0) } } pub fn parent(&self) -> Option<Node> { Self::new(unsafe { ffi::ts_node_parent(self.0) }) } pub fn next_sibling(&self) -> Option<Node> { Self::new(unsafe { ffi::ts_node_next_sibling(self.0) }) } pub fn prev_sibling(&self) -> Option<Node> { Self::new(unsafe { ffi::ts_node_prev_sibling(self.0) }) } pub fn next_named_sibling(&self) -> Option<Node> { Self::new(unsafe { ffi::ts_node_next_named_sibling(self.0) }) } pub fn prev_named_sibling(&self) -> Option<Node> { Self::new(unsafe { ffi::ts_node_prev_named_sibling(self.0) }) } pub fn to_sexp(&self) -> String { extern "C" { fn free(pointer: *mut c_void); } let c_string = unsafe { ffi::ts_node_string(self.0) }; let result = unsafe { CStr::from_ptr(c_string) }.to_str().unwrap().to_string(); unsafe { free(c_string as *mut c_void) }; result } } impl<'a> PartialEq for Node<'a> { fn eq(&self, other: &Self) -> bool { self.0.id == other.0.id } } impl<'a> fmt::Debug for Node<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{{Node {} {} - {}}}", self.kind(), self.start_position(), self.end_position()) } } impl<'a> TreeCursor<'a> { pub fn node(&'a self) -> Node<'a> { Node( unsafe { ffi::ts_tree_cursor_current_node(&self.0) }, PhantomData, ) } pub fn goto_first_child(&mut self) -> bool { return unsafe { ffi::ts_tree_cursor_goto_first_child(&mut self.0) }; } pub fn goto_parent(&mut self) -> bool { return unsafe { ffi::ts_tree_cursor_goto_parent(&mut self.0) }; } pub fn goto_next_sibling(&mut self) -> bool { return unsafe { ffi::ts_tree_cursor_goto_next_sibling(&mut self.0) }; } pub fn goto_first_child_for_index(&mut self, index: u32) -> Option<u32> { let result = unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index) }; if result < 0 { None } else { Some(result as u32) } } } impl<'a> Drop for TreeCursor<'a> { fn drop(&mut self) { unsafe { ffi::ts_tree_cursor_delete(&mut self.0) } } } impl fmt::Display for Point { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "({}, {})", self.row, self.column) } } impl Into<ffi::TSPoint> for Point { fn into(self) -> ffi::TSPoint { ffi::TSPoint { row: self.row, column: self.column, } } } impl<'a> Utf8Input for FlatInput<'a> { fn read(&mut self) -> &[u8] { let result = &self.bytes[self.offset..]; self.offset = self.bytes.len(); result } fn seek(&mut self, offset: u32, _position: Point) { self.offset = offset as usize; } } #[cfg(test)] mod tests { use super::*; fn rust() -> Language { unsafe { tree_sitter_rust() } } extern "C" { fn tree_sitter_rust() -> Language; } #[test] fn test_basic_parsing() { let mut parser = Parser::new(); parser.set_language(rust()).unwrap(); let tree = parser.parse_str(" struct Stuff {} fn main() {} ", None).unwrap(); let root_node = tree.root_node(); assert_eq!(root_node.kind(), "source_file"); assert_eq!( root_node.to_sexp(), "(source_file (struct_item (type_identifier) (field_declaration_list)) (function_item (identifier) (parameters) (block)))" ); let struct_node = root_node.child(0).unwrap(); assert_eq!(struct_node.kind(), "struct_item"); } #[test] fn test_logging() { let mut parser = Parser::new(); parser.set_language(rust()).unwrap(); let mut messages = Vec::new(); parser.set_logger(Some(&mut |log_type, message| { messages.push((log_type, message.to_string())); })); parser.parse_str(" struct Stuff {} fn main() {} ", None).unwrap(); assert!(messages.contains(&(LogType::Parse, "reduce sym:struct_item, child_count:3".to_string()))); assert!(messages.contains(&(LogType::Lex, "skip character:' '".to_string()))); } #[test] fn test_tree_cursor() { let mut parser = Parser::new(); parser.set_language(rust()).unwrap(); let tree = parser.parse_str(" struct Stuff { a: A; b: Option<B>, } ", None).unwrap(); let mut cursor = tree.walk(); assert_eq!(cursor.node().kind(), "source_file"); assert!(cursor.goto_first_child()); assert_eq!(cursor.node().kind(), "struct_item"); assert!(cursor.goto_first_child()); assert_eq!(cursor.node().kind(), "struct"); assert_eq!(cursor.node().is_named(), false); assert!(cursor.goto_next_sibling()); assert_eq!(cursor.node().kind(), "type_identifier"); assert_eq!(cursor.node().is_named(), true); assert!(cursor.goto_next_sibling()); assert_eq!(cursor.node().kind(), "field_declaration_list"); assert_eq!(cursor.node().is_named(), true); } #[test] fn test_custom_utf8_input() { struct LineBasedInput { lines: &'static [&'static str], row: usize, column: usize, } impl Utf8Input for LineBasedInput { fn read(&mut self) -> &[u8] { if self.row < self.lines.len() { let result = &self.lines[self.row].as_bytes()[self.column..]; self.row += 1; self.column = 0; result } else { &[] } } fn seek(&mut self, _byte: u32, position: Point) { self.row = position.row as usize; self.column = position.column as usize; } } let mut parser = Parser::new(); parser.set_language(rust()).unwrap(); let mut input = LineBasedInput { lines: &[ "pub fn main() {", "}", ], row: 0, column: 0 }; let tree = parser.parse_utf8(&mut input, None).unwrap(); let root = tree.root_node(); assert_eq!(root.kind(), "source_file"); assert_eq!(root.has_error(), false); let child = root.child(0).unwrap(); assert_eq!(child.kind(), "function_item"); } #[test] fn test_node_equality() { let mut parser = Parser::new(); parser.set_language(rust()).unwrap(); let tree = parser.parse_str("struct A {}", None).unwrap(); let node1 = tree.root_node(); let node2 = tree.root_node(); assert_eq!(node1, node2); assert_eq!(node1.child(0).unwrap(), node2.child(0).unwrap()); assert_ne!(node1.child(0).unwrap(), node2); } }