From b75196bb81176708faafb8b219991b628e3b0b08 Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Sat, 23 Aug 2025 10:18:15 +0300 Subject: [PATCH] feat(c): rename DecodeFunction to TSDecodeFunction Keep a typedef for backwards compatibility until ABI 16. --- docs/src/using-parsers/2-basic-parsing.md | 6 +++--- lib/binding_rust/bindings.rs | 4 ++-- lib/include/tree_sitter/api.h | 7 +++++-- lib/src/lexer.c | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/src/using-parsers/2-basic-parsing.md b/docs/src/using-parsers/2-basic-parsing.md index 3d03e1c9..77f6fb7a 100644 --- a/docs/src/using-parsers/2-basic-parsing.md +++ b/docs/src/using-parsers/2-basic-parsing.md @@ -38,15 +38,15 @@ typedef struct { uint32_t *bytes_read ); TSInputEncoding encoding; - DecodeFunction decode; + TSDecodeFunction decode; } TSInput; ``` If you want to decode text that is not encoded in UTF-8 or UTF-16, you can set the `decode` field of the input to your function -that will decode text. The signature of the `DecodeFunction` is as follows: +that will decode text. The signature of the `TSDecodeFunction` is as follows: ```c -typedef uint32_t (*DecodeFunction)( +typedef uint32_t (*TSDecodeFunction)( const uint8_t *string, uint32_t length, int32_t *code_point diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index 7d912665..c8d0e864 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -35,7 +35,7 @@ pub struct TSQueryCursor { pub struct TSLookaheadIterator { _unused: [u8; 0], } -pub type DecodeFunction = ::core::option::Option< +pub type TSDecodeFunction = ::core::option::Option< unsafe extern "C" fn(string: *const u8, length: u32, code_point: *mut i32) -> u32, >; pub const TSInputEncodingUTF8: TSInputEncoding = 0; @@ -75,7 +75,7 @@ pub struct TSInput { ) -> *const ::core::ffi::c_char, >, pub encoding: TSInputEncoding, - pub decode: DecodeFunction, + pub decode: TSDecodeFunction, } #[repr(C)] #[derive(Debug, Copy, Clone)] diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index aad67460..ffc108ca 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -51,12 +51,15 @@ typedef struct TSLookaheadIterator TSLookaheadIterator; // This function signature reads one code point from the given string, // returning the number of bytes consumed. It should write the code point // to the `code_point` pointer, or write -1 if the input is invalid. -typedef uint32_t (*DecodeFunction)( +typedef uint32_t (*TSDecodeFunction)( const uint8_t *string, uint32_t length, int32_t *code_point ); +// Deprecated alias to be removed in ABI 16 +typedef TSDecodeFunction DecodeFunction; + typedef enum TSInputEncoding { TSInputEncodingUTF8, TSInputEncodingUTF16LE, @@ -87,7 +90,7 @@ typedef struct TSInput { void *payload; const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read); TSInputEncoding encoding; - DecodeFunction decode; + TSDecodeFunction decode; } TSInput; typedef struct TSParseState { diff --git a/lib/src/lexer.c b/lib/src/lexer.c index 94124fd1..1709c418 100644 --- a/lib/src/lexer.c +++ b/lib/src/lexer.c @@ -114,7 +114,7 @@ static void ts_lexer__get_lookahead(Lexer *self) { } const uint8_t *chunk = (const uint8_t *)self->chunk + position_in_chunk; - DecodeFunction decode = + TSDecodeFunction decode = self->input.encoding == TSInputEncodingUTF8 ? ts_decode_utf8 : self->input.encoding == TSInputEncodingUTF16LE ? ts_decode_utf16_le : self->input.encoding == TSInputEncodingUTF16BE ? ts_decode_utf16_be : self->input.decode;