treesitter: update vendored tree-sitter runtime

tree-sitter/tree-sitter commit 6cb8d24de2d99c4c50c9a0fd1e719ca5b3abc87f

Included files are:
lib/include/tree-sitter/*.h
lib/src/*.[ch]
lib/src/unicode/*
LICENSE
This commit is contained in:
Björn Linse 2020-02-25 13:03:40 +01:00
parent 96059d72e5
commit 3ce9b05653
12 changed files with 242 additions and 156 deletions

View File

@ -174,8 +174,19 @@ const TSLanguage *ts_parser_language(const TSParser *self);
* The second and third parameters specify the location and length of an array * The second and third parameters specify the location and length of an array
* of ranges. The parser does *not* take ownership of these ranges; it copies * of ranges. The parser does *not* take ownership of these ranges; it copies
* the data, so it doesn't matter how these ranges are allocated. * the data, so it doesn't matter how these ranges are allocated.
*
* If `length` is zero, then the entire document will be parsed. Otherwise,
* the given ranges must be ordered from earliest to latest in the document,
* and they must not overlap. That is, the following must hold for all
* `i` < `length - 1`:
*
* ranges[i].end_byte <= ranges[i + 1].start_byte
*
* If this requirement is not satisfied, the operation will fail, the ranges
* will not be assigned, and this function will return `false`. On success,
* this function returns `true`
*/ */
void ts_parser_set_included_ranges( bool ts_parser_set_included_ranges(
TSParser *self, TSParser *self,
const TSRange *ranges, const TSRange *ranges,
uint32_t length uint32_t length
@ -325,14 +336,6 @@ TSLogger ts_parser_logger(const TSParser *self);
*/ */
void ts_parser_print_dot_graphs(TSParser *self, int file); void ts_parser_print_dot_graphs(TSParser *self, int file);
/**
* Set whether or not the parser should halt immediately upon detecting an
* error. This will generally result in a syntax tree with an error at the
* root, and one or more partial syntax trees within the error. This behavior
* may not be supported long-term.
*/
void ts_parser_halt_on_error(TSParser *self, bool halt);
/******************/ /******************/
/* Section - Tree */ /* Section - Tree */
/******************/ /******************/
@ -732,12 +735,22 @@ const char *ts_query_string_value_for_id(
); );
/** /**
* Disable a certain capture within a query. This prevents the capture * Disable a certain capture within a query.
* from being returned in matches, and also avoids any resource usage *
* associated with recording the capture. * This prevents the capture from being returned in matches, and also avoids
* any resource usage associated with recording the capture. Currently, there
* is no way to undo this.
*/ */
void ts_query_disable_capture(TSQuery *, const char *, uint32_t); void ts_query_disable_capture(TSQuery *, const char *, uint32_t);
/**
* Disable a certain pattern within a query.
*
* This prevents the pattern from matching and removes most of the overhead
* associated with the pattern. Currently, there is no way to undo this.
*/
void ts_query_disable_pattern(TSQuery *, uint32_t);
/** /**
* Create a new cursor for executing a given query. * Create a new cursor for executing a given query.
* *

View File

@ -126,12 +126,28 @@ static inline void array__splice(VoidArray *self, size_t element_size,
array__reserve(self, element_size, new_size); array__reserve(self, element_size, new_size);
char *contents = (char *)self->contents; char *contents = (char *)self->contents;
if (self->size > old_end) if (self->size > old_end) {
memmove(contents + new_end * element_size, contents + old_end * element_size, memmove(
(self->size - old_end) * element_size); contents + new_end * element_size,
if (new_count > 0) contents + old_end * element_size,
memcpy((contents + index * element_size), elements, (self->size - old_end) * element_size
new_count * 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; self->size += new_count - old_count;
} }

View File

@ -12,11 +12,11 @@ static inline size_t atomic_load(const volatile size_t *p) {
} }
static inline uint32_t atomic_inc(volatile uint32_t *p) { static inline uint32_t atomic_inc(volatile uint32_t *p) {
return InterlockedIncrement(p); return InterlockedIncrement((long volatile *)p);
} }
static inline uint32_t atomic_dec(volatile uint32_t *p) { static inline uint32_t atomic_dec(volatile uint32_t *p) {
return InterlockedDecrement(p); return InterlockedDecrement((long volatile *)p);
} }
#else #else

View File

@ -7,7 +7,7 @@ static inline uint32_t bitmask_for_index(uint16_t id) {
return (1u << (31 - id)); return (1u << (31 - id));
} }
#ifdef _WIN32 #if defined _WIN32 && !defined __GNUC__
#include <intrin.h> #include <intrin.h>

View File

@ -29,10 +29,12 @@ static inline bool ts_language_is_symbol_external(const TSLanguage *self, TSSymb
return 0 < symbol && symbol < self->external_token_count + 1; return 0 < symbol && symbol < self->external_token_count + 1;
} }
static inline const TSParseAction *ts_language_actions(const TSLanguage *self, static inline const TSParseAction *ts_language_actions(
TSStateId state, const TSLanguage *self,
TSSymbol symbol, TSStateId state,
uint32_t *count) { TSSymbol symbol,
uint32_t *count
) {
TableEntry entry; TableEntry entry;
ts_language_table_entry(self, state, symbol, &entry); ts_language_table_entry(self, state, symbol, &entry);
*count = entry.action_count; *count = entry.action_count;
@ -90,8 +92,8 @@ static inline TSStateId ts_language_next_state(const TSLanguage *self,
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count); const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
if (count > 0) { if (count > 0) {
TSParseAction action = actions[count - 1]; TSParseAction action = actions[count - 1];
if (action.type == TSParseActionTypeShift || action.type == TSParseActionTypeRecover) { if (action.type == TSParseActionTypeShift) {
return action.params.state; return action.params.extra ? state : action.params.state;
} }
} }
return 0; return 0;

View File

@ -355,7 +355,7 @@ void ts_lexer_mark_end(Lexer *self) {
ts_lexer__mark_end(&self->data); ts_lexer__mark_end(&self->data);
} }
void ts_lexer_set_included_ranges( bool ts_lexer_set_included_ranges(
Lexer *self, Lexer *self,
const TSRange *ranges, const TSRange *ranges,
uint32_t count uint32_t count
@ -363,6 +363,16 @@ void ts_lexer_set_included_ranges(
if (count == 0 || !ranges) { if (count == 0 || !ranges) {
ranges = &DEFAULT_RANGE; ranges = &DEFAULT_RANGE;
count = 1; count = 1;
} else {
uint32_t previous_byte = 0;
for (unsigned i = 0; i < count; i++) {
const TSRange *range = &ranges[i];
if (
range->start_byte < previous_byte ||
range->end_byte < range->start_byte
) return false;
previous_byte = range->end_byte;
}
} }
size_t size = count * sizeof(TSRange); size_t size = count * sizeof(TSRange);
@ -370,6 +380,7 @@ void ts_lexer_set_included_ranges(
memcpy(self->included_ranges, ranges, size); memcpy(self->included_ranges, ranges, size);
self->included_range_count = count; self->included_range_count = count;
ts_lexer_goto(self, self->current_position); ts_lexer_goto(self, self->current_position);
return true;
} }
TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count) { TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count) {

View File

@ -38,7 +38,7 @@ void ts_lexer_start(Lexer *);
void ts_lexer_finish(Lexer *, uint32_t *); void ts_lexer_finish(Lexer *, uint32_t *);
void ts_lexer_advance_to_end(Lexer *); void ts_lexer_advance_to_end(Lexer *);
void ts_lexer_mark_end(Lexer *); void ts_lexer_mark_end(Lexer *);
void ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count); bool ts_lexer_set_included_ranges(Lexer *self, const TSRange *ranges, uint32_t count);
TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count); TSRange *ts_lexer_included_ranges(const Lexer *self, uint32_t *count);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -71,7 +71,6 @@ struct TSParser {
unsigned accept_count; unsigned accept_count;
unsigned operation_count; unsigned operation_count;
const volatile size_t *cancellation_flag; const volatile size_t *cancellation_flag;
bool halt_on_error;
Subtree old_tree; Subtree old_tree;
TSRangeArray included_range_differences; TSRangeArray included_range_differences;
unsigned included_range_difference_index; unsigned included_range_difference_index;
@ -1014,7 +1013,9 @@ static void ts_parser__handle_error(
TSStateId state_after_missing_symbol = ts_language_next_state( TSStateId state_after_missing_symbol = ts_language_next_state(
self->language, state, missing_symbol self->language, state, missing_symbol
); );
if (state_after_missing_symbol == 0) continue; if (state_after_missing_symbol == 0 || state_after_missing_symbol == state) {
continue;
}
if (ts_language_has_reduce_action( if (ts_language_has_reduce_action(
self->language, self->language,
@ -1067,46 +1068,6 @@ static void ts_parser__handle_error(
LOG_STACK(); LOG_STACK();
} }
static void ts_parser__halt_parse(TSParser *self) {
LOG("halting_parse");
LOG_STACK();
ts_lexer_advance_to_end(&self->lexer);
Length remaining_length = length_sub(
self->lexer.current_position,
ts_stack_position(self->stack, 0)
);
Subtree filler_node = ts_subtree_new_error(
&self->tree_pool,
0,
length_zero(),
remaining_length,
remaining_length.bytes,
0,
self->language
);
ts_subtree_to_mut_unsafe(filler_node).ptr->visible = false;
ts_stack_push(self->stack, 0, filler_node, false, 0);
SubtreeArray children = array_new();
Subtree root_error = ts_subtree_new_error_node(&self->tree_pool, &children, false, self->language);
ts_stack_push(self->stack, 0, root_error, false, 0);
Subtree eof = ts_subtree_new_leaf(
&self->tree_pool,
ts_builtin_sym_end,
length_zero(),
length_zero(),
0,
0,
false,
false,
self->language
);
ts_parser__accept(self, 0, eof);
}
static bool ts_parser__recover_to_state( static bool ts_parser__recover_to_state(
TSParser *self, TSParser *self,
StackVersion version, StackVersion version,
@ -1661,7 +1622,6 @@ TSParser *ts_parser_new(void) {
self->finished_tree = NULL_SUBTREE; self->finished_tree = NULL_SUBTREE;
self->reusable_node = reusable_node_new(); self->reusable_node = reusable_node_new();
self->dot_graph_file = NULL; self->dot_graph_file = NULL;
self->halt_on_error = false;
self->cancellation_flag = NULL; self->cancellation_flag = NULL;
self->timeout_duration = 0; self->timeout_duration = 0;
self->end_clock = clock_null(); self->end_clock = clock_null();
@ -1741,10 +1701,6 @@ void ts_parser_print_dot_graphs(TSParser *self, int fd) {
} }
} }
void ts_parser_halt_on_error(TSParser *self, bool should_halt_on_error) {
self->halt_on_error = should_halt_on_error;
}
const size_t *ts_parser_cancellation_flag(const TSParser *self) { const size_t *ts_parser_cancellation_flag(const TSParser *self) {
return (const size_t *)self->cancellation_flag; return (const size_t *)self->cancellation_flag;
} }
@ -1761,8 +1717,12 @@ void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros) {
self->timeout_duration = duration_from_micros(timeout_micros); self->timeout_duration = duration_from_micros(timeout_micros);
} }
void ts_parser_set_included_ranges(TSParser *self, const TSRange *ranges, uint32_t count) { bool ts_parser_set_included_ranges(
ts_lexer_set_included_ranges(&self->lexer, ranges, count); TSParser *self,
const TSRange *ranges,
uint32_t count
) {
return ts_lexer_set_included_ranges(&self->lexer, ranges, count);
} }
const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count) { const TSRange *ts_parser_included_ranges(const TSParser *self, uint32_t *count) {
@ -1858,9 +1818,6 @@ TSTree *ts_parser_parse(
unsigned min_error_cost = ts_parser__condense_stack(self); unsigned min_error_cost = ts_parser__condense_stack(self);
if (self->finished_tree.ptr && ts_subtree_error_cost(self->finished_tree) < min_error_cost) { if (self->finished_tree.ptr && ts_subtree_error_cost(self->finished_tree) < min_error_cost) {
break; break;
} else if (self->halt_on_error && min_error_cost > 0) {
ts_parser__halt_parse(self);
break;
} }
while (self->included_range_difference_index < self->included_range_differences.size) { while (self->included_range_difference_index < self->included_range_differences.size) {

View File

@ -19,6 +19,8 @@ typedef struct {
uint8_t next_size; uint8_t next_size;
} Stream; } Stream;
#define MAX_STEP_CAPTURE_COUNT 4
/* /*
* QueryStep - A step in the process of matching a query. Each node within * QueryStep - A step in the process of matching a query. Each node within
* a query S-expression maps to one of these steps. An entire pattern is * a query S-expression maps to one of these steps. An entire pattern is
@ -37,9 +39,11 @@ typedef struct {
typedef struct { typedef struct {
TSSymbol symbol; TSSymbol symbol;
TSFieldId field; TSFieldId field;
uint16_t capture_id; uint16_t capture_ids[MAX_STEP_CAPTURE_COUNT];
uint16_t depth: 15; uint16_t depth: 13;
bool contains_captures: 1; bool contains_captures: 1;
bool is_immediate: 1;
bool is_last: 1;
} QueryStep; } QueryStep;
/* /*
@ -62,10 +66,12 @@ typedef struct {
} SymbolTable; } SymbolTable;
/* /*
* PatternEntry - The set of steps needed to match a particular pattern, * PatternEntry - Information about the starting point for matching a
* represented as a slice of a shared array. These entries are stored in a * particular pattern, consisting of the index of the pattern within the query,
* 'pattern map' - a sorted array that makes it possible to efficiently lookup * and the index of the patter's first step in the shared `steps` array. These
* patterns based on the symbol for their first step. * entries are stored in a 'pattern map' - a sorted array that makes it
* possible to efficiently lookup patterns based on the symbol for their first
* step.
*/ */
typedef struct { typedef struct {
uint16_t step_index; uint16_t step_index;
@ -140,6 +146,7 @@ static const TSQueryError PARENT_DONE = -1;
static const uint8_t PATTERN_DONE_MARKER = UINT8_MAX; static const uint8_t PATTERN_DONE_MARKER = UINT8_MAX;
static const uint16_t NONE = UINT16_MAX; static const uint16_t NONE = UINT16_MAX;
static const TSSymbol WILDCARD_SYMBOL = 0; static const TSSymbol WILDCARD_SYMBOL = 0;
static const TSSymbol NAMED_WILDCARD_SYMBOL = UINT16_MAX - 1;
static const uint16_t MAX_STATE_COUNT = 32; static const uint16_t MAX_STATE_COUNT = 32;
// #define LOG(...) fprintf(stderr, __VA_ARGS__) // #define LOG(...) fprintf(stderr, __VA_ARGS__)
@ -324,6 +331,49 @@ static uint16_t symbol_table_insert_name(
return self->slices.size - 1; return self->slices.size - 1;
} }
/************
* QueryStep
************/
static QueryStep query_step__new(
TSSymbol symbol,
uint16_t depth,
bool is_immediate
) {
return (QueryStep) {
.symbol = symbol,
.depth = depth,
.field = 0,
.capture_ids = {NONE, NONE, NONE, NONE},
.contains_captures = false,
.is_immediate = is_immediate,
};
}
static void query_step__add_capture(QueryStep *self, uint16_t capture_id) {
for (unsigned i = 0; i < MAX_STEP_CAPTURE_COUNT; i++) {
if (self->capture_ids[i] == NONE) {
self->capture_ids[i] = capture_id;
break;
}
}
}
static void query_step__remove_capture(QueryStep *self, uint16_t capture_id) {
for (unsigned i = 0; i < MAX_STEP_CAPTURE_COUNT; i++) {
if (self->capture_ids[i] == capture_id) {
self->capture_ids[i] = NONE;
while (i + 1 < MAX_STEP_CAPTURE_COUNT) {
if (self->capture_ids[i + 1] == NONE) break;
self->capture_ids[i] = self->capture_ids[i + 1];
self->capture_ids[i + 1] = NONE;
i++;
}
break;
}
}
}
/********* /*********
* Query * Query
*********/ *********/
@ -333,7 +383,7 @@ static uint16_t symbol_table_insert_name(
// to quickly find the starting steps of all of the patterns whose root matches // to quickly find the starting steps of all of the patterns whose root matches
// that node. Each entry has two fields: a `pattern_index`, which identifies one // that node. Each entry has two fields: a `pattern_index`, which identifies one
// of the patterns in the query, and a `step_index`, which indicates the start // of the patterns in the query, and a `step_index`, which indicates the start
// offset of that pattern's steps pattern within the `steps` array. // offset of that pattern's steps within the `steps` array.
// //
// The entries are sorted by the patterns' root symbols, and lookups use a // The entries are sorted by the patterns' root symbols, and lookups use a
// binary search. This ensures that the cost of this initial lookup step // binary search. This ensures that the cost of this initial lookup step
@ -399,14 +449,14 @@ static void ts_query__finalize_steps(TSQuery *self) {
for (unsigned i = 0; i < self->steps.size; i++) { for (unsigned i = 0; i < self->steps.size; i++) {
QueryStep *step = &self->steps.contents[i]; QueryStep *step = &self->steps.contents[i];
uint32_t depth = step->depth; uint32_t depth = step->depth;
if (step->capture_id != NONE) { if (step->capture_ids[0] != NONE) {
step->contains_captures = true; step->contains_captures = true;
} else { } else {
step->contains_captures = false; step->contains_captures = false;
for (unsigned j = i + 1; j < self->steps.size; j++) { for (unsigned j = i + 1; j < self->steps.size; j++) {
QueryStep *s = &self->steps.contents[j]; QueryStep *s = &self->steps.contents[j];
if (s->depth == PATTERN_DONE_MARKER || s->depth <= depth) break; if (s->depth == PATTERN_DONE_MARKER || s->depth <= depth) break;
if (s->capture_id != NONE) step->contains_captures = true; if (s->capture_ids[0] != NONE) step->contains_captures = true;
} }
} }
} }
@ -533,7 +583,8 @@ static TSQueryError ts_query__parse_pattern(
TSQuery *self, TSQuery *self,
Stream *stream, Stream *stream,
uint32_t depth, uint32_t depth,
uint32_t *capture_count uint32_t *capture_count,
bool is_immediate
) { ) {
uint16_t starting_step_index = self->steps.size; uint16_t starting_step_index = self->steps.size;
@ -552,7 +603,7 @@ static TSQueryError ts_query__parse_pattern(
// Parse a nested list, which represents a pattern followed by // Parse a nested list, which represents a pattern followed by
// zero-or-more predicates. // zero-or-more predicates.
if (stream->next == '(' && depth == 0) { if (stream->next == '(' && depth == 0) {
TSQueryError e = ts_query__parse_pattern(self, stream, 0, capture_count); TSQueryError e = ts_query__parse_pattern(self, stream, 0, capture_count, is_immediate);
if (e) return e; if (e) return e;
// Parse the predicates. // Parse the predicates.
@ -573,7 +624,7 @@ static TSQueryError ts_query__parse_pattern(
// Parse the wildcard symbol // Parse the wildcard symbol
if (stream->next == '*') { if (stream->next == '*') {
symbol = WILDCARD_SYMBOL; symbol = NAMED_WILDCARD_SYMBOL;
stream_advance(stream); stream_advance(stream);
} }
@ -597,24 +648,37 @@ static TSQueryError ts_query__parse_pattern(
} }
// Add a step for the node. // Add a step for the node.
array_push(&self->steps, ((QueryStep) { array_push(&self->steps, query_step__new(symbol, depth, is_immediate));
.depth = depth,
.symbol = symbol,
.field = 0,
.capture_id = NONE,
.contains_captures = false,
}));
// Parse the child patterns // Parse the child patterns
stream_skip_whitespace(stream); stream_skip_whitespace(stream);
bool child_is_immediate = false;
uint16_t child_start_step_index = self->steps.size;
for (;;) { for (;;) {
TSQueryError e = ts_query__parse_pattern(self, stream, depth + 1, capture_count); if (stream->next == '.') {
child_is_immediate = true;
stream_advance(stream);
stream_skip_whitespace(stream);
}
TSQueryError e = ts_query__parse_pattern(
self,
stream,
depth + 1,
capture_count,
child_is_immediate
);
if (e == PARENT_DONE) { if (e == PARENT_DONE) {
if (child_is_immediate) {
self->steps.contents[child_start_step_index].is_last = true;
}
stream_advance(stream); stream_advance(stream);
break; break;
} else if (e) { } else if (e) {
return e; return e;
} }
child_is_immediate = false;
} }
} }
@ -643,13 +707,7 @@ static TSQueryError ts_query__parse_pattern(
stream_reset(stream, string_content); stream_reset(stream, string_content);
return TSQueryErrorNodeType; return TSQueryErrorNodeType;
} }
array_push(&self->steps, ((QueryStep) { array_push(&self->steps, query_step__new(symbol, depth, is_immediate));
.depth = depth,
.symbol = symbol,
.field = 0,
.capture_id = NONE,
.contains_captures = false,
}));
if (stream->next != '"') return TSQueryErrorSyntax; if (stream->next != '"') return TSQueryErrorSyntax;
stream_advance(stream); stream_advance(stream);
@ -672,7 +730,13 @@ static TSQueryError ts_query__parse_pattern(
// Parse the pattern // Parse the pattern
uint32_t step_index = self->steps.size; uint32_t step_index = self->steps.size;
TSQueryError e = ts_query__parse_pattern(self, stream, depth, capture_count); TSQueryError e = ts_query__parse_pattern(
self,
stream,
depth,
capture_count,
is_immediate
);
if (e == PARENT_DONE) return TSQueryErrorSyntax; if (e == PARENT_DONE) return TSQueryErrorSyntax;
if (e) return e; if (e) return e;
@ -695,12 +759,7 @@ static TSQueryError ts_query__parse_pattern(
stream_skip_whitespace(stream); stream_skip_whitespace(stream);
// Add a step that matches any kind of node // Add a step that matches any kind of node
array_push(&self->steps, ((QueryStep) { array_push(&self->steps, query_step__new(WILDCARD_SYMBOL, depth, is_immediate));
.depth = depth,
.symbol = WILDCARD_SYMBOL,
.field = 0,
.contains_captures = false,
}));
} }
else { else {
@ -710,7 +769,7 @@ static TSQueryError ts_query__parse_pattern(
stream_skip_whitespace(stream); stream_skip_whitespace(stream);
// Parse an '@'-prefixed capture pattern // Parse an '@'-prefixed capture pattern
if (stream->next == '@') { while (stream->next == '@') {
stream_advance(stream); stream_advance(stream);
// Parse the capture name // Parse the capture name
@ -725,7 +784,8 @@ static TSQueryError ts_query__parse_pattern(
capture_name, capture_name,
length length
); );
self->steps.contents[starting_step_index].capture_id = capture_id; QueryStep *step = &self->steps.contents[starting_step_index];
query_step__add_capture(step, capture_id);
(*capture_count)++; (*capture_count)++;
stream_skip_whitespace(stream); stream_skip_whitespace(stream);
@ -794,8 +854,8 @@ TSQuery *ts_query_new(
.offset = self->predicate_steps.size, .offset = self->predicate_steps.size,
.length = 0, .length = 0,
})); }));
*error_type = ts_query__parse_pattern(self, &stream, 0, &capture_count); *error_type = ts_query__parse_pattern(self, &stream, 0, &capture_count, false);
array_push(&self->steps, ((QueryStep) { .depth = PATTERN_DONE_MARKER })); array_push(&self->steps, query_step__new(0, PATTERN_DONE_MARKER, false));
// If any pattern could not be parsed, then report the error information // If any pattern could not be parsed, then report the error information
// and terminate. // and terminate.
@ -891,16 +951,31 @@ void ts_query_disable_capture(
const char *name, const char *name,
uint32_t length uint32_t length
) { ) {
// Remove capture information for any pattern step that previously
// captured with the given name.
int id = symbol_table_id_for_name(&self->captures, name, length); int id = symbol_table_id_for_name(&self->captures, name, length);
if (id != -1) { if (id != -1) {
for (unsigned i = 0; i < self->steps.size; i++) { for (unsigned i = 0; i < self->steps.size; i++) {
QueryStep *step = &self->steps.contents[i]; QueryStep *step = &self->steps.contents[i];
if (step->capture_id == id) { query_step__remove_capture(step, id);
step->capture_id = NONE; }
} ts_query__finalize_steps(self);
}
}
void ts_query_disable_pattern(
TSQuery *self,
uint32_t pattern_index
) {
// Remove the given pattern from the pattern map. Its steps will still
// be in the `steps` array, but they will never be read.
for (unsigned i = 0; i < self->pattern_map.size; i++) {
PatternEntry *pattern = &self->pattern_map.contents[i];
if (pattern->pattern_index == pattern_index) {
array_erase(&self->pattern_map, i);
i--;
} }
} }
ts_query__finalize_steps(self);
} }
/*************** /***************
@ -1010,7 +1085,7 @@ static bool ts_query_cursor__first_in_progress_capture(
static bool ts_query__cursor_add_state( static bool ts_query__cursor_add_state(
TSQueryCursor *self, TSQueryCursor *self,
const PatternEntry *slice const PatternEntry *pattern
) { ) {
uint32_t list_id = capture_list_pool_acquire(&self->capture_list_pool); uint32_t list_id = capture_list_pool_acquire(&self->capture_list_pool);
@ -1037,11 +1112,11 @@ static bool ts_query__cursor_add_state(
} }
} }
LOG(" start state. pattern:%u\n", slice->pattern_index); LOG(" start state. pattern:%u\n", pattern->pattern_index);
array_push(&self->states, ((QueryState) { array_push(&self->states, ((QueryState) {
.capture_list_id = list_id, .capture_list_id = list_id,
.step_index = slice->step_index, .step_index = pattern->step_index,
.pattern_index = slice->pattern_index, .pattern_index = pattern->pattern_index,
.start_depth = self->depth, .start_depth = self->depth,
.capture_count = 0, .capture_count = 0,
.consumed_capture_count = 0, .consumed_capture_count = 0,
@ -1113,15 +1188,16 @@ static inline bool ts_query_cursor__advance(TSQueryCursor *self) {
return self->finished_states.size > 0; return self->finished_states.size > 0;
} }
} else { } else {
bool can_have_later_siblings; bool has_later_siblings;
bool can_have_later_siblings_with_this_field; bool can_have_later_siblings_with_this_field;
TSFieldId field_id = ts_tree_cursor_current_status( TSFieldId field_id = ts_tree_cursor_current_status(
&self->cursor, &self->cursor,
&can_have_later_siblings, &has_later_siblings,
&can_have_later_siblings_with_this_field &can_have_later_siblings_with_this_field
); );
TSNode node = ts_tree_cursor_current_node(&self->cursor); TSNode node = ts_tree_cursor_current_node(&self->cursor);
TSSymbol symbol = ts_node_symbol(node); TSSymbol symbol = ts_node_symbol(node);
bool is_named = ts_node_is_named(node);
if (symbol != ts_builtin_sym_error && self->query->symbol_map) { if (symbol != ts_builtin_sym_error && self->query->symbol_map) {
symbol = self->query->symbol_map[symbol]; symbol = self->query->symbol_map[symbol];
} }
@ -1145,43 +1221,46 @@ static inline bool ts_query_cursor__advance(TSQueryCursor *self) {
) return false; ) return false;
LOG( LOG(
"enter node. type:%s, field:%s, row:%u state_count:%u, finished_state_count:%u, can_have_later_siblings:%d, can_have_later_siblings_with_this_field:%d\n", "enter node. "
"type:%s, field:%s, row:%u state_count:%u, "
"finished_state_count:%u, has_later_siblings:%d, "
"can_have_later_siblings_with_this_field:%d\n",
ts_node_type(node), ts_node_type(node),
ts_language_field_name_for_id(self->query->language, field_id), ts_language_field_name_for_id(self->query->language, field_id),
ts_node_start_point(node).row, ts_node_start_point(node).row,
self->states.size, self->states.size,
self->finished_states.size, self->finished_states.size,
can_have_later_siblings, has_later_siblings,
can_have_later_siblings_with_this_field can_have_later_siblings_with_this_field
); );
// Add new states for any patterns whose root node is a wildcard. // Add new states for any patterns whose root node is a wildcard.
for (unsigned i = 0; i < self->query->wildcard_root_pattern_count; i++) { for (unsigned i = 0; i < self->query->wildcard_root_pattern_count; i++) {
PatternEntry *slice = &self->query->pattern_map.contents[i]; PatternEntry *pattern = &self->query->pattern_map.contents[i];
QueryStep *step = &self->query->steps.contents[slice->step_index]; QueryStep *step = &self->query->steps.contents[pattern->step_index];
// If this node matches the first step of the pattern, then add a new // If this node matches the first step of the pattern, then add a new
// state at the start of this pattern. // state at the start of this pattern.
if (step->field && field_id != step->field) continue; if (step->field && field_id != step->field) continue;
if (!ts_query__cursor_add_state(self, slice)) break; if (!ts_query__cursor_add_state(self, pattern)) break;
} }
// Add new states for any patterns whose root node matches this node. // Add new states for any patterns whose root node matches this node.
unsigned i; unsigned i;
if (ts_query__pattern_map_search(self->query, symbol, &i)) { if (ts_query__pattern_map_search(self->query, symbol, &i)) {
PatternEntry *slice = &self->query->pattern_map.contents[i]; PatternEntry *pattern = &self->query->pattern_map.contents[i];
QueryStep *step = &self->query->steps.contents[slice->step_index]; QueryStep *step = &self->query->steps.contents[pattern->step_index];
do { do {
// If this node matches the first step of the pattern, then add a new // If this node matches the first step of the pattern, then add a new
// state at the start of this pattern. // state at the start of this pattern.
if (step->field && field_id != step->field) continue; if (step->field && field_id != step->field) continue;
if (!ts_query__cursor_add_state(self, slice)) break; if (!ts_query__cursor_add_state(self, pattern)) break;
// Advance to the next pattern whose root node matches this node. // Advance to the next pattern whose root node matches this node.
i++; i++;
if (i == self->query->pattern_map.size) break; if (i == self->query->pattern_map.size) break;
slice = &self->query->pattern_map.contents[i]; pattern = &self->query->pattern_map.contents[i];
step = &self->query->steps.contents[slice->step_index]; step = &self->query->steps.contents[pattern->step_index];
} while (step->symbol == symbol); } while (step->symbol == symbol);
} }
@ -1191,14 +1270,23 @@ static inline bool ts_query_cursor__advance(TSQueryCursor *self) {
QueryStep *step = &self->query->steps.contents[state->step_index]; QueryStep *step = &self->query->steps.contents[state->step_index];
// Check that the node matches all of the criteria for the next // Check that the node matches all of the criteria for the next
// step of the pattern.if ( // step of the pattern.
if ((uint32_t)state->start_depth + (uint32_t)step->depth != self->depth) continue; if ((uint32_t)state->start_depth + (uint32_t)step->depth != self->depth) continue;
// Determine if this node matches this step of the pattern, and also // Determine if this node matches this step of the pattern, and also
// if this node can have later siblings that match this step of the // if this node can have later siblings that match this step of the
// pattern. // pattern.
bool node_does_match = !step->symbol || step->symbol == symbol; bool node_does_match =
bool later_sibling_can_match = can_have_later_siblings; step->symbol == symbol ||
step->symbol == WILDCARD_SYMBOL ||
(step->symbol == NAMED_WILDCARD_SYMBOL && is_named);
bool later_sibling_can_match = has_later_siblings;
if (step->is_immediate && is_named) {
later_sibling_can_match = false;
}
if (step->is_last && has_later_siblings) {
node_does_match = false;
}
if (step->field) { if (step->field) {
if (step->field == field_id) { if (step->field == field_id) {
if (!can_have_later_siblings_with_this_field) { if (!can_have_later_siblings_with_this_field) {
@ -1261,11 +1349,13 @@ static inline bool ts_query_cursor__advance(TSQueryCursor *self) {
// If the current node is captured in this pattern, add it to the // If the current node is captured in this pattern, add it to the
// capture list. // capture list.
if (step->capture_id != NONE) { for (unsigned j = 0; j < MAX_STEP_CAPTURE_COUNT; j++) {
uint16_t capture_id = step->capture_ids[j];
if (step->capture_ids[j] == NONE) break;
LOG( LOG(
" capture node. pattern:%u, capture_id:%u\n", " capture node. pattern:%u, capture_id:%u\n",
next_state->pattern_index, next_state->pattern_index,
step->capture_id capture_id
); );
TSQueryCapture *capture_list = capture_list_pool_get( TSQueryCapture *capture_list = capture_list_pool_get(
&self->capture_list_pool, &self->capture_list_pool,
@ -1273,7 +1363,7 @@ static inline bool ts_query_cursor__advance(TSQueryCursor *self) {
); );
capture_list[next_state->capture_count++] = (TSQueryCapture) { capture_list[next_state->capture_count++] = (TSQueryCapture) {
node, node,
step->capture_id capture_id
}; };
} }

View File

@ -11,7 +11,7 @@
#define MAX_NODE_POOL_SIZE 50 #define MAX_NODE_POOL_SIZE 50
#define MAX_ITERATOR_COUNT 64 #define MAX_ITERATOR_COUNT 64
#ifdef _WIN32 #if defined _WIN32 && !defined __GNUC__
#define inline __forceinline #define inline __forceinline
#else #else
#define inline static inline __attribute__((always_inline)) #define inline static inline __attribute__((always_inline))

View File

@ -322,12 +322,9 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu
if (tree.ptr->repeat_depth > 0) { if (tree.ptr->repeat_depth > 0) {
Subtree child1 = tree.ptr->children[0]; Subtree child1 = tree.ptr->children[0];
Subtree child2 = tree.ptr->children[tree.ptr->child_count - 1]; Subtree child2 = tree.ptr->children[tree.ptr->child_count - 1];
if ( long repeat_delta = (long)ts_subtree_repeat_depth(child1) - (long)ts_subtree_repeat_depth(child2);
ts_subtree_child_count(child1) > 0 && if (repeat_delta > 0) {
ts_subtree_child_count(child2) > 0 && unsigned n = repeat_delta;
child1.ptr->repeat_depth > child2.ptr->repeat_depth
) {
unsigned n = child1.ptr->repeat_depth - child2.ptr->repeat_depth;
for (unsigned i = n / 2; i > 0; i /= 2) { for (unsigned i = n / 2; i > 0; i /= 2) {
ts_subtree__compress(tree, i, language, &pool->tree_stack); ts_subtree__compress(tree, i, language, &pool->tree_stack);
n -= i; n -= i;
@ -344,10 +341,6 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu
} }
} }
static inline uint32_t ts_subtree_repeat_depth(Subtree self) {
return ts_subtree_child_count(self) ? self.ptr->repeat_depth : 0;
}
void ts_subtree_set_children( void ts_subtree_set_children(
MutableSubtree self, Subtree *children, uint32_t child_count, const TSLanguage *language MutableSubtree self, Subtree *children, uint32_t child_count, const TSLanguage *language
) { ) {

View File

@ -206,6 +206,10 @@ static inline uint32_t ts_subtree_child_count(Subtree self) {
return self.data.is_inline ? 0 : self.ptr->child_count; return self.data.is_inline ? 0 : self.ptr->child_count;
} }
static inline uint32_t ts_subtree_repeat_depth(Subtree self) {
return self.data.is_inline ? 0 : self.ptr->repeat_depth;
}
static inline uint32_t ts_subtree_node_count(Subtree self) { static inline uint32_t ts_subtree_node_count(Subtree self) {
return (self.data.is_inline || self.ptr->child_count == 0) ? 1 : self.ptr->node_count; return (self.data.is_inline || self.ptr->child_count == 0) ? 1 : self.ptr->node_count;
} }