From 0683136ca041f60add8fc9b2e206b79f68bc9204 Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Sun, 15 Sep 2024 20:10:08 -0700 Subject: [PATCH] feat(api): expose function to check if symbol represents a supertype --- cli/src/tests/language_test.rs | 75 +++++++++++++++++++++++++++++++++- lib/binding_rust/bindings.rs | 3 +- lib/include/tree_sitter/api.h | 1 + lib/src/language.c | 2 + 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/cli/src/tests/language_test.rs b/cli/src/tests/language_test.rs index 681b93f3..7a60c456 100644 --- a/cli/src/tests/language_test.rs +++ b/cli/src/tests/language_test.rs @@ -1,4 +1,12 @@ -use tree_sitter::Parser; +use std::ffi::CStr; + +use tree_sitter::{ + ffi::{ + ts_language_symbol_name, ts_language_symbol_type, TSSymbol, TSSymbolTypeAnonymous, + TSSymbolTypeAuxiliary, TSSymbolTypeRegular, TSSymbolTypeSupertype, + }, + Parser, +}; use super::helpers::fixtures::get_language; @@ -63,3 +71,68 @@ fn test_lookahead_iterator_modifiable_only_by_mut() { let mut names = lookahead.iter_names(); let _ = names.next(); } + +#[test] +fn test_symbol_metadata_checks() { + let language = get_language("rust"); + let ts_language = language.clone().into_raw(); + for i in 0..language.node_kind_count() { + let ts_symbol: TSSymbol = i.try_into().unwrap(); + unsafe { + let name = CStr::from_ptr(ts_language_symbol_name(ts_language, ts_symbol)) + .to_str() + .unwrap(); + match name { + "_type" + | "_expression" + | "_pattern" + | "_literal" + | "_literal_pattern" + | "_declaration_statement" => { + assert_eq!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeSupertype + ); + assert_ne!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeAuxiliary + ); + } + "_raw_string_literal_start" + | "_raw_string_literal_end" + | "_line_doc_comment" + | "_error_sentinel" => { + assert_eq!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeAuxiliary + ); + assert_ne!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeSupertype + ); + } + "enum_item" | "struct_item" | "type_item" => { + assert_ne!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeSupertype + ); + assert_eq!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeRegular + ); + } + "=>" | "[" | "]" | "(" | ")" | "{" | "}" => { + assert_ne!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeSupertype + ); + assert_eq!( + ts_language_symbol_type(ts_language, ts_symbol), + TSSymbolTypeAnonymous + ); + } + _ => {} + } + } + } +} diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 6b69a099..fe41b900 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -40,7 +40,8 @@ pub const TSInputEncodingUTF16: TSInputEncoding = 1; pub type TSInputEncoding = ::core::ffi::c_uint; pub const TSSymbolTypeRegular: TSSymbolType = 0; pub const TSSymbolTypeAnonymous: TSSymbolType = 1; -pub const TSSymbolTypeAuxiliary: TSSymbolType = 2; +pub const TSSymbolTypeSupertype: TSSymbolType = 2; +pub const TSSymbolTypeAuxiliary: TSSymbolType = 3; pub type TSSymbolType = ::core::ffi::c_uint; #[repr(C)] #[derive(Debug, Copy, Clone)] diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 8bfc4843..362c236e 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -56,6 +56,7 @@ typedef enum TSInputEncoding { typedef enum TSSymbolType { TSSymbolTypeRegular, TSSymbolTypeAnonymous, + TSSymbolTypeSupertype, TSSymbolTypeAuxiliary, } TSSymbolType; diff --git a/lib/src/language.c b/lib/src/language.c index 20699f22..880d2f23 100644 --- a/lib/src/language.c +++ b/lib/src/language.c @@ -138,6 +138,8 @@ TSSymbolType ts_language_symbol_type( return TSSymbolTypeRegular; } else if (metadata.visible) { return TSSymbolTypeAnonymous; + } else if (metadata.supertype) { + return TSSymbolTypeSupertype; } else { return TSSymbolTypeAuxiliary; }