Skip to content

Commit

Permalink
Bump tree-sitter to v0.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hinshun committed Jul 5, 2021
1 parent 0ba5d9f commit 12e786f
Show file tree
Hide file tree
Showing 18 changed files with 768 additions and 504 deletions.
2 changes: 1 addition & 1 deletion alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern "C" {
#include <stdbool.h>
#include <stdio.h>

#if defined(TREE_SITTER_TEST)
#if defined(TREE_SITTER_ALLOCATION_TRACKING)

void *ts_record_malloc(size_t);
void *ts_record_calloc(size_t, size_t);
Expand Down
28 changes: 25 additions & 3 deletions api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ 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 12
#define TREE_SITTER_LANGUAGE_VERSION 13

/**
* The earliest ABI version that is supported by the current version of the
* library.
*/
#define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 9
#define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13

/*******************/
/* Section - Types */
Expand Down Expand Up @@ -487,6 +487,12 @@ TSNode ts_node_parent(TSNode);
*/
TSNode ts_node_child(TSNode, uint32_t);

/**
* Get the field name for node's child at the given index, where zero represents
* the first child. Returns NULL, if no field is found.
*/
const char *ts_node_field_name_for_child(TSNode, uint32_t);

/**
* Get the node's number of children.
*/
Expand Down Expand Up @@ -645,12 +651,13 @@ bool ts_tree_cursor_goto_first_child(TSTreeCursor *);

/**
* Move the cursor to the first child of its current node that extends beyond
* the given byte offset.
* the given byte offset or point.
*
* This returns the index of the child node if one was found, and returns -1
* if no such child was found.
*/
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *, uint32_t);
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *, TSPoint);

TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *);

Expand Down Expand Up @@ -791,6 +798,21 @@ void ts_query_cursor_delete(TSQueryCursor *);
*/
void ts_query_cursor_exec(TSQueryCursor *, const TSQuery *, TSNode);

/**
* Manage the maximum number of in-progress matches allowed by this query
* cursor.
*
* Query cursors have an optional maximum capacity for storing lists of
* in-progress captures. If this capacity is exceeded, then the
* earliest-starting match will silently be dropped to make room for further
* matches. This maximum capacity is optional — by default, query cursors allow
* any number of pending matches, dynamically allocating new space for them as
* needed as the query is executed.
*/
bool ts_query_cursor_did_exceed_match_limit(const TSQueryCursor *);
uint32_t ts_query_cursor_match_limit(const TSQueryCursor *);
void ts_query_cursor_set_match_limit(TSQueryCursor *, uint32_t);

/**
* Set the range of bytes or (row, column) positions in which the query
* will be executed.
Expand Down
20 changes: 4 additions & 16 deletions language.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ uint32_t ts_language_version(const TSLanguage *self) {
}

uint32_t ts_language_field_count(const TSLanguage *self) {
if (self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS) {
return self->field_count;
} else {
return 0;
}
return self->field_count;
}

void ts_language_table_entry(
Expand Down Expand Up @@ -57,11 +53,7 @@ TSSymbol ts_language_public_symbol(
TSSymbol symbol
) {
if (symbol == ts_builtin_sym_error) return symbol;
if (self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_SYMBOL_DEDUPING) {
return self->public_symbol_map[symbol];
} else {
return symbol;
}
return self->public_symbol_map[symbol];
}

const char *ts_language_symbol_name(
Expand Down Expand Up @@ -92,11 +84,7 @@ TSSymbol ts_language_symbol_for_name(
if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) continue;
const char *symbol_name = self->symbol_names[i];
if (!strncmp(symbol_name, string, length) && !symbol_name[length]) {
if (self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_SYMBOL_DEDUPING) {
return self->public_symbol_map[i];
} else {
return i;
}
return self->public_symbol_map[i];
}
}
return 0;
Expand All @@ -107,7 +95,7 @@ TSSymbolType ts_language_symbol_type(
TSSymbol symbol
) {
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol);
if (metadata.named) {
if (metadata.named && metadata.visible) {
return TSSymbolTypeRegular;
} else if (metadata.visible) {
return TSSymbolTypeAnonymous;
Expand Down
38 changes: 12 additions & 26 deletions language.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ extern "C" {
#include "parser.h"

#define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1)
#define TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS 10
#define TREE_SITTER_LANGUAGE_VERSION_WITH_SYMBOL_DEDUPING 11
#define TREE_SITTER_LANGUAGE_VERSION_WITH_SMALL_STATES 11
#define TREE_SITTER_LANGUAGE_VERSION_WITH_STATE_COUNT 12
#define TREE_SITTER_LANGUAGE_VERSION_WITH_ALIAS_MAP 12

typedef struct {
const TSParseAction *actions;
Expand Down Expand Up @@ -59,16 +54,6 @@ static inline const TSParseAction *ts_language_actions(
return entry.actions;
}

static inline bool ts_language_has_actions(
const TSLanguage *self,
TSStateId state,
TSSymbol symbol
) {
TableEntry entry;
ts_language_table_entry(self, state, symbol, &entry);
return entry.action_count > 0;
}

static inline bool ts_language_has_reduce_action(
const TSLanguage *self,
TSStateId state,
Expand All @@ -91,10 +76,7 @@ static inline uint16_t ts_language_lookup(
TSStateId state,
TSSymbol symbol
) {
if (
self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_SMALL_STATES &&
state >= self->large_state_count
) {
if (state >= self->large_state_count) {
uint32_t index = self->small_parse_table_map[state - self->large_state_count];
const uint16_t *data = &self->small_parse_table[index];
uint16_t group_count = *(data++);
Expand All @@ -111,6 +93,14 @@ static inline uint16_t ts_language_lookup(
}
}

static inline bool ts_language_has_actions(
const TSLanguage *self,
TSStateId state,
TSSymbol symbol
) {
return ts_language_lookup(self, state, symbol) != 0;
}

// Iterate over all of the symbols that are valid in the given state.
//
// For 'large' parse states, this just requires iterating through
Expand All @@ -121,9 +111,7 @@ static inline LookaheadIterator ts_language_lookaheads(
const TSLanguage *self,
TSStateId state
) {
bool is_small_state =
self->version >= TREE_SITTER_LANGUAGE_VERSION_WITH_SMALL_STATES &&
state >= self->large_state_count;
bool is_small_state = state >= self->large_state_count;
const uint16_t *data;
const uint16_t *group_end = NULL;
uint16_t group_count = 0;
Expand Down Expand Up @@ -203,7 +191,7 @@ static inline TSStateId ts_language_next_state(
if (count > 0) {
TSParseAction action = actions[count - 1];
if (action.type == TSParseActionTypeShift) {
return action.params.shift.extra ? state : action.params.shift.state;
return action.shift.extra ? state : action.shift.state;
}
}
return 0;
Expand Down Expand Up @@ -248,7 +236,7 @@ static inline void ts_language_field_map(
const TSFieldMapEntry **start,
const TSFieldMapEntry **end
) {
if (self->version < TREE_SITTER_LANGUAGE_VERSION_WITH_FIELDS || self->field_count == 0) {
if (self->field_count == 0) {
*start = NULL;
*end = NULL;
return;
Expand All @@ -268,8 +256,6 @@ static inline void ts_language_aliases_for_symbol(
*start = &self->public_symbol_map[original_symbol];
*end = *start + 1;

if (self->version < TREE_SITTER_LANGUAGE_VERSION_WITH_ALIAS_MAP) return;

unsigned i = 0;
for (;;) {
TSSymbol symbol = self->alias_map[i++];
Expand Down
119 changes: 53 additions & 66 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,56 @@ static void ts_lexer__get_lookahead(Lexer *self) {
}
}

static void ts_lexer_goto(Lexer *self, Length position) {
self->current_position = position;
bool found_included_range = false;

// Move to the first valid position at or after the given position.
for (unsigned i = 0; i < self->included_range_count; i++) {
TSRange *included_range = &self->included_ranges[i];
if (included_range->end_byte > position.bytes) {
if (included_range->start_byte > position.bytes) {
self->current_position = (Length) {
.bytes = included_range->start_byte,
.extent = included_range->start_point,
};
}

self->current_included_range_index = i;
found_included_range = true;
break;
}
}

if (found_included_range) {
// If the current position is outside of the current chunk of text,
// then clear out the current chunk of text.
if (self->chunk && (
position.bytes < self->chunk_start ||
position.bytes >= self->chunk_start + self->chunk_size
)) {
ts_lexer__clear_chunk(self);
}

self->lookahead_size = 0;
self->data.lookahead = '\0';
}

// If the given position is beyond any of included ranges, move to the EOF
// state - past the end of the included ranges.
else {
self->current_included_range_index = self->included_range_count;
TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1];
self->current_position = (Length) {
.bytes = last_included_range->end_byte,
.extent = last_included_range->end_point,
};
ts_lexer__clear_chunk(self);
self->lookahead_size = 1;
self->data.lookahead = '\0';
}
}

// Advance to the next character in the source code, retrieving a new
// chunk of source code if needed.
static void ts_lexer__advance(TSLexer *_self, bool skip) {
Expand Down Expand Up @@ -183,22 +233,8 @@ static void ts_lexer__mark_end(TSLexer *_self) {

static uint32_t ts_lexer__get_column(TSLexer *_self) {
Lexer *self = (Lexer *)_self;
uint32_t goal_byte = self->current_position.bytes;

self->current_position.bytes -= self->current_position.extent.column;
self->current_position.extent.column = 0;

if (self->current_position.bytes < self->chunk_start) {
ts_lexer__get_chunk(self);
}

uint32_t result = 0;
while (self->current_position.bytes < goal_byte) {
ts_lexer__advance(&self->data, false);
result++;
}

return result;
self->did_get_column = true;
return self->current_position.extent.column;
}

// Is the lexer at a boundary between two disjoint included ranges of
Expand Down Expand Up @@ -247,56 +283,6 @@ void ts_lexer_delete(Lexer *self) {
ts_free(self->included_ranges);
}

static void ts_lexer_goto(Lexer *self, Length position) {
self->current_position = position;
bool found_included_range = false;

// Move to the first valid position at or after the given position.
for (unsigned i = 0; i < self->included_range_count; i++) {
TSRange *included_range = &self->included_ranges[i];
if (included_range->end_byte > position.bytes) {
if (included_range->start_byte > position.bytes) {
self->current_position = (Length) {
.bytes = included_range->start_byte,
.extent = included_range->start_point,
};
}

self->current_included_range_index = i;
found_included_range = true;
break;
}
}

if (found_included_range) {
// If the current position is outside of the current chunk of text,
// then clear out the current chunk of text.
if (self->chunk && (
position.bytes < self->chunk_start ||
position.bytes >= self->chunk_start + self->chunk_size
)) {
ts_lexer__clear_chunk(self);
}

self->lookahead_size = 0;
self->data.lookahead = '\0';
}

// If the given position is beyond any of included ranges, move to the EOF
// state - past the end of the included ranges.
else {
self->current_included_range_index = self->included_range_count;
TSRange *last_included_range = &self->included_ranges[self->included_range_count - 1];
self->current_position = (Length) {
.bytes = last_included_range->end_byte,
.extent = last_included_range->end_point,
};
ts_lexer__clear_chunk(self);
self->lookahead_size = 1;
self->data.lookahead = '\0';
}
}

void ts_lexer_set_input(Lexer *self, TSInput input) {
self->input = input;
ts_lexer__clear_chunk(self);
Expand All @@ -315,6 +301,7 @@ void ts_lexer_start(Lexer *self) {
self->token_start_position = self->current_position;
self->token_end_position = LENGTH_UNDEFINED;
self->data.result_symbol = 0;
self->did_get_column = false;
if (!ts_lexer__eof(&self->data)) {
if (!self->chunk_size) ts_lexer__get_chunk(self);
if (!self->lookahead_size) ts_lexer__get_lookahead(self);
Expand Down
11 changes: 6 additions & 5 deletions lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ typedef struct {
Length token_end_position;

TSRange *included_ranges;
size_t included_range_count;
size_t current_included_range_index;

const char *chunk;
TSInput input;
TSLogger logger;

uint32_t included_range_count;
uint32_t current_included_range_index;
uint32_t chunk_start;
uint32_t chunk_size;
uint32_t lookahead_size;
bool did_get_column;

TSInput input;
TSLogger logger;
char debug_buffer[TREE_SITTER_SERIALIZATION_BUFFER_SIZE];
} Lexer;

Expand Down
Loading

0 comments on commit 12e786f

Please sign in to comment.