An incremental parsing system for programming tools https://tree-sitter.github.io
Find a file
2018-12-13 16:30:40 -08:00
fixtures Get basic parsing working, add some unit tests 2018-05-17 17:16:35 -07:00
script Add appveyor config 2018-05-18 09:58:34 -07:00
src Add PropertySheet::map method 2018-12-13 16:30:40 -08:00
vendor Upgrade Tree-sitter, use single source file in build script 2018-11-20 15:56:16 -08:00
.gitignore Get basic parsing working, add some unit tests 2018-05-17 17:16:35 -07:00
.gitmodules Initial commit 2018-05-17 14:30:03 -07:00
.travis.yml Switch back to default c compiler on travis 2018-05-18 10:55:42 -07:00
appveyor.yml Fetch submodules on appveyor 2018-05-18 10:16:28 -07:00
build.rs Upgrade Tree-sitter, use single source file in build script 2018-11-20 15:56:16 -08:00
Cargo.toml 0.3.4 2018-12-11 10:35:03 -08:00
LICENSE Add boilerplate 2018-05-17 14:46:29 -07:00
README.md README.md: small fixes 2018-06-28 10:25:01 +02:00

Rust Tree-sitter

Build Status Build status Crates.io

Rust bindings to the Tree-sitter parsing library.

Basic Usage

First, create a parser:

use tree_sitter::{Parser, Language};

// ...

let mut parser = Parser::new();

Then assign a language to the parser. Tree-sitter languages consist of generated C code. To use them from rust, you must declare them as extern "C" functions and invoke them with unsafe:

extern "C" { fn tree_sitter_c() -> Language; }
extern "C" { fn tree_sitter_rust() -> Language; }
extern "C" { fn tree_sitter_javascript() -> Language; }

let language = unsafe { tree_sitter_rust() };
parser.set_language(language).unwrap();

Now you can parse source code:

let source_code = "fn test() {}";
let tree = parser.parse_str(source_code, None);
let root_node = tree.root_node();

assert_eq!(root_node.kind(), "source_file");
assert_eq!(root_node.start_position().column, 0);
assert_eq!(root_node.end_position().column, 12);

Editing

Once you have a syntax tree, you can update it when your source code changes. Passing in the previous edited tree makes parse run much more quickly:

let new_source_code = "fn test(a: u32) {}"

tree.edit(InputEdit {
  start_byte: 8,
  old_end_byte: 8,
  new_end_byte: 14,
  start_position: Point::new(0, 8),
  old_end_position: Point::new(0, 8),
  new_end_position: Point::new(0, 14),
});

let new_tree = parser.parse_str(new_source_code, Some(&tree));

Text Input

The source code to parse can be provided either as a string or as a function that returns text encoded as either UTF8 or UTF16:

// Store some source code in an array of lines.
let lines = &[
    "pub fn foo() {",
    "  1",
    "}",
];

// Parse the source code using a custom callback. The callback is called
// with both a byte offset and a row/column offset.
let tree = parser.parse_utf8(&mut |_byte: u32, position: Point| -> &[u8] {
    let row = position.row as usize;
    let column = position.column as usize;
    if row < lines.len() {
        if column < lines[row].as_bytes().len() {
            &lines[row].as_bytes()[column..]
        } else {
            "\n".as_bytes()
        }
    } else {
        &[]
    }
}, None).unwrap();

assert_eq!(
  tree.root_node().to_sexp(),
  "(source_file (function_item (visibility_modifier) (identifier) (parameters) (block (number_literal))))"
);