Remove old error recovery code
This commit is contained in:
parent
501e426d29
commit
e0c24e3be6
15 changed files with 78 additions and 75 deletions
|
|
@ -1,14 +1,16 @@
|
|||
#include "tree_sitter/parser.h"
|
||||
|
||||
const TSParseAction *ts_language_actions(const TSLanguage *language,
|
||||
TSStateId state, TSSymbol sym,
|
||||
TSStateId state, TSSymbol symbol,
|
||||
size_t *count) {
|
||||
if (state == ts_parse_state_error) {
|
||||
state = language->out_of_context_states[sym];
|
||||
size_t action_index = 0;
|
||||
if (symbol != ts_builtin_sym_error) {
|
||||
if (state == ts_parse_state_error)
|
||||
state = language->out_of_context_states[symbol];
|
||||
action_index =
|
||||
(language->parse_table + (state * language->symbol_count))[symbol];
|
||||
}
|
||||
|
||||
unsigned action_index =
|
||||
(language->parse_table + (state * language->symbol_count))[sym];
|
||||
*count = language->parse_actions[action_index].count;
|
||||
const TSParseActionEntry *entry = language->parse_actions + action_index + 1;
|
||||
return (const TSParseAction *)entry;
|
||||
|
|
@ -26,8 +28,23 @@ size_t ts_language_symbol_count(const TSLanguage *language) {
|
|||
return language->symbol_count;
|
||||
}
|
||||
|
||||
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *language, TSSymbol symbol) {
|
||||
if (symbol == ts_builtin_sym_error)
|
||||
return (TSSymbolMetadata){
|
||||
.visible = true,
|
||||
.named = true,
|
||||
.extra = false,
|
||||
.structural = true,
|
||||
};
|
||||
else
|
||||
return language->symbol_metadata[symbol];
|
||||
}
|
||||
|
||||
const char *ts_language_symbol_name(const TSLanguage *language, TSSymbol symbol) {
|
||||
return language->symbol_names[symbol];
|
||||
if (symbol == ts_builtin_sym_error)
|
||||
return "ERROR";
|
||||
else
|
||||
return language->symbol_names[symbol];
|
||||
}
|
||||
|
||||
bool ts_language_symbol_is_in_progress(const TSLanguage *self, TSStateId state,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ const TSParseAction *ts_language_actions(const TSLanguage *, TSStateId,
|
|||
TSSymbol, size_t *);
|
||||
TSParseAction ts_language_last_action(const TSLanguage *, TSStateId, TSSymbol);
|
||||
|
||||
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *language, TSSymbol symbol);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -225,7 +225,8 @@ void ts_symbol_iterator_next(TSSymbolIterator *self) {
|
|||
}
|
||||
|
||||
const char *ts_node_name(TSNode self, const TSDocument *document) {
|
||||
return document->parser.language->symbol_names[ts_node__tree(self)->symbol];
|
||||
TSSymbol symbol = ts_node__tree(self)->symbol;
|
||||
return ts_language_symbol_name(document->parser.language, symbol);
|
||||
}
|
||||
|
||||
static size_t write_lookahead_to_string(char *string, size_t limit,
|
||||
|
|
@ -239,7 +240,7 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
|
|||
}
|
||||
|
||||
static size_t ts_tree__write_to_string(const TSTree *self,
|
||||
const char **symbol_names, char *string,
|
||||
const TSLanguage *language, char *string,
|
||||
size_t limit, bool is_root,
|
||||
bool include_anonymous) {
|
||||
if (!self)
|
||||
|
|
@ -258,14 +259,15 @@ static size_t ts_tree__write_to_string(const TSTree *self,
|
|||
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
|
||||
cursor += write_lookahead_to_string(*writer, limit, self->lookahead_char);
|
||||
} else {
|
||||
cursor += snprintf(*writer, limit, "(%s", symbol_names[self->symbol]);
|
||||
cursor += snprintf(*writer, limit, "(%s",
|
||||
ts_language_symbol_name(language, self->symbol));
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
false, include_anonymous);
|
||||
cursor += ts_tree__write_to_string(child, language, *writer, limit, false,
|
||||
include_anonymous);
|
||||
}
|
||||
|
||||
if (visible)
|
||||
|
|
@ -277,11 +279,11 @@ static size_t ts_tree__write_to_string(const TSTree *self,
|
|||
char *ts_node_string(TSNode self, const TSDocument *document) {
|
||||
static char SCRATCH[1];
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
const char **symbol_names = document->parser.language->symbol_names;
|
||||
const TSLanguage *language = document->parser.language;
|
||||
size_t size =
|
||||
ts_tree__write_to_string(tree, symbol_names, SCRATCH, 0, true, false) + 1;
|
||||
ts_tree__write_to_string(tree, language, SCRATCH, 0, true, false) + 1;
|
||||
char *result = ts_malloc(size * sizeof(char));
|
||||
ts_tree__write_to_string(tree, symbol_names, result, size, true, false);
|
||||
ts_tree__write_to_string(tree, language, result, size, true, false);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,14 +30,16 @@
|
|||
fprintf(stderr, "\"\n}\n\n"); \
|
||||
}
|
||||
|
||||
#define LOG_STACK() \
|
||||
if (self->print_debugging_graphs) { \
|
||||
fputs(ts_stack_dot_graph(self->stack, self->language->symbol_names), \
|
||||
stderr); \
|
||||
fputs("\n\n", stderr); \
|
||||
#define LOG_STACK() \
|
||||
if (self->print_debugging_graphs) { \
|
||||
char *graph_string = \
|
||||
ts_stack_dot_graph(self->stack, self->language->symbol_names); \
|
||||
fputs(graph_string, stderr); \
|
||||
fputs("\n\n", stderr); \
|
||||
ts_free(graph_string); \
|
||||
}
|
||||
|
||||
#define SYM_NAME(sym) self->language->symbol_names[sym]
|
||||
#define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol)
|
||||
|
||||
#define BOOL_STRING(value) (value ? "true" : "false")
|
||||
|
||||
|
|
@ -258,9 +260,9 @@ static void ts_parser__remove_head(TSParser *self, int head) {
|
|||
}
|
||||
|
||||
static int ts_parser__select_tree(void *data, TSTree *left, TSTree *right) {
|
||||
if (!left)
|
||||
if (!left || left->symbol == ts_builtin_sym_error)
|
||||
return 1;
|
||||
if (!right)
|
||||
if (!right || right->symbol == ts_builtin_sym_error)
|
||||
return -1;
|
||||
|
||||
TSParser *self = data;
|
||||
|
|
@ -321,8 +323,7 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head,
|
|||
bool extra, bool fragile,
|
||||
bool count_extra) {
|
||||
array_clear(&self->reduce_parents);
|
||||
const TSSymbolMetadata *all_metadata = self->language->symbol_metadata;
|
||||
TSSymbolMetadata metadata = all_metadata[symbol];
|
||||
TSSymbolMetadata metadata = ts_language_symbol_metadata(self->language, symbol);
|
||||
StackPopResultArray pop_results =
|
||||
ts_stack_pop(self->stack, head, child_count, count_extra);
|
||||
if (!pop_results.size)
|
||||
|
|
@ -646,11 +647,6 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head,
|
|||
LookaheadState *lookahead_state =
|
||||
array_get(&self->lookahead_states, current_head);
|
||||
|
||||
// TODO: Remove this by making a separate symbol for errors returned from
|
||||
// the lexer.
|
||||
if (lookahead->symbol == ts_builtin_sym_error)
|
||||
action.type = TSParseActionTypeError;
|
||||
|
||||
LOG_STACK();
|
||||
|
||||
switch (action.type) {
|
||||
|
|
|
|||
|
|
@ -513,22 +513,30 @@ size_t ts_stack__write_dot_graph(Stack *self, char *string, size_t n,
|
|||
continue;
|
||||
all_paths_done = false;
|
||||
|
||||
cursor +=
|
||||
snprintf(*s, n, "node_%p [label=%d];\n", node, node->entry.state);
|
||||
cursor += snprintf(*s, n, "node_%p [label=", node);
|
||||
if (node->entry.state == ts_parse_state_error)
|
||||
cursor += snprintf(*s, n, "\"?\"");
|
||||
else
|
||||
cursor += snprintf(*s, n, "%d", node->entry.state);
|
||||
cursor += snprintf(*s, n, "];\n");
|
||||
|
||||
for (int j = 0; j < node->successor_count; j++) {
|
||||
StackLink successor = node->successors[j];
|
||||
cursor +=
|
||||
snprintf(*s, n, "node_%p -> node_%p [label=\"", node, successor.node);
|
||||
|
||||
const char *name = symbol_names[successor.tree->symbol];
|
||||
for (const char *c = name; *c; c++) {
|
||||
if (*c == '\"' || *c == '\\') {
|
||||
**s = '\\';
|
||||
if (successor.tree->symbol == ts_builtin_sym_error) {
|
||||
cursor += snprintf(*s, n, "ERROR");
|
||||
} else {
|
||||
const char *name = symbol_names[successor.tree->symbol];
|
||||
for (const char *c = name; *c; c++) {
|
||||
if (*c == '\"' || *c == '\\') {
|
||||
**s = '\\';
|
||||
cursor++;
|
||||
}
|
||||
**s = *c;
|
||||
cursor++;
|
||||
}
|
||||
**s = *c;
|
||||
cursor++;
|
||||
}
|
||||
|
||||
cursor += snprintf(*s, n, "\"];\n");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue