Skip to content

Commit

Permalink
update tree sitter to 0.17.3
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Dec 1, 2020
1 parent 295ad9d commit ac4081f
Show file tree
Hide file tree
Showing 20 changed files with 3,041 additions and 863 deletions.
48 changes: 27 additions & 21 deletions alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,64 @@ void *ts_record_realloc(void *, size_t);
void ts_record_free(void *);
bool ts_toggle_allocation_recording(bool);

static inline void *ts_malloc(size_t size) {
return ts_record_malloc(size);
}

static inline void *ts_calloc(size_t count, size_t size) {
return ts_record_calloc(count, size);
}
#define ts_malloc ts_record_malloc
#define ts_calloc ts_record_calloc
#define ts_realloc ts_record_realloc
#define ts_free ts_record_free

static inline void *ts_realloc(void *buffer, size_t size) {
return ts_record_realloc(buffer, size);
}
#else

static inline void ts_free(void *buffer) {
ts_record_free(buffer);
}
// Allow clients to override allocation functions

#else
#ifndef ts_malloc
#define ts_malloc ts_malloc_default
#endif
#ifndef ts_calloc
#define ts_calloc ts_calloc_default
#endif
#ifndef ts_realloc
#define ts_realloc ts_realloc_default
#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(size_t size) {

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

static inline void *ts_calloc(size_t count, size_t size) {
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 %lu bytes", count * size);
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size);
exit(1);
}
return result;
}

static inline void *ts_realloc(void *buffer, size_t size) {
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 %lu bytes", size);
fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size);
exit(1);
}
return result;
}

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

Expand Down
14 changes: 10 additions & 4 deletions 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 11
#define TREE_SITTER_LANGUAGE_VERSION 12

/**
* The earliest ABI version that is supported by the current version of the
Expand Down Expand Up @@ -130,6 +130,7 @@ typedef enum {
TSQueryErrorNodeType,
TSQueryErrorField,
TSQueryErrorCapture,
TSQueryErrorStructure,
} TSQueryError;

/********************/
Expand Down Expand Up @@ -219,8 +220,8 @@ const TSRange *ts_parser_included_ranges(
* following three fields:
* 1. `read`: A function to retrieve a chunk of text at a given byte offset
* and (row, column) position. The function should return a pointer to the
* text and write its length to the the `bytes_read` pointer. The parser
* does not take ownership of this buffer; it just borrows it until it has
* text and write its length to the `bytes_read` pointer. The parser does
* not take ownership of this buffer; it just borrows it until it has
* finished reading it. The function should write a zero value to the
* `bytes_read` pointer to indicate the end of the document.
* 2. `payload`: An arbitrary pointer that will be passed to each invocation
Expand Down Expand Up @@ -718,6 +719,11 @@ const TSQueryPredicateStep *ts_query_predicates_for_pattern(
uint32_t *length
);

bool ts_query_step_is_definite(
const TSQuery *self,
uint32_t byte_offset
);

/**
* Get the name and length of one of the query's captures, or one of the
* query's string literals. Each capture and string is associated with a
Expand Down Expand Up @@ -759,7 +765,7 @@ void ts_query_disable_pattern(TSQuery *, uint32_t);
* to start running a given query on a given syntax node. Then, there are
* two options for consuming the results of the query:
* 1. Repeatedly call `ts_query_cursor_next_match` to iterate over all of the
* the *matches* in the order that they were found. Each match contains the
* *matches* in the order that they were found. Each match contains the
* index of the pattern that matched, and an array of captures. Because
* multiple patterns can match the same set of nodes, one match may contain
* captures that appear *before* some of the captures from a previous match.
Expand Down
143 changes: 124 additions & 19 deletions array.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ extern "C" {
#include <stdbool.h>
#include "./alloc.h"

#define Array(T) \
struct { \
T *contents; \
#define Array(T) \
struct { \
T *contents; \
uint32_t size; \
uint32_t capacity; \
}
Expand All @@ -37,35 +37,94 @@ extern "C" {
#define array_reserve(self, new_capacity) \
array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity)

#define array_erase(self, index) \
array__erase((VoidArray *)(self), array__elem_size(self), index)

// Free any memory allocated for this array.
#define array_delete(self) array__delete((VoidArray *)self)

#define array_push(self, element) \
(array__grow((VoidArray *)(self), 1, array__elem_size(self)), \
(self)->contents[(self)->size++] = (element))

// Increase the array's size by a given number of elements, reallocating
// if necessary. New elements are zero-initialized.
#define array_grow_by(self, count) \
(array__grow((VoidArray *)(self), count, array__elem_size(self)), \
memset((self)->contents + (self)->size, 0, (count) * array__elem_size(self)), \
(self)->size += (count))

#define array_push_all(self, other) \
array_splice((self), (self)->size, 0, (other)->size, (other)->contents)

#define array_splice(self, index, old_count, new_count, new_contents) \
array__splice((VoidArray *)(self), array__elem_size(self), index, old_count, \
new_count, new_contents)

array_extend((self), (other)->size, (other)->contents)

// Append `count` elements to the end of the array, reading their values from the
// `contents` pointer.
#define array_extend(self, count, contents) \
array__splice( \
(VoidArray *)(self), array__elem_size(self), (self)->size, \
0, count, contents \
)

// Remove `old_count` elements from the array starting at the given `index`. At
// the same index, insert `new_count` new elements, reading their values from the
// `new_contents` pointer.
#define array_splice(self, index, old_count, new_count, new_contents) \
array__splice( \
(VoidArray *)(self), array__elem_size(self), index, \
old_count, new_count, new_contents \
)

// Insert one `element` into the array at the given `index`.
#define array_insert(self, index, element) \
array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element)

// Remove one `element` from the array at the given `index`.
#define array_erase(self, index) \
array__erase((VoidArray *)(self), array__elem_size(self), index)

#define array_pop(self) ((self)->contents[--(self)->size])

#define array_assign(self, other) \
array__assign((VoidArray *)(self), (const VoidArray *)(other), array__elem_size(self))

#define array_swap(self, other) \
array__swap((VoidArray *)(self), (VoidArray *)(other))

// Search a sorted array for a given `needle` value, using the given `compare`
// callback to determine the order.
//
// If an existing element is found to be equal to `needle`, then the `index`
// out-parameter is set to the existing value's index, and the `exists`
// out-parameter is set to true. Otherwise, `index` is set to an index where
// `needle` should be inserted in order to preserve the sorting, and `exists`
// is set to false.
#define array_search_sorted_with(self, compare, needle, index, exists) \
array__search_sorted(self, 0, compare, , needle, index, exists)

// Search a sorted array for a given `needle` value, using integer comparisons
// of a given struct field (specified with a leading dot) to determine the order.
//
// See also `array_search_sorted_with`.
#define array_search_sorted_by(self, field, needle, index, exists) \
array__search_sorted(self, 0, _compare_int, field, needle, index, exists)

// Insert a given `value` into a sorted array, using the given `compare`
// callback to determine the order.
#define array_insert_sorted_with(self, compare, value) \
do { \
unsigned index, exists; \
array_search_sorted_with(self, compare, &(value), &index, &exists); \
if (!exists) array_insert(self, index, value); \
} while (0)

// Insert a given `value` into a sorted array, using integer comparisons of
// a given struct field (specified with a leading dot) to determine the order.
//
// See also `array_search_sorted_by`.
#define array_insert_sorted_by(self, field, value) \
do { \
unsigned index, exists; \
array_search_sorted_by(self, field, (value) field, &index, &exists); \
if (!exists) array_insert(self, index, value); \
} while (0)

// Private

typedef Array(void) VoidArray;
Expand Down Expand Up @@ -93,7 +152,7 @@ static inline void array__reserve(VoidArray *self, size_t element_size, uint32_t
if (self->contents) {
self->contents = ts_realloc(self->contents, new_capacity * element_size);
} else {
self->contents = ts_calloc(new_capacity, element_size);
self->contents = ts_malloc(new_capacity * element_size);
}
self->capacity = new_capacity;
}
Expand All @@ -105,6 +164,12 @@ static inline void array__assign(VoidArray *self, const VoidArray *other, size_t
memcpy(self->contents, other->contents, self->size * element_size);
}

static inline void array__swap(VoidArray *self, VoidArray *other) {
VoidArray swap = *other;
*other = *self;
*self = swap;
}

static inline void array__grow(VoidArray *self, size_t count, size_t element_size) {
size_t new_size = self->size + count;
if (new_size > self->capacity) {
Expand All @@ -126,15 +191,55 @@ static inline void array__splice(VoidArray *self, size_t element_size,
array__reserve(self, element_size, new_size);

char *contents = (char *)self->contents;
if (self->size > old_end)
memmove(contents + new_end * element_size, contents + old_end * element_size,
(self->size - old_end) * element_size);
if (new_count > 0)
memcpy((contents + index * element_size), elements,
new_count * element_size);
if (self->size > old_end) {
memmove(
contents + new_end * element_size,
contents + old_end * element_size,
(self->size - old_end) * element_size
);
}
if (new_count > 0) {
if (elements) {
memcpy(
(contents + index * element_size),
elements,
new_count * element_size
);
} else {
memset(
(contents + index * element_size),
0,
new_count * element_size
);
}
}
self->size += new_count - old_count;
}

// A binary search routine, based on Rust's `std::slice::binary_search_by`.
#define array__search_sorted(self, start, compare, suffix, needle, index, exists) \
do { \
*(index) = start; \
*(exists) = false; \
uint32_t size = (self)->size - *(index); \
if (size == 0) break; \
int comparison; \
while (size > 1) { \
uint32_t half_size = size / 2; \
uint32_t mid_index = *(index) + half_size; \
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
if (comparison <= 0) *(index) = mid_index; \
size -= half_size; \
} \
comparison = compare(&((self)->contents[*(index)] suffix), (needle)); \
if (comparison == 0) *(exists) = true; \
else if (comparison < 0) *(index) += 1; \
} while (0)

// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
// parameter by reference in order to work with the generic sorting function above.
#define _compare_int(a, b) ((int)*(a) - (int)(b))

#ifdef __cplusplus
}
#endif
Expand Down
18 changes: 17 additions & 1 deletion atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@

#include <stdint.h>

#ifdef _WIN32
#ifdef __TINYC__

static inline size_t atomic_load(const volatile size_t *p) {
return *p;
}

static inline uint32_t atomic_inc(volatile uint32_t *p) {
*p += 1;
return *p;
}

static inline uint32_t atomic_dec(volatile uint32_t *p) {
*p-= 1;
return *p;
}

#elif defined(_WIN32)

#include <windows.h>

Expand Down
15 changes: 14 additions & 1 deletion bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ static inline uint32_t bitmask_for_index(uint16_t id) {
return (1u << (31 - id));
}

#if defined _WIN32 && !defined __GNUC__
#ifdef __TINYC__

// Algorithm taken from the Hacker's Delight book
// See also https://graphics.stanford.edu/~seander/bithacks.html
static inline uint32_t count_leading_zeros(uint32_t x) {
int count = 0;
if (x == 0) return 32;
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
count = (((x + (x >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
return count;
}

#elif defined _WIN32 && !defined __GNUC__

#include <intrin.h>

Expand Down
Loading

0 comments on commit ac4081f

Please sign in to comment.