feat(lib): support no_std
This commit is contained in:
parent
1c7b518b9d
commit
d13d7235d2
8 changed files with 101 additions and 94 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1417,6 +1417,7 @@ dependencies = [
|
|||
"bindgen",
|
||||
"cc",
|
||||
"regex",
|
||||
"regex-syntax",
|
||||
"tree-sitter-language",
|
||||
"wasmtime-c-api-impl",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -26,10 +26,13 @@ include = [
|
|||
]
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["regex/std", "regex/perf", "regex-syntax/unicode"]
|
||||
wasm = ["wasmtime-c-api"]
|
||||
|
||||
[dependencies]
|
||||
regex.workspace = true
|
||||
regex = { version = "1.10.4", default-features = false, features = ["unicode"] }
|
||||
regex-syntax = { version = "0.8.4", default-features = false }
|
||||
tree-sitter-language = { version = "0.1", path = "language" }
|
||||
|
||||
[dependencies.wasmtime-c-api]
|
||||
|
|
|
|||
|
|
@ -37,11 +37,11 @@ pub struct TSLookaheadIterator {
|
|||
}
|
||||
pub const TSInputEncodingUTF8: TSInputEncoding = 0;
|
||||
pub const TSInputEncodingUTF16: TSInputEncoding = 1;
|
||||
pub type TSInputEncoding = ::std::os::raw::c_uint;
|
||||
pub type TSInputEncoding = ::core::ffi::c_uint;
|
||||
pub const TSSymbolTypeRegular: TSSymbolType = 0;
|
||||
pub const TSSymbolTypeAnonymous: TSSymbolType = 1;
|
||||
pub const TSSymbolTypeAuxiliary: TSSymbolType = 2;
|
||||
pub type TSSymbolType = ::std::os::raw::c_uint;
|
||||
pub type TSSymbolType = ::core::ffi::c_uint;
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct TSPoint {
|
||||
|
|
@ -59,29 +59,29 @@ pub struct TSRange {
|
|||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TSInput {
|
||||
pub payload: *mut ::std::os::raw::c_void,
|
||||
pub read: ::std::option::Option<
|
||||
pub payload: *mut ::core::ffi::c_void,
|
||||
pub read: ::core::option::Option<
|
||||
unsafe extern "C" fn(
|
||||
payload: *mut ::std::os::raw::c_void,
|
||||
payload: *mut ::core::ffi::c_void,
|
||||
byte_index: u32,
|
||||
position: TSPoint,
|
||||
bytes_read: *mut u32,
|
||||
) -> *const ::std::os::raw::c_char,
|
||||
) -> *const ::core::ffi::c_char,
|
||||
>,
|
||||
pub encoding: TSInputEncoding,
|
||||
}
|
||||
pub const TSLogTypeParse: TSLogType = 0;
|
||||
pub const TSLogTypeLex: TSLogType = 1;
|
||||
pub type TSLogType = ::std::os::raw::c_uint;
|
||||
pub type TSLogType = ::core::ffi::c_uint;
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TSLogger {
|
||||
pub payload: *mut ::std::os::raw::c_void,
|
||||
pub log: ::std::option::Option<
|
||||
pub payload: *mut ::core::ffi::c_void,
|
||||
pub log: ::core::option::Option<
|
||||
unsafe extern "C" fn(
|
||||
payload: *mut ::std::os::raw::c_void,
|
||||
payload: *mut ::core::ffi::c_void,
|
||||
log_type: TSLogType,
|
||||
buffer: *const ::std::os::raw::c_char,
|
||||
buffer: *const ::core::ffi::c_char,
|
||||
),
|
||||
>,
|
||||
}
|
||||
|
|
@ -99,14 +99,14 @@ pub struct TSInputEdit {
|
|||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct TSNode {
|
||||
pub context: [u32; 4usize],
|
||||
pub id: *const ::std::os::raw::c_void,
|
||||
pub id: *const ::core::ffi::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 tree: *const ::core::ffi::c_void,
|
||||
pub id: *const ::core::ffi::c_void,
|
||||
pub context: [u32; 3usize],
|
||||
}
|
||||
#[repr(C)]
|
||||
|
|
@ -120,7 +120,7 @@ pub const TSQuantifierZeroOrOne: TSQuantifier = 1;
|
|||
pub const TSQuantifierZeroOrMore: TSQuantifier = 2;
|
||||
pub const TSQuantifierOne: TSQuantifier = 3;
|
||||
pub const TSQuantifierOneOrMore: TSQuantifier = 4;
|
||||
pub type TSQuantifier = ::std::os::raw::c_uint;
|
||||
pub type TSQuantifier = ::core::ffi::c_uint;
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TSQueryMatch {
|
||||
|
|
@ -132,7 +132,7 @@ pub struct TSQueryMatch {
|
|||
pub const TSQueryPredicateStepTypeDone: TSQueryPredicateStepType = 0;
|
||||
pub const TSQueryPredicateStepTypeCapture: TSQueryPredicateStepType = 1;
|
||||
pub const TSQueryPredicateStepTypeString: TSQueryPredicateStepType = 2;
|
||||
pub type TSQueryPredicateStepType = ::std::os::raw::c_uint;
|
||||
pub type TSQueryPredicateStepType = ::core::ffi::c_uint;
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TSQueryPredicateStep {
|
||||
|
|
@ -146,7 +146,7 @@ pub const TSQueryErrorField: TSQueryError = 3;
|
|||
pub const TSQueryErrorCapture: TSQueryError = 4;
|
||||
pub const TSQueryErrorStructure: TSQueryError = 5;
|
||||
pub const TSQueryErrorLanguage: TSQueryError = 6;
|
||||
pub type TSQueryError = ::std::os::raw::c_uint;
|
||||
pub type TSQueryError = ::core::ffi::c_uint;
|
||||
extern "C" {
|
||||
#[doc = " Create a new parser."]
|
||||
pub fn ts_parser_new() -> *mut TSParser;
|
||||
|
|
@ -188,7 +188,7 @@ extern "C" {
|
|||
pub fn ts_parser_parse_string(
|
||||
self_: *mut TSParser,
|
||||
old_tree: *const TSTree,
|
||||
string: *const ::std::os::raw::c_char,
|
||||
string: *const ::core::ffi::c_char,
|
||||
length: u32,
|
||||
) -> *mut TSTree;
|
||||
}
|
||||
|
|
@ -197,7 +197,7 @@ extern "C" {
|
|||
pub fn ts_parser_parse_string_encoding(
|
||||
self_: *mut TSParser,
|
||||
old_tree: *const TSTree,
|
||||
string: *const ::std::os::raw::c_char,
|
||||
string: *const ::core::ffi::c_char,
|
||||
length: u32,
|
||||
encoding: TSInputEncoding,
|
||||
) -> *mut TSTree;
|
||||
|
|
@ -232,7 +232,7 @@ extern "C" {
|
|||
}
|
||||
extern "C" {
|
||||
#[doc = " Set the file descriptor to which the parser should write debugging graphs\n during parsing. The graphs are formatted in the DOT language. You may want\n to pipe these graphs directly to a `dot(1)` process in order to generate\n SVG output. You can turn off this logging by passing a negative number."]
|
||||
pub fn ts_parser_print_dot_graphs(self_: *mut TSParser, fd: ::std::os::raw::c_int);
|
||||
pub fn ts_parser_print_dot_graphs(self_: *mut TSParser, fd: ::core::ffi::c_int);
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Create a shallow copy of the syntax tree. This is very fast.\n\n You need to copy a syntax tree in order to use it on more than one thread at\n a time, as syntax trees are not thread safe."]
|
||||
|
|
@ -276,11 +276,11 @@ extern "C" {
|
|||
}
|
||||
extern "C" {
|
||||
#[doc = " Write a DOT graph describing the syntax tree to the given file."]
|
||||
pub fn ts_tree_print_dot_graph(self_: *const TSTree, file_descriptor: ::std::os::raw::c_int);
|
||||
pub fn ts_tree_print_dot_graph(self_: *const TSTree, file_descriptor: ::core::ffi::c_int);
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the node's type as a null-terminated string."]
|
||||
pub fn ts_node_type(self_: TSNode) -> *const ::std::os::raw::c_char;
|
||||
pub fn ts_node_type(self_: TSNode) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the node's type as a numerical id."]
|
||||
|
|
@ -292,7 +292,7 @@ extern "C" {
|
|||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the node's type as it appears in the grammar ignoring aliases as a\n null-terminated string."]
|
||||
pub fn ts_node_grammar_type(self_: TSNode) -> *const ::std::os::raw::c_char;
|
||||
pub fn ts_node_grammar_type(self_: TSNode) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the node's type as a numerical id as it appears in the grammar ignoring\n aliases. This should be used in [`ts_language_next_state`] instead of\n [`ts_node_symbol`]."]
|
||||
|
|
@ -316,7 +316,7 @@ extern "C" {
|
|||
}
|
||||
extern "C" {
|
||||
#[doc = " Get an S-expression representing the node as a string.\n\n This string is allocated with `malloc` and the caller is responsible for\n freeing it using `free`."]
|
||||
pub fn ts_node_string(self_: TSNode) -> *mut ::std::os::raw::c_char;
|
||||
pub fn ts_node_string(self_: TSNode) -> *mut ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Check if the node is null. Functions like [`ts_node_child`] and\n [`ts_node_next_sibling`] will return a null node to indicate that no such node\n was found."]
|
||||
|
|
@ -371,7 +371,7 @@ extern "C" {
|
|||
pub fn ts_node_field_name_for_child(
|
||||
self_: TSNode,
|
||||
child_index: u32,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the node's number of children."]
|
||||
|
|
@ -389,7 +389,7 @@ extern "C" {
|
|||
#[doc = " Get the node's child with the given field name."]
|
||||
pub fn ts_node_child_by_field_name(
|
||||
self_: TSNode,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
name: *const ::core::ffi::c_char,
|
||||
name_length: u32,
|
||||
) -> TSNode;
|
||||
}
|
||||
|
|
@ -477,7 +477,7 @@ extern "C" {
|
|||
#[doc = " Get the field name of the tree cursor's current node.\n\n This returns `NULL` if the current node doesn't have a field.\n See also [`ts_node_child_by_field_name`]."]
|
||||
pub fn ts_tree_cursor_current_field_name(
|
||||
self_: *const TSTreeCursor,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the field id of the tree cursor's current node.\n\n This returns zero if the current node doesn't have a field.\n See also [`ts_node_child_by_field_id`], [`ts_language_field_id_for_name`]."]
|
||||
|
|
@ -535,7 +535,7 @@ extern "C" {
|
|||
#[doc = " Create a new query from a string containing one or more S-expression\n patterns. The query is associated with a particular language, and can\n only be run on syntax nodes parsed with that language.\n\n If all of the given patterns are valid, this returns a [`TSQuery`].\n If a pattern is invalid, this returns `NULL`, and provides two pieces\n of information about the problem:\n 1. The byte offset of the error is written to the `error_offset` parameter.\n 2. The type of error is written to the `error_type` parameter."]
|
||||
pub fn ts_query_new(
|
||||
language: *const TSLanguage,
|
||||
source: *const ::std::os::raw::c_char,
|
||||
source: *const ::core::ffi::c_char,
|
||||
source_len: u32,
|
||||
error_offset: *mut u32,
|
||||
error_type: *mut TSQueryError,
|
||||
|
|
@ -586,7 +586,7 @@ extern "C" {
|
|||
self_: *const TSQuery,
|
||||
index: u32,
|
||||
length: *mut u32,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the quantifier of the query's captures. Each capture is * associated\n with a numeric id based on the order that it appeared in the query's source."]
|
||||
|
|
@ -601,13 +601,13 @@ extern "C" {
|
|||
self_: *const TSQuery,
|
||||
index: u32,
|
||||
length: *mut u32,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Disable a certain capture within a query.\n\n This prevents the capture from being returned in matches, and also avoids\n any resource usage associated with recording the capture. Currently, there\n is no way to undo this."]
|
||||
pub fn ts_query_disable_capture(
|
||||
self_: *mut TSQuery,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
name: *const ::core::ffi::c_char,
|
||||
length: u32,
|
||||
);
|
||||
}
|
||||
|
|
@ -693,13 +693,13 @@ extern "C" {
|
|||
pub fn ts_language_symbol_name(
|
||||
self_: *const TSLanguage,
|
||||
symbol: TSSymbol,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the numerical id for the given node type string."]
|
||||
pub fn ts_language_symbol_for_name(
|
||||
self_: *const TSLanguage,
|
||||
string: *const ::std::os::raw::c_char,
|
||||
string: *const ::core::ffi::c_char,
|
||||
length: u32,
|
||||
is_named: bool,
|
||||
) -> TSSymbol;
|
||||
|
|
@ -713,13 +713,13 @@ extern "C" {
|
|||
pub fn ts_language_field_name_for_id(
|
||||
self_: *const TSLanguage,
|
||||
id: TSFieldId,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Get the numerical id for the given field name string."]
|
||||
pub fn ts_language_field_id_for_name(
|
||||
self_: *const TSLanguage,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
name: *const ::core::ffi::c_char,
|
||||
name_length: u32,
|
||||
) -> TSFieldId;
|
||||
}
|
||||
|
|
@ -781,7 +781,7 @@ extern "C" {
|
|||
#[doc = " Get the current symbol type of the lookahead iterator as a null terminated\n string."]
|
||||
pub fn ts_lookahead_iterator_current_symbol_name(
|
||||
self_: *const TSLookaheadIterator,
|
||||
) -> *const ::std::os::raw::c_char;
|
||||
) -> *const ::core::ffi::c_char;
|
||||
}
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
|
|
@ -799,12 +799,12 @@ pub const TSWasmErrorKindParse: TSWasmErrorKind = 1;
|
|||
pub const TSWasmErrorKindCompile: TSWasmErrorKind = 2;
|
||||
pub const TSWasmErrorKindInstantiate: TSWasmErrorKind = 3;
|
||||
pub const TSWasmErrorKindAllocate: TSWasmErrorKind = 4;
|
||||
pub type TSWasmErrorKind = ::std::os::raw::c_uint;
|
||||
pub type TSWasmErrorKind = ::core::ffi::c_uint;
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct TSWasmError {
|
||||
pub kind: TSWasmErrorKind,
|
||||
pub message: *mut ::std::os::raw::c_char,
|
||||
pub message: *mut ::core::ffi::c_char,
|
||||
}
|
||||
extern "C" {
|
||||
#[doc = " Create a Wasm store."]
|
||||
|
|
@ -821,8 +821,8 @@ extern "C" {
|
|||
#[doc = " Create a language from a buffer of Wasm. The resulting language behaves\n like any other Tree-sitter language, except that in order to use it with\n a parser, that parser must have a Wasm store. Note that the language\n can be used with any Wasm store, it doesn't need to be the same store that\n was used to originally load it."]
|
||||
pub fn ts_wasm_store_load_language(
|
||||
arg1: *mut TSWasmStore,
|
||||
name: *const ::std::os::raw::c_char,
|
||||
wasm: *const ::std::os::raw::c_char,
|
||||
name: *const ::core::ffi::c_char,
|
||||
wasm: *const ::core::ffi::c_char,
|
||||
wasm_len: u32,
|
||||
error: *mut TSWasmError,
|
||||
) -> *const TSLanguage;
|
||||
|
|
@ -846,18 +846,18 @@ extern "C" {
|
|||
extern "C" {
|
||||
#[doc = " Set the allocation functions used by the library.\n\n By default, Tree-sitter uses the standard libc allocation functions,\n but aborts the process when an allocation fails. This function lets\n you supply alternative allocation functions at runtime.\n\n If you pass `NULL` for any parameter, Tree-sitter will switch back to\n its default implementation of that function.\n\n If you call this function after the library has already been used, then\n you must ensure that either:\n 1. All the existing objects have been freed.\n 2. The new allocator shares its state with the old one, so it is capable\n of freeing memory that was allocated by the old allocator."]
|
||||
pub fn ts_set_allocator(
|
||||
new_malloc: ::std::option::Option<
|
||||
unsafe extern "C" fn(arg1: usize) -> *mut ::std::os::raw::c_void,
|
||||
new_malloc: ::core::option::Option<
|
||||
unsafe extern "C" fn(arg1: usize) -> *mut ::core::ffi::c_void,
|
||||
>,
|
||||
new_calloc: ::std::option::Option<
|
||||
unsafe extern "C" fn(arg1: usize, arg2: usize) -> *mut ::std::os::raw::c_void,
|
||||
new_calloc: ::core::option::Option<
|
||||
unsafe extern "C" fn(arg1: usize, arg2: usize) -> *mut ::core::ffi::c_void,
|
||||
>,
|
||||
new_realloc: ::std::option::Option<
|
||||
new_realloc: ::core::option::Option<
|
||||
unsafe extern "C" fn(
|
||||
arg1: *mut ::std::os::raw::c_void,
|
||||
arg1: *mut ::core::ffi::c_void,
|
||||
arg2: usize,
|
||||
) -> *mut ::std::os::raw::c_void,
|
||||
) -> *mut ::core::ffi::c_void,
|
||||
>,
|
||||
new_free: ::std::option::Option<unsafe extern "C" fn(arg1: *mut ::std::os::raw::c_void)>,
|
||||
new_free: ::core::option::Option<unsafe extern "C" fn(arg1: *mut ::core::ffi::c_void)>,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,9 +37,11 @@ fn main() {
|
|||
.flag_if_supported("-fvisibility=hidden")
|
||||
.flag_if_supported("-Wshadow")
|
||||
.flag_if_supported("-Wno-unused-parameter")
|
||||
.flag_if_supported("-Wno-incompatible-pointer-types")
|
||||
.include(&src_path)
|
||||
.include(&wasm_path)
|
||||
.include(&include_path)
|
||||
.warnings(false)
|
||||
.file(src_path.join("lib.c"))
|
||||
.compile("tree-sitter");
|
||||
|
||||
|
|
@ -47,7 +49,7 @@ fn main() {
|
|||
}
|
||||
|
||||
#[cfg(feature = "bindgen")]
|
||||
fn generate_bindings(out_dir: &Path) {
|
||||
fn generate_bindings(out_dir: &std::path::Path) {
|
||||
const HEADER_PATH: &str = "include/tree_sitter/api.h";
|
||||
|
||||
println!("cargo:rerun-if-changed={HEADER_PATH}");
|
||||
|
|
@ -74,6 +76,7 @@ fn generate_bindings(out_dir: &Path) {
|
|||
.allowlist_var("^TREE_SITTER.*")
|
||||
.no_copy(no_copy.join("|"))
|
||||
.prepend_enum_name(false)
|
||||
.use_core()
|
||||
.generate()
|
||||
.expect("Failed to generate bindings");
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
|||
include!("./bindings.rs");
|
||||
|
||||
#[cfg(any(unix, target_os = "wasi"))]
|
||||
#[cfg(feature = "std")]
|
||||
extern "C" {
|
||||
pub(crate) fn _ts_dup(fd: std::os::raw::c_int) -> std::os::raw::c_int;
|
||||
}
|
||||
|
|
@ -18,7 +19,7 @@ extern "C" {
|
|||
pub(crate) fn _ts_dup(handle: *mut std::os::raw::c_void) -> std::os::raw::c_int;
|
||||
}
|
||||
|
||||
use std::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull, str};
|
||||
use core::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull, str};
|
||||
|
||||
use crate::{
|
||||
Language, LookaheadIterator, Node, Parser, Query, QueryCursor, QueryError, Tree, TreeCursor,
|
||||
|
|
|
|||
|
|
@ -1,26 +1,31 @@
|
|||
#![doc = include_str!("./README.md")]
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
pub mod ffi;
|
||||
mod util;
|
||||
|
||||
#[cfg(any(unix, target_os = "wasi"))]
|
||||
use std::os::fd::AsRawFd;
|
||||
#[cfg(windows)]
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::{
|
||||
char, error,
|
||||
ffi::CStr,
|
||||
#[cfg(not(feature = "std"))]
|
||||
extern crate alloc;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::{boxed::Box, format, string::String, string::ToString, vec::Vec};
|
||||
use core::{
|
||||
char,
|
||||
ffi::{c_char, c_void, CStr},
|
||||
fmt::{self, Write},
|
||||
hash, iter,
|
||||
marker::PhantomData,
|
||||
mem::MaybeUninit,
|
||||
num::NonZeroU16,
|
||||
ops::{self, Deref},
|
||||
os::raw::{c_char, c_void},
|
||||
ptr::{self, NonNull},
|
||||
slice, str,
|
||||
sync::atomic::AtomicUsize,
|
||||
};
|
||||
#[cfg(feature = "std")]
|
||||
use std::error;
|
||||
#[cfg(all(feature = "std", any(unix, target_os = "wasi")))]
|
||||
use std::os::fd::AsRawFd;
|
||||
#[cfg(all(windows, feature = "std"))]
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
|
||||
use tree_sitter_language::LanguageFn;
|
||||
|
||||
|
|
@ -434,7 +439,7 @@ impl<'a> Deref for LanguageRef<'a> {
|
|||
type Target = Language;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { &*(std::ptr::addr_of!(self.0).cast::<Language>()) }
|
||||
unsafe { &*(core::ptr::addr_of!(self.0).cast::<Language>()) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -543,6 +548,7 @@ impl Parser {
|
|||
/// want to pipe these graphs directly to a `dot(1)` process in order to
|
||||
/// generate SVG output.
|
||||
#[doc(alias = "ts_parser_print_dot_graphs")]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn print_dot_graphs(
|
||||
&mut self,
|
||||
#[cfg(any(unix, target_os = "wasi"))] file: &impl AsRawFd,
|
||||
|
|
@ -650,7 +656,7 @@ impl Parser {
|
|||
}
|
||||
|
||||
let c_input = ffi::TSInput {
|
||||
payload: std::ptr::addr_of_mut!(payload).cast::<c_void>(),
|
||||
payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
|
||||
read: Some(read::<T, F>),
|
||||
encoding: ffi::TSInputEncodingUTF8,
|
||||
};
|
||||
|
|
@ -705,7 +711,7 @@ impl Parser {
|
|||
}
|
||||
|
||||
let c_input = ffi::TSInput {
|
||||
payload: std::ptr::addr_of_mut!(payload).cast::<c_void>(),
|
||||
payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
|
||||
read: Some(read::<T, F>),
|
||||
encoding: ffi::TSInputEncodingUTF16,
|
||||
};
|
||||
|
|
@ -767,11 +773,7 @@ impl Parser {
|
|||
/// slice pointing to a first incorrect range.
|
||||
#[doc(alias = "ts_parser_set_included_ranges")]
|
||||
pub fn set_included_ranges(&mut self, ranges: &[Range]) -> Result<(), IncludedRangesError> {
|
||||
let ts_ranges = ranges
|
||||
.iter()
|
||||
.copied()
|
||||
.map(std::convert::Into::into)
|
||||
.collect::<Vec<_>>();
|
||||
let ts_ranges = ranges.iter().copied().map(Into::into).collect::<Vec<_>>();
|
||||
let result = unsafe {
|
||||
ffi::ts_parser_set_included_ranges(
|
||||
self.0.as_ptr(),
|
||||
|
|
@ -801,13 +803,9 @@ impl Parser {
|
|||
let mut count = 0u32;
|
||||
unsafe {
|
||||
let ptr =
|
||||
ffi::ts_parser_included_ranges(self.0.as_ptr(), std::ptr::addr_of_mut!(count));
|
||||
ffi::ts_parser_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
|
||||
let ranges = slice::from_raw_parts(ptr, count as usize);
|
||||
let result = ranges
|
||||
.iter()
|
||||
.copied()
|
||||
.map(std::convert::Into::into)
|
||||
.collect();
|
||||
let result = ranges.iter().copied().map(Into::into).collect();
|
||||
result
|
||||
}
|
||||
}
|
||||
|
|
@ -923,9 +921,9 @@ impl Tree {
|
|||
let ptr = ffi::ts_tree_get_changed_ranges(
|
||||
self.0.as_ptr(),
|
||||
other.0.as_ptr(),
|
||||
std::ptr::addr_of_mut!(count),
|
||||
core::ptr::addr_of_mut!(count),
|
||||
);
|
||||
util::CBufferIter::new(ptr, count as usize).map(std::convert::Into::into)
|
||||
util::CBufferIter::new(ptr, count as usize).map(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -935,13 +933,9 @@ impl Tree {
|
|||
pub fn included_ranges(&self) -> Vec<Range> {
|
||||
let mut count = 0u32;
|
||||
unsafe {
|
||||
let ptr = ffi::ts_tree_included_ranges(self.0.as_ptr(), std::ptr::addr_of_mut!(count));
|
||||
let ptr = ffi::ts_tree_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
|
||||
let ranges = slice::from_raw_parts(ptr, count as usize);
|
||||
let result = ranges
|
||||
.iter()
|
||||
.copied()
|
||||
.map(std::convert::Into::into)
|
||||
.collect();
|
||||
let result = ranges.iter().copied().map(Into::into).collect();
|
||||
(FREE_FN)(ptr.cast::<c_void>());
|
||||
result
|
||||
}
|
||||
|
|
@ -952,6 +946,7 @@ impl Tree {
|
|||
/// graph directly to a `dot(1)` process in order to generate SVG
|
||||
/// output.
|
||||
#[doc(alias = "ts_tree_print_dot_graph")]
|
||||
#[cfg(feature = "std")]
|
||||
pub fn print_dot_graph(
|
||||
&self,
|
||||
#[cfg(any(unix, target_os = "wasi"))] file: &impl AsRawFd,
|
||||
|
|
@ -1131,7 +1126,7 @@ impl<'tree> Node<'tree> {
|
|||
|
||||
/// Get the byte range of source code that this node represents.
|
||||
#[must_use]
|
||||
pub fn byte_range(&self) -> std::ops::Range<usize> {
|
||||
pub fn byte_range(&self) -> core::ops::Range<usize> {
|
||||
self.start_byte()..self.end_byte()
|
||||
}
|
||||
|
||||
|
|
@ -1467,7 +1462,7 @@ impl<'tree> Node<'tree> {
|
|||
#[doc(alias = "ts_node_edit")]
|
||||
pub fn edit(&mut self, edit: &InputEdit) {
|
||||
let edit = edit.into();
|
||||
unsafe { ffi::ts_node_edit(std::ptr::addr_of_mut!(self.0), &edit) }
|
||||
unsafe { ffi::ts_node_edit(core::ptr::addr_of_mut!(self.0), &edit) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1778,8 +1773,8 @@ impl Query {
|
|||
language.0,
|
||||
bytes.as_ptr().cast::<c_char>(),
|
||||
bytes.len() as u32,
|
||||
std::ptr::addr_of_mut!(error_offset),
|
||||
std::ptr::addr_of_mut!(error_type),
|
||||
core::ptr::addr_of_mut!(error_offset),
|
||||
core::ptr::addr_of_mut!(error_type),
|
||||
)
|
||||
};
|
||||
|
||||
|
|
@ -1884,7 +1879,7 @@ impl Query {
|
|||
unsafe {
|
||||
let mut length = 0u32;
|
||||
let name =
|
||||
ffi::ts_query_capture_name_for_id(ptr.0, i, std::ptr::addr_of_mut!(length))
|
||||
ffi::ts_query_capture_name_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
|
||||
.cast::<u8>();
|
||||
let name = slice::from_raw_parts(name, length as usize);
|
||||
let name = str::from_utf8_unchecked(name);
|
||||
|
|
@ -1909,7 +1904,7 @@ impl Query {
|
|||
.map(|i| unsafe {
|
||||
let mut length = 0u32;
|
||||
let value =
|
||||
ffi::ts_query_string_value_for_id(ptr.0, i, std::ptr::addr_of_mut!(length))
|
||||
ffi::ts_query_string_value_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
|
||||
.cast::<u8>();
|
||||
let value = slice::from_raw_parts(value, length as usize);
|
||||
let value = str::from_utf8_unchecked(value);
|
||||
|
|
@ -1924,7 +1919,7 @@ impl Query {
|
|||
let raw_predicates = ffi::ts_query_predicates_for_pattern(
|
||||
ptr.0,
|
||||
i as u32,
|
||||
std::ptr::addr_of_mut!(length),
|
||||
core::ptr::addr_of_mut!(length),
|
||||
);
|
||||
(length > 0)
|
||||
.then(|| slice::from_raw_parts(raw_predicates, length as usize))
|
||||
|
|
@ -2132,7 +2127,7 @@ impl Query {
|
|||
general_predicates: general_predicates_vec.into(),
|
||||
};
|
||||
|
||||
std::mem::forget(ptr);
|
||||
core::mem::forget(ptr);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
|
@ -2672,7 +2667,7 @@ impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> Iterator
|
|||
if ffi::ts_query_cursor_next_capture(
|
||||
self.ptr,
|
||||
m.as_mut_ptr(),
|
||||
std::ptr::addr_of_mut!(capture_index),
|
||||
core::ptr::addr_of_mut!(capture_index),
|
||||
) {
|
||||
let result = QueryMatch::new(&m.assume_init(), self.ptr);
|
||||
if result.satisfies_text_predicates(
|
||||
|
|
@ -2860,7 +2855,7 @@ impl<'a> Iterator for LossyUtf8<'a> {
|
|||
self.in_replacement = false;
|
||||
return Some("\u{fffd}");
|
||||
}
|
||||
match std::str::from_utf8(self.bytes) {
|
||||
match core::str::from_utf8(self.bytes) {
|
||||
Ok(valid) => {
|
||||
self.bytes = &[];
|
||||
Some(valid)
|
||||
|
|
@ -2870,7 +2865,7 @@ impl<'a> Iterator for LossyUtf8<'a> {
|
|||
let error_start = error.valid_up_to();
|
||||
if error_start > 0 {
|
||||
let result =
|
||||
unsafe { std::str::from_utf8_unchecked(&self.bytes[..error_start]) };
|
||||
unsafe { core::str::from_utf8_unchecked(&self.bytes[..error_start]) };
|
||||
self.bytes = &self.bytes[(error_start + error_len)..];
|
||||
self.in_replacement = true;
|
||||
Some(result)
|
||||
|
|
@ -3071,8 +3066,11 @@ pub unsafe fn set_allocator(
|
|||
ffi::ts_set_allocator(new_malloc, new_calloc, new_realloc, new_free);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl error::Error for IncludedRangesError {}
|
||||
#[cfg(feature = "std")]
|
||||
impl error::Error for LanguageError {}
|
||||
#[cfg(feature = "std")]
|
||||
impl error::Error for QueryError {}
|
||||
|
||||
unsafe impl Send for Language {}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::os::raw::c_void;
|
||||
use core::ffi::c_void;
|
||||
|
||||
use super::FREE_FN;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#![no_std]
|
||||
/// LanguageFn wraps a C function that returns a pointer to a tree-sitter grammer.
|
||||
#[repr(transparent)]
|
||||
pub struct LanguageFn(unsafe extern "C" fn() -> *const ());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue