Reorganize language bindings

* Move rust binding: lib/binding -> lib/binding_rust
* Move wasm bindinig: lib/web -> lib/binding_web
* Add wasm readme
This commit is contained in:
Max Brunsfeld 2019-05-07 10:27:45 -07:00
parent a3ceb8f3a5
commit 3fc459a84b
23 changed files with 125 additions and 18 deletions

View file

@ -0,0 +1,98 @@
Rust Tree-sitter
================
[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter.svg?branch=master)](https://travis-ci.org/tree-sitter/tree-sitter)
[![Build status](https://ci.appveyor.com/api/projects/status/vtmbd6i92e97l55w/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter/branch/master)
[![Crates.io](https://img.shields.io/crates/v/tree-sitter.svg)](https://crates.io/crates/tree-sitter)
Rust bindings to the [Tree-sitter][] parsing library.
### Basic Usage
First, create a parser:
```rust
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`:
```rust
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:
```rust
let source_code = "fn test() {}";
let tree = parser.parse(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:
```rust
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(new_source_code, Some(&tree));
```
### Text Input
The source code to parse can be provided either either as a string, a slice, a vector, or as a function that returns a slice. The text can be encoded as either UTF8 or UTF16:
```rust
// 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_with(&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))))"
);
```
[tree-sitter]: https://github.com/tree-sitter/tree-sitter

View file

@ -0,0 +1,359 @@
/* automatically generated by rust-bindgen */
pub type __darwin_size_t = ::std::os::raw::c_ulong;
pub type FILE = [u64; 19usize];
pub type TSSymbol = u16;
pub type TSFieldId = u16;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSLanguage {
_unused: [u8; 0],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSParser {
_unused: [u8; 0],
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSTree {
_unused: [u8; 0],
}
pub const TSInputEncoding_TSInputEncodingUTF8: TSInputEncoding = 0;
pub const TSInputEncoding_TSInputEncodingUTF16: TSInputEncoding = 1;
pub type TSInputEncoding = u32;
pub const TSSymbolType_TSSymbolTypeRegular: TSSymbolType = 0;
pub const TSSymbolType_TSSymbolTypeAnonymous: TSSymbolType = 1;
pub const TSSymbolType_TSSymbolTypeAuxiliary: TSSymbolType = 2;
pub type TSSymbolType = u32;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSPoint {
pub row: u32,
pub column: u32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSRange {
pub start_point: TSPoint,
pub end_point: TSPoint,
pub start_byte: u32,
pub end_byte: u32,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSInput {
pub payload: *mut ::std::os::raw::c_void,
pub read: ::std::option::Option<
unsafe extern "C" fn(
payload: *mut ::std::os::raw::c_void,
byte_index: u32,
position: TSPoint,
bytes_read: *mut u32,
) -> *const ::std::os::raw::c_char,
>,
pub encoding: TSInputEncoding,
}
pub const TSLogType_TSLogTypeParse: TSLogType = 0;
pub const TSLogType_TSLogTypeLex: TSLogType = 1;
pub type TSLogType = u32;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSLogger {
pub payload: *mut ::std::os::raw::c_void,
pub log: ::std::option::Option<
unsafe extern "C" fn(
payload: *mut ::std::os::raw::c_void,
arg1: TSLogType,
arg2: *const ::std::os::raw::c_char,
),
>,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSInputEdit {
pub start_byte: u32,
pub old_end_byte: u32,
pub new_end_byte: u32,
pub start_point: TSPoint,
pub old_end_point: TSPoint,
pub new_end_point: TSPoint,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSNode {
pub context: [u32; 4usize],
pub id: *const ::std::os::raw::c_void,
pub tree: *const TSTree,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct TSTreeCursor {
pub tree: *const ::std::os::raw::c_void,
pub id: *const ::std::os::raw::c_void,
pub context: [u32; 2usize],
}
extern "C" {
pub fn ts_parser_new() -> *mut TSParser;
}
extern "C" {
pub fn ts_parser_delete(arg1: *mut TSParser);
}
extern "C" {
pub fn ts_parser_language(arg1: *const TSParser) -> *const TSLanguage;
}
extern "C" {
pub fn ts_parser_set_language(arg1: *mut TSParser, arg2: *const TSLanguage) -> bool;
}
extern "C" {
pub fn ts_parser_logger(arg1: *const TSParser) -> TSLogger;
}
extern "C" {
pub fn ts_parser_set_logger(arg1: *mut TSParser, arg2: TSLogger);
}
extern "C" {
pub fn ts_parser_print_dot_graphs(arg1: *mut TSParser, arg2: ::std::os::raw::c_int);
}
extern "C" {
pub fn ts_parser_halt_on_error(arg1: *mut TSParser, arg2: bool);
}
extern "C" {
pub fn ts_parser_parse(arg1: *mut TSParser, arg2: *const TSTree, arg3: TSInput) -> *mut TSTree;
}
extern "C" {
pub fn ts_parser_parse_string(
arg1: *mut TSParser,
arg2: *const TSTree,
arg3: *const ::std::os::raw::c_char,
arg4: u32,
) -> *mut TSTree;
}
extern "C" {
pub fn ts_parser_parse_string_encoding(
arg1: *mut TSParser,
arg2: *const TSTree,
arg3: *const ::std::os::raw::c_char,
arg4: u32,
arg5: TSInputEncoding,
) -> *mut TSTree;
}
extern "C" {
pub fn ts_parser_cancellation_flag(arg1: *const TSParser) -> *const usize;
}
extern "C" {
pub fn ts_parser_set_cancellation_flag(arg1: *mut TSParser, arg2: *const usize);
}
extern "C" {
pub fn ts_parser_timeout_micros(arg1: *const TSParser) -> u64;
}
extern "C" {
pub fn ts_parser_set_timeout_micros(arg1: *mut TSParser, arg2: u64);
}
extern "C" {
pub fn ts_parser_reset(arg1: *mut TSParser);
}
extern "C" {
pub fn ts_parser_set_included_ranges(arg1: *mut TSParser, arg2: *const TSRange, arg3: u32);
}
extern "C" {
pub fn ts_parser_included_ranges(arg1: *const TSParser, arg2: *mut u32) -> *const TSRange;
}
extern "C" {
pub fn ts_tree_copy(arg1: *const TSTree) -> *mut TSTree;
}
extern "C" {
pub fn ts_tree_delete(arg1: *mut TSTree);
}
extern "C" {
pub fn ts_tree_root_node(arg1: *const TSTree) -> TSNode;
}
extern "C" {
pub fn ts_tree_edit(arg1: *mut TSTree, arg2: *const TSInputEdit);
}
extern "C" {
pub fn ts_tree_get_changed_ranges(
arg1: *const TSTree,
arg2: *const TSTree,
arg3: *mut u32,
) -> *mut TSRange;
}
extern "C" {
pub fn ts_tree_print_dot_graph(arg1: *const TSTree, arg2: *mut FILE);
}
extern "C" {
pub fn ts_tree_language(arg1: *const TSTree) -> *const TSLanguage;
}
extern "C" {
pub fn ts_node_start_byte(arg1: TSNode) -> u32;
}
extern "C" {
pub fn ts_node_start_point(arg1: TSNode) -> TSPoint;
}
extern "C" {
pub fn ts_node_end_byte(arg1: TSNode) -> u32;
}
extern "C" {
pub fn ts_node_end_point(arg1: TSNode) -> TSPoint;
}
extern "C" {
pub fn ts_node_symbol(arg1: TSNode) -> TSSymbol;
}
extern "C" {
pub fn ts_node_type(arg1: TSNode) -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn ts_node_string(arg1: TSNode) -> *mut ::std::os::raw::c_char;
}
extern "C" {
pub fn ts_node_eq(arg1: TSNode, arg2: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_is_null(arg1: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_is_named(arg1: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_is_missing(arg1: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_has_changes(arg1: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_has_error(arg1: TSNode) -> bool;
}
extern "C" {
pub fn ts_node_parent(arg1: TSNode) -> TSNode;
}
extern "C" {
pub fn ts_node_child(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_child_by_field_id(arg1: TSNode, arg2: TSFieldId) -> TSNode;
}
extern "C" {
pub fn ts_node_child_by_field_name(
arg1: TSNode,
arg2: *const ::std::os::raw::c_char,
arg3: u32,
) -> TSNode;
}
extern "C" {
pub fn ts_node_named_child(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_child_count(arg1: TSNode) -> u32;
}
extern "C" {
pub fn ts_node_named_child_count(arg1: TSNode) -> u32;
}
extern "C" {
pub fn ts_node_next_sibling(arg1: TSNode) -> TSNode;
}
extern "C" {
pub fn ts_node_next_named_sibling(arg1: TSNode) -> TSNode;
}
extern "C" {
pub fn ts_node_prev_sibling(arg1: TSNode) -> TSNode;
}
extern "C" {
pub fn ts_node_prev_named_sibling(arg1: TSNode) -> TSNode;
}
extern "C" {
pub fn ts_node_first_child_for_byte(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_first_named_child_for_byte(arg1: TSNode, arg2: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_descendant_for_byte_range(arg1: TSNode, arg2: u32, arg3: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_named_descendant_for_byte_range(arg1: TSNode, arg2: u32, arg3: u32) -> TSNode;
}
extern "C" {
pub fn ts_node_descendant_for_point_range(arg1: TSNode, arg2: TSPoint, arg3: TSPoint)
-> TSNode;
}
extern "C" {
pub fn ts_node_named_descendant_for_point_range(
arg1: TSNode,
arg2: TSPoint,
arg3: TSPoint,
) -> TSNode;
}
extern "C" {
pub fn ts_node_edit(arg1: *mut TSNode, arg2: *const TSInputEdit);
}
extern "C" {
pub fn ts_tree_cursor_new(arg1: TSNode) -> TSTreeCursor;
}
extern "C" {
pub fn ts_tree_cursor_delete(arg1: *mut TSTreeCursor);
}
extern "C" {
pub fn ts_tree_cursor_reset(arg1: *mut TSTreeCursor, arg2: TSNode);
}
extern "C" {
pub fn ts_tree_cursor_current_node(arg1: *const TSTreeCursor) -> TSNode;
}
extern "C" {
pub fn ts_tree_cursor_current_field_id(arg1: *const TSTreeCursor) -> TSFieldId;
}
extern "C" {
pub fn ts_tree_cursor_current_field_name(
arg1: *const TSTreeCursor,
) -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn ts_tree_cursor_goto_parent(arg1: *mut TSTreeCursor) -> bool;
}
extern "C" {
pub fn ts_tree_cursor_goto_next_sibling(arg1: *mut TSTreeCursor) -> bool;
}
extern "C" {
pub fn ts_tree_cursor_goto_first_child(arg1: *mut TSTreeCursor) -> bool;
}
extern "C" {
pub fn ts_tree_cursor_goto_first_child_for_byte(arg1: *mut TSTreeCursor, arg2: u32) -> i64;
}
extern "C" {
pub fn ts_language_symbol_count(arg1: *const TSLanguage) -> u32;
}
extern "C" {
pub fn ts_language_symbol_name(
arg1: *const TSLanguage,
arg2: TSSymbol,
) -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn ts_language_symbol_for_name(
arg1: *const TSLanguage,
arg2: *const ::std::os::raw::c_char,
) -> TSSymbol;
}
extern "C" {
pub fn ts_language_field_count(arg1: *const TSLanguage) -> u32;
}
extern "C" {
pub fn ts_language_field_name_for_id(
arg1: *const TSLanguage,
arg2: TSFieldId,
) -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn ts_language_field_id_for_name(
arg1: *const TSLanguage,
arg2: *const ::std::os::raw::c_char,
arg3: u32,
) -> TSFieldId;
}
extern "C" {
pub fn ts_language_symbol_type(arg1: *const TSLanguage, arg2: TSSymbol) -> TSSymbolType;
}
extern "C" {
pub fn ts_language_version(arg1: *const TSLanguage) -> u32;
}
pub const TREE_SITTER_LANGUAGE_VERSION: usize = 10;
pub const TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION: usize = 9;

58
lib/binding_rust/build.rs Normal file
View file

@ -0,0 +1,58 @@
extern crate cc;
use std::{env, fs};
use std::path::{Path, PathBuf};
fn main() {
println!("cargo:rerun-if-env-changed=TREE_SITTER_STATIC_ANALYSIS");
if env::var("TREE_SITTER_STATIC_ANALYSIS").is_ok() {
if let (Some(clang_path), Some(scan_build_path)) = (which("clang"), which("scan-build")) {
let clang_path = clang_path.to_str().unwrap();
let scan_build_path = scan_build_path.to_str().unwrap();
env::set_var(
"CC",
&format!(
"{} -analyze-headers --use-analyzer={} cc",
scan_build_path, clang_path
),
);
}
}
let mut config = cc::Build::new();
println!("cargo:rerun-if-env-changed=TREE_SITTER_TEST");
if env::var("TREE_SITTER_TEST").is_ok() {
config.define("TREE_SITTER_TEST", "");
}
let src_path = Path::new("src");
for entry in fs::read_dir(&src_path).unwrap() {
let entry = entry.unwrap();
let path = src_path.join(entry.file_name());
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
}
config
.flag_if_supported("-std=c99")
.flag_if_supported("-Wno-unused-parameter")
.include("include")
.include("utf8proc")
.file(src_path.join("lib.c"))
.file(Path::new("binding_rust").join("helper.c"))
.compile("tree-sitter");
}
fn which(exe_name: impl AsRef<Path>) -> Option<PathBuf> {
env::var_os("PATH").and_then(|paths| {
env::split_paths(&paths).find_map(|dir| {
let full_path = dir.join(&exe_name);
if full_path.is_file() {
Some(full_path)
} else {
None
}
})
})
}

9
lib/binding_rust/ffi.rs Normal file
View file

@ -0,0 +1,9 @@
#![allow(dead_code)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
include!("./bindings.rs");
extern "C" {
pub(crate) fn dup(fd: std::os::raw::c_int) -> std::os::raw::c_int;
}

17
lib/binding_rust/helper.c Normal file
View file

@ -0,0 +1,17 @@
#if defined(TREE_SITTER_TEST)
void ts_record_free(void *);
void rust_tree_sitter_free(void *p) {
ts_record_free(p);
}
#else
void free(void *);
void rust_tree_sitter_free(void *p) {
free(p);
}
#endif

1036
lib/binding_rust/lib.rs Normal file

File diff suppressed because it is too large Load diff