feat(api): expose function to check if symbol represents a supertype

This commit is contained in:
Riley Bruins 2024-09-15 20:10:08 -07:00 committed by Amaan Qureshi
parent 939e61c58d
commit 0683136ca0
4 changed files with 79 additions and 2 deletions

View file

@ -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
);
}
_ => {}
}
}
}
}

View file

@ -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)]

View file

@ -56,6 +56,7 @@ typedef enum TSInputEncoding {
typedef enum TSSymbolType {
TSSymbolTypeRegular,
TSSymbolTypeAnonymous,
TSSymbolTypeSupertype,
TSSymbolTypeAuxiliary,
} TSSymbolType;

View file

@ -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;
}