Merge pull request #2840 from tree-sitter/language-reference-count
Introduce APIs for managing the lifetimes of languages, allow WASM languages to be deleted
This commit is contained in:
commit
1d8975319c
38 changed files with 724 additions and 483 deletions
|
|
@ -11,9 +11,9 @@ use std::{
|
|||
ffi::CStr,
|
||||
fmt, hash, iter,
|
||||
marker::PhantomData,
|
||||
mem::MaybeUninit,
|
||||
mem::{self, MaybeUninit},
|
||||
num::NonZeroU16,
|
||||
ops,
|
||||
ops::{self, Deref},
|
||||
os::raw::{c_char, c_void},
|
||||
ptr::{self, NonNull},
|
||||
slice, str,
|
||||
|
|
@ -47,10 +47,12 @@ pub const PARSER_HEADER: &'static str = include_str!("../include/tree_sitter/par
|
|||
/// An opaque object that defines how to parse a particular language. The code for each
|
||||
/// `Language` is generated by the Tree-sitter CLI.
|
||||
#[doc(alias = "TSLanguage")]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct Language(*const ffi::TSLanguage);
|
||||
|
||||
pub struct LanguageRef<'a>(*const ffi::TSLanguage, PhantomData<&'a ()>);
|
||||
|
||||
/// A tree that represents the syntactic structure of a source code file.
|
||||
#[doc(alias = "TSTree")]
|
||||
pub struct Tree(NonNull<ffi::TSTree>);
|
||||
|
|
@ -385,6 +387,26 @@ impl Language {
|
|||
}
|
||||
}
|
||||
|
||||
impl Clone for Language {
|
||||
fn clone(&self) -> Self {
|
||||
unsafe { Self(ffi::ts_language_copy(self.0)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Language {
|
||||
fn drop(&mut self) {
|
||||
unsafe { ffi::ts_language_delete(self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for LanguageRef<'a> {
|
||||
type Target = Language;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { mem::transmute(&self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
/// Create a new parser.
|
||||
pub fn new() -> Parser {
|
||||
|
|
@ -403,7 +425,7 @@ impl Parser {
|
|||
/// and compare it to this library's [`LANGUAGE_VERSION`](LANGUAGE_VERSION) and
|
||||
/// [`MIN_COMPATIBLE_LANGUAGE_VERSION`](MIN_COMPATIBLE_LANGUAGE_VERSION) constants.
|
||||
#[doc(alias = "ts_parser_set_language")]
|
||||
pub fn set_language(&mut self, language: Language) -> Result<(), LanguageError> {
|
||||
pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
|
||||
let version = language.version();
|
||||
if version < MIN_COMPATIBLE_LANGUAGE_VERSION || version > LANGUAGE_VERSION {
|
||||
Err(LanguageError { version })
|
||||
|
|
@ -766,8 +788,11 @@ impl Tree {
|
|||
|
||||
/// Get the language that was used to parse the syntax tree.
|
||||
#[doc(alias = "ts_tree_language")]
|
||||
pub fn language(&self) -> Language {
|
||||
Language(unsafe { ffi::ts_tree_language(self.0.as_ptr()) })
|
||||
pub fn language(&self) -> LanguageRef {
|
||||
LanguageRef(
|
||||
unsafe { ffi::ts_tree_language(self.0.as_ptr()) },
|
||||
PhantomData,
|
||||
)
|
||||
}
|
||||
|
||||
/// Edit the syntax tree to keep it in sync with source code that has been
|
||||
|
|
@ -894,8 +919,8 @@ impl<'tree> Node<'tree> {
|
|||
|
||||
/// Get the [`Language`] that was used to parse this node's syntax tree.
|
||||
#[doc(alias = "ts_node_language")]
|
||||
pub fn language(&self) -> Language {
|
||||
Language(unsafe { ffi::ts_node_language(self.0) })
|
||||
pub fn language(&self) -> LanguageRef {
|
||||
LanguageRef(unsafe { ffi::ts_node_language(self.0) }, PhantomData)
|
||||
}
|
||||
|
||||
/// Check if this node is *named*.
|
||||
|
|
@ -1473,8 +1498,11 @@ impl Drop for TreeCursor<'_> {
|
|||
impl LookaheadIterator {
|
||||
/// Get the current language of the lookahead iterator.
|
||||
#[doc(alias = "ts_lookahead_iterator_language")]
|
||||
pub fn language(&self) -> Language {
|
||||
Language(unsafe { ffi::ts_lookahead_iterator_language(self.0.as_ptr()) })
|
||||
pub fn language(&self) -> LanguageRef<'_> {
|
||||
LanguageRef(
|
||||
unsafe { ffi::ts_lookahead_iterator_language(self.0.as_ptr()) },
|
||||
PhantomData,
|
||||
)
|
||||
}
|
||||
|
||||
/// Get the current symbol of the lookahead iterator.
|
||||
|
|
@ -1553,7 +1581,7 @@ impl Query {
|
|||
/// The query is associated with a particular language, and can only be run
|
||||
/// on syntax nodes parsed with that language. References to Queries can be
|
||||
/// shared between multiple threads.
|
||||
pub fn new(language: Language, source: &str) -> Result<Self, QueryError> {
|
||||
pub fn new(language: &Language, source: &str) -> Result<Self, QueryError> {
|
||||
let mut error_offset = 0u32;
|
||||
let mut error_type: ffi::TSQueryError = 0;
|
||||
let bytes = source.as_bytes();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue