Skip to content

Commit 9cc1daa

Browse files
committed
chore(ffi): remove enum name prefixes from all C enum values
1 parent 5921b36 commit 9cc1daa

File tree

6 files changed

+57
-58
lines changed

6 files changed

+57
-58
lines changed

cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,8 @@ fn run() -> Result<()> {
448448
matches
449449
.values_of("encoding")
450450
.map_or(Ok(None), |mut e| match e.next() {
451-
Some("utf16") => Ok(Some(ffi::TSInputEncoding_TSInputEncodingUTF16)),
452-
Some("utf8") => Ok(Some(ffi::TSInputEncoding_TSInputEncodingUTF8)),
451+
Some("utf16") => Ok(Some(ffi::TSInputEncodingUTF16)),
452+
Some("utf8") => Ok(Some(ffi::TSInputEncodingUTF8)),
453453
Some(_) => Err(anyhow!("Invalid encoding. Expected one of: utf8, utf16")),
454454
None => Ok(None),
455455
})?;

cli/src/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn parse_file_at_path(opts: ParseFileOptions) -> Result<bool> {
8888
}
8989

9090
let tree = match opts.encoding {
91-
Some(encoding) if encoding == ffi::TSInputEncoding_TSInputEncodingUTF16 => {
91+
Some(encoding) if encoding == ffi::TSInputEncodingUTF16 => {
9292
let source_code_utf16 = source_code
9393
.chunks_exact(2)
9494
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))

lib/binding_rust/bindings.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ pub struct TSQueryCursor {
3535
pub struct TSLookaheadIterator {
3636
_unused: [u8; 0],
3737
}
38-
pub const TSInputEncoding_TSInputEncodingUTF8: TSInputEncoding = 0;
39-
pub const TSInputEncoding_TSInputEncodingUTF16: TSInputEncoding = 1;
38+
pub const TSInputEncodingUTF8: TSInputEncoding = 0;
39+
pub const TSInputEncodingUTF16: TSInputEncoding = 1;
4040
pub type TSInputEncoding = ::std::os::raw::c_uint;
41-
pub const TSSymbolType_TSSymbolTypeRegular: TSSymbolType = 0;
42-
pub const TSSymbolType_TSSymbolTypeAnonymous: TSSymbolType = 1;
43-
pub const TSSymbolType_TSSymbolTypeAuxiliary: TSSymbolType = 2;
41+
pub const TSSymbolTypeRegular: TSSymbolType = 0;
42+
pub const TSSymbolTypeAnonymous: TSSymbolType = 1;
43+
pub const TSSymbolTypeAuxiliary: TSSymbolType = 2;
4444
pub type TSSymbolType = ::std::os::raw::c_uint;
4545
#[repr(C)]
4646
#[derive(Debug, Copy, Clone)]
@@ -70,8 +70,8 @@ pub struct TSInput {
7070
>,
7171
pub encoding: TSInputEncoding,
7272
}
73-
pub const TSLogType_TSLogTypeParse: TSLogType = 0;
74-
pub const TSLogType_TSLogTypeLex: TSLogType = 1;
73+
pub const TSLogTypeParse: TSLogType = 0;
74+
pub const TSLogTypeLex: TSLogType = 1;
7575
pub type TSLogType = ::std::os::raw::c_uint;
7676
#[repr(C)]
7777
#[derive(Debug)]
@@ -115,11 +115,11 @@ pub struct TSQueryCapture {
115115
pub node: TSNode,
116116
pub index: u32,
117117
}
118-
pub const TSQuantifier_TSQuantifierZero: TSQuantifier = 0;
119-
pub const TSQuantifier_TSQuantifierZeroOrOne: TSQuantifier = 1;
120-
pub const TSQuantifier_TSQuantifierZeroOrMore: TSQuantifier = 2;
121-
pub const TSQuantifier_TSQuantifierOne: TSQuantifier = 3;
122-
pub const TSQuantifier_TSQuantifierOneOrMore: TSQuantifier = 4;
118+
pub const TSQuantifierZero: TSQuantifier = 0;
119+
pub const TSQuantifierZeroOrOne: TSQuantifier = 1;
120+
pub const TSQuantifierZeroOrMore: TSQuantifier = 2;
121+
pub const TSQuantifierOne: TSQuantifier = 3;
122+
pub const TSQuantifierOneOrMore: TSQuantifier = 4;
123123
pub type TSQuantifier = ::std::os::raw::c_uint;
124124
#[repr(C)]
125125
#[derive(Debug)]
@@ -129,23 +129,23 @@ pub struct TSQueryMatch {
129129
pub capture_count: u16,
130130
pub captures: *const TSQueryCapture,
131131
}
132-
pub const TSQueryPredicateStepType_TSQueryPredicateStepTypeDone: TSQueryPredicateStepType = 0;
133-
pub const TSQueryPredicateStepType_TSQueryPredicateStepTypeCapture: TSQueryPredicateStepType = 1;
134-
pub const TSQueryPredicateStepType_TSQueryPredicateStepTypeString: TSQueryPredicateStepType = 2;
132+
pub const TSQueryPredicateStepTypeDone: TSQueryPredicateStepType = 0;
133+
pub const TSQueryPredicateStepTypeCapture: TSQueryPredicateStepType = 1;
134+
pub const TSQueryPredicateStepTypeString: TSQueryPredicateStepType = 2;
135135
pub type TSQueryPredicateStepType = ::std::os::raw::c_uint;
136136
#[repr(C)]
137137
#[derive(Debug)]
138138
pub struct TSQueryPredicateStep {
139139
pub type_: TSQueryPredicateStepType,
140140
pub value_id: u32,
141141
}
142-
pub const TSQueryError_TSQueryErrorNone: TSQueryError = 0;
143-
pub const TSQueryError_TSQueryErrorSyntax: TSQueryError = 1;
144-
pub const TSQueryError_TSQueryErrorNodeType: TSQueryError = 2;
145-
pub const TSQueryError_TSQueryErrorField: TSQueryError = 3;
146-
pub const TSQueryError_TSQueryErrorCapture: TSQueryError = 4;
147-
pub const TSQueryError_TSQueryErrorStructure: TSQueryError = 5;
148-
pub const TSQueryError_TSQueryErrorLanguage: TSQueryError = 6;
142+
pub const TSQueryErrorNone: TSQueryError = 0;
143+
pub const TSQueryErrorSyntax: TSQueryError = 1;
144+
pub const TSQueryErrorNodeType: TSQueryError = 2;
145+
pub const TSQueryErrorField: TSQueryError = 3;
146+
pub const TSQueryErrorCapture: TSQueryError = 4;
147+
pub const TSQueryErrorStructure: TSQueryError = 5;
148+
pub const TSQueryErrorLanguage: TSQueryError = 6;
149149
pub type TSQueryError = ::std::os::raw::c_uint;
150150
extern "C" {
151151
#[doc = " Create a new parser."]

lib/binding_rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ fn generate_bindings() {
6464
.allowlist_function("^ts_.*")
6565
.allowlist_var("^TREE_SITTER.*")
6666
.no_copy(no_copy.join("|"))
67+
.prepend_enum_name(false)
6768
.generate()
6869
.expect("Failed to generate bindings");
6970

lib/binding_rust/lib.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ pub enum CaptureQuantifier {
137137
impl From<ffi::TSQuantifier> for CaptureQuantifier {
138138
fn from(value: ffi::TSQuantifier) -> Self {
139139
match value {
140-
ffi::TSQuantifier_TSQuantifierZero => CaptureQuantifier::Zero,
141-
ffi::TSQuantifier_TSQuantifierZeroOrOne => CaptureQuantifier::ZeroOrOne,
142-
ffi::TSQuantifier_TSQuantifierZeroOrMore => CaptureQuantifier::ZeroOrMore,
143-
ffi::TSQuantifier_TSQuantifierOne => CaptureQuantifier::One,
144-
ffi::TSQuantifier_TSQuantifierOneOrMore => CaptureQuantifier::OneOrMore,
140+
ffi::TSQuantifierZero => CaptureQuantifier::Zero,
141+
ffi::TSQuantifierZeroOrOne => CaptureQuantifier::ZeroOrOne,
142+
ffi::TSQuantifierZeroOrMore => CaptureQuantifier::ZeroOrMore,
143+
ffi::TSQuantifierOne => CaptureQuantifier::One,
144+
ffi::TSQuantifierOneOrMore => CaptureQuantifier::OneOrMore,
145145
_ => panic!("Unrecognized quantifier: {}", value),
146146
}
147147
}
@@ -312,14 +312,12 @@ impl Language {
312312
/// Check if the node type for the given numerical id is named (as opposed
313313
/// to an anonymous node type).
314314
pub fn node_kind_is_named(&self, id: u16) -> bool {
315-
unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolType_TSSymbolTypeRegular }
315+
unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeRegular }
316316
}
317317

318318
#[doc(alias = "ts_language_symbol_type")]
319319
pub fn node_kind_is_visible(&self, id: u16) -> bool {
320-
unsafe {
321-
ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolType_TSSymbolTypeAnonymous
322-
}
320+
unsafe { ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolTypeAnonymous }
323321
}
324322

325323
/// Get the number of distinct field names in this language.
@@ -445,7 +443,7 @@ impl Parser {
445443
) {
446444
let callback = (payload as *mut Logger).as_mut().unwrap();
447445
if let Ok(message) = CStr::from_ptr(c_message).to_str() {
448-
let log_type = if c_log_type == ffi::TSLogType_TSLogTypeParse {
446+
let log_type = if c_log_type == ffi::TSLogTypeParse {
449447
LogType::Parse
450448
} else {
451449
LogType::Lex
@@ -571,7 +569,7 @@ impl Parser {
571569
let c_input = ffi::TSInput {
572570
payload: &mut payload as *mut (&mut F, Option<T>) as *mut c_void,
573571
read: Some(read::<T, F>),
574-
encoding: ffi::TSInputEncoding_TSInputEncodingUTF8,
572+
encoding: ffi::TSInputEncodingUTF8,
575573
};
576574

577575
let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
@@ -627,7 +625,7 @@ impl Parser {
627625
let c_input = ffi::TSInput {
628626
payload: &mut payload as *mut (&mut F, Option<T>) as *mut c_void,
629627
read: Some(read::<T, F>),
630-
encoding: ffi::TSInputEncoding_TSInputEncodingUTF16,
628+
encoding: ffi::TSInputEncodingUTF16,
631629
};
632630

633631
let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
@@ -1568,7 +1566,7 @@ impl Query {
15681566

15691567
// On failure, build an error based on the error code and offset.
15701568
if ptr.is_null() {
1571-
if error_type == ffi::TSQueryError_TSQueryErrorLanguage {
1569+
if error_type == ffi::TSQueryErrorLanguage {
15721570
return Err(QueryError {
15731571
row: 0,
15741572
column: 0,
@@ -1600,18 +1598,16 @@ impl Query {
16001598
let message;
16011599
match error_type {
16021600
// Error types that report names
1603-
ffi::TSQueryError_TSQueryErrorNodeType
1604-
| ffi::TSQueryError_TSQueryErrorField
1605-
| ffi::TSQueryError_TSQueryErrorCapture => {
1601+
ffi::TSQueryErrorNodeType | ffi::TSQueryErrorField | ffi::TSQueryErrorCapture => {
16061602
let suffix = source.split_at(offset).1;
16071603
let end_offset = suffix
16081604
.find(|c| !char::is_alphanumeric(c) && c != '_' && c != '-')
16091605
.unwrap_or(suffix.len());
16101606
message = suffix.split_at(end_offset).0.to_string();
16111607
kind = match error_type {
1612-
ffi::TSQueryError_TSQueryErrorNodeType => QueryErrorKind::NodeType,
1613-
ffi::TSQueryError_TSQueryErrorField => QueryErrorKind::Field,
1614-
ffi::TSQueryError_TSQueryErrorCapture => QueryErrorKind::Capture,
1608+
ffi::TSQueryErrorNodeType => QueryErrorKind::NodeType,
1609+
ffi::TSQueryErrorField => QueryErrorKind::Field,
1610+
ffi::TSQueryErrorCapture => QueryErrorKind::Capture,
16151611
_ => unreachable!(),
16161612
};
16171613
}
@@ -1624,7 +1620,7 @@ impl Query {
16241620
"Unexpected EOF".to_string()
16251621
};
16261622
kind = match error_type {
1627-
ffi::TSQueryError_TSQueryErrorStructure => QueryErrorKind::Structure,
1623+
ffi::TSQueryErrorStructure => QueryErrorKind::Structure,
16281624
_ => QueryErrorKind::Syntax,
16291625
};
16301626
}
@@ -1720,20 +1716,21 @@ impl Query {
17201716
.filter(|(_, c)| *c == '\n')
17211717
.count();
17221718

1723-
let type_done = ffi::TSQueryPredicateStepType_TSQueryPredicateStepTypeDone;
1724-
let type_capture = ffi::TSQueryPredicateStepType_TSQueryPredicateStepTypeCapture;
1725-
let type_string = ffi::TSQueryPredicateStepType_TSQueryPredicateStepTypeString;
1719+
use ffi::TSQueryPredicateStepType as T;
1720+
const TYPE_DONE: T = ffi::TSQueryPredicateStepTypeDone;
1721+
const TYPE_CAPTURE: T = ffi::TSQueryPredicateStepTypeCapture;
1722+
const TYPE_STRING: T = ffi::TSQueryPredicateStepTypeString;
17261723

17271724
let mut text_predicates = Vec::new();
17281725
let mut property_predicates = Vec::new();
17291726
let mut property_settings = Vec::new();
17301727
let mut general_predicates = Vec::new();
1731-
for p in predicate_steps.split(|s| s.type_ == type_done) {
1728+
for p in predicate_steps.split(|s| s.type_ == TYPE_DONE) {
17321729
if p.is_empty() {
17331730
continue;
17341731
}
17351732

1736-
if p[0].type_ != type_string {
1733+
if p[0].type_ != TYPE_STRING {
17371734
return Err(predicate_error(
17381735
row,
17391736
format!(
@@ -1756,7 +1753,7 @@ impl Query {
17561753
),
17571754
));
17581755
}
1759-
if p[1].type_ != type_capture {
1756+
if p[1].type_ != TYPE_CAPTURE {
17601757
return Err(predicate_error(row, format!(
17611758
"First argument to #eq? predicate must be a capture name. Got literal \"{}\".",
17621759
string_values[p[1].value_id as usize],
@@ -1769,7 +1766,7 @@ impl Query {
17691766
"any-eq?" | "any-not-eq?" => false,
17701767
_ => unreachable!(),
17711768
};
1772-
text_predicates.push(if p[2].type_ == type_capture {
1769+
text_predicates.push(if p[2].type_ == TYPE_CAPTURE {
17731770
TextPredicateCapture::EqCapture(
17741771
p[1].value_id,
17751772
p[2].value_id,
@@ -1793,13 +1790,13 @@ impl Query {
17931790
p.len() - 1
17941791
)));
17951792
}
1796-
if p[1].type_ != type_capture {
1793+
if p[1].type_ != TYPE_CAPTURE {
17971794
return Err(predicate_error(row, format!(
17981795
"First argument to #match? predicate must be a capture name. Got literal \"{}\".",
17991796
string_values[p[1].value_id as usize],
18001797
)));
18011798
}
1802-
if p[2].type_ == type_capture {
1799+
if p[2].type_ == TYPE_CAPTURE {
18031800
return Err(predicate_error(row, format!(
18041801
"Second argument to #match? predicate must be a literal. Got capture @{}.",
18051802
capture_names[p[2].value_id as usize],
@@ -1850,7 +1847,7 @@ impl Query {
18501847
p.len() - 1
18511848
)));
18521849
}
1853-
if p[1].type_ != type_capture {
1850+
if p[1].type_ != TYPE_CAPTURE {
18541851
return Err(predicate_error(row, format!(
18551852
"First argument to #any-of? predicate must be a capture name. Got literal \"{}\".",
18561853
string_values[p[1].value_id as usize],
@@ -1860,7 +1857,7 @@ impl Query {
18601857
let is_positive = operator_name == "any-of?";
18611858
let mut values = Vec::new();
18621859
for arg in &p[2..] {
1863-
if arg.type_ == type_capture {
1860+
if arg.type_ == TYPE_CAPTURE {
18641861
return Err(predicate_error(row, format!(
18651862
"Arguments to #any-of? predicate must be literals. Got capture @{}.",
18661863
capture_names[arg.value_id as usize],
@@ -1884,7 +1881,7 @@ impl Query {
18841881
args: p[1..]
18851882
.iter()
18861883
.map(|a| {
1887-
if a.type_ == type_capture {
1884+
if a.type_ == TYPE_CAPTURE {
18881885
QueryPredicateArg::Capture(a.value_id)
18891886
} else {
18901887
QueryPredicateArg::String(
@@ -2052,7 +2049,7 @@ impl Query {
20522049
let mut value = None;
20532050

20542051
for arg in args {
2055-
if arg.type_ == ffi::TSQueryPredicateStepType_TSQueryPredicateStepTypeCapture {
2052+
if arg.type_ == ffi::TSQueryPredicateStepTypeCapture {
20562053
if capture_id.is_some() {
20572054
return Err(predicate_error(
20582055
row,

script/generate-bindings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ bindgen \
3535
--allowlist-function '^ts_.*' \
3636
--allowlist-var "^TREE_SITTER.*" \
3737
--blocklist-type '^__.*' \
38+
--no-prepend-enum-name \
3839
--no-copy "$no_copy" \
3940
$header_path > $output_path

0 commit comments

Comments
 (0)