Rename receiver parameter to 'self'
This commit is contained in:
parent
02e549202f
commit
216ce8c80b
7 changed files with 436 additions and 443 deletions
|
|
@ -12,74 +12,73 @@ TSDocument *ts_document_make() {
|
|||
return document;
|
||||
}
|
||||
|
||||
void ts_document_free(TSDocument *document) {
|
||||
ts_parser_destroy(&document->parser);
|
||||
if (document->tree)
|
||||
ts_tree_release(document->tree);
|
||||
free(document);
|
||||
void ts_document_free(TSDocument *self) {
|
||||
ts_parser_destroy(&self->parser);
|
||||
if (self->tree)
|
||||
ts_tree_release(self->tree);
|
||||
free(self);
|
||||
}
|
||||
|
||||
const TSLanguage *ts_document_language(TSDocument *document) {
|
||||
return document->parser.language;
|
||||
const TSLanguage *ts_document_language(TSDocument *self) {
|
||||
return self->parser.language;
|
||||
}
|
||||
|
||||
void ts_document_set_language(TSDocument *document, const TSLanguage *language) {
|
||||
document->parser.language = language;
|
||||
document->tree = NULL;
|
||||
void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
|
||||
self->parser.language = language;
|
||||
self->tree = NULL;
|
||||
}
|
||||
|
||||
TSDebugger ts_document_debugger(const TSDocument *document) {
|
||||
return ts_parser_debugger(&document->parser);
|
||||
TSDebugger ts_document_debugger(const TSDocument *self) {
|
||||
return ts_parser_debugger(&self->parser);
|
||||
}
|
||||
|
||||
void ts_document_set_debugger(TSDocument *document, TSDebugger debugger) {
|
||||
ts_parser_set_debugger(&document->parser, debugger);
|
||||
void ts_document_set_debugger(TSDocument *self, TSDebugger debugger) {
|
||||
ts_parser_set_debugger(&self->parser, debugger);
|
||||
}
|
||||
|
||||
TSInput ts_document_input(TSDocument *document) {
|
||||
return document->input;
|
||||
TSInput ts_document_input(TSDocument *self) {
|
||||
return self->input;
|
||||
}
|
||||
|
||||
void ts_document_set_input(TSDocument *document, TSInput input) {
|
||||
document->input = input;
|
||||
void ts_document_set_input(TSDocument *self, TSInput input) {
|
||||
self->input = input;
|
||||
}
|
||||
|
||||
void ts_document_set_input_string(TSDocument *document, const char *text) {
|
||||
ts_document_set_input(document, ts_string_input_make(text));
|
||||
void ts_document_set_input_string(TSDocument *self, const char *text) {
|
||||
ts_document_set_input(self, ts_string_input_make(text));
|
||||
}
|
||||
|
||||
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
|
||||
if (!document->tree)
|
||||
void ts_document_edit(TSDocument *self, TSInputEdit edit) {
|
||||
if (!self->tree)
|
||||
return;
|
||||
|
||||
size_t max_chars = ts_tree_total_size(document->tree).chars;
|
||||
size_t max_chars = ts_tree_total_size(self->tree).chars;
|
||||
if (edit.position > max_chars)
|
||||
edit.position = max_chars;
|
||||
if (edit.chars_removed > max_chars - edit.position)
|
||||
edit.chars_removed = max_chars - edit.position;
|
||||
|
||||
ts_tree_edit(document->tree, edit);
|
||||
ts_tree_edit(self->tree, edit);
|
||||
}
|
||||
|
||||
void ts_document_parse(TSDocument *document) {
|
||||
if (document->input.read_fn && document->parser.language) {
|
||||
TSTree *tree =
|
||||
ts_parser_parse(&document->parser, document->input, document->tree);
|
||||
if (document->tree)
|
||||
ts_tree_release(document->tree);
|
||||
document->tree = tree;
|
||||
void ts_document_parse(TSDocument *self) {
|
||||
if (self->input.read_fn && self->parser.language) {
|
||||
TSTree *tree = ts_parser_parse(&self->parser, self->input, self->tree);
|
||||
if (self->tree)
|
||||
ts_tree_release(self->tree);
|
||||
self->tree = tree;
|
||||
ts_tree_retain(tree);
|
||||
document->parse_count++;
|
||||
self->parse_count++;
|
||||
}
|
||||
}
|
||||
|
||||
TSNode ts_document_root_node(const TSDocument *document) {
|
||||
TSNode result = ts_node_make(document->tree, ts_length_zero());
|
||||
TSNode ts_document_root_node(const TSDocument *self) {
|
||||
TSNode result = ts_node_make(self->tree, ts_length_zero());
|
||||
while (result.data && !ts_tree_is_visible(result.data))
|
||||
result = ts_node_named_child(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t ts_document_parse_count(const TSDocument *document) {
|
||||
return document->parse_count;
|
||||
size_t ts_document_parse_count(const TSDocument *self) {
|
||||
return self->parse_count;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,84 +6,83 @@
|
|||
#include "runtime/debugger.h"
|
||||
#include "utf8proc.h"
|
||||
|
||||
#define DEBUG(...) \
|
||||
if (lexer->debugger.debug_fn) { \
|
||||
snprintf(lexer->debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
||||
lexer->debugger.debug_fn(lexer->debugger.payload, TSDebugTypeLex, \
|
||||
lexer->debug_buffer); \
|
||||
#define DEBUG(...) \
|
||||
if (self->debugger.debug_fn) { \
|
||||
snprintf(self->debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
||||
self->debugger.debug_fn(self->debugger.payload, TSDebugTypeLex, \
|
||||
self->debug_buffer); \
|
||||
}
|
||||
|
||||
#define DEBUG_LOOKAHEAD() \
|
||||
DEBUG((0 < lexer->lookahead && lexer->lookahead < 256) \
|
||||
? "lookahead char:'%c'" \
|
||||
: "lookahead char:%d", \
|
||||
lexer->lookahead);
|
||||
#define DEBUG_LOOKAHEAD() \
|
||||
DEBUG((0 < self->lookahead && self->lookahead < 256) ? "lookahead char:'%c'" \
|
||||
: "lookahead char:%d", \
|
||||
self->lookahead);
|
||||
|
||||
static const char *empty_chunk = "";
|
||||
|
||||
static void ts_lexer__get_chunk(TSLexer *lexer) {
|
||||
TSInput input = lexer->input;
|
||||
if (lexer->current_position.bytes != lexer->chunk_start + lexer->chunk_size)
|
||||
input.seek_fn(input.payload, lexer->current_position);
|
||||
static void ts_lexer__get_chunk(TSLexer *self) {
|
||||
TSInput input = self->input;
|
||||
if (self->current_position.bytes != self->chunk_start + self->chunk_size)
|
||||
input.seek_fn(input.payload, self->current_position);
|
||||
|
||||
lexer->chunk_start = lexer->current_position.bytes;
|
||||
lexer->chunk = input.read_fn(input.payload, &lexer->chunk_size);
|
||||
if (!lexer->chunk_size)
|
||||
lexer->chunk = empty_chunk;
|
||||
self->chunk_start = self->current_position.bytes;
|
||||
self->chunk = input.read_fn(input.payload, &self->chunk_size);
|
||||
if (!self->chunk_size)
|
||||
self->chunk = empty_chunk;
|
||||
}
|
||||
|
||||
static void ts_lexer__get_lookahead(TSLexer *lexer) {
|
||||
size_t position_in_chunk = lexer->current_position.bytes - lexer->chunk_start;
|
||||
lexer->lookahead_size = utf8proc_iterate(
|
||||
(const uint8_t *)lexer->chunk + position_in_chunk,
|
||||
lexer->chunk_size - position_in_chunk + 1, &lexer->lookahead);
|
||||
static void ts_lexer__get_lookahead(TSLexer *self) {
|
||||
size_t position_in_chunk = self->current_position.bytes - self->chunk_start;
|
||||
self->lookahead_size = utf8proc_iterate(
|
||||
(const uint8_t *)self->chunk + position_in_chunk,
|
||||
self->chunk_size - position_in_chunk + 1, &self->lookahead);
|
||||
DEBUG_LOOKAHEAD();
|
||||
}
|
||||
|
||||
static void ts_lexer__start(TSLexer *lexer, TSStateId lex_state) {
|
||||
static void ts_lexer__start(TSLexer *self, TSStateId lex_state) {
|
||||
DEBUG("start_lex state:%d", lex_state);
|
||||
DEBUG_LOOKAHEAD();
|
||||
|
||||
if (!lexer->chunk)
|
||||
ts_lexer__get_chunk(lexer);
|
||||
if (!lexer->lookahead_size)
|
||||
ts_lexer__get_lookahead(lexer);
|
||||
if (!self->chunk)
|
||||
ts_lexer__get_chunk(self);
|
||||
if (!self->lookahead_size)
|
||||
ts_lexer__get_lookahead(self);
|
||||
}
|
||||
|
||||
static void ts_lexer__start_token(TSLexer *lexer) {
|
||||
DEBUG("start_token chars:%lu", lexer->current_position.chars);
|
||||
lexer->token_start_position = lexer->current_position;
|
||||
static void ts_lexer__start_token(TSLexer *self) {
|
||||
DEBUG("start_token chars:%lu", self->current_position.chars);
|
||||
self->token_start_position = self->current_position;
|
||||
}
|
||||
|
||||
static bool ts_lexer__advance(TSLexer *lexer, TSStateId state) {
|
||||
static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
||||
DEBUG("advance state:%d", state);
|
||||
|
||||
if (lexer->chunk == empty_chunk)
|
||||
if (self->chunk == empty_chunk)
|
||||
return false;
|
||||
|
||||
if (lexer->lookahead_size) {
|
||||
lexer->current_position.bytes += lexer->lookahead_size;
|
||||
lexer->current_position.chars += 1;
|
||||
if (self->lookahead_size) {
|
||||
self->current_position.bytes += self->lookahead_size;
|
||||
self->current_position.chars += 1;
|
||||
}
|
||||
|
||||
if (lexer->current_position.bytes >= lexer->chunk_start + lexer->chunk_size)
|
||||
ts_lexer__get_chunk(lexer);
|
||||
if (self->current_position.bytes >= self->chunk_start + self->chunk_size)
|
||||
ts_lexer__get_chunk(self);
|
||||
|
||||
ts_lexer__get_lookahead(lexer);
|
||||
ts_lexer__get_lookahead(self);
|
||||
return true;
|
||||
}
|
||||
|
||||
static TSTree *ts_lexer__accept(TSLexer *lexer, TSSymbol symbol,
|
||||
static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
|
||||
TSNodeType node_type, const char *symbol_name) {
|
||||
TSLength size =
|
||||
ts_length_sub(lexer->current_position, lexer->token_start_position);
|
||||
ts_length_sub(self->current_position, self->token_start_position);
|
||||
TSLength padding =
|
||||
ts_length_sub(lexer->token_start_position, lexer->token_end_position);
|
||||
lexer->token_end_position = lexer->current_position;
|
||||
ts_length_sub(self->token_start_position, self->token_end_position);
|
||||
self->token_end_position = self->current_position;
|
||||
|
||||
if (symbol == ts_builtin_sym_error) {
|
||||
DEBUG("error_char");
|
||||
return ts_tree_make_error(size, padding, lexer->lookahead);
|
||||
return ts_tree_make_error(size, padding, self->lookahead);
|
||||
} else {
|
||||
DEBUG("accept_token sym:%s", symbol_name);
|
||||
return ts_tree_make_leaf(symbol, padding, size, node_type);
|
||||
|
|
@ -109,12 +108,12 @@ TSLexer ts_lexer_make() {
|
|||
return result;
|
||||
}
|
||||
|
||||
void ts_lexer_reset(TSLexer *lexer, TSLength position) {
|
||||
lexer->token_start_position = position;
|
||||
lexer->token_end_position = position;
|
||||
lexer->current_position = position;
|
||||
lexer->chunk = 0;
|
||||
lexer->chunk_size = 0;
|
||||
lexer->lookahead_size = 0;
|
||||
lexer->lookahead = 0;
|
||||
void ts_lexer_reset(TSLexer *self, TSLength position) {
|
||||
self->token_start_position = position;
|
||||
self->token_end_position = position;
|
||||
self->current_position = position;
|
||||
self->chunk = 0;
|
||||
self->chunk_size = 0;
|
||||
self->lookahead_size = 0;
|
||||
self->lookahead = 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,18 +16,18 @@ static inline TSNode ts_node__null() {
|
|||
return ts_node_make(NULL, ts_length_zero());
|
||||
}
|
||||
|
||||
static inline const TSTree *ts_node__tree(TSNode this) {
|
||||
return this.data;
|
||||
static inline const TSTree *ts_node__tree(TSNode self) {
|
||||
return self.data;
|
||||
}
|
||||
|
||||
static inline TSLength ts_node__offset(TSNode this) {
|
||||
return this.offset;
|
||||
static inline TSLength ts_node__offset(TSNode self) {
|
||||
return self.offset;
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__child(TSNode this, size_t child_index,
|
||||
static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
||||
TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
|
||||
bool did_descend = true;
|
||||
while (did_descend) {
|
||||
|
|
@ -60,9 +60,9 @@ static inline TSNode ts_node__child(TSNode this, size_t child_index,
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__prev_sibling(TSNode this, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
|
||||
do {
|
||||
size_t index = tree->context.index;
|
||||
|
|
@ -88,9 +88,9 @@ static inline TSNode ts_node__prev_sibling(TSNode this, TSNodeType type) {
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__next_sibling(TSNode this, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
|
||||
do {
|
||||
size_t index = tree->context.index;
|
||||
|
|
@ -115,10 +115,10 @@ static inline TSNode ts_node__next_sibling(TSNode this, TSNodeType type) {
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__descendent_for_range(TSNode this, size_t min,
|
||||
static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
|
||||
size_t max, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree;
|
||||
TSLength position = ts_node__offset(this), last_visible_position = position;
|
||||
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
|
||||
TSLength position = ts_node__offset(self), last_visible_position = position;
|
||||
|
||||
bool did_descend = true;
|
||||
while (did_descend) {
|
||||
|
|
@ -148,43 +148,43 @@ static inline TSNode ts_node__descendent_for_range(TSNode this, size_t min,
|
|||
* Public
|
||||
*/
|
||||
|
||||
TSLength ts_node_pos(TSNode this) {
|
||||
return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding);
|
||||
TSLength ts_node_pos(TSNode self) {
|
||||
return ts_length_add(ts_node__offset(self), ts_node__tree(self)->padding);
|
||||
}
|
||||
|
||||
TSLength ts_node_size(TSNode this) {
|
||||
return ts_node__tree(this)->size;
|
||||
TSLength ts_node_size(TSNode self) {
|
||||
return ts_node__tree(self)->size;
|
||||
}
|
||||
|
||||
TSSymbol ts_node_symbol(TSNode this) {
|
||||
return ts_node__tree(this)->symbol;
|
||||
TSSymbol ts_node_symbol(TSNode self) {
|
||||
return ts_node__tree(self)->symbol;
|
||||
}
|
||||
|
||||
const char *ts_node_name(TSNode this, const TSDocument *document) {
|
||||
return document->parser.language->symbol_names[ts_node__tree(this)->symbol];
|
||||
const char *ts_node_name(TSNode self, const TSDocument *document) {
|
||||
return document->parser.language->symbol_names[ts_node__tree(self)->symbol];
|
||||
}
|
||||
|
||||
const char *ts_node_string(TSNode this, const TSDocument *document) {
|
||||
return ts_tree_string(ts_node__tree(this),
|
||||
const char *ts_node_string(TSNode self, const TSDocument *document) {
|
||||
return ts_tree_string(ts_node__tree(self),
|
||||
document->parser.language->symbol_names);
|
||||
}
|
||||
|
||||
bool ts_node_eq(TSNode this, TSNode other) {
|
||||
return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) &&
|
||||
ts_length_eq(ts_node__offset(this), ts_node__offset(other));
|
||||
bool ts_node_eq(TSNode self, TSNode other) {
|
||||
return ts_tree_eq(ts_node__tree(self), ts_node__tree(other)) &&
|
||||
ts_length_eq(ts_node__offset(self), ts_node__offset(other));
|
||||
}
|
||||
|
||||
bool ts_node_is_named(TSNode this) {
|
||||
return ts_node__tree(this)->options.type == TSNodeTypeNamed;
|
||||
bool ts_node_is_named(TSNode self) {
|
||||
return ts_node__tree(self)->options.type == TSNodeTypeNamed;
|
||||
}
|
||||
|
||||
bool ts_node_has_changes(TSNode this) {
|
||||
return ts_node__tree(this)->options.has_changes;
|
||||
bool ts_node_has_changes(TSNode self) {
|
||||
return ts_node__tree(self)->options.has_changes;
|
||||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode this) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
TSNode ts_node_parent(TSNode self) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
|
||||
do {
|
||||
position = ts_length_sub(position, tree->context.offset);
|
||||
|
|
@ -196,42 +196,42 @@ TSNode ts_node_parent(TSNode this) {
|
|||
return ts_node_make(tree, position);
|
||||
}
|
||||
|
||||
TSNode ts_node_child(TSNode this, size_t child_index) {
|
||||
return ts_node__child(this, child_index, TSNodeTypeAnonymous);
|
||||
TSNode ts_node_child(TSNode self, size_t child_index) {
|
||||
return ts_node__child(self, child_index, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_child(TSNode this, size_t child_index) {
|
||||
return ts_node__child(this, child_index, TSNodeTypeNamed);
|
||||
TSNode ts_node_named_child(TSNode self, size_t child_index) {
|
||||
return ts_node__child(self, child_index, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
size_t ts_node_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->visible_child_count;
|
||||
size_t ts_node_child_count(TSNode self) {
|
||||
return ts_node__tree(self)->visible_child_count;
|
||||
}
|
||||
|
||||
size_t ts_node_named_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->named_child_count;
|
||||
size_t ts_node_named_child_count(TSNode self) {
|
||||
return ts_node__tree(self)->named_child_count;
|
||||
}
|
||||
|
||||
TSNode ts_node_next_sibling(TSNode this) {
|
||||
return ts_node__next_sibling(this, TSNodeTypeAnonymous);
|
||||
TSNode ts_node_next_sibling(TSNode self) {
|
||||
return ts_node__next_sibling(self, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_next_named_sibling(TSNode this) {
|
||||
return ts_node__next_sibling(this, TSNodeTypeNamed);
|
||||
TSNode ts_node_next_named_sibling(TSNode self) {
|
||||
return ts_node__next_sibling(self, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling(this, TSNodeTypeAnonymous);
|
||||
TSNode ts_node_prev_sibling(TSNode self) {
|
||||
return ts_node__prev_sibling(self, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_named_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling(this, TSNodeTypeNamed);
|
||||
TSNode ts_node_prev_named_sibling(TSNode self) {
|
||||
return ts_node__prev_sibling(self, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
TSNode ts_node_descendent_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(this, min, max, TSNodeTypeAnonymous);
|
||||
TSNode ts_node_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_descendent_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(this, min, max, TSNodeTypeNamed);
|
||||
TSNode ts_node_named_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, TSNodeTypeNamed);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,14 @@
|
|||
* Debugging
|
||||
*/
|
||||
|
||||
#define DEBUG(...) \
|
||||
if (parser->lexer.debugger.debug_fn) { \
|
||||
snprintf(parser->lexer.debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
||||
parser->lexer.debugger.debug_fn(parser->lexer.debugger.payload, \
|
||||
TSDebugTypeParse, \
|
||||
parser->lexer.debug_buffer); \
|
||||
#define DEBUG(...) \
|
||||
if (self->lexer.debugger.debug_fn) { \
|
||||
snprintf(self->lexer.debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
||||
self->lexer.debugger.debug_fn(self->lexer.debugger.payload, \
|
||||
TSDebugTypeParse, self->lexer.debug_buffer); \
|
||||
}
|
||||
|
||||
#define SYM_NAME(sym) parser->language->symbol_names[sym]
|
||||
#define SYM_NAME(sym) self->language->symbol_names[sym]
|
||||
|
||||
/*
|
||||
* Private
|
||||
|
|
@ -42,17 +41,17 @@ static TSParseAction ts_language__action(const TSLanguage *language,
|
|||
}
|
||||
|
||||
/*
|
||||
* Replace parser's reusable_subtree with its first non-fragile descendant.
|
||||
* Replace the parser's reusable_subtree with its first non-fragile descendant.
|
||||
* Return true if a suitable descendant is found, false otherwise.
|
||||
*/
|
||||
static bool ts_parser__breakdown_reusable_subtree(TSParser *parser) {
|
||||
static bool ts_parser__breakdown_reusable_subtree(TSParser *self) {
|
||||
do {
|
||||
if (parser->reusable_subtree->symbol == ts_builtin_sym_error)
|
||||
if (self->reusable_subtree->symbol == ts_builtin_sym_error)
|
||||
return false;
|
||||
if (parser->reusable_subtree->child_count == 0)
|
||||
if (self->reusable_subtree->child_count == 0)
|
||||
return false;
|
||||
parser->reusable_subtree = parser->reusable_subtree->children[0];
|
||||
} while (ts_tree_is_fragile(parser->reusable_subtree));
|
||||
self->reusable_subtree = self->reusable_subtree->children[0];
|
||||
} while (ts_tree_is_fragile(self->reusable_subtree));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -60,18 +59,17 @@ static bool ts_parser__breakdown_reusable_subtree(TSParser *parser) {
|
|||
* Replace the parser's reusable_subtree with its largest right neighbor, or
|
||||
* NULL if no right neighbor exists.
|
||||
*/
|
||||
static void ts_parser__pop_reusable_subtree(TSParser *parser) {
|
||||
parser->reusable_subtree_pos +=
|
||||
ts_tree_total_size(parser->reusable_subtree).chars;
|
||||
static void ts_parser__pop_reusable_subtree(TSParser *self) {
|
||||
self->reusable_subtree_pos += ts_tree_total_size(self->reusable_subtree).chars;
|
||||
|
||||
while (parser->reusable_subtree) {
|
||||
TSTree *parent = parser->reusable_subtree->context.parent;
|
||||
size_t next_index = parser->reusable_subtree->context.index + 1;
|
||||
while (self->reusable_subtree) {
|
||||
TSTree *parent = self->reusable_subtree->context.parent;
|
||||
size_t next_index = self->reusable_subtree->context.index + 1;
|
||||
if (parent && parent->child_count > next_index) {
|
||||
parser->reusable_subtree = parent->children[next_index];
|
||||
self->reusable_subtree = parent->children[next_index];
|
||||
return;
|
||||
}
|
||||
parser->reusable_subtree = parent;
|
||||
self->reusable_subtree = parent;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,59 +78,59 @@ static void ts_parser__pop_reusable_subtree(TSParser *parser) {
|
|||
* at the correct position in the parser's previous tree, use that. Otherwise,
|
||||
* run the lexer.
|
||||
*/
|
||||
static void ts_parser__get_next_lookahead(TSParser *parser) {
|
||||
while (parser->reusable_subtree) {
|
||||
if (parser->reusable_subtree_pos > parser->lexer.current_position.chars) {
|
||||
static void ts_parser__get_next_lookahead(TSParser *self) {
|
||||
while (self->reusable_subtree) {
|
||||
if (self->reusable_subtree_pos > self->lexer.current_position.chars) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (parser->reusable_subtree_pos < parser->lexer.current_position.chars) {
|
||||
DEBUG("past_reuse sym:%s", SYM_NAME(parser->reusable_subtree->symbol));
|
||||
ts_parser__pop_reusable_subtree(parser);
|
||||
if (self->reusable_subtree_pos < self->lexer.current_position.chars) {
|
||||
DEBUG("past_reuse sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
||||
ts_parser__pop_reusable_subtree(self);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ts_tree_has_changes(parser->reusable_subtree) ||
|
||||
ts_tree_is_fragile(parser->reusable_subtree) ||
|
||||
ts_tree_is_extra(parser->reusable_subtree)) {
|
||||
DEBUG("breakdown sym:%s", SYM_NAME(parser->reusable_subtree->symbol));
|
||||
if (!ts_parser__breakdown_reusable_subtree(parser))
|
||||
ts_parser__pop_reusable_subtree(parser);
|
||||
if (ts_tree_has_changes(self->reusable_subtree) ||
|
||||
ts_tree_is_fragile(self->reusable_subtree) ||
|
||||
ts_tree_is_extra(self->reusable_subtree)) {
|
||||
DEBUG("breakdown sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
||||
if (!ts_parser__breakdown_reusable_subtree(self))
|
||||
ts_parser__pop_reusable_subtree(self);
|
||||
continue;
|
||||
}
|
||||
|
||||
TSStateId top_state = ts_stack_top_state(parser->stack, 0);
|
||||
TSSymbol symbol = parser->reusable_subtree->symbol;
|
||||
if (ts_language__action(parser->language, top_state, symbol).type ==
|
||||
TSStateId top_state = ts_stack_top_state(self->stack, 0);
|
||||
TSSymbol symbol = self->reusable_subtree->symbol;
|
||||
if (ts_language__action(self->language, top_state, symbol).type ==
|
||||
TSParseActionTypeError) {
|
||||
DEBUG("cant_reuse sym:%s", SYM_NAME(parser->reusable_subtree->symbol));
|
||||
ts_parser__pop_reusable_subtree(parser);
|
||||
DEBUG("cant_reuse sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
||||
ts_parser__pop_reusable_subtree(self);
|
||||
continue;
|
||||
}
|
||||
|
||||
parser->lookahead = parser->reusable_subtree;
|
||||
TSLength size = ts_tree_total_size(parser->lookahead);
|
||||
DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(parser->lookahead->symbol),
|
||||
size.chars, parser->lookahead->options.extra);
|
||||
ts_lexer_reset(&parser->lexer,
|
||||
ts_length_add(parser->lexer.current_position, size));
|
||||
ts_parser__pop_reusable_subtree(parser);
|
||||
self->lookahead = self->reusable_subtree;
|
||||
TSLength size = ts_tree_total_size(self->lookahead);
|
||||
DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(self->lookahead->symbol),
|
||||
size.chars, self->lookahead->options.extra);
|
||||
ts_lexer_reset(&self->lexer,
|
||||
ts_length_add(self->lexer.current_position, size));
|
||||
ts_parser__pop_reusable_subtree(self);
|
||||
return;
|
||||
}
|
||||
|
||||
TSLength position = parser->lexer.current_position;
|
||||
for (size_t i = 0, count = ts_stack_head_count(parser->stack); i < count; i++) {
|
||||
TSLength position = self->lexer.current_position;
|
||||
for (size_t i = 0, count = ts_stack_head_count(self->stack); i < count; i++) {
|
||||
if (i > 0) {
|
||||
ts_lexer_reset(&parser->lexer, position);
|
||||
ts_tree_release(parser->lookahead);
|
||||
ts_lexer_reset(&self->lexer, position);
|
||||
ts_tree_release(self->lookahead);
|
||||
}
|
||||
|
||||
TSStateId parse_state = ts_stack_top_state(parser->stack, i);
|
||||
TSStateId lex_state = parser->language->lex_states[parse_state];
|
||||
TSStateId parse_state = ts_stack_top_state(self->stack, i);
|
||||
TSStateId lex_state = self->language->lex_states[parse_state];
|
||||
DEBUG("lex state:%d", lex_state);
|
||||
parser->lookahead = parser->language->lex_fn(&parser->lexer, lex_state);
|
||||
self->lookahead = self->language->lex_fn(&self->lexer, lex_state);
|
||||
|
||||
if (parser->lookahead->symbol != ts_builtin_sym_error)
|
||||
if (self->lookahead->symbol != ts_builtin_sym_error)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -141,21 +139,21 @@ static void ts_parser__get_next_lookahead(TSParser *parser) {
|
|||
* Parse Actions
|
||||
*/
|
||||
|
||||
static void ts_parser__shift(TSParser *parser, int head, TSStateId parse_state) {
|
||||
ts_stack_push(parser->stack, head, parse_state, parser->lookahead);
|
||||
static void ts_parser__shift(TSParser *self, int head, TSStateId parse_state) {
|
||||
ts_stack_push(self->stack, head, parse_state, self->lookahead);
|
||||
}
|
||||
|
||||
static void ts_parser__shift_extra(TSParser *parser, int head, TSStateId state) {
|
||||
ts_tree_set_extra(parser->lookahead);
|
||||
ts_parser__shift(parser, head, state);
|
||||
static void ts_parser__shift_extra(TSParser *self, int head, TSStateId state) {
|
||||
ts_tree_set_extra(self->lookahead);
|
||||
ts_parser__shift(self, head, state);
|
||||
}
|
||||
|
||||
static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
|
||||
static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
||||
size_t child_count, bool extra,
|
||||
bool count_extra) {
|
||||
TSNodeType node_type = parser->language->node_types[symbol];
|
||||
TSNodeType node_type = self->language->node_types[symbol];
|
||||
StackPopResultList pop_results =
|
||||
ts_stack_pop(parser->stack, head, child_count, count_extra);
|
||||
ts_stack_pop(self->stack, head, child_count, count_extra);
|
||||
|
||||
TSTree *parent = NULL;
|
||||
TSTree **last_children = NULL;
|
||||
|
|
@ -170,20 +168,20 @@ static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
|
|||
}
|
||||
|
||||
if (pop_result.index == last_index) {
|
||||
ts_stack_add_alternative(parser->stack, pop_result.index, parent);
|
||||
ts_stack_add_alternative(self->stack, pop_result.index, parent);
|
||||
} else {
|
||||
TSStateId top_state = ts_stack_top_state(parser->stack, pop_result.index);
|
||||
TSStateId top_state = ts_stack_top_state(self->stack, pop_result.index);
|
||||
TSStateId state;
|
||||
|
||||
if (extra) {
|
||||
ts_tree_set_extra(parent);
|
||||
state = top_state;
|
||||
} else {
|
||||
state = ts_language__action(parser->language, top_state, symbol)
|
||||
.data.to_state;
|
||||
state =
|
||||
ts_language__action(self->language, top_state, symbol).data.to_state;
|
||||
}
|
||||
|
||||
ts_stack_push(parser->stack, pop_result.index, state, parent);
|
||||
ts_stack_push(self->stack, pop_result.index, state, parent);
|
||||
}
|
||||
|
||||
last_index = pop_result.index;
|
||||
|
|
@ -193,27 +191,27 @@ static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
|
|||
return parent;
|
||||
}
|
||||
|
||||
static void ts_parser__reduce_fragile(TSParser *parser, int head,
|
||||
TSSymbol symbol, size_t child_count) {
|
||||
static void ts_parser__reduce_fragile(TSParser *self, int head, TSSymbol symbol,
|
||||
size_t child_count) {
|
||||
TSTree *reduced =
|
||||
ts_parser__reduce(parser, head, symbol, child_count, false, false);
|
||||
ts_parser__reduce(self, head, symbol, child_count, false, false);
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static void ts_parser__reduce_error(TSParser *parser, int head,
|
||||
static void ts_parser__reduce_error(TSParser *self, int head,
|
||||
size_t child_count) {
|
||||
TSTree *reduced = ts_parser__reduce(parser, head, ts_builtin_sym_error,
|
||||
TSTree *reduced = ts_parser__reduce(self, head, ts_builtin_sym_error,
|
||||
child_count, false, true);
|
||||
reduced->size = ts_length_add(reduced->size, parser->lookahead->padding);
|
||||
parser->lookahead->padding = ts_length_zero();
|
||||
reduced->size = ts_length_add(reduced->size, self->lookahead->padding);
|
||||
self->lookahead->padding = ts_length_zero();
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static bool ts_parser__handle_error(TSParser *parser, int head) {
|
||||
static bool ts_parser__handle_error(TSParser *self, int head) {
|
||||
size_t error_token_count = 1;
|
||||
StackEntry *entry_before_error = ts_stack_head(parser->stack, head);
|
||||
StackEntry *entry_before_error = ts_stack_head(self->stack, head);
|
||||
|
||||
for (;;) {
|
||||
|
||||
|
|
@ -225,18 +223,18 @@ static bool ts_parser__handle_error(TSParser *parser, int head) {
|
|||
for (StackEntry *entry = entry_before_error; entry != NULL;
|
||||
entry = ts_stack_entry_next(entry, head), i++) {
|
||||
TSStateId stack_state = entry->state;
|
||||
TSParseAction action_on_error = ts_language__action(
|
||||
parser->language, stack_state, ts_builtin_sym_error);
|
||||
TSParseAction action_on_error =
|
||||
ts_language__action(self->language, stack_state, ts_builtin_sym_error);
|
||||
|
||||
if (action_on_error.type == TSParseActionTypeShift) {
|
||||
TSStateId state_after_error = action_on_error.data.to_state;
|
||||
TSParseAction action_after_error = ts_language__action(
|
||||
parser->language, state_after_error, parser->lookahead->symbol);
|
||||
self->language, state_after_error, self->lookahead->symbol);
|
||||
|
||||
if (action_after_error.type != TSParseActionTypeError) {
|
||||
DEBUG("recover state:%u, count:%lu", state_after_error,
|
||||
error_token_count + i);
|
||||
ts_parser__reduce_error(parser, head, error_token_count + i);
|
||||
ts_parser__reduce_error(self, head, error_token_count + i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -246,24 +244,23 @@ static bool ts_parser__handle_error(TSParser *parser, int head) {
|
|||
* If there is no state in the stack for which we can recover with the
|
||||
* current lookahead token, advance to the next token.
|
||||
*/
|
||||
DEBUG("skip token:%s", SYM_NAME(parser->lookahead->symbol));
|
||||
ts_parser__shift(parser, head, ts_stack_top_state(parser->stack, head));
|
||||
parser->lookahead =
|
||||
parser->language->lex_fn(&parser->lexer, ts_lex_state_error);
|
||||
DEBUG("skip token:%s", SYM_NAME(self->lookahead->symbol));
|
||||
ts_parser__shift(self, head, ts_stack_top_state(self->stack, head));
|
||||
self->lookahead = self->language->lex_fn(&self->lexer, ts_lex_state_error);
|
||||
error_token_count++;
|
||||
|
||||
/*
|
||||
* If the end of input is reached, exit.
|
||||
*/
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_end) {
|
||||
if (self->lookahead->symbol == ts_builtin_sym_end) {
|
||||
DEBUG("fail_to_recover");
|
||||
ts_parser__reduce_error(parser, head, error_token_count - 1);
|
||||
ts_parser__reduce_error(self, head, error_token_count - 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ts_parser__start(TSParser *parser, TSInput input,
|
||||
static void ts_parser__start(TSParser *self, TSInput input,
|
||||
TSTree *previous_tree) {
|
||||
if (previous_tree) {
|
||||
DEBUG("parse_after_edit");
|
||||
|
|
@ -271,18 +268,17 @@ static void ts_parser__start(TSParser *parser, TSInput input,
|
|||
DEBUG("new_parse");
|
||||
}
|
||||
|
||||
parser->lexer.input = input;
|
||||
ts_lexer_reset(&parser->lexer, ts_length_zero());
|
||||
ts_stack_clear(parser->stack);
|
||||
self->lexer.input = input;
|
||||
ts_lexer_reset(&self->lexer, ts_length_zero());
|
||||
ts_stack_clear(self->stack);
|
||||
|
||||
parser->reusable_subtree = previous_tree;
|
||||
parser->reusable_subtree_pos = 0;
|
||||
parser->lookahead = NULL;
|
||||
self->reusable_subtree = previous_tree;
|
||||
self->reusable_subtree_pos = 0;
|
||||
self->lookahead = NULL;
|
||||
}
|
||||
|
||||
static TSTree *ts_parser__finish(TSParser *parser) {
|
||||
StackPopResult pop_result =
|
||||
ts_stack_pop(parser->stack, 0, -1, true).contents[0];
|
||||
static TSTree *ts_parser__finish(TSParser *self) {
|
||||
StackPopResult pop_result = ts_stack_pop(self->stack, 0, -1, true).contents[0];
|
||||
|
||||
TSTree **trees = pop_result.trees;
|
||||
size_t extra_count = pop_result.tree_count - 1;
|
||||
|
|
@ -302,11 +298,11 @@ typedef enum {
|
|||
* Continue performing parse actions for the given head until the current
|
||||
* lookahead symbol is consumed.
|
||||
*/
|
||||
static ConsumeResult ts_parser__consume_lookahead(TSParser *parser, int head) {
|
||||
static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head) {
|
||||
for (;;) {
|
||||
TSStateId state = ts_stack_top_state(parser->stack, head);
|
||||
TSStateId state = ts_stack_top_state(self->stack, head);
|
||||
const TSParseAction *next_action =
|
||||
ts_language__actions(parser->language, state, parser->lookahead->symbol);
|
||||
ts_language__actions(self->language, state, self->lookahead->symbol);
|
||||
|
||||
/*
|
||||
* If there are multiple actions for the current state and lookahead symbol,
|
||||
|
|
@ -323,57 +319,57 @@ static ConsumeResult ts_parser__consume_lookahead(TSParser *parser, int head) {
|
|||
current_head = head;
|
||||
DEBUG("action current_head:%d, state:%d", current_head, state);
|
||||
} else {
|
||||
current_head = ts_stack_split(parser->stack, head);
|
||||
current_head = ts_stack_split(self->stack, head);
|
||||
DEBUG("split_action from_head:%d, current_head:%d, state:%d", head,
|
||||
current_head, state);
|
||||
}
|
||||
|
||||
// TODO: Remove this by making a separate symbol for errors returned from
|
||||
// the lexer.
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_error)
|
||||
if (self->lookahead->symbol == ts_builtin_sym_error)
|
||||
action.type = TSParseActionTypeError;
|
||||
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeError:
|
||||
DEBUG("error_sym");
|
||||
if (ts_stack_head_count(parser->stack) == 1) {
|
||||
if (ts_parser__handle_error(parser, current_head))
|
||||
if (ts_stack_head_count(self->stack) == 1) {
|
||||
if (ts_parser__handle_error(self, current_head))
|
||||
break;
|
||||
else
|
||||
return ConsumeResultFinished;
|
||||
} else {
|
||||
DEBUG("bail current_head:%d", current_head);
|
||||
ts_stack_remove_head(parser->stack, current_head);
|
||||
ts_stack_remove_head(self->stack, current_head);
|
||||
return ConsumeResultRemoved;
|
||||
}
|
||||
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG("shift state:%u", action.data.to_state);
|
||||
ts_parser__shift(parser, current_head, action.data.to_state);
|
||||
ts_parser__shift(self, current_head, action.data.to_state);
|
||||
return ConsumeResultShifted;
|
||||
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG("shift_extra");
|
||||
ts_parser__shift_extra(parser, current_head, state);
|
||||
ts_parser__shift_extra(self, current_head, state);
|
||||
return ConsumeResultShifted;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG("reduce sym:%s, child_count:%u", SYM_NAME(action.data.symbol),
|
||||
action.data.child_count);
|
||||
ts_parser__reduce(parser, current_head, action.data.symbol,
|
||||
ts_parser__reduce(self, current_head, action.data.symbol,
|
||||
action.data.child_count, false, false);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceExtra:
|
||||
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
||||
ts_parser__reduce(parser, current_head, action.data.symbol, 1, true,
|
||||
ts_parser__reduce(self, current_head, action.data.symbol, 1, true,
|
||||
false);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceFragile:
|
||||
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
||||
action.data.child_count);
|
||||
ts_parser__reduce_fragile(parser, current_head, action.data.symbol,
|
||||
ts_parser__reduce_fragile(self, current_head, action.data.symbol,
|
||||
action.data.child_count);
|
||||
break;
|
||||
|
||||
|
|
@ -403,40 +399,39 @@ TSParser ts_parser_make() {
|
|||
};
|
||||
}
|
||||
|
||||
void ts_parser_destroy(TSParser *parser) {
|
||||
ts_stack_delete(parser->stack);
|
||||
if (parser->lookahead)
|
||||
ts_tree_release(parser->lookahead);
|
||||
void ts_parser_destroy(TSParser *self) {
|
||||
ts_stack_delete(self->stack);
|
||||
if (self->lookahead)
|
||||
ts_tree_release(self->lookahead);
|
||||
}
|
||||
|
||||
TSDebugger ts_parser_debugger(const TSParser *parser) {
|
||||
return parser->lexer.debugger;
|
||||
TSDebugger ts_parser_debugger(const TSParser *self) {
|
||||
return self->lexer.debugger;
|
||||
}
|
||||
|
||||
void ts_parser_set_debugger(TSParser *parser, TSDebugger debugger) {
|
||||
parser->lexer.debugger = debugger;
|
||||
void ts_parser_set_debugger(TSParser *self, TSDebugger debugger) {
|
||||
self->lexer.debugger = debugger;
|
||||
}
|
||||
|
||||
TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSTree *previous_tree) {
|
||||
ts_parser__start(parser, input, previous_tree);
|
||||
TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
|
||||
ts_parser__start(self, input, previous_tree);
|
||||
|
||||
for (;;) {
|
||||
ts_parser__get_next_lookahead(parser);
|
||||
ts_parser__get_next_lookahead(self);
|
||||
|
||||
DEBUG("lookahead sym:%s, pos:%lu, head_count:%d",
|
||||
SYM_NAME(parser->lookahead->symbol),
|
||||
parser->lexer.current_position.chars,
|
||||
ts_stack_head_count(parser->stack));
|
||||
SYM_NAME(self->lookahead->symbol), self->lexer.current_position.chars,
|
||||
ts_stack_head_count(self->stack));
|
||||
|
||||
for (int head = 0; head < ts_stack_head_count(parser->stack);) {
|
||||
switch (ts_parser__consume_lookahead(parser, head)) {
|
||||
for (int head = 0; head < ts_stack_head_count(self->stack);) {
|
||||
switch (ts_parser__consume_lookahead(self, head)) {
|
||||
case ConsumeResultRemoved:
|
||||
break;
|
||||
case ConsumeResultShifted:
|
||||
head++;
|
||||
break;
|
||||
case ConsumeResultFinished:
|
||||
return ts_parser__finish(parser);
|
||||
return ts_parser__finish(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,43 +29,43 @@ struct Stack {
|
|||
*/
|
||||
|
||||
Stack *ts_stack_new(TreeSelectionCallback tree_selection_callback) {
|
||||
Stack *this = malloc(sizeof(Stack));
|
||||
*this = (Stack){
|
||||
Stack *self = malloc(sizeof(Stack));
|
||||
*self = (Stack){
|
||||
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)),
|
||||
.head_count = 1,
|
||||
.head_capacity = INITIAL_HEAD_CAPACITY,
|
||||
.tree_selection_callback = tree_selection_callback,
|
||||
};
|
||||
return this;
|
||||
return self;
|
||||
}
|
||||
|
||||
void ts_stack_delete(Stack *this) {
|
||||
free(this->heads);
|
||||
free(this);
|
||||
void ts_stack_delete(Stack *self) {
|
||||
free(self->heads);
|
||||
free(self);
|
||||
}
|
||||
|
||||
/*
|
||||
* Section: Reading from the stack
|
||||
*/
|
||||
|
||||
TSStateId ts_stack_top_state(const Stack *this, int head) {
|
||||
StackEntry *entry = ts_stack_head((Stack *)this, head);
|
||||
TSStateId ts_stack_top_state(const Stack *self, int head) {
|
||||
StackEntry *entry = ts_stack_head((Stack *)self, head);
|
||||
return entry ? entry->state : 0;
|
||||
}
|
||||
|
||||
TSTree *ts_stack_top_tree(const Stack *this, int head) {
|
||||
StackEntry *entry = ts_stack_head((Stack *)this, head);
|
||||
TSTree *ts_stack_top_tree(const Stack *self, int head) {
|
||||
StackEntry *entry = ts_stack_head((Stack *)self, head);
|
||||
return entry ? entry->tree : NULL;
|
||||
}
|
||||
|
||||
StackEntry *ts_stack_head(Stack *this, int head) {
|
||||
assert(head < this->head_count);
|
||||
StackNode *node = this->heads[head];
|
||||
StackEntry *ts_stack_head(Stack *self, int head) {
|
||||
assert(head < self->head_count);
|
||||
StackNode *node = self->heads[head];
|
||||
return node ? &node->entry : NULL;
|
||||
}
|
||||
|
||||
int ts_stack_head_count(const Stack *this) {
|
||||
return this->head_count;
|
||||
int ts_stack_head_count(const Stack *self) {
|
||||
return self->head_count;
|
||||
}
|
||||
|
||||
int ts_stack_entry_next_count(const StackEntry *entry) {
|
||||
|
|
@ -80,23 +80,23 @@ StackEntry *ts_stack_entry_next(const StackEntry *entry, int i) {
|
|||
* Section: Manipulating nodes (Private)
|
||||
*/
|
||||
|
||||
static void stack_node_retain(StackNode *this) {
|
||||
if (!this)
|
||||
static void stack_node_retain(StackNode *self) {
|
||||
if (!self)
|
||||
return;
|
||||
assert(this->ref_count != 0);
|
||||
this->ref_count++;
|
||||
assert(self->ref_count != 0);
|
||||
self->ref_count++;
|
||||
}
|
||||
|
||||
static bool stack_node_release(StackNode *this) {
|
||||
if (!this)
|
||||
static bool stack_node_release(StackNode *self) {
|
||||
if (!self)
|
||||
return false;
|
||||
assert(this->ref_count != 0);
|
||||
this->ref_count--;
|
||||
if (this->ref_count == 0) {
|
||||
for (int i = 0; i < this->successor_count; i++)
|
||||
stack_node_release(this->successors[i]);
|
||||
ts_tree_release(this->entry.tree);
|
||||
free(this);
|
||||
assert(self->ref_count != 0);
|
||||
self->ref_count--;
|
||||
if (self->ref_count == 0) {
|
||||
for (int i = 0; i < self->successor_count; i++)
|
||||
stack_node_release(self->successors[i]);
|
||||
ts_tree_release(self->entry.tree);
|
||||
free(self);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
@ -104,10 +104,10 @@ static bool stack_node_release(StackNode *this) {
|
|||
}
|
||||
|
||||
static StackNode *stack_node_new(StackNode *next, TSStateId state, TSTree *tree) {
|
||||
StackNode *this = malloc(sizeof(StackNode));
|
||||
StackNode *self = malloc(sizeof(StackNode));
|
||||
ts_tree_retain(tree);
|
||||
stack_node_retain(next);
|
||||
*this = (StackNode){
|
||||
*self = (StackNode){
|
||||
.ref_count = 1,
|
||||
.successor_count = 1,
|
||||
.successors = { next, NULL, NULL },
|
||||
|
|
@ -116,10 +116,10 @@ static StackNode *stack_node_new(StackNode *next, TSStateId state, TSTree *tree)
|
|||
.state = state, .tree = tree,
|
||||
},
|
||||
};
|
||||
return this;
|
||||
return self;
|
||||
}
|
||||
|
||||
static void ts_stack__add_node_successor(Stack *this, StackNode *node,
|
||||
static void ts_stack__add_node_successor(Stack *self, StackNode *node,
|
||||
StackNode *new_successor) {
|
||||
for (int i = 0; i < node->successor_count; i++) {
|
||||
StackNode *successor = node->successors[i];
|
||||
|
|
@ -127,11 +127,11 @@ static void ts_stack__add_node_successor(Stack *this, StackNode *node,
|
|||
return;
|
||||
if (successor->entry.state == new_successor->entry.state) {
|
||||
if (successor->entry.tree != new_successor->entry.tree)
|
||||
successor->entry.tree = this->tree_selection_callback.callback(
|
||||
this->tree_selection_callback.data, successor->entry.tree,
|
||||
successor->entry.tree = self->tree_selection_callback.callback(
|
||||
self->tree_selection_callback.data, successor->entry.tree,
|
||||
new_successor->entry.tree);
|
||||
for (int j = 0; j < new_successor->successor_count; j++)
|
||||
ts_stack__add_node_successor(this, successor,
|
||||
ts_stack__add_node_successor(self, successor,
|
||||
new_successor->successors[j]);
|
||||
return;
|
||||
}
|
||||
|
|
@ -146,45 +146,45 @@ static void ts_stack__add_node_successor(Stack *this, StackNode *node,
|
|||
* Section: Mutating the stack (Private)
|
||||
*/
|
||||
|
||||
static int ts_stack__add_head(Stack *this, StackNode *node) {
|
||||
if (this->head_count == this->head_capacity) {
|
||||
this->head_capacity += 3;
|
||||
this->heads =
|
||||
realloc(this->heads, this->head_capacity * sizeof(StackNode *));
|
||||
static int ts_stack__add_head(Stack *self, StackNode *node) {
|
||||
if (self->head_count == self->head_capacity) {
|
||||
self->head_capacity += 3;
|
||||
self->heads =
|
||||
realloc(self->heads, self->head_capacity * sizeof(StackNode *));
|
||||
}
|
||||
int new_index = this->head_count++;
|
||||
this->heads[new_index] = node;
|
||||
int new_index = self->head_count++;
|
||||
self->heads[new_index] = node;
|
||||
stack_node_retain(node);
|
||||
return new_index;
|
||||
}
|
||||
|
||||
static int ts_stack__find_or_add_head(Stack *this, StackNode *node) {
|
||||
for (int i = 0; i < this->head_count; i++)
|
||||
if (this->heads[i] == node) {
|
||||
static int ts_stack__find_or_add_head(Stack *self, StackNode *node) {
|
||||
for (int i = 0; i < self->head_count; i++)
|
||||
if (self->heads[i] == node) {
|
||||
return i;
|
||||
}
|
||||
return ts_stack__add_head(this, node);
|
||||
return ts_stack__add_head(self, node);
|
||||
}
|
||||
|
||||
void ts_stack_remove_head(Stack *this, int head_index) {
|
||||
stack_node_release(this->heads[head_index]);
|
||||
for (int i = head_index; i < this->head_count - 1; i++) {
|
||||
this->heads[head_index] = this->heads[head_index + 1];
|
||||
void ts_stack_remove_head(Stack *self, int head_index) {
|
||||
stack_node_release(self->heads[head_index]);
|
||||
for (int i = head_index; i < self->head_count - 1; i++) {
|
||||
self->heads[head_index] = self->heads[head_index + 1];
|
||||
}
|
||||
this->head_count--;
|
||||
self->head_count--;
|
||||
}
|
||||
|
||||
static bool ts_stack__merge_head(Stack *this, int head_index, TSStateId state,
|
||||
static bool ts_stack__merge_head(Stack *self, int head_index, TSStateId state,
|
||||
TSTree *tree) {
|
||||
for (int i = 0; i < head_index; i++) {
|
||||
StackNode *head = this->heads[i];
|
||||
StackNode *head = self->heads[i];
|
||||
if (head->entry.state == state) {
|
||||
if (head->entry.tree != tree) {
|
||||
head->entry.tree = this->tree_selection_callback.callback(
|
||||
this->tree_selection_callback.data, head->entry.tree, tree);
|
||||
head->entry.tree = self->tree_selection_callback.callback(
|
||||
self->tree_selection_callback.data, head->entry.tree, tree);
|
||||
}
|
||||
ts_stack__add_node_successor(this, head, this->heads[head_index]);
|
||||
ts_stack_remove_head(this, head_index);
|
||||
ts_stack__add_node_successor(self, head, self->heads[head_index]);
|
||||
ts_stack_remove_head(self, head_index);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -195,29 +195,29 @@ static bool ts_stack__merge_head(Stack *this, int head_index, TSStateId state,
|
|||
* Section: Mutating the stack (Public)
|
||||
*/
|
||||
|
||||
bool ts_stack_push(Stack *this, int head_index, TSStateId state, TSTree *tree) {
|
||||
assert(head_index < this->head_count);
|
||||
if (ts_stack__merge_head(this, head_index, state, tree))
|
||||
bool ts_stack_push(Stack *self, int head_index, TSStateId state, TSTree *tree) {
|
||||
assert(head_index < self->head_count);
|
||||
if (ts_stack__merge_head(self, head_index, state, tree))
|
||||
return true;
|
||||
this->heads[head_index] = stack_node_new(this->heads[head_index], state, tree);
|
||||
self->heads[head_index] = stack_node_new(self->heads[head_index], state, tree);
|
||||
return false;
|
||||
}
|
||||
|
||||
void ts_stack_add_alternative(Stack *this, int head_index, TSTree *tree) {
|
||||
assert(head_index < this->head_count);
|
||||
StackEntry *entry = &this->heads[head_index]->entry;
|
||||
entry->tree = this->tree_selection_callback.callback(
|
||||
this->tree_selection_callback.data, entry->tree, tree);
|
||||
void ts_stack_add_alternative(Stack *self, int head_index, TSTree *tree) {
|
||||
assert(head_index < self->head_count);
|
||||
StackEntry *entry = &self->heads[head_index]->entry;
|
||||
entry->tree = self->tree_selection_callback.callback(
|
||||
self->tree_selection_callback.data, entry->tree, tree);
|
||||
}
|
||||
|
||||
int ts_stack_split(Stack *this, int head_index) {
|
||||
assert(head_index < this->head_count);
|
||||
return ts_stack__add_head(this, this->heads[head_index]);
|
||||
int ts_stack_split(Stack *self, int head_index) {
|
||||
assert(head_index < self->head_count);
|
||||
return ts_stack__add_head(self, self->heads[head_index]);
|
||||
}
|
||||
|
||||
StackPopResultList ts_stack_pop(Stack *this, int head_index, int child_count,
|
||||
StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count,
|
||||
bool count_extra) {
|
||||
StackNode *previous_head = this->heads[head_index];
|
||||
StackNode *previous_head = self->heads[head_index];
|
||||
|
||||
int path_count = 1;
|
||||
int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count;
|
||||
|
|
@ -280,13 +280,13 @@ StackPopResultList ts_stack_pop(Stack *this, int head_index, int child_count,
|
|||
int index = -1;
|
||||
if (path == 0) {
|
||||
stack_node_retain(nodes_by_path[path]);
|
||||
this->heads[head_index] = nodes_by_path[path];
|
||||
self->heads[head_index] = nodes_by_path[path];
|
||||
index = head_index;
|
||||
} else {
|
||||
index = ts_stack__find_or_add_head(this, nodes_by_path[path]);
|
||||
index = ts_stack__find_or_add_head(self, nodes_by_path[path]);
|
||||
}
|
||||
|
||||
this->last_pop_results[path] = (StackPopResult){
|
||||
self->last_pop_results[path] = (StackPopResult){
|
||||
.index = index,
|
||||
.tree_count = trees_by_path[path].size,
|
||||
.trees = trees_by_path[path].contents,
|
||||
|
|
@ -295,12 +295,12 @@ StackPopResultList ts_stack_pop(Stack *this, int head_index, int child_count,
|
|||
|
||||
stack_node_release(previous_head);
|
||||
return (StackPopResultList){
|
||||
.size = path_count, .contents = this->last_pop_results,
|
||||
.size = path_count, .contents = self->last_pop_results,
|
||||
};
|
||||
}
|
||||
|
||||
void ts_stack_shrink(Stack *this, int head_index, int count) {
|
||||
StackNode *head = this->heads[head_index];
|
||||
void ts_stack_shrink(Stack *self, int head_index, int count) {
|
||||
StackNode *head = self->heads[head_index];
|
||||
StackNode *new_head = head;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (new_head->successor_count == 0)
|
||||
|
|
@ -309,12 +309,12 @@ void ts_stack_shrink(Stack *this, int head_index, int count) {
|
|||
}
|
||||
stack_node_retain(new_head);
|
||||
stack_node_release(head);
|
||||
this->heads[head_index] = new_head;
|
||||
self->heads[head_index] = new_head;
|
||||
}
|
||||
|
||||
void ts_stack_clear(Stack *this) {
|
||||
for (int i = 0; i < this->head_count; i++)
|
||||
stack_node_release(this->heads[i]);
|
||||
this->head_count = 1;
|
||||
this->heads[0] = NULL;
|
||||
void ts_stack_clear(Stack *self) {
|
||||
for (int i = 0; i < self->head_count; i++)
|
||||
stack_node_release(self->heads[i]);
|
||||
self->head_count = 1;
|
||||
self->heads[0] = NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,45 +36,45 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char)
|
|||
return result;
|
||||
}
|
||||
|
||||
static void ts_tree__set_children(TSTree *this, TSTree **children,
|
||||
static void ts_tree__set_children(TSTree *self, TSTree **children,
|
||||
size_t child_count) {
|
||||
this->children = children;
|
||||
this->child_count = child_count;
|
||||
self->children = children;
|
||||
self->child_count = child_count;
|
||||
for (size_t i = 0; i < child_count; i++) {
|
||||
TSTree *child = children[i];
|
||||
ts_tree_retain(child);
|
||||
child->context.parent = this;
|
||||
child->context.parent = self;
|
||||
child->context.index = i;
|
||||
child->context.offset = ts_tree_total_size(this);
|
||||
child->context.offset = ts_tree_total_size(self);
|
||||
|
||||
if (i == 0) {
|
||||
this->padding = child->padding;
|
||||
this->size = child->size;
|
||||
self->padding = child->padding;
|
||||
self->size = child->size;
|
||||
} else {
|
||||
this->size =
|
||||
ts_length_add(ts_length_add(this->size, child->padding), child->size);
|
||||
self->size =
|
||||
ts_length_add(ts_length_add(self->size, child->padding), child->size);
|
||||
}
|
||||
|
||||
switch (child->options.type) {
|
||||
case TSNodeTypeNamed:
|
||||
this->visible_child_count++;
|
||||
this->named_child_count++;
|
||||
self->visible_child_count++;
|
||||
self->named_child_count++;
|
||||
break;
|
||||
case TSNodeTypeAnonymous:
|
||||
this->visible_child_count++;
|
||||
self->visible_child_count++;
|
||||
break;
|
||||
case TSNodeTypeHidden:
|
||||
this->visible_child_count += child->visible_child_count;
|
||||
this->named_child_count += child->named_child_count;
|
||||
self->visible_child_count += child->visible_child_count;
|
||||
self->named_child_count += child->named_child_count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (child_count > 0) {
|
||||
if (children[0]->options.fragile_left)
|
||||
this->options.fragile_left = true;
|
||||
self->options.fragile_left = true;
|
||||
if (children[child_count - 1]->options.fragile_right)
|
||||
this->options.fragile_right = true;
|
||||
self->options.fragile_right = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,47 +86,47 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_retain(TSTree *tree) {
|
||||
assert(tree->ref_count > 0);
|
||||
tree->ref_count++;
|
||||
void ts_tree_retain(TSTree *self) {
|
||||
assert(self->ref_count > 0);
|
||||
self->ref_count++;
|
||||
}
|
||||
|
||||
void ts_tree_release(TSTree *tree) {
|
||||
assert(tree->ref_count > 0);
|
||||
tree->ref_count--;
|
||||
if (tree->ref_count == 0) {
|
||||
for (size_t i = 0; i < tree->child_count; i++)
|
||||
ts_tree_release(tree->children[i]);
|
||||
if (tree->child_count > 0)
|
||||
free(tree->children);
|
||||
free(tree);
|
||||
void ts_tree_release(TSTree *self) {
|
||||
assert(self->ref_count > 0);
|
||||
self->ref_count--;
|
||||
if (self->ref_count == 0) {
|
||||
for (size_t i = 0; i < self->child_count; i++)
|
||||
ts_tree_release(self->children[i]);
|
||||
if (self->child_count > 0)
|
||||
free(self->children);
|
||||
free(self);
|
||||
}
|
||||
}
|
||||
|
||||
TSLength ts_tree_total_size(const TSTree *tree) {
|
||||
return ts_length_add(tree->padding, tree->size);
|
||||
TSLength ts_tree_total_size(const TSTree *self) {
|
||||
return ts_length_add(self->padding, self->size);
|
||||
}
|
||||
|
||||
bool ts_tree_eq(const TSTree *node1, const TSTree *node2) {
|
||||
if (node1) {
|
||||
if (!node2)
|
||||
bool ts_tree_eq(const TSTree *self, const TSTree *other) {
|
||||
if (self) {
|
||||
if (!other)
|
||||
return false;
|
||||
} else {
|
||||
return !node2;
|
||||
return !other;
|
||||
}
|
||||
|
||||
if (node1->symbol != node2->symbol)
|
||||
if (self->symbol != other->symbol)
|
||||
return false;
|
||||
if (node1->symbol == ts_builtin_sym_error)
|
||||
return node1->lookahead_char == node2->lookahead_char;
|
||||
if (node1->child_count != node2->child_count)
|
||||
if (self->symbol == ts_builtin_sym_error)
|
||||
return self->lookahead_char == other->lookahead_char;
|
||||
if (self->child_count != other->child_count)
|
||||
return false;
|
||||
if (node1->visible_child_count != node2->visible_child_count)
|
||||
if (self->visible_child_count != other->visible_child_count)
|
||||
return false;
|
||||
if (node1->named_child_count != node2->named_child_count)
|
||||
if (self->named_child_count != other->named_child_count)
|
||||
return false;
|
||||
for (size_t i = 0; i < node1->child_count; i++)
|
||||
if (!ts_tree_eq(node1->children[i], node2->children[i]))
|
||||
for (size_t i = 0; i < self->child_count; i++)
|
||||
if (!ts_tree_eq(self->children[i], other->children[i]))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
@ -141,30 +141,30 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
|
|||
}
|
||||
}
|
||||
|
||||
static size_t ts_tree__write_to_string(const TSTree *tree,
|
||||
static size_t ts_tree__write_to_string(const TSTree *self,
|
||||
const char **symbol_names, char *string,
|
||||
size_t limit, int is_root) {
|
||||
if (!tree)
|
||||
if (!self)
|
||||
return snprintf(string, limit, "(NULL)");
|
||||
|
||||
char *cursor = string;
|
||||
char **writer = (limit > 0) ? &cursor : &string;
|
||||
int visible = tree->options.type == TSNodeTypeNamed || is_root;
|
||||
int visible = self->options.type == TSNodeTypeNamed || is_root;
|
||||
|
||||
if (visible && !is_root)
|
||||
cursor += snprintf(*writer, limit, " ");
|
||||
|
||||
if (visible) {
|
||||
if (tree->symbol == ts_builtin_sym_error && tree->child_count == 0) {
|
||||
if (self->symbol == ts_builtin_sym_error && self->child_count == 0) {
|
||||
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
|
||||
cursor += write_lookahead_to_string(*writer, limit, tree->lookahead_char);
|
||||
cursor += write_lookahead_to_string(*writer, limit, self->lookahead_char);
|
||||
} else {
|
||||
cursor += snprintf(*writer, limit, "(%s", symbol_names[tree->symbol]);
|
||||
cursor += snprintf(*writer, limit, "(%s", symbol_names[self->symbol]);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
TSTree *child = tree->children[i];
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
TSTree *child = self->children[i];
|
||||
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0);
|
||||
}
|
||||
|
||||
|
|
@ -174,62 +174,62 @@ static size_t ts_tree__write_to_string(const TSTree *tree,
|
|||
return cursor - string;
|
||||
}
|
||||
|
||||
char *ts_tree_string(const TSTree *tree, const char **symbol_names) {
|
||||
char *ts_tree_string(const TSTree *self, const char **symbol_names) {
|
||||
static char SCRATCH[1];
|
||||
size_t size = ts_tree__write_to_string(tree, symbol_names, SCRATCH, 0, 1) + 1;
|
||||
size_t size = ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, 1) + 1;
|
||||
char *result = malloc(size * sizeof(char));
|
||||
ts_tree__write_to_string(tree, symbol_names, result, size, 1);
|
||||
ts_tree__write_to_string(self, symbol_names, result, size, 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_prepend_children(TSTree *tree, size_t count, TSTree **children) {
|
||||
void ts_tree_prepend_children(TSTree *self, size_t count, TSTree **children) {
|
||||
if (count == 0)
|
||||
return;
|
||||
|
||||
size_t new_child_count = count + tree->child_count;
|
||||
size_t new_child_count = count + self->child_count;
|
||||
TSTree **new_children = realloc(children, new_child_count * sizeof(TSTree *));
|
||||
memcpy(new_children + count, tree->children,
|
||||
tree->child_count * sizeof(TSTree *));
|
||||
free(tree->children);
|
||||
memcpy(new_children + count, self->children,
|
||||
self->child_count * sizeof(TSTree *));
|
||||
free(self->children);
|
||||
|
||||
ts_tree__set_children(tree, new_children, new_child_count);
|
||||
ts_tree__set_children(self, new_children, new_child_count);
|
||||
}
|
||||
|
||||
static inline long min(long a, long b) {
|
||||
return a <= b ? a : b;
|
||||
}
|
||||
|
||||
void ts_tree_edit(TSTree *tree, TSInputEdit edit) {
|
||||
void ts_tree_edit(TSTree *self, TSInputEdit edit) {
|
||||
size_t start = edit.position;
|
||||
size_t new_end = edit.position + edit.chars_inserted;
|
||||
size_t old_end = edit.position + edit.chars_removed;
|
||||
assert(old_end <= ts_tree_total_size(tree).chars);
|
||||
assert(old_end <= ts_tree_total_size(self).chars);
|
||||
|
||||
tree->options.has_changes = true;
|
||||
self->options.has_changes = true;
|
||||
|
||||
if (start < tree->padding.chars) {
|
||||
tree->padding.bytes = 0;
|
||||
long remaining_padding = tree->padding.chars - old_end;
|
||||
if (start < self->padding.chars) {
|
||||
self->padding.bytes = 0;
|
||||
long remaining_padding = self->padding.chars - old_end;
|
||||
if (remaining_padding >= 0) {
|
||||
tree->padding.chars = new_end + remaining_padding;
|
||||
self->padding.chars = new_end + remaining_padding;
|
||||
} else {
|
||||
tree->padding.chars = new_end;
|
||||
tree->size.chars += remaining_padding;
|
||||
tree->size.bytes = 0;
|
||||
self->padding.chars = new_end;
|
||||
self->size.chars += remaining_padding;
|
||||
self->size.bytes = 0;
|
||||
}
|
||||
} else if (start == tree->padding.chars && edit.chars_removed == 0) {
|
||||
tree->padding.bytes = 0;
|
||||
tree->padding.chars += edit.chars_inserted;
|
||||
} else if (start == self->padding.chars && edit.chars_removed == 0) {
|
||||
self->padding.bytes = 0;
|
||||
self->padding.chars += edit.chars_inserted;
|
||||
} else {
|
||||
tree->size.bytes = 0;
|
||||
tree->size.chars += (edit.chars_inserted - edit.chars_removed);
|
||||
self->size.bytes = 0;
|
||||
self->size.chars += (edit.chars_inserted - edit.chars_removed);
|
||||
}
|
||||
|
||||
bool found_first_child = false;
|
||||
long remainder_to_delete = edit.chars_removed - edit.chars_inserted;
|
||||
size_t child_left = 0, child_right = 0;
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
TSTree *child = tree->children[i];
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
TSTree *child = self->children[i];
|
||||
size_t child_size = ts_tree_total_size(child).chars;
|
||||
child_left = child_right;
|
||||
child_right += child_size;
|
||||
|
|
|
|||
|
|
@ -20,31 +20,31 @@ static inline TreeVector tree_vector_new(size_t size) {
|
|||
};
|
||||
}
|
||||
|
||||
static inline void tree_vector_push(TreeVector *this, TSTree *tree) {
|
||||
if (this->size == this->capacity) {
|
||||
this->capacity += 4;
|
||||
this->contents = realloc(this->contents, this->capacity * sizeof(TSTree *));
|
||||
static inline void tree_vector_push(TreeVector *self, TSTree *tree) {
|
||||
if (self->size == self->capacity) {
|
||||
self->capacity += 4;
|
||||
self->contents = realloc(self->contents, self->capacity * sizeof(TSTree *));
|
||||
}
|
||||
ts_tree_retain(tree);
|
||||
this->contents[this->size++] = tree;
|
||||
self->contents[self->size++] = tree;
|
||||
}
|
||||
|
||||
static inline void tree_vector_reverse(TreeVector *this) {
|
||||
static inline void tree_vector_reverse(TreeVector *self) {
|
||||
TSTree *swap;
|
||||
size_t limit = this->size / 2;
|
||||
size_t limit = self->size / 2;
|
||||
for (size_t i = 0; i < limit; i++) {
|
||||
swap = this->contents[i];
|
||||
this->contents[i] = this->contents[this->size - 1 - i];
|
||||
this->contents[this->size - 1 - i] = swap;
|
||||
swap = self->contents[i];
|
||||
self->contents[i] = self->contents[self->size - 1 - i];
|
||||
self->contents[self->size - 1 - i] = swap;
|
||||
}
|
||||
}
|
||||
|
||||
static inline TreeVector tree_vector_copy(TreeVector *this) {
|
||||
static inline TreeVector tree_vector_copy(TreeVector *self) {
|
||||
return (TreeVector){
|
||||
.contents = memcpy(malloc(this->capacity * sizeof(TSTree *)),
|
||||
this->contents, this->size * sizeof(TSTree *)),
|
||||
.capacity = this->capacity,
|
||||
.size = this->size,
|
||||
.contents = memcpy(malloc(self->capacity * sizeof(TSTree *)),
|
||||
self->contents, self->size * sizeof(TSTree *)),
|
||||
.capacity = self->capacity,
|
||||
.size = self->size,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue