Skip to content

Commit

Permalink
bump tree-sitter to v0.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
smacker committed Oct 23, 2022
1 parent b194e83 commit c25dd19
Show file tree
Hide file tree
Showing 17 changed files with 941 additions and 302 deletions.
48 changes: 48 additions & 0 deletions alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "alloc.h"
#include <stdlib.h>

static void *ts_malloc_default(size_t size) {
void *result = malloc(size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size);
exit(1);
}
return result;
}

static void *ts_calloc_default(size_t count, size_t size) {
void *result = calloc(count, size);
if (count > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size);
exit(1);
}
return result;
}

static void *ts_realloc_default(void *buffer, size_t size) {
void *result = realloc(buffer, size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size);
exit(1);
}
return result;
}

// Allow clients to override allocation functions dynamically
void *(*ts_current_malloc)(size_t) = ts_malloc_default;
void *(*ts_current_calloc)(size_t, size_t) = ts_calloc_default;
void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default;
void (*ts_current_free)(void *) = free;

void ts_set_allocator(
void *(*new_malloc)(size_t),
void *(*new_calloc)(size_t, size_t),
void *(*new_realloc)(void *, size_t),
void (*new_free)(void *)
) {
ts_current_malloc = new_malloc ? new_malloc : ts_malloc_default;
ts_current_calloc = new_calloc ? new_calloc : ts_calloc_default;
ts_current_realloc = new_realloc ? new_realloc : ts_realloc_default;
ts_current_free = new_free ? new_free : free;
}

70 changes: 10 additions & 60 deletions alloc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef TREE_SITTER_ALLOC_H_
#define TREE_SITTER_ALLOC_H_

#include "api.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -9,75 +11,23 @@ extern "C" {
#include <stdbool.h>
#include <stdio.h>

#if defined(TREE_SITTER_ALLOCATION_TRACKING)

void *ts_record_malloc(size_t);
void *ts_record_calloc(size_t, size_t);
void *ts_record_realloc(void *, size_t);
void ts_record_free(void *);
bool ts_toggle_allocation_recording(bool);

#define ts_malloc ts_record_malloc
#define ts_calloc ts_record_calloc
#define ts_realloc ts_record_realloc
#define ts_free ts_record_free

#else
extern void *(*ts_current_malloc)(size_t);
extern void *(*ts_current_calloc)(size_t, size_t);
extern void *(*ts_current_realloc)(void *, size_t);
extern void (*ts_current_free)(void *);

// Allow clients to override allocation functions

#ifndef ts_malloc
#define ts_malloc ts_malloc_default
#define ts_malloc ts_current_malloc
#endif
#ifndef ts_calloc
#define ts_calloc ts_calloc_default
#define ts_calloc ts_current_calloc
#endif
#ifndef ts_realloc
#define ts_realloc ts_realloc_default
#define ts_realloc ts_current_realloc
#endif
#ifndef ts_free
#define ts_free ts_free_default
#endif

#include <stdlib.h>

static inline bool ts_toggle_allocation_recording(bool value) {
(void)value;
return false;
}


static inline void *ts_malloc_default(size_t size) {
void *result = malloc(size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size);
exit(1);
}
return result;
}

static inline void *ts_calloc_default(size_t count, size_t size) {
void *result = calloc(count, size);
if (count > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size);
exit(1);
}
return result;
}

static inline void *ts_realloc_default(void *buffer, size_t size) {
void *result = realloc(buffer, size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size);
exit(1);
}
return result;
}

static inline void ts_free_default(void *buffer) {
free(buffer);
}

#define ts_free ts_current_free
#endif

#ifdef __cplusplus
Expand Down
48 changes: 47 additions & 1 deletion api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {
* The Tree-sitter library is generally backwards-compatible with languages
* generated using older CLI versions, but is not forwards-compatible.
*/
#define TREE_SITTER_LANGUAGE_VERSION 13
#define TREE_SITTER_LANGUAGE_VERSION 14

/**
* The earliest ABI version that is supported by the current version of the
Expand Down Expand Up @@ -106,6 +106,14 @@ typedef struct {
uint32_t index;
} TSQueryCapture;

typedef enum {
TSQuantifierZero = 0, // must match the array initialization value
TSQuantifierZeroOrOne,
TSQuantifierZeroOrMore,
TSQuantifierOne,
TSQuantifierOneOrMore,
} TSQuantifier;

typedef struct {
uint32_t id;
uint16_t pattern_index;
Expand Down Expand Up @@ -740,6 +748,17 @@ const char *ts_query_capture_name_for_id(
uint32_t id,
uint32_t *length
);

/**
* Get the quantifier of the query's captures. Each capture is * associated
* with a numeric id based on the order that it appeared in the query's source.
*/
TSQuantifier ts_query_capture_quantifier_for_id(
const TSQuery *,
uint32_t pattern_id,
uint32_t capture_id
);

const char *ts_query_string_value_for_id(
const TSQuery *,
uint32_t id,
Expand Down Expand Up @@ -896,6 +915,33 @@ TSSymbolType ts_language_symbol_type(const TSLanguage *, TSSymbol);
*/
uint32_t ts_language_version(const TSLanguage *);

/**********************************/
/* Section - Global Configuration */
/**********************************/

/**
* Set the allocation functions used by the library.
*
* By default, Tree-sitter uses the standard libc allocation functions,
* but aborts the process when an allocation fails. This function lets
* you supply alternative allocation functions at runtime.
*
* If you pass `NULL` for any parameter, Tree-sitter will switch back to
* its default implementation of that function.
*
* If you call this function after the library has already been used, then
* you must ensure that either:
* 1. All the existing objects have been freed.
* 2. The new allocator shares its state with the old one, so it is capable
* of freeing memory that was allocated by the old allocator.
*/
void ts_set_allocator(
void *(*new_malloc)(size_t),
void *(*new_calloc)(size_t, size_t),
void *(*new_realloc)(void *, size_t),
void (*new_free)(void *)
);

#ifdef __cplusplus
}
#endif
Expand Down
43 changes: 30 additions & 13 deletions get_changed_ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

// #define DEBUG_GET_CHANGED_RANGES

static void ts_range_array_add(TSRangeArray *self, Length start, Length end) {
static void ts_range_array_add(
TSRangeArray *self,
Length start,
Length end
) {
if (self->size > 0) {
TSRange *last_range = array_back(self);
if (start.bytes <= last_range->end_byte) {
Expand All @@ -23,8 +27,12 @@ static void ts_range_array_add(TSRangeArray *self, Length start, Length end) {
}
}

bool ts_range_array_intersects(const TSRangeArray *self, unsigned start_index,
uint32_t start_byte, uint32_t end_byte) {
bool ts_range_array_intersects(
const TSRangeArray *self,
unsigned start_index,
uint32_t start_byte,
uint32_t end_byte
) {
for (unsigned i = start_index; i < self->size; i++) {
TSRange *range = &self->contents[i];
if (range->end_byte > start_byte) {
Expand Down Expand Up @@ -102,9 +110,13 @@ typedef struct {
bool in_padding;
} Iterator;

static Iterator iterator_new(TreeCursor *cursor, const Subtree *tree, const TSLanguage *language) {
static Iterator iterator_new(
TreeCursor *cursor,
const Subtree *tree,
const TSLanguage *language
) {
array_clear(&cursor->stack);
array_push(&cursor->stack, ((TreeCursorEntry){
array_push(&cursor->stack, ((TreeCursorEntry) {
.subtree = tree,
.position = length_zero(),
.child_index = 0,
Expand Down Expand Up @@ -210,7 +222,7 @@ static bool iterator_descend(Iterator *self, uint32_t goal_position) {
Length child_right = length_add(child_left, ts_subtree_size(*child));

if (child_right.bytes > goal_position) {
array_push(&self->cursor.stack, ((TreeCursorEntry){
array_push(&self->cursor.stack, ((TreeCursorEntry) {
.subtree = child,
.position = position,
.child_index = i,
Expand Down Expand Up @@ -262,7 +274,7 @@ static void iterator_advance(Iterator *self) {
if (!ts_subtree_extra(*entry.subtree)) structural_child_index++;
const Subtree *next_child = &ts_subtree_children(*parent)[child_index];

array_push(&self->cursor.stack, ((TreeCursorEntry){
array_push(&self->cursor.stack, ((TreeCursorEntry) {
.subtree = next_child,
.position = position,
.child_index = child_index,
Expand All @@ -289,7 +301,10 @@ typedef enum {
IteratorMatches,
} IteratorComparison;

static IteratorComparison iterator_compare(const Iterator *old_iter, const Iterator *new_iter) {
static IteratorComparison iterator_compare(
const Iterator *old_iter,
const Iterator *new_iter
) {
Subtree old_tree = NULL_SUBTREE;
Subtree new_tree = NULL_SUBTREE;
uint32_t old_start = 0;
Expand Down Expand Up @@ -339,11 +354,13 @@ static inline void iterator_print_state(Iterator *self) {
}
#endif

unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *new_tree,
TreeCursor *cursor1, TreeCursor *cursor2,
const TSLanguage *language,
const TSRangeArray *included_range_differences,
TSRange **ranges) {
unsigned ts_subtree_get_changed_ranges(
const Subtree *old_tree, const Subtree *new_tree,
TreeCursor *cursor1, TreeCursor *cursor2,
const TSLanguage *language,
const TSRangeArray *included_range_differences,
TSRange **ranges
) {
TSRangeArray results = array_new();

Iterator old_iter = iterator_new(cursor1, old_tree, language);
Expand Down
21 changes: 21 additions & 0 deletions host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// Determine endian and pointer size based on known defines.
// TS_BIG_ENDIAN and TS_PTR_SIZE can be set as -D compiler arguments
// to override this.

#if !defined(TS_BIG_ENDIAN)
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
|| (defined( __APPLE_CC__) && (defined(__ppc__) || defined(__ppc64__)))
#define TS_BIG_ENDIAN 1
#else
#define TS_BIG_ENDIAN 0
#endif
#endif

#if !defined(TS_PTR_SIZE)
#if UINTPTR_MAX == 0xFFFFFFFF
#define TS_PTR_SIZE 32
#else
#define TS_PTR_SIZE 64
#endif
#endif
4 changes: 2 additions & 2 deletions language.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ TSSymbolMetadata ts_language_symbol_metadata(
TSSymbol symbol
) {
if (symbol == ts_builtin_sym_error) {
return (TSSymbolMetadata){.visible = true, .named = true};
return (TSSymbolMetadata) {.visible = true, .named = true};
} else if (symbol == ts_builtin_sym_error_repeat) {
return (TSSymbolMetadata){.visible = false, .named = false};
return (TSSymbolMetadata) {.visible = false, .named = false};
} else {
return self->symbol_metadata[symbol];
}
Expand Down
13 changes: 13 additions & 0 deletions language.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ static inline TSStateId ts_language_next_state(
}
}

// Whether the state is a "primary state". If this returns false, it indicates that there exists
// another state that behaves identically to this one with respect to query analysis.
static inline bool ts_language_state_is_primary(
const TSLanguage *self,
TSStateId state
) {
if (self->version >= 14) {
return state == self->primary_state_ids[state];
} else {
return true;
}
}

static inline const bool *ts_language_enabled_external_tokens(
const TSLanguage *self,
unsigned external_scanner_state
Expand Down
Loading

0 comments on commit c25dd19

Please sign in to comment.