feat!: introduce parser introspection via the repo's Semantic Version baked in

This commit is contained in:
Amaan Qureshi 2024-02-12 02:27:17 -05:00
parent b66b1a7a92
commit 2c192fa038
No known key found for this signature in database
GPG key ID: E67890ADC4227273
15 changed files with 1862 additions and 2156 deletions

View file

@ -1,7 +1,7 @@
/* automatically generated by rust-bindgen 0.69.4 */
pub const TREE_SITTER_LANGUAGE_VERSION: u32 = 14;
pub const TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION: u32 = 13;
pub const TREE_SITTER_LANGUAGE_VERSION: u32 = 15;
pub const TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION: u32 = 15;
pub type TSStateId = u16;
pub type TSSymbol = u16;
pub type TSFieldId = u16;
@ -721,7 +721,11 @@ extern "C" {
}
extern "C" {
#[doc = " Get the ABI version number for this language. This version number is used\n to ensure that languages were generated by a compatible version of\n Tree-sitter.\n\n See also [`ts_parser_set_language`]."]
pub fn ts_language_version(self_: *const TSLanguage) -> u32;
pub fn ts_language_abi_version(self_: *const TSLanguage) -> u32;
}
extern "C" {
#[doc = " Get the [Semantic Version](https://semver.org/) for this language. This\n version number is used to signal if a given parser might be incompatible\n with existing queries when upgraded between major versions.\n\n The Semantic Version is encoded as an unsigned 32-bit integer, where the\n major, minor, and patch version numbers are encoded in the 24 least significant\n bits of the integer, using 8 bits for each number.\n\n Layout of the returned integer:\n\n MSB LSB\n +--------+--------+--------+--------+\n |00000000| Major | Minor | Patch |\n +--------+--------+--------+--------+\n 31 24 23 16 15 8 7 0\n"]
pub fn ts_language_semantic_version(self_: *const TSLanguage) -> u32;
}
extern "C" {
#[doc = " Get the next parse state. Combine this with lookahead iterators to generate\n completion suggestions or valid symbols in error nodes. Use\n [`ts_node_grammar_symbol`] for valid symbols."]

View file

@ -282,10 +282,31 @@ pub struct LossyUtf8<'a> {
impl Language {
/// Get the ABI version number that indicates which version of the Tree-sitter CLI
/// that was used to generate this [`Language`].
#[doc(alias = "ts_language_version")]
#[doc(alias = "ts_language_abi_version")]
#[must_use]
pub fn version(&self) -> usize {
unsafe { ffi::ts_language_version(self.0) as usize }
pub fn abi_version(&self) -> usize {
unsafe { ffi::ts_language_abi_version(self.0) as usize }
}
/// Get the [Semantic Version](https://semver.org/) for this language. This
/// version number is used to signal if a given parser might be incompatible
/// with existing queries when upgraded between major versions.
///
/// The Semantic Version is encoded as an unsigned 32-bit integer, where the
/// major, minor, and patch version numbers are encoded in the 24 least significant
/// bits of the integer, using 8 bits for each number.
///
/// Layout of the returned integer:
///
/// MSB LSB
/// +--------+--------+--------+--------+
/// |00000000| Major | Minor | Patch |
/// +--------+--------+--------+--------+
/// 31 24 23 16 15 8 7 0
#[doc(alias = "ts_language_semantic_version")]
#[must_use]
pub fn semantic_version(&self) -> usize {
unsafe { ffi::ts_language_semantic_version(self.0) as usize }
}
/// Get the number of distinct node types in this language.
@ -448,7 +469,7 @@ impl Parser {
/// [`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> {
let version = language.version();
let version = language.abi_version();
if (MIN_COMPATIBLE_LANGUAGE_VERSION..=LANGUAGE_VERSION).contains(&version) {
unsafe {
ffi::ts_parser_set_language(self.0.as_ptr(), language.0);
@ -1728,7 +1749,7 @@ impl Query {
column: 0,
offset: 0,
message: LanguageError {
version: language.version(),
version: language.abi_version(),
}
.to_string(),
kind: QueryErrorKind::Language,