2018-05-17 17:29:23 -07:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
#![allow(non_upper_case_globals)]
|
2019-01-08 21:03:51 -08:00
|
|
|
#![allow(non_camel_case_types)]
|
2016-07-10 14:03:00 -07:00
|
|
|
|
2023-08-20 16:00:33 +03:00
|
|
|
#[cfg(feature = "bindgen")]
|
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
|
|
|
|
|
|
|
|
|
#[cfg(not(feature = "bindgen"))]
|
2018-05-17 17:29:23 -07:00
|
|
|
include!("./bindings.rs");
|
2019-01-08 21:03:51 -08:00
|
|
|
|
2024-04-23 10:13:38 -04:00
|
|
|
#[cfg(any(unix, target_os = "wasi"))]
|
2019-01-08 21:03:51 -08:00
|
|
|
extern "C" {
|
2024-02-05 23:15:38 -05:00
|
|
|
pub(crate) fn _ts_dup(fd: std::os::raw::c_int) -> std::os::raw::c_int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
extern "C" {
|
|
|
|
|
pub(crate) fn _ts_dup(handle: *mut std::os::raw::c_void) -> std::os::raw::c_int;
|
2019-01-08 21:03:51 -08:00
|
|
|
}
|
2023-03-14 19:34:18 +01:00
|
|
|
|
2024-04-09 13:35:08 -04:00
|
|
|
use std::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull, str};
|
|
|
|
|
|
2023-05-17 10:39:37 +03:00
|
|
|
use crate::{
|
|
|
|
|
Language, LookaheadIterator, Node, Parser, Query, QueryCursor, QueryError, Tree, TreeCursor,
|
|
|
|
|
};
|
2023-03-14 19:34:18 +01:00
|
|
|
|
|
|
|
|
impl Language {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`Language`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub const unsafe fn from_raw(ptr: *const TSLanguage) -> Self {
|
|
|
|
|
Self(ptr)
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`Language`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-03-14 19:34:18 +01:00
|
|
|
pub fn into_raw(self) -> *const TSLanguage {
|
|
|
|
|
ManuallyDrop::new(self).0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Parser {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`Parser`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub const unsafe fn from_raw(ptr: *mut TSParser) -> Self {
|
|
|
|
|
Self(NonNull::new_unchecked(ptr))
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`Parser`], returning a raw pointer to the underlying C structure.
|
2023-07-13 23:44:19 +03:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// It's a caller responsibility to adjust parser's state
|
|
|
|
|
/// like disable logging or dot graphs printing if this
|
|
|
|
|
/// may cause issues like use after free.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-07-13 23:44:19 +03:00
|
|
|
pub fn into_raw(self) -> *mut TSParser {
|
2023-03-14 19:34:18 +01:00
|
|
|
ManuallyDrop::new(self).0.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tree {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`Tree`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub const unsafe fn from_raw(ptr: *mut TSTree) -> Self {
|
|
|
|
|
Self(NonNull::new_unchecked(ptr))
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`Tree`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-03-14 19:34:18 +01:00
|
|
|
pub fn into_raw(self) -> *mut TSTree {
|
|
|
|
|
ManuallyDrop::new(self).0.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tree> Node<'tree> {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`Node`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2024-04-09 21:42:59 -04:00
|
|
|
pub const unsafe fn from_raw(raw: TSNode) -> Self {
|
2024-02-04 01:30:46 -05:00
|
|
|
Self(raw, PhantomData)
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`Node`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-07-13 23:44:19 +03:00
|
|
|
pub fn into_raw(self) -> TSNode {
|
|
|
|
|
ManuallyDrop::new(self).0
|
2023-03-14 19:34:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TreeCursor<'a> {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`TreeCursor`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2024-04-09 21:42:59 -04:00
|
|
|
pub const unsafe fn from_raw(raw: TSTreeCursor) -> Self {
|
2024-02-04 01:30:46 -05:00
|
|
|
Self(raw, PhantomData)
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`TreeCursor`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-07-13 23:44:19 +03:00
|
|
|
pub fn into_raw(self) -> TSTreeCursor {
|
|
|
|
|
ManuallyDrop::new(self).0
|
2023-03-14 19:34:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Query {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`Query`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
pub unsafe fn from_raw(ptr: *mut TSQuery, source: &str) -> Result<Self, QueryError> {
|
|
|
|
|
Self::from_raw_parts(ptr, source)
|
2023-03-14 21:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`Query`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-03-14 19:34:18 +01:00
|
|
|
pub fn into_raw(self) -> *mut TSQuery {
|
|
|
|
|
ManuallyDrop::new(self).ptr.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl QueryCursor {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`QueryCursor`] from a raw pointer.
|
2023-03-14 21:32:48 +01:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub const unsafe fn from_raw(ptr: *mut TSQueryCursor) -> Self {
|
|
|
|
|
Self {
|
2023-03-14 21:32:48 +01:00
|
|
|
ptr: NonNull::new_unchecked(ptr),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`QueryCursor`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-03-14 19:34:18 +01:00
|
|
|
pub fn into_raw(self) -> *mut TSQueryCursor {
|
|
|
|
|
ManuallyDrop::new(self).ptr.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-17 10:39:37 +03:00
|
|
|
|
2023-07-12 15:34:08 +03:00
|
|
|
impl LookaheadIterator {
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Reconstructs a [`LookaheadIterator`] from a raw pointer.
|
2023-05-17 10:39:37 +03:00
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// `ptr` must be non-null.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
|
|
|
|
pub const unsafe fn from_raw(ptr: *mut TSLookaheadIterator) -> Self {
|
|
|
|
|
Self(NonNull::new_unchecked(ptr))
|
2023-05-17 10:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 19:26:38 +03:00
|
|
|
/// Consumes the [`LookaheadIterator`], returning a raw pointer to the underlying C structure.
|
2024-02-04 01:30:46 -05:00
|
|
|
#[must_use]
|
2023-05-17 10:39:37 +03:00
|
|
|
pub fn into_raw(self) -> *mut TSLookaheadIterator {
|
|
|
|
|
ManuallyDrop::new(self).0.as_ptr()
|
|
|
|
|
}
|
|
|
|
|
}
|