clang-format everything
This commit is contained in:
parent
766e3bab2c
commit
f9b057f3a9
44 changed files with 496 additions and 416 deletions
|
|
@ -27,11 +27,11 @@ void ts_document_free(TSDocument *document) {
|
|||
static void reparse(TSDocument *document, TSInputEdit *edit) {
|
||||
if (document->input.read_fn && document->parser.language) {
|
||||
const TSTree *tree =
|
||||
ts_parser_parse(&document->parser, document->input, edit);
|
||||
ts_parser_parse(&document->parser, document->input, edit);
|
||||
if (document->node)
|
||||
ts_node_release(document->node);
|
||||
document->node =
|
||||
ts_node_make_root(tree, document->parser.language->symbol_names);
|
||||
ts_node_make_root(tree, document->parser.language->symbol_names);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@
|
|||
lexer->debug_buffer); \
|
||||
}
|
||||
|
||||
#define DEBUG_LOOKAHEAD() \
|
||||
DEBUG((0 < lexer->lookahead &&lexer->lookahead < 256) \
|
||||
? "lookahead char:'%c'" \
|
||||
: "lookahead char:%d", \
|
||||
#define DEBUG_LOOKAHEAD() \
|
||||
DEBUG((0 < lexer->lookahead && lexer->lookahead < 256) \
|
||||
? "lookahead char:'%c'" \
|
||||
: "lookahead char:%d", \
|
||||
lexer->lookahead);
|
||||
|
||||
static const char *empty_chunk = "";
|
||||
|
|
@ -35,8 +35,8 @@ static void read_next_chunk(TSLexer *lexer) {
|
|||
static void read_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);
|
||||
(const uint8_t *)lexer->chunk + position_in_chunk,
|
||||
lexer->chunk_size - position_in_chunk + 1, &lexer->lookahead);
|
||||
DEBUG_LOOKAHEAD();
|
||||
}
|
||||
|
||||
|
|
@ -71,9 +71,9 @@ static bool advance(TSLexer *lexer, TSStateId state) {
|
|||
static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden,
|
||||
const char *symbol_name) {
|
||||
TSLength size =
|
||||
ts_length_sub(lexer->current_position, lexer->token_start_position);
|
||||
ts_length_sub(lexer->current_position, lexer->token_start_position);
|
||||
TSLength padding =
|
||||
ts_length_sub(lexer->token_start_position, lexer->token_end_position);
|
||||
ts_length_sub(lexer->token_start_position, lexer->token_end_position);
|
||||
lexer->token_end_position = lexer->current_position;
|
||||
|
||||
if (symbol == ts_builtin_sym_error) {
|
||||
|
|
@ -91,19 +91,19 @@ static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden,
|
|||
*/
|
||||
|
||||
TSLexer ts_lexer_make() {
|
||||
TSLexer result = (TSLexer) { .start_fn = start,
|
||||
.start_token_fn = start_token,
|
||||
.advance_fn = advance,
|
||||
.accept_fn = accept,
|
||||
.chunk = NULL,
|
||||
.chunk_start = 0,
|
||||
.chunk_size = 0,
|
||||
.current_position = ts_length_zero(),
|
||||
.token_start_position = ts_length_zero(),
|
||||
.token_end_position = ts_length_zero(),
|
||||
.lookahead = 0,
|
||||
.lookahead_size = 0,
|
||||
.debugger = ts_debugger_null() };
|
||||
TSLexer result = (TSLexer){.start_fn = start,
|
||||
.start_token_fn = start_token,
|
||||
.advance_fn = advance,
|
||||
.accept_fn = accept,
|
||||
.chunk = NULL,
|
||||
.chunk_start = 0,
|
||||
.chunk_size = 0,
|
||||
.current_position = ts_length_zero(),
|
||||
.token_start_position = ts_length_zero(),
|
||||
.token_end_position = ts_length_zero(),
|
||||
.lookahead = 0,
|
||||
.lookahead_size = 0,
|
||||
.debugger = ts_debugger_null() };
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,12 +8,14 @@ TSNode *ts_node_make(const TSTree *tree, TSNode *parent, size_t index,
|
|||
if (parent)
|
||||
ts_node_retain(parent);
|
||||
TSNode *result = malloc(sizeof(TSNode));
|
||||
*result = (TSNode) { .ref_count = 1,
|
||||
.parent = parent,
|
||||
.index = index,
|
||||
.content = tree,
|
||||
.position = position,
|
||||
.names = names, };
|
||||
*result = (TSNode){
|
||||
.ref_count = 1,
|
||||
.parent = parent,
|
||||
.index = index,
|
||||
.content = tree,
|
||||
.position = position,
|
||||
.names = names,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ struct ParseStack {
|
|||
|
||||
ParseStack *ts_parse_stack_new(TreeSelectionCallback tree_selection_callback) {
|
||||
ParseStack *this = malloc(sizeof(ParseStack));
|
||||
*this = (ParseStack) {
|
||||
*this = (ParseStack){
|
||||
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(ParseStackNode *)),
|
||||
.head_count = 1,
|
||||
.head_capacity = INITIAL_HEAD_CAPACITY,
|
||||
|
|
@ -81,13 +81,15 @@ ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *entry, int i)
|
|||
*/
|
||||
|
||||
static void stack_node_retain(ParseStackNode *this) {
|
||||
if (!this) return;
|
||||
if (!this)
|
||||
return;
|
||||
assert(this->ref_count != 0);
|
||||
this->ref_count++;
|
||||
}
|
||||
|
||||
static bool stack_node_release(ParseStackNode *this) {
|
||||
if (!this) return false;
|
||||
if (!this)
|
||||
return false;
|
||||
assert(this->ref_count != 0);
|
||||
this->ref_count--;
|
||||
if (this->ref_count == 0) {
|
||||
|
|
@ -101,23 +103,26 @@ static bool stack_node_release(ParseStackNode *this) {
|
|||
}
|
||||
}
|
||||
|
||||
static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state, TSTree *tree) {
|
||||
static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state,
|
||||
TSTree *tree) {
|
||||
ParseStackNode *this = malloc(sizeof(ParseStackNode));
|
||||
ts_tree_retain(tree);
|
||||
stack_node_retain(next);
|
||||
*this = (ParseStackNode) {
|
||||
*this = (ParseStackNode){
|
||||
.ref_count = 1,
|
||||
.successor_count = 1,
|
||||
.successors = {next, NULL, NULL},
|
||||
.entry = {
|
||||
.state = state,
|
||||
.tree = tree,
|
||||
},
|
||||
.successors = { next, NULL, NULL },
|
||||
.entry =
|
||||
{
|
||||
.state = state, .tree = tree,
|
||||
},
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode *node, ParseStackNode *new_successor) {
|
||||
static void ts_parse_stack_add_node_successor(ParseStack *this,
|
||||
ParseStackNode *node,
|
||||
ParseStackNode *new_successor) {
|
||||
for (int i = 0; i < node->successor_count; i++) {
|
||||
ParseStackNode *successor = node->successors[i];
|
||||
if (successor == new_successor)
|
||||
|
|
@ -125,12 +130,11 @@ static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode *
|
|||
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,
|
||||
new_successor->entry.tree
|
||||
);
|
||||
this->tree_selection_callback.data, successor->entry.tree,
|
||||
new_successor->entry.tree);
|
||||
for (int j = 0; j < new_successor->successor_count; j++)
|
||||
ts_parse_stack_add_node_successor(this, successor, new_successor->successors[j]);
|
||||
ts_parse_stack_add_node_successor(this, successor,
|
||||
new_successor->successors[j]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -147,7 +151,8 @@ static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode *
|
|||
static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) {
|
||||
if (this->head_count == this->head_capacity) {
|
||||
this->head_capacity += 3;
|
||||
this->heads = realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *));
|
||||
this->heads =
|
||||
realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *));
|
||||
}
|
||||
int new_index = this->head_count++;
|
||||
this->heads[new_index] = node;
|
||||
|
|
@ -155,7 +160,8 @@ static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) {
|
|||
return new_index;
|
||||
}
|
||||
|
||||
static int ts_parse_stack_find_or_add_head(ParseStack *this, ParseStackNode *node) {
|
||||
static int ts_parse_stack_find_or_add_head(ParseStack *this,
|
||||
ParseStackNode *node) {
|
||||
for (int i = 0; i < this->head_count; i++)
|
||||
if (this->heads[i] == node) {
|
||||
return i;
|
||||
|
|
@ -171,16 +177,14 @@ void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
|
|||
this->head_count--;
|
||||
}
|
||||
|
||||
static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateId state, TSTree *tree) {
|
||||
for (int i = 0; i < head_index; i++) {
|
||||
static bool ts_parse_stack_merge_head(ParseStack *this, int head_index,
|
||||
TSStateId state, TSTree *tree) {
|
||||
for (int i = 0; i < head_index; i++) {
|
||||
ParseStackNode *head = this->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
|
||||
);
|
||||
this->tree_selection_callback.data, head->entry.tree, tree);
|
||||
}
|
||||
ts_parse_stack_add_node_successor(this, head, this->heads[head_index]);
|
||||
ts_parse_stack_remove_head(this, head_index);
|
||||
|
|
@ -194,7 +198,8 @@ static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateI
|
|||
* Section: Mutating the stack (Public)
|
||||
*/
|
||||
|
||||
bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTree *tree) {
|
||||
bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state,
|
||||
TSTree *tree) {
|
||||
assert(head_index < this->head_count);
|
||||
if (ts_parse_stack_merge_head(this, head_index, state, tree))
|
||||
return true;
|
||||
|
|
@ -202,14 +207,12 @@ bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTr
|
|||
return false;
|
||||
}
|
||||
|
||||
void ts_parse_stack_add_alternative(ParseStack *this, int head_index, TSTree *tree) {
|
||||
void ts_parse_stack_add_alternative(ParseStack *this, int head_index,
|
||||
TSTree *tree) {
|
||||
assert(head_index < this->head_count);
|
||||
ParseStackEntry *entry = &this->heads[head_index]->entry;
|
||||
entry->tree = this->tree_selection_callback.callback(
|
||||
this->tree_selection_callback.data,
|
||||
entry->tree,
|
||||
tree
|
||||
);
|
||||
this->tree_selection_callback.data, entry->tree, tree);
|
||||
}
|
||||
|
||||
int ts_parse_stack_split(ParseStack *this, int head_index) {
|
||||
|
|
@ -217,15 +220,16 @@ int ts_parse_stack_split(ParseStack *this, int head_index) {
|
|||
return ts_parse_stack_add_head(this, this->heads[head_index]);
|
||||
}
|
||||
|
||||
ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int child_count, bool count_extra) {
|
||||
ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
|
||||
int child_count, bool count_extra) {
|
||||
ParseStackNode *previous_head = this->heads[head_index];
|
||||
|
||||
int path_count = 1;
|
||||
int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count;
|
||||
size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = {child_count};
|
||||
ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = {previous_head};
|
||||
TreeVector trees_by_path[MAX_POP_PATH_COUNT] = {tree_vector_new(capacity)};
|
||||
bool is_shared_by_path[MAX_POP_PATH_COUNT] = {false};
|
||||
size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = { child_count };
|
||||
ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head };
|
||||
TreeVector trees_by_path[MAX_POP_PATH_COUNT] = { tree_vector_new(capacity) };
|
||||
bool is_shared_by_path[MAX_POP_PATH_COUNT] = { false };
|
||||
|
||||
/*
|
||||
* Reduce along every possible path in parallel. Stop when the given number
|
||||
|
|
@ -260,7 +264,8 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
for (int i = 0; i < node->successor_count; i++) {
|
||||
int next_path;
|
||||
if (i > 0) {
|
||||
if (path_count == MAX_POP_PATH_COUNT) break;
|
||||
if (path_count == MAX_POP_PATH_COUNT)
|
||||
break;
|
||||
next_path = path_count;
|
||||
tree_counts_by_path[next_path] = tree_counts_by_path[path];
|
||||
trees_by_path[next_path] = trees_by_path[path];
|
||||
|
|
@ -286,7 +291,7 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
index = ts_parse_stack_find_or_add_head(this, nodes_by_path[path]);
|
||||
}
|
||||
|
||||
this->last_pop_results[path] = (ParseStackPopResult) {
|
||||
this->last_pop_results[path] = (ParseStackPopResult){
|
||||
.index = index,
|
||||
.tree_count = trees_by_path[path].size,
|
||||
.trees = trees_by_path[path].contents,
|
||||
|
|
@ -294,9 +299,8 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
}
|
||||
|
||||
stack_node_release(previous_head);
|
||||
return (ParseStackPopResultList) {
|
||||
.size = path_count,
|
||||
.contents = this->last_pop_results,
|
||||
return (ParseStackPopResultList){
|
||||
.size = path_count, .contents = this->last_pop_results,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -304,7 +308,8 @@ void ts_parse_stack_shrink(ParseStack *this, int head_index, int count) {
|
|||
ParseStackNode *head = this->heads[head_index];
|
||||
ParseStackNode *new_head = head;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (new_head->successor_count == 0) break;
|
||||
if (new_head->successor_count == 0)
|
||||
break;
|
||||
new_head = new_head->successors[0];
|
||||
}
|
||||
stack_node_retain(new_head);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
void *data;
|
||||
TSTree * (*callback)(void *data, TSTree *, TSTree *);
|
||||
TSTree *(*callback)(void *data, TSTree *, TSTree *);
|
||||
} TreeSelectionCallback;
|
||||
|
||||
/*
|
||||
|
|
@ -90,7 +90,8 @@ void ts_parse_stack_add_alternative(ParseStack *, int head, TSTree *);
|
|||
* which had previously been merged. It returns a struct that indicates the
|
||||
* index of each revealed head and the trees removed from that head.
|
||||
*/
|
||||
ParseStackPopResultList ts_parse_stack_pop(ParseStack *, int head, int count, bool count_extra);
|
||||
ParseStackPopResultList ts_parse_stack_pop(ParseStack *, int head, int count,
|
||||
bool count_extra);
|
||||
|
||||
/*
|
||||
* Remove the given number of entries from the given head of the stack.
|
||||
|
|
|
|||
|
|
@ -29,13 +29,13 @@
|
|||
*/
|
||||
|
||||
static const TSParseAction ERROR_ACTIONS[2] = {
|
||||
{.type = TSParseActionTypeError},
|
||||
{.type = 0}
|
||||
{.type = TSParseActionTypeError }, {.type = 0 }
|
||||
};
|
||||
|
||||
static const TSParseAction *get_actions(const TSLanguage *language, TSStateId state,
|
||||
TSSymbol sym) {
|
||||
const TSParseAction *actions = (language->parse_table + (state * language->symbol_count))[sym];
|
||||
static const TSParseAction *get_actions(const TSLanguage *language,
|
||||
TSStateId state, TSSymbol sym) {
|
||||
const TSParseAction *actions =
|
||||
(language->parse_table + (state * language->symbol_count))[sym];
|
||||
return actions ? actions : ERROR_ACTIONS;
|
||||
}
|
||||
|
||||
|
|
@ -47,9 +47,10 @@ static TSParseAction get_action(const TSLanguage *language, TSStateId state,
|
|||
static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
||||
ts_stack_shrink(&parser->right_stack, 0);
|
||||
|
||||
TSLength prev_size = ts_tree_total_size(ts_parse_stack_top_tree(parser->stack, 0));
|
||||
TSLength prev_size =
|
||||
ts_tree_total_size(ts_parse_stack_top_tree(parser->stack, 0));
|
||||
parser->total_chars =
|
||||
prev_size.chars + edit.chars_inserted - edit.chars_removed;
|
||||
prev_size.chars + edit.chars_inserted - edit.chars_removed;
|
||||
TSLength left_subtree_end = prev_size;
|
||||
size_t right_subtree_start = parser->total_chars;
|
||||
|
||||
|
|
@ -61,7 +62,8 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
|||
|
||||
size_t child_count;
|
||||
TSTree **children = ts_tree_children(node, &child_count);
|
||||
if (left_subtree_end.chars < edit.position && !children && node->symbol != ts_builtin_sym_error)
|
||||
if (left_subtree_end.chars < edit.position && !children &&
|
||||
node->symbol != ts_builtin_sym_error)
|
||||
break;
|
||||
|
||||
DEBUG("pop_left sym:%s, state:%u", SYM_NAME(node->symbol),
|
||||
|
|
@ -75,12 +77,12 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
|||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
TSParseAction action = get_action(parser->language, state, child->symbol);
|
||||
TSStateId next_state =
|
||||
ts_tree_is_extra(child) ? state : action.data.to_state;
|
||||
ts_tree_is_extra(child) ? state : action.data.to_state;
|
||||
|
||||
DEBUG("push_left sym:%s, state:%u", SYM_NAME(child->symbol), next_state);
|
||||
ts_parse_stack_push(parser->stack, 0, next_state, child);
|
||||
left_subtree_end =
|
||||
ts_length_add(left_subtree_end, ts_tree_total_size(child));
|
||||
ts_length_add(left_subtree_end, ts_tree_total_size(child));
|
||||
}
|
||||
|
||||
for (size_t j = child_count - 1; j + 1 > i; j--) {
|
||||
|
|
@ -107,7 +109,7 @@ static TSTree *break_down_right_stack(TSParser *parser) {
|
|||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
|
||||
size_t right_subtree_start =
|
||||
parser->total_chars - ts_stack_total_tree_size(stack).chars;
|
||||
parser->total_chars - ts_stack_total_tree_size(stack).chars;
|
||||
|
||||
for (;;) {
|
||||
TSTree *node = ts_stack_top_node(stack);
|
||||
|
|
@ -119,8 +121,7 @@ static TSTree *break_down_right_stack(TSParser *parser) {
|
|||
|
||||
TSParseAction action = get_action(parser->language, state, node->symbol);
|
||||
bool is_usable = (action.type != TSParseActionTypeError) &&
|
||||
!ts_tree_is_extra(node) &&
|
||||
!ts_tree_is_empty(node) &&
|
||||
!ts_tree_is_extra(node) && !ts_tree_is_empty(node) &&
|
||||
!ts_tree_is_fragile_left(node) &&
|
||||
!ts_tree_is_fragile_right(node);
|
||||
if (is_usable && right_subtree_start == current_position.chars) {
|
||||
|
|
@ -157,9 +158,9 @@ static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) {
|
|||
ts_tree_is_extra(node), ts_tree_total_size(node).chars);
|
||||
|
||||
parser->lexer.token_start_position =
|
||||
ts_length_add(parser->lexer.current_position, node->padding);
|
||||
ts_length_add(parser->lexer.current_position, node->padding);
|
||||
parser->lexer.token_end_position = parser->lexer.current_position =
|
||||
ts_length_add(parser->lexer.token_start_position, node->size);
|
||||
ts_length_add(parser->lexer.token_start_position, node->size);
|
||||
|
||||
parser->lexer.lookahead = 0;
|
||||
parser->lexer.lookahead_size = 0;
|
||||
|
|
@ -184,9 +185,11 @@ static void shift_extra(TSParser *parser, int head, TSStateId state) {
|
|||
shift(parser, head, state);
|
||||
}
|
||||
|
||||
static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_t child_count, bool extra, bool count_extra) {
|
||||
static TSTree *reduce_helper(TSParser *parser, int head, TSSymbol symbol,
|
||||
size_t child_count, bool extra, bool count_extra) {
|
||||
bool hidden = parser->language->hidden_symbol_flags[symbol];
|
||||
ParseStackPopResultList pop_results = ts_parse_stack_pop(parser->stack, head, child_count, count_extra);
|
||||
ParseStackPopResultList pop_results =
|
||||
ts_parse_stack_pop(parser->stack, head, child_count, count_extra);
|
||||
|
||||
TSTree *parent = NULL;
|
||||
TSTree **last_children = NULL;
|
||||
|
|
@ -196,13 +199,15 @@ static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_
|
|||
ParseStackPopResult pop_result = pop_results.contents[i];
|
||||
|
||||
if (pop_result.trees != last_children) {
|
||||
parent = ts_tree_make_node(symbol, pop_result.tree_count, pop_result.trees, hidden);
|
||||
parent = ts_tree_make_node(symbol, pop_result.tree_count,
|
||||
pop_result.trees, hidden);
|
||||
}
|
||||
|
||||
if (pop_result.index == last_index) {
|
||||
ts_parse_stack_add_alternative(parser->stack, pop_result.index, parent);
|
||||
} else {
|
||||
TSStateId top_state = ts_parse_stack_top_state(parser->stack, pop_result.index);
|
||||
TSStateId top_state =
|
||||
ts_parse_stack_top_state(parser->stack, pop_result.index);
|
||||
TSStateId state;
|
||||
|
||||
if (extra) {
|
||||
|
|
@ -222,7 +227,8 @@ static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_
|
|||
return parent;
|
||||
}
|
||||
|
||||
static void reduce(TSParser *parser, int head, TSSymbol symbol, size_t child_count) {
|
||||
static void reduce(TSParser *parser, int head, TSSymbol symbol,
|
||||
size_t child_count) {
|
||||
reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
}
|
||||
|
||||
|
|
@ -230,14 +236,17 @@ static void reduce_extra(TSParser *parser, int head, TSSymbol symbol) {
|
|||
reduce_helper(parser, head, symbol, 1, true, false);
|
||||
}
|
||||
|
||||
static void reduce_fragile(TSParser *parser, int head, TSSymbol symbol, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
static void reduce_fragile(TSParser *parser, int head, TSSymbol symbol,
|
||||
size_t child_count) {
|
||||
TSTree *reduced =
|
||||
reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static void reduce_error(TSParser *parser, int head, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, head, ts_builtin_sym_error, child_count, false, true);
|
||||
TSTree *reduced =
|
||||
reduce_helper(parser, 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();
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
|
|
@ -255,20 +264,20 @@ static bool handle_error(TSParser *parser, int head) {
|
|||
* expected and the current lookahead token is expected afterwards.
|
||||
*/
|
||||
int i = -1;
|
||||
for (ParseStackEntry *entry = entry_before_error;
|
||||
entry != NULL;
|
||||
for (ParseStackEntry *entry = entry_before_error; entry != NULL;
|
||||
entry = ts_parse_stack_entry_next(entry, head), i++) {
|
||||
TSStateId stack_state = entry->state;
|
||||
TSParseAction action_on_error = get_action(
|
||||
parser->language, stack_state, ts_builtin_sym_error);
|
||||
TSParseAction action_on_error =
|
||||
get_action(parser->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 = get_action(
|
||||
parser->language, state_after_error, parser->lookahead->symbol);
|
||||
parser->language, state_after_error, parser->lookahead->symbol);
|
||||
|
||||
if (action_after_error.type != TSParseActionTypeError) {
|
||||
DEBUG("recover state:%u, count:%lu", state_after_error, error_token_count + i);
|
||||
DEBUG("recover state:%u, count:%lu", state_after_error,
|
||||
error_token_count + i);
|
||||
reduce_error(parser, head, error_token_count + i);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -303,14 +312,15 @@ static TSTree *finish(TSParser *parser) {
|
|||
* Public
|
||||
*/
|
||||
|
||||
TSTree * ts_parser_select_tree(void *data, TSTree *left, TSTree *right) {
|
||||
TSTree *ts_parser_select_tree(void *data, TSTree *left, TSTree *right) {
|
||||
return left;
|
||||
}
|
||||
|
||||
TSParser ts_parser_make() {
|
||||
return (TSParser) { .lexer = ts_lexer_make(),
|
||||
.stack = ts_parse_stack_new((TreeSelectionCallback) {NULL, ts_parser_select_tree}),
|
||||
.right_stack = ts_stack_make() };
|
||||
return (TSParser){.lexer = ts_lexer_make(),
|
||||
.stack = ts_parse_stack_new(
|
||||
(TreeSelectionCallback){ NULL, ts_parser_select_tree }),
|
||||
.right_stack = ts_stack_make() };
|
||||
}
|
||||
|
||||
void ts_parser_destroy(TSParser *parser) {
|
||||
|
|
@ -343,7 +353,8 @@ typedef enum {
|
|||
|
||||
ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) {
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, head_to_advance);
|
||||
const TSParseAction *next_action = get_actions(parser->language, state, parser->lookahead->symbol);
|
||||
const TSParseAction *next_action =
|
||||
get_actions(parser->language, state, parser->lookahead->symbol);
|
||||
int head, next_head = head_to_advance;
|
||||
|
||||
ParserNextResult result = ParserNextResultNone;
|
||||
|
|
@ -394,7 +405,8 @@ ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) {
|
|||
break;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
||||
action.data.child_count);
|
||||
reduce(parser, head, action.data.symbol, action.data.child_count);
|
||||
break;
|
||||
|
||||
|
|
@ -404,8 +416,10 @@ ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) {
|
|||
break;
|
||||
|
||||
case TSParseActionTypeReduceFragile:
|
||||
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
reduce_fragile(parser, head, action.data.symbol, action.data.child_count);
|
||||
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
||||
action.data.child_count);
|
||||
reduce_fragile(parser, head, action.data.symbol,
|
||||
action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
|
|
@ -435,7 +449,8 @@ const TSTree *ts_parser_parse(TSParser *parser, TSInput input,
|
|||
|
||||
for (;;) {
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
parser->lookahead = get_next_node(parser, parser->language->lex_states[state]);
|
||||
parser->lookahead =
|
||||
get_next_node(parser, parser->language->lex_states[state]);
|
||||
|
||||
DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol));
|
||||
DEBUG("head_count: %d", ts_parse_stack_head_count(parser->stack));
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ static size_t INITIAL_SIZE = 100;
|
|||
static TSStateId INITIAL_STATE = 0;
|
||||
|
||||
TSStack ts_stack_make() {
|
||||
return (TSStack) { .size = 0,
|
||||
.capacity = INITIAL_SIZE,
|
||||
.entries = malloc(INITIAL_SIZE * sizeof(TSStackEntry)) };
|
||||
return (TSStack){.size = 0,
|
||||
.capacity = INITIAL_SIZE,
|
||||
.entries = malloc(INITIAL_SIZE * sizeof(TSStackEntry)) };
|
||||
}
|
||||
|
||||
void ts_stack_delete(TSStack *stack) {
|
||||
|
|
@ -33,7 +33,7 @@ void ts_stack_push(TSStack *stack, TSStateId state, TSTree *node) {
|
|||
if (stack->size == stack->capacity) {
|
||||
stack->capacity *= 2;
|
||||
stack->entries =
|
||||
realloc(stack->entries, stack->capacity * sizeof(*stack->entries));
|
||||
realloc(stack->entries, stack->capacity * sizeof(*stack->entries));
|
||||
}
|
||||
stack->entries[stack->size].state = state;
|
||||
stack->entries[stack->size].node = node;
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ TSInput ts_string_input_make(const char *string) {
|
|||
data->string = string;
|
||||
data->position = 0;
|
||||
data->length = strlen(string);
|
||||
return (TSInput) { .data = (void *)data,
|
||||
.read_fn = ts_string_input_read,
|
||||
.seek_fn = ts_string_input_seek,
|
||||
.release_fn = free };
|
||||
return (TSInput){.data = (void *)data,
|
||||
.read_fn = ts_string_input_read,
|
||||
.seek_fn = ts_string_input_seek,
|
||||
.release_fn = free };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,15 @@
|
|||
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
|
||||
bool is_hidden) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
*result = (TSTree) { .ref_count = 1,
|
||||
.symbol = sym,
|
||||
.size = size,
|
||||
.child_count = 0,
|
||||
.children = NULL,
|
||||
.padding = padding,
|
||||
.options = is_hidden ? TSTreeOptionsHidden : 0, };
|
||||
*result = (TSTree){
|
||||
.ref_count = 1,
|
||||
.symbol = sym,
|
||||
.size = size,
|
||||
.child_count = 0,
|
||||
.children = NULL,
|
||||
.padding = padding,
|
||||
.options = is_hidden ? TSTreeOptionsHidden : 0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -78,15 +80,15 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
* performing a second allocation and storing an additional pointer.
|
||||
*/
|
||||
TSTree *result =
|
||||
malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild)));
|
||||
*result = (TSTree) { .ref_count = 1,
|
||||
.symbol = symbol,
|
||||
.children = children,
|
||||
.child_count = child_count,
|
||||
.visible_child_count = visible_child_count,
|
||||
.size = size,
|
||||
.padding = padding,
|
||||
.options = options };
|
||||
malloc(sizeof(TSTree) + (visible_child_count * sizeof(TSTreeChild)));
|
||||
*result = (TSTree){.ref_count = 1,
|
||||
.symbol = symbol,
|
||||
.children = children,
|
||||
.child_count = child_count,
|
||||
.visible_child_count = visible_child_count,
|
||||
.size = size,
|
||||
.padding = padding,
|
||||
.options = options };
|
||||
|
||||
/*
|
||||
* Associate a relative offset with each of the visible child nodes, so that
|
||||
|
|
@ -110,7 +112,7 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
for (size_t j = 0; j < n; j++) {
|
||||
visible_children[vis_i].tree = grandchildren[j].tree;
|
||||
visible_children[vis_i].offset =
|
||||
ts_length_add(offset, grandchildren[j].offset);
|
||||
ts_length_add(offset, grandchildren[j].offset);
|
||||
vis_i++;
|
||||
}
|
||||
}
|
||||
|
|
@ -147,7 +149,8 @@ TSLength ts_tree_total_size(const TSTree *tree) {
|
|||
|
||||
bool ts_tree_eq(const TSTree *node1, const TSTree *node2) {
|
||||
if (node1) {
|
||||
if (!node2) return false;
|
||||
if (!node2)
|
||||
return false;
|
||||
} else {
|
||||
return !node2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,10 +15,8 @@ typedef struct {
|
|||
} TreeVector;
|
||||
|
||||
static inline TreeVector tree_vector_new(size_t size) {
|
||||
return (TreeVector) {
|
||||
.contents = malloc(size * sizeof(TSTree *)),
|
||||
.capacity = size,
|
||||
.size = 0,
|
||||
return (TreeVector){
|
||||
.contents = malloc(size * sizeof(TSTree *)), .capacity = size, .size = 0,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -42,12 +40,9 @@ static inline void tree_vector_reverse(TreeVector *this) {
|
|||
}
|
||||
|
||||
static inline TreeVector tree_vector_copy(TreeVector *this) {
|
||||
return (TreeVector) {
|
||||
.contents = memcpy(
|
||||
malloc(this->capacity * sizeof(TSTree *)),
|
||||
this->contents,
|
||||
this->size * sizeof(TSTree *)
|
||||
),
|
||||
return (TreeVector){
|
||||
.contents = memcpy(malloc(this->capacity * sizeof(TSTree *)),
|
||||
this->contents, this->size * sizeof(TSTree *)),
|
||||
.capacity = this->capacity,
|
||||
.size = this->size,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue