🎨 naming conventions
This commit is contained in:
parent
3e17172f6e
commit
6933d7b425
6 changed files with 172 additions and 173 deletions
|
|
@ -20,7 +20,7 @@ void ts_document_free(TSDocument *document) {
|
|||
free(document);
|
||||
}
|
||||
|
||||
static void reparse(TSDocument *document, TSInputEdit *edit) {
|
||||
static void ts_document__reparse(TSDocument *document, TSInputEdit *edit) {
|
||||
if (document->input.read_fn && document->parser.language) {
|
||||
TSTree *tree = ts_parser_parse(&document->parser, document->input, edit);
|
||||
if (document->tree)
|
||||
|
|
@ -32,7 +32,7 @@ static void reparse(TSDocument *document, TSInputEdit *edit) {
|
|||
|
||||
void ts_document_set_language(TSDocument *document, const TSLanguage *language) {
|
||||
document->parser.language = language;
|
||||
reparse(document, NULL);
|
||||
ts_document__reparse(document, NULL);
|
||||
}
|
||||
|
||||
TSDebugger ts_document_get_debugger(const TSDocument *document) {
|
||||
|
|
@ -45,11 +45,11 @@ void ts_document_set_debugger(TSDocument *document, TSDebugger debugger) {
|
|||
|
||||
void ts_document_set_input(TSDocument *document, TSInput input) {
|
||||
document->input = input;
|
||||
reparse(document, NULL);
|
||||
ts_document__reparse(document, NULL);
|
||||
}
|
||||
|
||||
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
|
||||
reparse(document, &edit);
|
||||
ts_document__reparse(document, &edit);
|
||||
}
|
||||
|
||||
const char *ts_document_symbol_name(const TSDocument *document,
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
static const char *empty_chunk = "";
|
||||
|
||||
static void read_next_chunk(TSLexer *lexer) {
|
||||
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.data, lexer->current_position);
|
||||
|
|
@ -32,7 +32,7 @@ static void read_next_chunk(TSLexer *lexer) {
|
|||
lexer->chunk = empty_chunk;
|
||||
}
|
||||
|
||||
static void read_lookahead(TSLexer *lexer) {
|
||||
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,
|
||||
|
|
@ -40,17 +40,17 @@ static void read_lookahead(TSLexer *lexer) {
|
|||
DEBUG_LOOKAHEAD();
|
||||
}
|
||||
|
||||
static void start(TSLexer *lexer, TSStateId lex_state) {
|
||||
static void ts_lexer__start(TSLexer *lexer, TSStateId lex_state) {
|
||||
DEBUG("start_lex state:%d", lex_state);
|
||||
DEBUG_LOOKAHEAD();
|
||||
}
|
||||
|
||||
static void start_token(TSLexer *lexer) {
|
||||
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 bool advance(TSLexer *lexer, TSStateId state) {
|
||||
static bool ts_lexer__advance(TSLexer *lexer, TSStateId state) {
|
||||
DEBUG("advance state:%d", state);
|
||||
|
||||
if (lexer->chunk == empty_chunk)
|
||||
|
|
@ -62,14 +62,14 @@ static bool advance(TSLexer *lexer, TSStateId state) {
|
|||
}
|
||||
|
||||
if (lexer->current_position.bytes >= lexer->chunk_start + lexer->chunk_size)
|
||||
read_next_chunk(lexer);
|
||||
ts_lexer__get_chunk(lexer);
|
||||
|
||||
read_lookahead(lexer);
|
||||
ts_lexer__get_lookahead(lexer);
|
||||
return true;
|
||||
}
|
||||
|
||||
static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden,
|
||||
const char *symbol_name) {
|
||||
static TSTree *ts_lexer__accept(TSLexer *lexer, TSSymbol symbol, int is_hidden,
|
||||
const char *symbol_name) {
|
||||
TSLength size =
|
||||
ts_length_sub(lexer->current_position, lexer->token_start_position);
|
||||
TSLength padding =
|
||||
|
|
@ -91,10 +91,10 @@ 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,
|
||||
TSLexer result = (TSLexer){.start_fn = ts_lexer__start,
|
||||
.start_token_fn = ts_lexer__start_token,
|
||||
.advance_fn = ts_lexer__advance,
|
||||
.accept_fn = ts_lexer__accept,
|
||||
.chunk = NULL,
|
||||
.chunk_start = 0,
|
||||
.chunk_size = 0,
|
||||
|
|
@ -110,6 +110,6 @@ TSLexer ts_lexer_make() {
|
|||
void ts_lexer_reset(TSLexer *lexer, TSLength position) {
|
||||
lexer->token_end_position = position;
|
||||
lexer->current_position = position;
|
||||
read_next_chunk(lexer);
|
||||
read_lookahead(lexer);
|
||||
ts_lexer__get_chunk(lexer);
|
||||
ts_lexer__get_lookahead(lexer);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,9 +120,9 @@ static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state,
|
|||
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)
|
||||
|
|
@ -133,8 +133,8 @@ static void ts_parse_stack_add_node_successor(ParseStack *this,
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -148,7 +148,7 @@ static void ts_parse_stack_add_node_successor(ParseStack *this,
|
|||
* Section: Mutating the stack (Private)
|
||||
*/
|
||||
|
||||
static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) {
|
||||
static int ts_parse_stack__add_head(ParseStack *this, ParseStackNode *node) {
|
||||
if (this->head_count == this->head_capacity) {
|
||||
this->head_capacity += 3;
|
||||
this->heads =
|
||||
|
|
@ -160,13 +160,13 @@ 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;
|
||||
}
|
||||
return ts_parse_stack_add_head(this, node);
|
||||
return ts_parse_stack__add_head(this, node);
|
||||
}
|
||||
|
||||
void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
|
||||
|
|
@ -177,8 +177,8 @@ 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) {
|
||||
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) {
|
||||
|
|
@ -186,7 +186,7 @@ static bool ts_parse_stack_merge_head(ParseStack *this, int head_index,
|
|||
head->entry.tree = this->tree_selection_callback.callback(
|
||||
this->tree_selection_callback.data, head->entry.tree, tree);
|
||||
}
|
||||
ts_parse_stack_add_node_successor(this, head, this->heads[head_index]);
|
||||
ts_parse_stack__add_node_successor(this, head, this->heads[head_index]);
|
||||
ts_parse_stack_remove_head(this, head_index);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -201,7 +201,7 @@ static bool ts_parse_stack_merge_head(ParseStack *this, int head_index,
|
|||
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))
|
||||
if (ts_parse_stack__merge_head(this, head_index, state, tree))
|
||||
return true;
|
||||
this->heads[head_index] = stack_node_new(this->heads[head_index], state, tree);
|
||||
return false;
|
||||
|
|
@ -217,7 +217,7 @@ void ts_parse_stack_add_alternative(ParseStack *this, int head_index,
|
|||
|
||||
int ts_parse_stack_split(ParseStack *this, int head_index) {
|
||||
assert(head_index < this->head_count);
|
||||
return ts_parse_stack_add_head(this, this->heads[head_index]);
|
||||
return ts_parse_stack__add_head(this, this->heads[head_index]);
|
||||
}
|
||||
|
||||
ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
|
||||
|
|
@ -288,7 +288,7 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index,
|
|||
this->heads[head_index] = nodes_by_path[path];
|
||||
index = head_index;
|
||||
} else {
|
||||
index = ts_parse_stack_find_or_add_head(this, nodes_by_path[path]);
|
||||
index = ts_parse_stack__find_or_add_head(this, nodes_by_path[path]);
|
||||
}
|
||||
|
||||
this->last_pop_results[path] = (ParseStackPopResult){
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ typedef struct {
|
|||
} TreeSelectionCallback;
|
||||
|
||||
/*
|
||||
* Create a ParseStack.
|
||||
* Create a parse stack.
|
||||
*/
|
||||
ParseStack *ts_parse_stack_new(TreeSelectionCallback);
|
||||
|
||||
|
|
|
|||
|
|
@ -32,19 +32,19 @@ static const TSParseAction ERROR_ACTIONS[2] = {
|
|||
{.type = TSParseActionTypeError }, {.type = 0 }
|
||||
};
|
||||
|
||||
static const TSParseAction *get_actions(const TSLanguage *language,
|
||||
TSStateId state, TSSymbol sym) {
|
||||
static const TSParseAction *ts_language__actions(const TSLanguage *language,
|
||||
TSStateId state, TSSymbol sym) {
|
||||
const TSParseAction *actions =
|
||||
(language->parse_table + (state * language->symbol_count))[sym];
|
||||
return actions ? actions : ERROR_ACTIONS;
|
||||
}
|
||||
|
||||
static TSParseAction get_action(const TSLanguage *language, TSStateId state,
|
||||
TSSymbol sym) {
|
||||
return get_actions(language, state, sym)[0];
|
||||
static TSParseAction ts_language__action(const TSLanguage *language,
|
||||
TSStateId state, TSSymbol sym) {
|
||||
return ts_language__actions(language, state, sym)[0];
|
||||
}
|
||||
|
||||
static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
||||
static TSLength ts_parser__break_down_left(TSParser *parser, TSInputEdit edit) {
|
||||
ts_stack_shrink(&parser->right_stack, 0);
|
||||
|
||||
TSLength prev_size =
|
||||
|
|
@ -78,7 +78,8 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
|||
for (; i < child_count && left_subtree_end.chars < edit.position; i++) {
|
||||
TSTree *child = children[i];
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
TSParseAction action = get_action(parser->language, state, child->symbol);
|
||||
TSParseAction action =
|
||||
ts_language__action(parser->language, state, child->symbol);
|
||||
TSStateId next_state =
|
||||
ts_tree_is_extra(child) ? state : action.data.to_state;
|
||||
|
||||
|
|
@ -106,7 +107,7 @@ static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
|||
return left_subtree_end;
|
||||
}
|
||||
|
||||
static TSTree *break_down_right_stack(TSParser *parser) {
|
||||
static TSTree *ts_parser__break_down_right(TSParser *parser) {
|
||||
TSStack *stack = &parser->right_stack;
|
||||
TSLength current_position = parser->lexer.current_position;
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
|
|
@ -122,7 +123,8 @@ static TSTree *break_down_right_stack(TSParser *parser) {
|
|||
if (right_subtree_start > current_position.chars)
|
||||
return NULL;
|
||||
|
||||
TSParseAction action = get_action(parser->language, state, node->symbol);
|
||||
TSParseAction action =
|
||||
ts_language__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_fragile_left(node) &&
|
||||
|
|
@ -153,10 +155,10 @@ static TSTree *break_down_right_stack(TSParser *parser) {
|
|||
}
|
||||
}
|
||||
|
||||
static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) {
|
||||
static TSTree *ts_parser__next_node(TSParser *parser, TSStateId lex_state) {
|
||||
TSTree *node;
|
||||
|
||||
if ((node = break_down_right_stack(parser))) {
|
||||
if ((node = ts_parser__break_down_right(parser))) {
|
||||
DEBUG("reuse sym:%s, is_extra:%u, size:%lu", SYM_NAME(node->symbol),
|
||||
ts_tree_is_extra(node), ts_tree_total_size(node).chars);
|
||||
|
||||
|
|
@ -179,17 +181,18 @@ static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) {
|
|||
* Parse Actions
|
||||
*/
|
||||
|
||||
static void shift(TSParser *parser, int head, TSStateId parse_state) {
|
||||
static void ts_parser__shift(TSParser *parser, int head, TSStateId parse_state) {
|
||||
ts_parse_stack_push(parser->stack, head, parse_state, parser->lookahead);
|
||||
}
|
||||
|
||||
static void shift_extra(TSParser *parser, int head, TSStateId state) {
|
||||
static void ts_parser__shift_extra(TSParser *parser, int head, TSStateId state) {
|
||||
ts_tree_set_extra(parser->lookahead);
|
||||
shift(parser, head, state);
|
||||
ts_parser__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 *ts_parser__reduce(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);
|
||||
|
|
@ -217,7 +220,8 @@ static TSTree *reduce_helper(TSParser *parser, int head, TSSymbol symbol,
|
|||
ts_tree_set_extra(parent);
|
||||
state = top_state;
|
||||
} else {
|
||||
state = get_action(parser->language, top_state, symbol).data.to_state;
|
||||
state = ts_language__action(parser->language, top_state, symbol)
|
||||
.data.to_state;
|
||||
}
|
||||
|
||||
ts_parse_stack_push(parser->stack, pop_result.index, state, parent);
|
||||
|
|
@ -230,33 +234,25 @@ static TSTree *reduce_helper(TSParser *parser, int head, TSSymbol symbol,
|
|||
return parent;
|
||||
}
|
||||
|
||||
static void reduce(TSParser *parser, int head, TSSymbol symbol,
|
||||
size_t child_count) {
|
||||
reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
}
|
||||
|
||||
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) {
|
||||
static void ts_parser__reduce_fragile(TSParser *parser, int head,
|
||||
TSSymbol symbol, size_t child_count) {
|
||||
TSTree *reduced =
|
||||
reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
ts_parser__reduce(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);
|
||||
static void ts_parser__reduce_error(TSParser *parser, int head,
|
||||
size_t child_count) {
|
||||
TSTree *reduced = ts_parser__reduce(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);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static bool handle_error(TSParser *parser, int head) {
|
||||
static bool ts_parser__handle_error(TSParser *parser, int head) {
|
||||
size_t error_token_count = 1;
|
||||
ParseStackEntry *entry_before_error = ts_parse_stack_head(parser->stack, head);
|
||||
|
||||
|
|
@ -270,18 +266,18 @@ static bool handle_error(TSParser *parser, int head) {
|
|||
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 = ts_language__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(
|
||||
TSParseAction action_after_error = ts_language__action(
|
||||
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);
|
||||
reduce_error(parser, head, error_token_count + i);
|
||||
ts_parser__reduce_error(parser, head, error_token_count + i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -292,8 +288,9 @@ static bool handle_error(TSParser *parser, int head) {
|
|||
* current lookahead token, advance to the next token.
|
||||
*/
|
||||
DEBUG("skip token:%s", SYM_NAME(parser->lookahead->symbol));
|
||||
shift(parser, head, ts_parse_stack_top_state(parser->stack, head));
|
||||
parser->lookahead = get_next_node(parser, ts_lex_state_error);
|
||||
ts_parser__shift(parser, head,
|
||||
ts_parse_stack_top_state(parser->stack, head));
|
||||
parser->lookahead = ts_parser__next_node(parser, ts_lex_state_error);
|
||||
error_token_count++;
|
||||
|
||||
/*
|
||||
|
|
@ -301,14 +298,102 @@ static bool handle_error(TSParser *parser, int head) {
|
|||
*/
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_end) {
|
||||
DEBUG("fail_to_recover");
|
||||
reduce_error(parser, head, error_token_count - 1);
|
||||
ts_parser__reduce_error(parser, head, error_token_count - 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TSTree *finish(TSParser *parser) {
|
||||
return reduce_helper(parser, 0, ts_builtin_sym_document, -1, false, true);
|
||||
static TSTree *ts_parser__finish(TSParser *parser) {
|
||||
return ts_parser__reduce(parser, 0, ts_builtin_sym_document, -1, false, true);
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
ParserNextResultNone,
|
||||
ParserNextResultAdvanced,
|
||||
ParserNextResultRemoved,
|
||||
ParserNextResultFinished
|
||||
} ParserNextResult;
|
||||
|
||||
static 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 =
|
||||
ts_language__actions(parser->language, state, parser->lookahead->symbol);
|
||||
int head, next_head = head_to_advance;
|
||||
|
||||
ParserNextResult result = ParserNextResultNone;
|
||||
|
||||
while (next_action) {
|
||||
TSParseAction action = *next_action;
|
||||
head = next_head;
|
||||
|
||||
next_action++;
|
||||
if (next_action->type == 0) {
|
||||
next_action = NULL;
|
||||
} else {
|
||||
next_head = ts_parse_stack_split(parser->stack, head);
|
||||
DEBUG("split head:%d, created:%d", head, next_head);
|
||||
}
|
||||
|
||||
DEBUG("iteration state:%d, head:%d", state, head);
|
||||
|
||||
// TODO: Remove this by making a separate symbol for errors returned from
|
||||
// the lexer.
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_error)
|
||||
action.type = TSParseActionTypeError;
|
||||
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeError:
|
||||
DEBUG("error_sym");
|
||||
if (ts_parse_stack_head_count(parser->stack) == 1) {
|
||||
if (ts_parser__handle_error(parser, head))
|
||||
break;
|
||||
else
|
||||
return ParserNextResultFinished;
|
||||
} else {
|
||||
DEBUG("bail head:%d", head);
|
||||
ts_parse_stack_remove_head(parser->stack, head);
|
||||
return ParserNextResultRemoved;
|
||||
}
|
||||
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG("shift state:%u", action.data.to_state);
|
||||
ts_parser__shift(parser, head, action.data.to_state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG("shift_extra");
|
||||
ts_parser__shift_extra(parser, head, state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
||||
action.data.child_count);
|
||||
ts_parser__reduce(parser, 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, 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, head, action.data.symbol,
|
||||
action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG("accept");
|
||||
return ParserNextResultFinished;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -347,99 +432,12 @@ void ts_parser_set_debugger(TSParser *parser, TSDebugger debugger) {
|
|||
parser->lexer.debugger = debugger;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
ParserNextResultNone,
|
||||
ParserNextResultAdvanced,
|
||||
ParserNextResultRemoved,
|
||||
ParserNextResultFinished
|
||||
} ParserNextResult;
|
||||
|
||||
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);
|
||||
int head, next_head = head_to_advance;
|
||||
|
||||
ParserNextResult result = ParserNextResultNone;
|
||||
|
||||
while (next_action) {
|
||||
TSParseAction action = *next_action;
|
||||
head = next_head;
|
||||
|
||||
next_action++;
|
||||
if (next_action->type == 0) {
|
||||
next_action = NULL;
|
||||
} else {
|
||||
next_head = ts_parse_stack_split(parser->stack, head);
|
||||
DEBUG("split head:%d, created:%d", head, next_head);
|
||||
}
|
||||
|
||||
DEBUG("iteration state:%d, head:%d", state, head);
|
||||
|
||||
// TODO: Remove this by making a separate symbol for errors returned from
|
||||
// the lexer.
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_error)
|
||||
action.type = TSParseActionTypeError;
|
||||
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeError:
|
||||
DEBUG("error_sym");
|
||||
if (ts_parse_stack_head_count(parser->stack) == 1) {
|
||||
if (handle_error(parser, head))
|
||||
break;
|
||||
else
|
||||
return ParserNextResultFinished;
|
||||
} else {
|
||||
DEBUG("bail head:%d", head);
|
||||
ts_parse_stack_remove_head(parser->stack, head);
|
||||
return ParserNextResultRemoved;
|
||||
}
|
||||
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG("shift state:%u", action.data.to_state);
|
||||
shift(parser, head, action.data.to_state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG("shift_extra");
|
||||
shift_extra(parser, head, state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
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;
|
||||
|
||||
case TSParseActionTypeReduceExtra:
|
||||
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
||||
reduce_extra(parser, head, action.data.symbol);
|
||||
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);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG("accept");
|
||||
return ParserNextResultFinished;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
||||
TSLength position;
|
||||
if (edit) {
|
||||
DEBUG("edit pos:%lu, inserted:%lu, deleted:%lu", edit->position,
|
||||
edit->chars_inserted, edit->chars_removed);
|
||||
position = break_down_left_stack(parser, *edit);
|
||||
position = ts_parser__break_down_left(parser, *edit);
|
||||
} else {
|
||||
DEBUG("new_parse");
|
||||
ts_parse_stack_clear(parser->stack);
|
||||
|
|
@ -452,7 +450,7 @@ TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
|||
for (;;) {
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
parser->lookahead =
|
||||
get_next_node(parser, parser->language->lex_states[state]);
|
||||
ts_parser__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));
|
||||
|
|
@ -462,7 +460,7 @@ TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
|||
bool removed = false, advanced = false;
|
||||
|
||||
while (!(advanced || removed)) {
|
||||
switch (ts_parser_next(parser, head)) {
|
||||
switch (ts_parser__next(parser, head)) {
|
||||
case ParserNextResultNone:
|
||||
break;
|
||||
case ParserNextResultRemoved:
|
||||
|
|
@ -472,7 +470,7 @@ TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
|||
advanced = true;
|
||||
break;
|
||||
case ParserNextResultFinished:
|
||||
return finish(parser);
|
||||
return ts_parser__finish(parser);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -143,8 +143,9 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
|
|||
}
|
||||
}
|
||||
|
||||
static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names,
|
||||
char *string, size_t limit, int is_root) {
|
||||
static size_t ts_tree__write_to_string(const TSTree *tree,
|
||||
const char **symbol_names, char *string,
|
||||
size_t limit, int is_root) {
|
||||
if (!tree)
|
||||
return snprintf(string, limit, "(NULL)");
|
||||
|
||||
|
|
@ -166,7 +167,7 @@ static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names
|
|||
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
TSTree *child = tree->children[i];
|
||||
cursor += tree_write_to_string(child, symbol_names, *writer, limit, 0);
|
||||
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0);
|
||||
}
|
||||
|
||||
if (visible)
|
||||
|
|
@ -177,8 +178,8 @@ static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names
|
|||
|
||||
char *ts_tree_string(const TSTree *tree, const char **symbol_names) {
|
||||
static char SCRATCH[1];
|
||||
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH, 0, 1) + 1;
|
||||
size_t size = ts_tree__write_to_string(tree, symbol_names, SCRATCH, 0, 1) + 1;
|
||||
char *result = malloc(size * sizeof(char));
|
||||
tree_write_to_string(tree, symbol_names, result, size, 1);
|
||||
ts_tree__write_to_string(tree, symbol_names, result, size, 1);
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue