2015-09-19 13:19:49 -07:00
|
|
|
#include "runtime/parser.h"
|
2015-11-20 12:55:01 -08:00
|
|
|
#include <assert.h>
|
2014-07-10 13:14:52 -07:00
|
|
|
#include <stdio.h>
|
2016-06-02 14:04:48 -07:00
|
|
|
#include <limits.h>
|
2014-10-09 14:02:03 -07:00
|
|
|
#include <stdbool.h>
|
2014-07-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
|
|
|
|
#include "tree_sitter/parser.h"
|
|
|
|
|
#include "runtime/tree.h"
|
2014-07-30 23:40:02 -07:00
|
|
|
#include "runtime/lexer.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2016-02-17 20:41:29 -08:00
|
|
|
#include "runtime/array.h"
|
2015-11-20 12:00:49 -08:00
|
|
|
#include "runtime/language.h"
|
2016-01-15 15:08:42 -08:00
|
|
|
#include "runtime/alloc.h"
|
2016-05-09 14:31:44 -07:00
|
|
|
#include "runtime/reduce_action.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-06-23 11:42:43 -07:00
|
|
|
#define LOG(...) \
|
2015-10-14 21:52:13 -07:00
|
|
|
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); \
|
2016-06-23 11:42:43 -07:00
|
|
|
} \
|
|
|
|
|
if (self->print_debugging_graphs) { \
|
|
|
|
|
fprintf(stderr, "graph {\nlabel=\""); \
|
|
|
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
|
|
|
fprintf(stderr, "\"\n}\n\n"); \
|
2016-02-23 09:45:27 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
#define LOG_STACK() \
|
|
|
|
|
if (self->print_debugging_graphs) { \
|
|
|
|
|
ts_stack_print_dot_graph(self->stack, self->language->symbol_names, \
|
|
|
|
|
stderr); \
|
|
|
|
|
fputs("\n\n", stderr); \
|
2016-02-23 09:45:27 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-22 21:04:35 -07:00
|
|
|
#define LOG_TREE() \
|
|
|
|
|
if (self->print_debugging_graphs) { \
|
|
|
|
|
ts_tree_print_dot_graph(self->finished_tree, self->language, stderr); \
|
2016-06-22 22:03:27 -07:00
|
|
|
fputs("\n", stderr); \
|
2016-06-22 21:04:35 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 09:55:25 -08:00
|
|
|
#define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol)
|
2014-10-13 21:20:08 -07:00
|
|
|
|
2015-12-17 12:48:55 -08:00
|
|
|
#define BOOL_STRING(value) (value ? "true" : "false")
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
#define CHECK(expr) \
|
|
|
|
|
if (!(expr)) { \
|
|
|
|
|
goto error; \
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-14 14:48:31 -07:00
|
|
|
static const unsigned ERROR_COST_THRESHOLD = 3;
|
2016-06-02 14:04:48 -07:00
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
typedef struct {
|
|
|
|
|
TSTree *tree;
|
|
|
|
|
size_t char_index;
|
|
|
|
|
} ReusableNode;
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
typedef struct {
|
|
|
|
|
TSParser *parser;
|
|
|
|
|
TSSymbol lookahead_symbol;
|
2016-05-09 14:31:44 -07:00
|
|
|
TreeArray *trees_above_error;
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t tree_count_above_error;
|
2016-04-15 21:28:00 -07:00
|
|
|
bool found_repair;
|
2016-05-09 14:31:44 -07:00
|
|
|
ReduceAction best_repair;
|
2016-04-15 21:28:00 -07:00
|
|
|
TSStateId best_repair_next_state;
|
|
|
|
|
size_t best_repair_skip_count;
|
2016-03-07 20:06:46 -08:00
|
|
|
} ErrorRepairSession;
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
typedef struct {
|
|
|
|
|
enum {
|
|
|
|
|
ReduceFailed,
|
|
|
|
|
ReduceSucceeded,
|
|
|
|
|
ReduceStoppedAtError,
|
|
|
|
|
} status;
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
StackSlice slice;
|
2016-04-18 11:16:56 -07:00
|
|
|
} Reduction;
|
2016-03-03 10:21:57 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
typedef enum {
|
|
|
|
|
RepairFailed,
|
|
|
|
|
RepairSucceeded,
|
|
|
|
|
RepairNoneFound,
|
|
|
|
|
} RepairResult;
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
typedef enum {
|
|
|
|
|
BreakdownFailed,
|
|
|
|
|
BreakdownPerformed,
|
|
|
|
|
BreakdownAborted,
|
|
|
|
|
} BreakdownResult;
|
|
|
|
|
|
2016-06-22 22:32:38 -07:00
|
|
|
static bool ts_parser__push(TSParser *self, StackVersion version, TSTree *tree,
|
|
|
|
|
TSStateId state) {
|
|
|
|
|
bool result = ts_stack_push(self->stack, version, tree, false, state);
|
|
|
|
|
ts_tree_release(tree);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self,
|
2016-04-04 12:25:57 -07:00
|
|
|
StackVersion version) {
|
2016-03-31 12:03:07 -07:00
|
|
|
bool did_break_down = false;
|
2016-06-22 22:32:38 -07:00
|
|
|
bool pending = false;
|
2015-12-06 21:10:47 -08:00
|
|
|
|
2015-12-02 11:24:13 -08:00
|
|
|
do {
|
2016-04-04 12:25:57 -07:00
|
|
|
StackPopResult pop = ts_stack_pop_pending(self->stack, version);
|
2016-06-22 22:32:38 -07:00
|
|
|
CHECK(pop.status);
|
2016-03-03 11:05:37 -08:00
|
|
|
if (!pop.slices.size)
|
2016-03-31 12:03:07 -07:00
|
|
|
break;
|
2015-12-06 21:10:47 -08:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
did_break_down = true;
|
2016-06-22 22:32:38 -07:00
|
|
|
pending = false;
|
2016-03-03 11:05:37 -08:00
|
|
|
for (size_t i = 0; i < pop.slices.size; i++) {
|
|
|
|
|
StackSlice slice = pop.slices.contents[i];
|
2016-04-04 12:25:57 -07:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, slice.version);
|
2016-06-22 22:32:38 -07:00
|
|
|
TSTree *parent = *array_front(&slice.trees);
|
|
|
|
|
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("breakdown_top_of_stack tree:%s", SYM_NAME(parent->symbol));
|
2016-06-22 22:32:38 -07:00
|
|
|
|
2015-12-06 21:10:47 -08:00
|
|
|
for (size_t j = 0; j < parent->child_count; j++) {
|
2016-06-22 22:32:38 -07:00
|
|
|
TSTree *child = parent->children[j];
|
|
|
|
|
pending = child->child_count > 0;
|
2016-03-31 12:03:07 -07:00
|
|
|
|
2016-06-22 22:32:38 -07:00
|
|
|
if (child->symbol == ts_builtin_sym_error) {
|
2016-03-07 20:06:46 -08:00
|
|
|
state = ts_parse_state_error;
|
2016-06-22 22:32:38 -07:00
|
|
|
} else if (!child->extra) {
|
2016-06-21 22:53:48 -07:00
|
|
|
const TSParseAction *action =
|
2016-06-22 22:32:38 -07:00
|
|
|
ts_language_last_action(self->language, state, child->symbol);
|
|
|
|
|
assert(action && (action->type == TSParseActionTypeShift ||
|
|
|
|
|
action->type == TSParseActionTypeRecover));
|
2016-06-21 22:53:48 -07:00
|
|
|
state = action->to_state;
|
2015-12-06 21:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-22 22:32:38 -07:00
|
|
|
CHECK(ts_stack_push(self->stack, slice.version, child, pending, state));
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2015-12-02 11:24:13 -08:00
|
|
|
|
2016-06-22 22:32:38 -07:00
|
|
|
for (size_t j = 1; j < slice.trees.size; j++) {
|
2016-03-03 10:16:10 -08:00
|
|
|
TSTree *tree = slice.trees.contents[j];
|
2016-06-22 22:32:38 -07:00
|
|
|
CHECK(ts_parser__push(self, slice.version, tree, state));
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2016-01-21 14:07:38 -07:00
|
|
|
|
2016-06-22 22:32:38 -07:00
|
|
|
ts_tree_release(parent);
|
|
|
|
|
array_delete(&slice.trees);
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
2016-06-22 22:32:38 -07:00
|
|
|
} while (pending);
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
return did_break_down ? BreakdownPerformed : BreakdownAborted;
|
2016-01-21 14:07:38 -07:00
|
|
|
|
|
|
|
|
error:
|
2016-03-31 12:03:07 -07:00
|
|
|
return BreakdownFailed;
|
2015-12-02 11:24:13 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-02 20:58:19 -07:00
|
|
|
static void ts_parser__pop_reusable_node(ReusableNode *reusable_node) {
|
|
|
|
|
reusable_node->char_index += ts_tree_total_chars(reusable_node->tree);
|
|
|
|
|
while (reusable_node->tree) {
|
|
|
|
|
TSTree *parent = reusable_node->tree->context.parent;
|
|
|
|
|
size_t next_index = reusable_node->tree->context.index + 1;
|
|
|
|
|
if (parent && parent->child_count > next_index) {
|
|
|
|
|
reusable_node->tree = parent->children[next_index];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reusable_node->tree = parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-06-22 22:03:27 -07:00
|
|
|
static bool ts_parser__breakdown_reusable_node(ReusableNode *reusable_node) {
|
|
|
|
|
if (reusable_node->tree->symbol == ts_builtin_sym_error ||
|
|
|
|
|
reusable_node->tree->child_count == 0) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2016-04-01 21:17:23 -07:00
|
|
|
reusable_node->tree = reusable_node->tree->children[0];
|
2016-06-22 22:03:27 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
2014-10-12 11:47:00 -07:00
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
static bool ts_parser__condense_stack(TSParser *self) {
|
|
|
|
|
bool result = false;
|
|
|
|
|
for (StackVersion i = 0; i < ts_stack_version_count(self->stack); i++) {
|
|
|
|
|
if (ts_stack_is_halted(self->stack, i)) {
|
|
|
|
|
ts_stack_remove_version(self->stack, i);
|
|
|
|
|
i--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool did_merge = false;
|
|
|
|
|
for (size_t j = 0; j < i; j++) {
|
|
|
|
|
|
|
|
|
|
if (ts_stack_merge(self->stack, j, i)) {
|
|
|
|
|
did_merge = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (did_merge) {
|
|
|
|
|
result = true;
|
|
|
|
|
i--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
static bool ts_parser__can_reuse(TSParser *self, StackVersion version,
|
|
|
|
|
TSTree *tree) {
|
2016-06-22 22:03:27 -07:00
|
|
|
if (tree->symbol == ts_builtin_sym_error) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("cant_reuse_error tree:%s", SYM_NAME(tree->symbol));
|
2016-06-22 22:03:27 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tree->has_changes) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("cant_reuse_changed tree:%s", SYM_NAME(tree->symbol));
|
2016-04-02 20:58:19 -07:00
|
|
|
return false;
|
2016-06-22 22:03:27 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, version);
|
|
|
|
|
if (tree->parse_state != state) {
|
|
|
|
|
if (ts_tree_is_fragile(tree)) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_fragile sym:%s", SYM_NAME(tree->symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
TableEntry entry;
|
|
|
|
|
ts_language_table_entry(self->language, state, tree->symbol, &entry);
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
if (!entry.is_reusable) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_ambiguous sym:%s", SYM_NAME(tree->symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-06-21 22:53:48 -07:00
|
|
|
if (entry.action_count == 0) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_unexpected sym:%s", SYM_NAME(tree->symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-06-21 22:53:48 -07:00
|
|
|
TSParseAction action = entry.actions[entry.action_count - 1];
|
2016-06-21 07:28:04 -07:00
|
|
|
if (tree->extra != action.extra) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_extra sym:%s", SYM_NAME(tree->symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSStateId lex_state = ts_language_lex_state(self->language, state);
|
|
|
|
|
if (tree->first_leaf.lex_state != lex_state) {
|
|
|
|
|
if (tree->child_count > 0) {
|
|
|
|
|
TableEntry leaf_entry;
|
|
|
|
|
ts_language_table_entry(self->language, state, tree->first_leaf.symbol,
|
|
|
|
|
&leaf_entry);
|
|
|
|
|
|
|
|
|
|
if (!leaf_entry.is_reusable) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_first_leaf sym:%s, leaf_sym:%s",
|
|
|
|
|
SYM_NAME(tree->symbol), SYM_NAME(tree->first_leaf.symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tree->child_count == 1 && leaf_entry.depends_on_lookahead) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_lookahead_dependent sym:%s, leaf_sym:%s",
|
|
|
|
|
SYM_NAME(tree->symbol), SYM_NAME(tree->first_leaf.symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if (entry.depends_on_lookahead) {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("cant_reuse_lookahead_dependent sym:%s, leaf_sym:%s",
|
|
|
|
|
SYM_NAME(tree->symbol), SYM_NAME(tree->first_leaf.symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
|
|
|
|
return true;
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
static TSTree *ts_parser__lex(TSParser *self, TSStateId parse_state,
|
|
|
|
|
bool error_mode) {
|
|
|
|
|
TSStateId state = error_mode ? 0 : self->language->lex_states[parse_state];
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("lex state:%d", state);
|
2016-06-21 07:28:04 -07:00
|
|
|
|
2016-06-17 21:26:03 -07:00
|
|
|
TSLength position = self->lexer.current_position;
|
|
|
|
|
|
2016-05-20 20:26:03 -07:00
|
|
|
ts_lexer_start(&self->lexer, state);
|
2016-06-17 21:26:03 -07:00
|
|
|
if (!self->language->lex_fn(&self->lexer, state, error_mode)) {
|
|
|
|
|
ts_lexer_reset(&self->lexer, position);
|
|
|
|
|
ts_lexer_start(&self->lexer, state);
|
|
|
|
|
assert(self->language->lex_fn(&self->lexer, 0, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSLexerResult lex_result;
|
2016-05-20 20:26:03 -07:00
|
|
|
ts_lexer_finish(&self->lexer, &lex_result);
|
|
|
|
|
|
|
|
|
|
TSTree *result;
|
|
|
|
|
if (lex_result.symbol == ts_builtin_sym_error) {
|
|
|
|
|
result = ts_tree_make_error(lex_result.size, lex_result.padding,
|
|
|
|
|
lex_result.first_unexpected_character);
|
|
|
|
|
} else {
|
|
|
|
|
result = ts_tree_make_leaf(
|
|
|
|
|
lex_result.symbol, lex_result.padding, lex_result.size,
|
|
|
|
|
ts_language_symbol_metadata(self->language, lex_result.symbol));
|
2016-06-21 07:28:04 -07:00
|
|
|
if (!result)
|
|
|
|
|
return NULL;
|
|
|
|
|
result->parse_state = parse_state;
|
|
|
|
|
result->first_leaf.lex_state = state;
|
2016-05-20 20:26:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static TSTree *ts_parser__get_lookahead(TSParser *self, StackVersion version,
|
|
|
|
|
ReusableNode *reusable_node) {
|
2016-04-04 12:25:57 -07:00
|
|
|
TSLength position = ts_stack_top_position(self->stack, version);
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
while (reusable_node->tree) {
|
|
|
|
|
if (reusable_node->char_index > position.chars) {
|
2015-11-18 08:47:15 -08:00
|
|
|
break;
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
if (reusable_node->char_index < position.chars) {
|
|
|
|
|
LOG("past_reusable sym:%s", SYM_NAME(reusable_node->tree->symbol));
|
2016-04-02 20:58:19 -07:00
|
|
|
ts_parser__pop_reusable_node(reusable_node);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
if (!ts_parser__can_reuse(self, version, reusable_node->tree)) {
|
2016-06-22 22:03:27 -07:00
|
|
|
if (!ts_parser__breakdown_reusable_node(reusable_node)) {
|
|
|
|
|
ts_parser__pop_reusable_node(reusable_node);
|
|
|
|
|
CHECK(ts_parser__breakdown_top_of_stack(self, version));
|
|
|
|
|
}
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
TSTree *result = reusable_node->tree;
|
2015-11-18 08:47:15 -08:00
|
|
|
TSLength size = ts_tree_total_size(result);
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("reuse sym:%s size:%lu extra:%d", SYM_NAME(result->symbol), size.chars,
|
|
|
|
|
result->extra);
|
2016-04-02 20:58:19 -07:00
|
|
|
ts_parser__pop_reusable_node(reusable_node);
|
2016-01-29 17:31:43 -08:00
|
|
|
ts_tree_retain(result);
|
2015-11-18 08:47:15 -08:00
|
|
|
return result;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
2014-10-22 12:54:46 -07:00
|
|
|
|
2015-12-24 10:21:42 -08:00
|
|
|
ts_lexer_reset(&self->lexer, position);
|
2016-04-04 12:25:57 -07:00
|
|
|
TSStateId parse_state = ts_stack_top_state(self->stack, version);
|
2016-05-09 14:31:44 -07:00
|
|
|
bool error_mode = parse_state == ts_parse_state_error;
|
2016-06-21 07:28:04 -07:00
|
|
|
return ts_parser__lex(self, parse_state, error_mode);
|
2016-06-22 22:03:27 -07:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
return NULL;
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2015-10-06 16:22:58 -07:00
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
static bool ts_parser__select_tree(TSParser *self, TSTree *left, TSTree *right) {
|
|
|
|
|
if (!left)
|
|
|
|
|
return true;
|
|
|
|
|
if (!right)
|
|
|
|
|
return false;
|
|
|
|
|
if (right->error_size < left->error_size) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("select_smaller_error symbol:%s, over_symbol:%s",
|
2016-06-23 11:42:43 -07:00
|
|
|
SYM_NAME(right->symbol), SYM_NAME(left->symbol));
|
2016-04-24 00:54:20 -07:00
|
|
|
return true;
|
2015-12-08 12:25:41 -08:00
|
|
|
}
|
2016-04-24 00:54:20 -07:00
|
|
|
if (left->error_size < right->error_size) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("select_smaller_error symbol:%s, over_symbol:%s",
|
2016-06-23 11:42:43 -07:00
|
|
|
SYM_NAME(left->symbol), SYM_NAME(right->symbol));
|
2016-04-24 00:54:20 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-10-09 14:02:03 -07:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
int comparison = ts_tree_compare(left, right);
|
|
|
|
|
switch (comparison) {
|
|
|
|
|
case -1:
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("select_earlier symbol:%s, over_symbol:%s", SYM_NAME(left->symbol),
|
|
|
|
|
SYM_NAME(right->symbol));
|
2016-05-09 14:31:44 -07:00
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("select_earlier symbol:%s, over_symbol:%s", SYM_NAME(right->symbol),
|
|
|
|
|
SYM_NAME(left->symbol));
|
2016-05-09 14:31:44 -07:00
|
|
|
return true;
|
|
|
|
|
default:
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("select_existing symbol:%s, over_symbol:%s", SYM_NAME(left->symbol),
|
|
|
|
|
SYM_NAME(right->symbol));
|
2016-05-09 14:31:44 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2016-04-18 11:16:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static bool ts_parser__shift(TSParser *self, StackVersion version,
|
2016-04-18 11:16:56 -07:00
|
|
|
TSStateId state, TSTree *lookahead, bool extra) {
|
2016-06-12 17:26:15 -07:00
|
|
|
if (extra != lookahead->extra) {
|
2016-04-18 11:16:56 -07:00
|
|
|
TSSymbolMetadata metadata =
|
|
|
|
|
ts_language_symbol_metadata(self->language, lookahead->symbol);
|
|
|
|
|
if (metadata.structural && ts_stack_version_count(self->stack) > 1) {
|
|
|
|
|
CHECK(lookahead = ts_tree_make_copy(lookahead));
|
|
|
|
|
} else {
|
|
|
|
|
ts_tree_retain(lookahead);
|
|
|
|
|
}
|
2016-06-12 17:26:15 -07:00
|
|
|
lookahead->extra = extra;
|
2016-04-18 11:16:56 -07:00
|
|
|
} else {
|
|
|
|
|
ts_tree_retain(lookahead);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 20:58:19 -07:00
|
|
|
bool is_pending = lookahead->child_count > 0;
|
2016-04-18 11:16:56 -07:00
|
|
|
CHECK(ts_stack_push(self->stack, version, lookahead, is_pending, state));
|
|
|
|
|
ts_tree_release(lookahead);
|
2016-04-10 14:12:24 -07:00
|
|
|
return true;
|
2016-04-18 11:16:56 -07:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
ts_tree_release(lookahead);
|
|
|
|
|
return false;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
static bool ts_parser__switch_children(TSParser *self, TSTree *tree,
|
2016-04-25 21:59:40 -07:00
|
|
|
TSTree **children, size_t count) {
|
2016-04-24 00:54:20 -07:00
|
|
|
self->scratch_tree.symbol = tree->symbol;
|
|
|
|
|
self->scratch_tree.child_count = 0;
|
|
|
|
|
ts_tree_set_children(&self->scratch_tree, count, children);
|
|
|
|
|
if (ts_parser__select_tree(self, tree, &self->scratch_tree)) {
|
|
|
|
|
tree->size = self->scratch_tree.size;
|
|
|
|
|
tree->padding = self->scratch_tree.padding;
|
|
|
|
|
tree->error_size = self->scratch_tree.error_size;
|
|
|
|
|
tree->children = self->scratch_tree.children;
|
|
|
|
|
tree->child_count = self->scratch_tree.child_count;
|
|
|
|
|
tree->named_child_count = self->scratch_tree.named_child_count;
|
|
|
|
|
tree->visible_child_count = self->scratch_tree.visible_child_count;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 11:16:56 -07:00
|
|
|
static Reduction ts_parser__reduce(TSParser *self, StackVersion version,
|
|
|
|
|
TSSymbol symbol, unsigned count, bool extra,
|
2016-06-24 10:28:05 -07:00
|
|
|
bool fragile, bool allow_skipping) {
|
2016-04-24 00:55:19 -07:00
|
|
|
size_t initial_version_count = ts_stack_version_count(self->stack);
|
2016-04-18 11:16:56 -07:00
|
|
|
StackPopResult pop = ts_stack_pop_count(self->stack, version, count);
|
2016-04-07 10:40:33 -07:00
|
|
|
switch (pop.status) {
|
|
|
|
|
case StackPopFailed:
|
|
|
|
|
goto error;
|
|
|
|
|
case StackPopStoppedAtError:
|
2016-04-18 11:16:56 -07:00
|
|
|
return (Reduction){ ReduceStoppedAtError, pop.slices.contents[0] };
|
2016-04-07 10:40:33 -07:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 11:16:56 -07:00
|
|
|
const TSLanguage *language = self->language;
|
|
|
|
|
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-03-03 11:05:37 -08:00
|
|
|
for (size_t i = 0; i < pop.slices.size; i++) {
|
|
|
|
|
StackSlice slice = pop.slices.contents[i];
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-04-07 10:40:33 -07:00
|
|
|
size_t child_count = slice.trees.size;
|
|
|
|
|
while (child_count > 0 && slice.trees.contents[child_count - 1]->extra)
|
|
|
|
|
child_count--;
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
TSTree *parent =
|
|
|
|
|
ts_tree_make_node(symbol, child_count, slice.trees.contents, metadata);
|
2015-12-02 07:53:15 -08:00
|
|
|
if (!parent) {
|
2016-03-07 20:06:46 -08:00
|
|
|
ts_tree_array_delete(&slice.trees);
|
2016-02-23 17:35:50 -08:00
|
|
|
goto error;
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2016-02-08 12:08:15 -08:00
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
while (i + 1 < pop.slices.size) {
|
|
|
|
|
StackSlice next_slice = pop.slices.contents[i + 1];
|
|
|
|
|
if (next_slice.version != slice.version)
|
|
|
|
|
break;
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
size_t child_count = next_slice.trees.size;
|
|
|
|
|
while (child_count > 0 && next_slice.trees.contents[child_count - 1]->extra)
|
|
|
|
|
child_count--;
|
|
|
|
|
|
|
|
|
|
if (ts_parser__switch_children(self, parent, next_slice.trees.contents,
|
|
|
|
|
child_count)) {
|
|
|
|
|
ts_tree_array_delete(&slice.trees);
|
|
|
|
|
slice = next_slice;
|
|
|
|
|
} else {
|
|
|
|
|
ts_tree_array_delete(&next_slice.trees);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 11:16:56 -07:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, slice.version);
|
2016-05-30 14:16:55 -07:00
|
|
|
if (fragile || self->is_split || initial_version_count > 1) {
|
2016-03-07 20:06:46 -08:00
|
|
|
parent->fragile_left = true;
|
|
|
|
|
parent->fragile_right = true;
|
|
|
|
|
parent->parse_state = TS_TREE_STATE_ERROR;
|
2016-04-07 10:40:33 -07:00
|
|
|
} else {
|
2016-04-18 11:16:56 -07:00
|
|
|
parent->parse_state = state;
|
2016-03-07 20:06:46 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-04-07 10:40:33 -07:00
|
|
|
TSStateId new_state;
|
2015-11-20 00:01:53 -08:00
|
|
|
if (extra) {
|
2015-12-22 14:20:58 -08:00
|
|
|
parent->extra = true;
|
2016-04-18 11:16:56 -07:00
|
|
|
new_state = state;
|
2015-07-08 17:34:21 -07:00
|
|
|
} else {
|
2016-06-22 14:11:08 -07:00
|
|
|
const TSParseAction *action =
|
|
|
|
|
ts_language_last_action(language, state, symbol);
|
2016-06-21 22:53:48 -07:00
|
|
|
assert(action->type == TSParseActionTypeShift ||
|
|
|
|
|
action->type == TSParseActionTypeRecover);
|
|
|
|
|
new_state = action->to_state;
|
2016-06-24 10:28:05 -07:00
|
|
|
|
|
|
|
|
if (action->type == TSParseActionTypeRecover && allow_skipping) {
|
|
|
|
|
StackVersion other_version =
|
|
|
|
|
ts_stack_duplicate_version(self->stack, slice.version);
|
|
|
|
|
CHECK(other_version != STACK_VERSION_NONE);
|
|
|
|
|
|
|
|
|
|
CHECK(ts_stack_push(self->stack, other_version, parent, false,
|
|
|
|
|
ts_parse_state_error));
|
|
|
|
|
for (size_t j = parent->child_count; j < slice.trees.size; j++) {
|
|
|
|
|
TSTree *tree = slice.trees.contents[j];
|
|
|
|
|
CHECK(ts_stack_push(self->stack, other_version, tree, false,
|
|
|
|
|
ts_parse_state_error));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
CHECK(ts_parser__push(self, slice.version, parent, new_state));
|
2016-04-24 00:54:20 -07:00
|
|
|
for (size_t j = parent->child_count; j < slice.trees.size; j++) {
|
2016-04-10 14:12:24 -07:00
|
|
|
TSTree *tree = slice.trees.contents[j];
|
|
|
|
|
CHECK(ts_parser__push(self, slice.version, tree, new_state));
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2015-05-27 10:53:02 -07:00
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
for (StackVersion i = initial_version_count;
|
|
|
|
|
i < ts_stack_version_count(self->stack); i++) {
|
|
|
|
|
for (StackVersion j = initial_version_count; j < i; j++) {
|
|
|
|
|
if (ts_stack_merge(self->stack, j, i)) {
|
|
|
|
|
i--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-24 00:55:19 -07:00
|
|
|
|
2016-04-18 11:16:56 -07:00
|
|
|
return (Reduction){ ReduceSucceeded, pop.slices.contents[0] };
|
2016-04-10 14:12:24 -07:00
|
|
|
|
2016-01-29 17:31:43 -08:00
|
|
|
error:
|
2016-06-21 07:28:04 -07:00
|
|
|
return (Reduction){ ReduceFailed, {} };
|
2015-06-15 15:24:15 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-12 17:41:25 -07:00
|
|
|
static inline const TSParseAction *ts_parser__reductions_after_sequence(
|
2016-06-10 13:15:45 -07:00
|
|
|
TSParser *self, TSStateId start_state, const TreeArray *trees_below,
|
|
|
|
|
size_t tree_count_below, const TreeArray *trees_above,
|
|
|
|
|
TSSymbol lookahead_symbol, size_t *count) {
|
2016-05-09 14:31:44 -07:00
|
|
|
TSStateId state = start_state;
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t child_count = 0;
|
|
|
|
|
*count = 0;
|
2016-05-09 14:31:44 -07:00
|
|
|
|
2016-06-10 13:15:45 -07:00
|
|
|
for (size_t i = 0; i < tree_count_below; i++) {
|
|
|
|
|
TSTree *tree = trees_below->contents[trees_below->size - 1 - i];
|
2016-06-21 22:53:48 -07:00
|
|
|
const TSParseAction *action =
|
2016-06-10 13:15:45 -07:00
|
|
|
ts_language_last_action(self->language, state, tree->symbol);
|
2016-06-21 22:53:48 -07:00
|
|
|
if (!action || action->type != TSParseActionTypeShift)
|
2016-06-10 13:15:45 -07:00
|
|
|
return NULL;
|
2016-06-21 22:53:48 -07:00
|
|
|
if (action->extra || tree->extra)
|
2016-05-09 14:31:44 -07:00
|
|
|
continue;
|
2016-06-10 13:15:45 -07:00
|
|
|
child_count++;
|
2016-06-21 22:53:48 -07:00
|
|
|
state = action->to_state;
|
2016-06-10 13:15:45 -07:00
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
|
2016-06-10 13:15:45 -07:00
|
|
|
for (size_t i = 0; i < trees_above->size; i++) {
|
|
|
|
|
TSTree *tree = trees_above->contents[i];
|
2016-06-21 22:53:48 -07:00
|
|
|
const TSParseAction *action =
|
2016-06-10 13:15:45 -07:00
|
|
|
ts_language_last_action(self->language, state, tree->symbol);
|
2016-06-21 22:53:48 -07:00
|
|
|
if (!action || action->type != TSParseActionTypeShift)
|
2016-06-10 13:15:45 -07:00
|
|
|
return NULL;
|
2016-06-21 22:53:48 -07:00
|
|
|
if (action->extra || tree->extra)
|
2016-06-10 13:15:45 -07:00
|
|
|
continue;
|
|
|
|
|
child_count++;
|
2016-06-21 22:53:48 -07:00
|
|
|
state = action->to_state;
|
2016-06-10 13:15:45 -07:00
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
|
2016-06-12 17:41:25 -07:00
|
|
|
const TSParseAction *actions =
|
|
|
|
|
ts_language_actions(self->language, state, lookahead_symbol, count);
|
2016-06-10 13:15:45 -07:00
|
|
|
|
2016-06-21 22:53:48 -07:00
|
|
|
if (*count > 0 && actions[*count - 1].type != TSParseActionTypeReduce) {
|
2016-06-10 13:15:45 -07:00
|
|
|
(*count)--;
|
2016-06-21 22:53:48 -07:00
|
|
|
}
|
2016-06-10 13:15:45 -07:00
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
while (*count > 0 && actions[0].child_count < child_count) {
|
2016-06-10 13:15:45 -07:00
|
|
|
actions++;
|
|
|
|
|
(*count)--;
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
while (*count > 0 && actions[*count - 1].child_count > child_count) {
|
2016-06-10 13:15:45 -07:00
|
|
|
(*count)--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actions;
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static StackIterateAction ts_parser__error_repair_callback(
|
|
|
|
|
void *payload, TSStateId state, TreeArray *trees, size_t tree_count,
|
|
|
|
|
bool is_done, bool is_pending) {
|
2016-06-10 13:15:45 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
ErrorRepairSession *session = (ErrorRepairSession *)payload;
|
2016-05-09 14:31:44 -07:00
|
|
|
TSParser *self = session->parser;
|
2016-03-07 20:06:46 -08:00
|
|
|
TSSymbol lookahead_symbol = session->lookahead_symbol;
|
2016-05-09 14:31:44 -07:00
|
|
|
ReduceActionSet *repairs = &self->reduce_actions;
|
|
|
|
|
TreeArray *trees_above_error = session->trees_above_error;
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t tree_count_above_error = session->tree_count_above_error;
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
StackIterateAction result = StackIterateNone;
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2016-06-14 14:48:31 -07:00
|
|
|
size_t last_repair_count = -1;
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t repair_reduction_count = -1;
|
|
|
|
|
const TSParseAction *repair_reductions = NULL;
|
|
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
for (size_t i = 0; i < repairs->size; i++) {
|
|
|
|
|
ReduceAction *repair = &repairs->contents[i];
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t count_needed_below_error = repair->count - tree_count_above_error;
|
|
|
|
|
if (count_needed_below_error > tree_count)
|
|
|
|
|
break;
|
2016-04-15 21:28:00 -07:00
|
|
|
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t skip_count = tree_count - count_needed_below_error;
|
2016-05-09 14:31:44 -07:00
|
|
|
if (session->found_repair && skip_count >= session->best_repair_skip_count) {
|
|
|
|
|
array_erase(repairs, i--);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-04-15 21:28:00 -07:00
|
|
|
|
2016-06-21 22:53:48 -07:00
|
|
|
const TSParseAction *repair_symbol_action =
|
2016-06-10 13:15:45 -07:00
|
|
|
ts_language_last_action(self->language, state, repair->symbol);
|
2016-06-22 14:11:08 -07:00
|
|
|
if (!repair_symbol_action ||
|
|
|
|
|
repair_symbol_action->type != TSParseActionTypeShift)
|
2016-05-09 14:31:44 -07:00
|
|
|
continue;
|
2016-04-15 21:28:00 -07:00
|
|
|
|
2016-06-21 22:53:48 -07:00
|
|
|
TSStateId state_after_repair = repair_symbol_action->to_state;
|
|
|
|
|
if (!ts_language_last_action(self->language, state_after_repair,
|
|
|
|
|
lookahead_symbol))
|
2016-05-09 14:31:44 -07:00
|
|
|
continue;
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-06-10 13:15:45 -07:00
|
|
|
if (count_needed_below_error != last_repair_count) {
|
|
|
|
|
last_repair_count = count_needed_below_error;
|
2016-06-12 17:41:25 -07:00
|
|
|
repair_reductions = ts_parser__reductions_after_sequence(
|
|
|
|
|
self, state, trees, count_needed_below_error, trees_above_error,
|
|
|
|
|
lookahead_symbol, &repair_reduction_count);
|
2016-06-10 13:15:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < repair_reduction_count; j++) {
|
2016-06-21 21:36:33 -07:00
|
|
|
if (repair_reductions[j].symbol == repair->symbol) {
|
2016-06-10 13:15:45 -07:00
|
|
|
result |= StackIteratePop;
|
|
|
|
|
session->found_repair = true;
|
|
|
|
|
session->best_repair = *repair;
|
|
|
|
|
session->best_repair_skip_count = skip_count;
|
|
|
|
|
session->best_repair_next_state = state_after_repair;
|
|
|
|
|
array_erase(repairs, i--);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
if (repairs->size == 0)
|
2016-04-15 21:28:00 -07:00
|
|
|
result |= StackIterateStop;
|
|
|
|
|
|
|
|
|
|
return result;
|
2015-02-21 10:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-14 14:48:31 -07:00
|
|
|
static bool ts_parser__halt_if_better_version_exists(TSParser *self,
|
|
|
|
|
StackVersion version,
|
|
|
|
|
unsigned my_error_depth,
|
|
|
|
|
unsigned my_error_cost) {
|
|
|
|
|
for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) {
|
|
|
|
|
if (i == version || ts_stack_is_halted(self->stack, i))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
unsigned error_cost = ts_stack_error_cost(self->stack, i);
|
|
|
|
|
unsigned error_depth = ts_stack_error_depth(self->stack, i);
|
|
|
|
|
|
|
|
|
|
if ((error_depth > my_error_depth && error_cost >= my_error_cost) ||
|
|
|
|
|
(error_depth == my_error_depth &&
|
|
|
|
|
error_cost >= my_error_cost + ERROR_COST_THRESHOLD)) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("halt_other version:%u", i);
|
2016-06-14 14:48:31 -07:00
|
|
|
ts_stack_halt(self->stack, i);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((my_error_depth > error_depth && my_error_cost >= error_cost) ||
|
|
|
|
|
(my_error_depth == error_depth &&
|
|
|
|
|
my_error_cost >= error_cost + ERROR_COST_THRESHOLD)) {
|
|
|
|
|
ts_stack_halt(self->stack, version);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static RepairResult ts_parser__repair_error(TSParser *self, StackSlice slice,
|
2016-03-07 20:06:46 -08:00
|
|
|
TSTree *lookahead,
|
|
|
|
|
const TSParseAction *actions,
|
|
|
|
|
size_t action_count) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("repair_error");
|
2016-03-07 20:06:46 -08:00
|
|
|
ErrorRepairSession session = {
|
|
|
|
|
.parser = self,
|
2016-04-15 21:28:00 -07:00
|
|
|
.lookahead_symbol = lookahead->symbol,
|
|
|
|
|
.found_repair = false,
|
2016-05-09 14:31:44 -07:00
|
|
|
.trees_above_error = &slice.trees,
|
2016-06-10 13:15:45 -07:00
|
|
|
.tree_count_above_error = ts_tree_array_essential_count(&slice.trees),
|
2016-03-07 20:06:46 -08:00
|
|
|
};
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
array_clear(&self->reduce_actions);
|
2016-06-14 14:48:31 -07:00
|
|
|
for (size_t i = 0; i < action_count; i++) {
|
|
|
|
|
if (actions[i].type == TSParseActionTypeReduce) {
|
2016-06-21 21:36:33 -07:00
|
|
|
TSSymbol symbol = actions[i].symbol;
|
|
|
|
|
size_t child_count = actions[i].child_count;
|
2016-06-14 14:48:31 -07:00
|
|
|
if ((child_count > session.tree_count_above_error) ||
|
|
|
|
|
(child_count == session.tree_count_above_error &&
|
|
|
|
|
!ts_language_symbol_metadata(self->language, symbol).visible))
|
|
|
|
|
CHECK(array_push(
|
|
|
|
|
&self->reduce_actions,
|
|
|
|
|
((ReduceAction){.symbol = symbol, .count = child_count })));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-15 21:28:00 -07:00
|
|
|
|
|
|
|
|
StackPopResult pop = ts_stack_iterate(
|
2016-04-10 14:12:24 -07:00
|
|
|
self->stack, slice.version, ts_parser__error_repair_callback, &session);
|
2016-04-15 21:28:00 -07:00
|
|
|
CHECK(pop.status);
|
2016-04-18 11:16:56 -07:00
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
if (!session.found_repair) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("no_repair_found");
|
2016-04-18 11:16:56 -07:00
|
|
|
ts_stack_remove_version(self->stack, slice.version);
|
2016-04-10 14:12:24 -07:00
|
|
|
ts_tree_array_delete(&slice.trees);
|
2016-03-07 20:06:46 -08:00
|
|
|
return RepairNoneFound;
|
2016-03-03 11:05:37 -08:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
ReduceAction repair = session.best_repair;
|
2016-04-15 21:28:00 -07:00
|
|
|
TSStateId next_state = session.best_repair_next_state;
|
|
|
|
|
size_t skip_count = session.best_repair_skip_count;
|
2016-06-10 13:15:45 -07:00
|
|
|
size_t count_below = repair.count - session.tree_count_above_error;
|
2016-04-15 21:28:00 -07:00
|
|
|
TSSymbol symbol = repair.symbol;
|
|
|
|
|
|
|
|
|
|
StackSlice new_slice = array_pop(&pop.slices);
|
2016-05-29 22:36:47 -07:00
|
|
|
TreeArray children = new_slice.trees;
|
2016-04-15 21:28:00 -07:00
|
|
|
ts_stack_renumber_version(self->stack, new_slice.version, slice.version);
|
|
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
for (size_t i = pop.slices.size - 1; i + 1 > 0; i--) {
|
|
|
|
|
StackSlice other_slice = pop.slices.contents[i];
|
2016-04-15 21:28:00 -07:00
|
|
|
ts_tree_array_delete(&other_slice.trees);
|
2016-04-24 00:54:20 -07:00
|
|
|
if (other_slice.version != pop.slices.contents[i + 1].version)
|
|
|
|
|
ts_stack_remove_version(self->stack, other_slice.version);
|
2016-04-15 21:28:00 -07:00
|
|
|
}
|
2015-11-11 16:56:58 -08:00
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
TreeArray skipped_children = array_new();
|
|
|
|
|
CHECK(array_grow(&skipped_children, skip_count));
|
2016-06-10 13:15:45 -07:00
|
|
|
for (size_t i = count_below; i < children.size; i++)
|
2016-05-29 22:36:47 -07:00
|
|
|
array_push(&skipped_children, children.contents[i]);
|
|
|
|
|
|
|
|
|
|
TSTree *error = ts_tree_make_error_node(&skipped_children);
|
|
|
|
|
CHECK(error);
|
2016-06-10 13:15:45 -07:00
|
|
|
children.size = count_below;
|
2016-05-29 22:36:47 -07:00
|
|
|
array_push(&children, error);
|
2014-09-29 10:33:56 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
for (size_t i = 0; i < slice.trees.size; i++)
|
2016-05-29 22:36:47 -07:00
|
|
|
array_push(&children, slice.trees.contents[i]);
|
2016-04-15 21:28:00 -07:00
|
|
|
array_delete(&slice.trees);
|
2016-04-10 14:12:24 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
TSTree *parent =
|
2016-05-29 22:36:47 -07:00
|
|
|
ts_tree_make_node(symbol, children.size, children.contents,
|
2016-03-07 20:06:46 -08:00
|
|
|
ts_language_symbol_metadata(self->language, symbol));
|
2016-04-10 14:12:24 -07:00
|
|
|
CHECK(parent);
|
2016-04-15 21:28:00 -07:00
|
|
|
CHECK(ts_parser__push(self, slice.version, parent, next_state));
|
2016-05-10 15:24:06 -07:00
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
unsigned my_error_cost = ts_stack_error_cost(self->stack, slice.version);
|
|
|
|
|
unsigned my_error_depth = ts_stack_error_depth(self->stack, slice.version);
|
2016-06-14 14:48:31 -07:00
|
|
|
if (ts_parser__halt_if_better_version_exists(self, slice.version,
|
|
|
|
|
my_error_depth, my_error_cost)) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("no_better_repair_found");
|
2016-06-14 14:48:31 -07:00
|
|
|
ts_stack_halt(self->stack, slice.version);
|
|
|
|
|
return RepairNoneFound;
|
|
|
|
|
} else {
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("repair_found sym:%s, child_count:%lu, skipped:%lu", SYM_NAME(symbol),
|
|
|
|
|
repair.count, parent->error_size);
|
2016-06-14 14:48:31 -07:00
|
|
|
return RepairSucceeded;
|
2016-06-02 14:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
error:
|
2016-05-16 10:49:22 -07:00
|
|
|
ts_tree_array_delete(&slice.trees);
|
2016-03-07 20:06:46 -08:00
|
|
|
return RepairFailed;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static void ts_parser__start(TSParser *self, TSInput input,
|
|
|
|
|
TSTree *previous_tree) {
|
2015-09-18 23:20:06 -07:00
|
|
|
if (previous_tree) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("parse_after_edit");
|
2015-09-13 19:47:45 -07:00
|
|
|
} else {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("new_parse");
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
2015-09-18 23:20:06 -07:00
|
|
|
|
2015-12-03 10:00:39 -08:00
|
|
|
ts_lexer_set_input(&self->lexer, input);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_clear(self->stack);
|
2015-09-18 23:20:06 -07:00
|
|
|
|
2015-12-24 22:04:20 -08:00
|
|
|
self->finished_tree = NULL;
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static bool ts_parser__accept(TSParser *self, StackVersion version) {
|
2016-04-24 00:54:20 -07:00
|
|
|
StackPopResult pop = ts_stack_pop_all(self->stack, version);
|
|
|
|
|
CHECK(pop.status);
|
|
|
|
|
CHECK(pop.slices.size);
|
2016-03-10 11:57:33 -08:00
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
for (size_t i = 0; i < pop.slices.size; i++) {
|
|
|
|
|
StackSlice slice = pop.slices.contents[i];
|
|
|
|
|
TreeArray trees = slice.trees;
|
|
|
|
|
|
2016-06-26 11:57:42 -07:00
|
|
|
TSTree *root = NULL;
|
|
|
|
|
if (trees.size == 1) {
|
|
|
|
|
root = trees.contents[0];
|
|
|
|
|
array_delete(&trees);
|
|
|
|
|
} else {
|
|
|
|
|
for (size_t j = trees.size - 1; j + 1 > 0; j--) {
|
|
|
|
|
TSTree *child = trees.contents[j];
|
|
|
|
|
if (!child->extra) {
|
|
|
|
|
root = ts_tree_make_copy(child);
|
|
|
|
|
root->child_count = 0;
|
|
|
|
|
for (size_t k = 0; k < child->child_count; k++)
|
|
|
|
|
ts_tree_retain(child->children[k]);
|
|
|
|
|
CHECK(array_splice(&trees, j, 1, child->child_count, child->children));
|
|
|
|
|
ts_tree_set_children(root, trees.size, trees.contents);
|
|
|
|
|
ts_tree_release(child);
|
|
|
|
|
break;
|
2016-04-24 00:54:20 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2016-06-26 11:57:42 -07:00
|
|
|
|
|
|
|
|
if (ts_parser__select_tree(self, self->finished_tree, root)) {
|
|
|
|
|
ts_tree_release(self->finished_tree);
|
|
|
|
|
self->finished_tree = root;
|
|
|
|
|
} else {
|
|
|
|
|
ts_tree_release(root);
|
|
|
|
|
}
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
ts_stack_remove_version(self->stack, pop.slices.contents[0].version);
|
2016-06-02 14:04:48 -07:00
|
|
|
ts_stack_halt(self->stack, version);
|
2016-04-24 00:54:20 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
return true;
|
2016-02-04 11:15:46 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-04-10 14:12:24 -07:00
|
|
|
return false;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
static bool ts_parser__handle_error(TSParser *self, StackVersion version,
|
|
|
|
|
TSStateId state, TSTree *lookahead) {
|
2016-05-29 22:36:47 -07:00
|
|
|
size_t previous_version_count = ts_stack_version_count(self->stack);
|
|
|
|
|
|
2016-06-12 17:41:25 -07:00
|
|
|
unsigned my_error_cost = ts_stack_error_cost(self->stack, version);
|
|
|
|
|
unsigned my_error_depth = ts_stack_error_depth(self->stack, version) + 1;
|
|
|
|
|
if (ts_parser__halt_if_better_version_exists(self, version, my_error_depth,
|
|
|
|
|
my_error_cost)) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("bail_on_error");
|
2016-06-12 17:41:25 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("handle_error");
|
2016-06-12 17:41:25 -07:00
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
bool has_shift_action = false;
|
2016-05-09 14:31:44 -07:00
|
|
|
array_clear(&self->reduce_actions);
|
|
|
|
|
for (TSSymbol symbol = 0; symbol < self->language->symbol_count; symbol++) {
|
|
|
|
|
size_t action_count;
|
|
|
|
|
const TSParseAction *actions =
|
|
|
|
|
ts_language_actions(self->language, state, symbol, &action_count);
|
2016-05-29 22:36:47 -07:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
for (size_t i = 0; i < action_count; i++) {
|
|
|
|
|
TSParseAction action = actions[i];
|
2016-05-29 22:36:47 -07:00
|
|
|
if (action.extra)
|
|
|
|
|
continue;
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case TSParseActionTypeShift:
|
|
|
|
|
case TSParseActionTypeRecover:
|
|
|
|
|
has_shift_action = true;
|
|
|
|
|
break;
|
|
|
|
|
case TSParseActionTypeReduce:
|
2016-06-21 21:36:33 -07:00
|
|
|
if (action.child_count > 0)
|
2016-05-29 22:36:47 -07:00
|
|
|
CHECK(ts_reduce_action_set_add(
|
|
|
|
|
&self->reduce_actions,
|
|
|
|
|
(ReduceAction){
|
2016-06-21 21:36:33 -07:00
|
|
|
.symbol = action.symbol, .count = action.child_count,
|
2016-05-29 22:36:47 -07:00
|
|
|
}));
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-03-10 11:57:33 -08:00
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
2016-03-10 11:57:33 -08:00
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
bool did_reduce = false;
|
2016-05-09 14:31:44 -07:00
|
|
|
for (size_t i = 0; i < self->reduce_actions.size; i++) {
|
2016-05-28 21:22:10 -07:00
|
|
|
ReduceAction action = self->reduce_actions.contents[i];
|
2016-05-29 22:36:47 -07:00
|
|
|
Reduction reduction = ts_parser__reduce(self, version, action.symbol,
|
2016-06-24 10:28:05 -07:00
|
|
|
action.count, false, true, false);
|
2016-05-29 22:36:47 -07:00
|
|
|
switch (reduction.status) {
|
|
|
|
|
case ReduceFailed:
|
|
|
|
|
goto error;
|
|
|
|
|
case ReduceStoppedAtError:
|
|
|
|
|
ts_tree_array_delete(&reduction.slice.trees);
|
|
|
|
|
ts_stack_remove_version(self->stack, reduction.slice.version);
|
|
|
|
|
continue;
|
|
|
|
|
default:
|
|
|
|
|
did_reduce = true;
|
|
|
|
|
break;
|
2016-05-28 21:22:10 -07:00
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
if (did_reduce && !has_shift_action)
|
|
|
|
|
ts_stack_renumber_version(self->stack, previous_version_count, version);
|
|
|
|
|
|
|
|
|
|
CHECK(ts_stack_push(self->stack, version, NULL, false, ts_parse_state_error));
|
|
|
|
|
while (ts_stack_version_count(self->stack) > previous_version_count) {
|
|
|
|
|
CHECK(ts_stack_push(self->stack, previous_version_count, NULL, false,
|
|
|
|
|
ts_parse_state_error));
|
|
|
|
|
assert(ts_stack_merge(self->stack, version, previous_version_count));
|
|
|
|
|
}
|
2016-05-20 20:26:03 -07:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool ts_parser__recover(TSParser *self, StackVersion version,
|
|
|
|
|
TSStateId state, TSTree *lookahead) {
|
2016-06-02 14:04:48 -07:00
|
|
|
if (lookahead->symbol == ts_builtin_sym_end) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("recover_eof");
|
2016-06-02 14:04:48 -07:00
|
|
|
TreeArray children = array_new();
|
|
|
|
|
TSTree *parent = ts_tree_make_error_node(&children);
|
2016-06-22 21:05:05 -07:00
|
|
|
CHECK(ts_parser__push(self, version, parent, 1));
|
|
|
|
|
return ts_parser__accept(self, version);
|
2016-06-02 14:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned my_error_cost = ts_stack_error_cost(self->stack, version);
|
|
|
|
|
unsigned my_error_depth = ts_stack_error_depth(self->stack, version);
|
2016-06-12 17:41:25 -07:00
|
|
|
if (ts_parser__halt_if_better_version_exists(self, version, my_error_depth,
|
|
|
|
|
my_error_cost)) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("bail_on_recovery");
|
2016-06-12 17:41:25 -07:00
|
|
|
return true;
|
2016-06-02 14:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("recover state:%u", state);
|
2016-05-09 14:31:44 -07:00
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
StackVersion new_version = ts_stack_duplicate_version(self->stack, version);
|
|
|
|
|
CHECK(new_version != STACK_VERSION_NONE);
|
|
|
|
|
CHECK(ts_parser__shift(
|
|
|
|
|
self, new_version, ts_parse_state_error, lookahead,
|
|
|
|
|
ts_language_symbol_metadata(self->language, lookahead->symbol).extra));
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
CHECK(ts_parser__shift(self, version, state, lookahead, false));
|
|
|
|
|
return true;
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
error:
|
2016-04-10 14:12:24 -07:00
|
|
|
return false;
|
2016-03-07 20:06:46 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
static bool ts_parser__consume_lookahead(TSParser *self, StackVersion version,
|
|
|
|
|
TSTree *lookahead) {
|
2015-10-07 12:58:32 -07:00
|
|
|
for (;;) {
|
2016-04-04 12:25:57 -07:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, version);
|
2016-06-21 22:58:06 -07:00
|
|
|
bool reduction_stopped_at_error = false;
|
2016-04-10 14:12:24 -07:00
|
|
|
StackVersion last_reduction_version = STACK_VERSION_NONE;
|
|
|
|
|
|
2015-12-29 11:20:52 -08:00
|
|
|
size_t action_count;
|
2015-12-29 21:17:31 -08:00
|
|
|
const TSParseAction *actions = ts_language_actions(
|
|
|
|
|
self->language, state, lookahead->symbol, &action_count);
|
2015-10-07 12:58:32 -07:00
|
|
|
|
2016-06-21 21:27:33 -07:00
|
|
|
for (size_t i = 0; i < action_count; i++) {
|
|
|
|
|
TSParseAction action = actions[i];
|
2016-04-10 14:12:24 -07:00
|
|
|
|
2015-10-07 12:58:32 -07:00
|
|
|
switch (action.type) {
|
2016-04-10 14:12:24 -07:00
|
|
|
case TSParseActionTypeShift: {
|
2016-05-09 14:31:44 -07:00
|
|
|
TSStateId next_state;
|
|
|
|
|
if (action.extra) {
|
|
|
|
|
next_state = state;
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("shift_extra");
|
2016-05-09 14:31:44 -07:00
|
|
|
} else {
|
2016-06-21 21:36:33 -07:00
|
|
|
next_state = action.to_state;
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("shift state:%u", next_state);
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CHECK(ts_parser__shift(self, version, next_state, lookahead,
|
|
|
|
|
action.extra));
|
2016-06-02 14:04:48 -07:00
|
|
|
return true;
|
2016-04-10 14:12:24 -07:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
case TSParseActionTypeReduce: {
|
2016-06-21 22:58:06 -07:00
|
|
|
if (reduction_stopped_at_error)
|
2016-06-21 22:53:48 -07:00
|
|
|
continue;
|
|
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
if (action.extra) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("reduce_extra");
|
2016-05-09 14:31:44 -07:00
|
|
|
} else {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("reduce sym:%s, child_count:%u, fragile:%s",
|
2016-06-23 11:42:43 -07:00
|
|
|
SYM_NAME(action.symbol), action.child_count,
|
|
|
|
|
BOOL_STRING(action.fragile));
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:36:33 -07:00
|
|
|
Reduction reduction =
|
|
|
|
|
ts_parser__reduce(self, version, action.symbol, action.child_count,
|
2016-06-24 10:28:05 -07:00
|
|
|
action.extra, action.fragile, true);
|
2016-04-18 11:16:56 -07:00
|
|
|
|
|
|
|
|
switch (reduction.status) {
|
|
|
|
|
case ReduceFailed:
|
|
|
|
|
goto error;
|
|
|
|
|
case ReduceSucceeded:
|
|
|
|
|
last_reduction_version = reduction.slice.version;
|
|
|
|
|
break;
|
|
|
|
|
case ReduceStoppedAtError: {
|
2016-06-21 22:58:06 -07:00
|
|
|
reduction_stopped_at_error = true;
|
2016-04-18 11:16:56 -07:00
|
|
|
switch (ts_parser__repair_error(self, reduction.slice, lookahead,
|
|
|
|
|
actions, action_count)) {
|
|
|
|
|
case RepairFailed:
|
|
|
|
|
goto error;
|
|
|
|
|
case RepairNoneFound:
|
|
|
|
|
break;
|
|
|
|
|
case RepairSucceeded:
|
|
|
|
|
last_reduction_version = reduction.slice.version;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2015-12-17 12:48:55 -08:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
2016-04-10 14:12:24 -07:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
case TSParseActionTypeAccept: {
|
2016-06-22 21:05:05 -07:00
|
|
|
if (ts_stack_error_depth(self->stack, version) > 0)
|
|
|
|
|
continue;
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("accept");
|
2016-04-10 14:12:24 -07:00
|
|
|
CHECK(ts_parser__accept(self, version));
|
2016-06-02 14:04:48 -07:00
|
|
|
return true;
|
2016-04-10 14:12:24 -07:00
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeRecover: {
|
2016-06-21 21:36:33 -07:00
|
|
|
CHECK(ts_parser__recover(self, version, action.to_state, lookahead));
|
2016-06-02 14:04:48 -07:00
|
|
|
return true;
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
}
|
2016-06-21 22:53:48 -07:00
|
|
|
|
|
|
|
|
LOG_STACK();
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2016-04-10 14:12:24 -07:00
|
|
|
|
2016-06-21 21:27:33 -07:00
|
|
|
if (last_reduction_version != STACK_VERSION_NONE) {
|
2016-04-10 14:12:24 -07:00
|
|
|
ts_stack_renumber_version(self->stack, last_reduction_version, version);
|
2016-06-21 21:27:33 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (ts_parser__breakdown_top_of_stack(self, version)) {
|
|
|
|
|
case BreakdownFailed:
|
|
|
|
|
goto error;
|
|
|
|
|
case BreakdownPerformed:
|
|
|
|
|
break;
|
|
|
|
|
case BreakdownAborted:
|
|
|
|
|
CHECK(ts_parser__handle_error(self, version, state, lookahead));
|
|
|
|
|
if (ts_stack_is_halted(self->stack, version))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2016-04-10 14:12:24 -07:00
|
|
|
|
|
|
|
|
error:
|
2016-06-02 14:04:48 -07:00
|
|
|
return false;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-18 10:44:49 -08:00
|
|
|
bool ts_parser_init(TSParser *self) {
|
2016-02-04 11:15:46 -08:00
|
|
|
ts_lexer_init(&self->lexer);
|
2016-01-18 10:44:49 -08:00
|
|
|
self->finished_tree = NULL;
|
2016-02-04 11:15:46 -08:00
|
|
|
self->stack = NULL;
|
2016-05-09 14:31:44 -07:00
|
|
|
array_init(&self->reduce_actions);
|
2016-01-18 10:44:49 -08:00
|
|
|
|
|
|
|
|
self->stack = ts_stack_new();
|
2016-02-04 11:15:46 -08:00
|
|
|
if (!self->stack)
|
|
|
|
|
goto error;
|
2016-01-18 10:44:49 -08:00
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
if (!array_grow(&self->reduce_actions, 4))
|
2016-02-04 11:15:46 -08:00
|
|
|
goto error;
|
2016-01-18 10:44:49 -08:00
|
|
|
|
|
|
|
|
return true;
|
2016-02-04 11:15:46 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (self->stack) {
|
|
|
|
|
ts_stack_delete(self->stack);
|
|
|
|
|
self->stack = NULL;
|
|
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
if (self->reduce_actions.contents)
|
|
|
|
|
array_delete(&self->reduce_actions);
|
2016-02-04 11:15:46 -08:00
|
|
|
return false;
|
2015-08-16 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_parser_destroy(TSParser *self) {
|
2016-02-04 11:15:46 -08:00
|
|
|
if (self->stack)
|
|
|
|
|
ts_stack_delete(self->stack);
|
2016-05-09 14:31:44 -07:00
|
|
|
if (self->reduce_actions.contents)
|
|
|
|
|
array_delete(&self->reduce_actions);
|
2015-08-16 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSDebugger ts_parser_debugger(const TSParser *self) {
|
|
|
|
|
return self->lexer.debugger;
|
2015-08-16 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_parser_set_debugger(TSParser *self, TSDebugger debugger) {
|
|
|
|
|
self->lexer.debugger = debugger;
|
2015-08-16 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-22 14:10:54 -07:00
|
|
|
TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *old_tree) {
|
|
|
|
|
ts_parser__start(self, input, old_tree);
|
|
|
|
|
StackVersion version = 0;
|
|
|
|
|
size_t last_position = 0, position = 0;
|
|
|
|
|
ReusableNode reusable_node, current_reusable_node = { old_tree, 0 };
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
2015-11-20 12:55:01 -08:00
|
|
|
TSTree *lookahead = NULL;
|
2016-06-22 14:10:54 -07:00
|
|
|
size_t lookahead_position = 0;
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-06-22 14:10:54 -07:00
|
|
|
for (version = 0; version < ts_stack_version_count(self->stack); version++) {
|
2016-05-29 22:36:47 -07:00
|
|
|
reusable_node = current_reusable_node;
|
2016-06-22 14:10:54 -07:00
|
|
|
last_position = position;
|
2016-04-01 21:17:23 -07:00
|
|
|
|
2016-06-22 14:10:54 -07:00
|
|
|
while (!ts_stack_is_halted(self->stack, version)) {
|
|
|
|
|
position = ts_stack_top_position(self->stack, version).chars;
|
|
|
|
|
if (position > last_position ||
|
|
|
|
|
(version > 0 && position == last_position))
|
2015-12-26 16:49:23 -08:00
|
|
|
break;
|
2016-02-23 00:09:42 -08:00
|
|
|
|
2016-06-23 11:42:43 -07:00
|
|
|
LOG("process version:%d, version_count:%lu, state:%d, row:%lu, col:%lu",
|
|
|
|
|
version, ts_stack_version_count(self->stack),
|
|
|
|
|
ts_stack_top_state(self->stack, version),
|
|
|
|
|
ts_stack_top_position(self->stack, version).rows + 1,
|
|
|
|
|
ts_stack_top_position(self->stack, version).columns + 1);
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2016-06-22 14:10:54 -07:00
|
|
|
if (!lookahead || (position != lookahead_position) ||
|
2016-04-04 12:25:57 -07:00
|
|
|
!ts_parser__can_reuse(self, version, lookahead)) {
|
2016-01-29 17:31:43 -08:00
|
|
|
ts_tree_release(lookahead);
|
2016-04-10 14:12:24 -07:00
|
|
|
lookahead = ts_parser__get_lookahead(self, version, &reusable_node);
|
2016-06-22 14:10:54 -07:00
|
|
|
lookahead_position = position;
|
|
|
|
|
CHECK(lookahead);
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol),
|
2016-06-23 11:42:43 -07:00
|
|
|
ts_tree_total_chars(lookahead));
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
if (!ts_parser__consume_lookahead(self, version, lookahead)) {
|
|
|
|
|
ts_tree_release(lookahead);
|
|
|
|
|
goto error;
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2016-06-21 22:53:48 -07:00
|
|
|
|
|
|
|
|
LOG_STACK();
|
2015-12-26 16:49:23 -08:00
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
2015-12-08 13:01:33 -08:00
|
|
|
|
2016-05-29 22:36:47 -07:00
|
|
|
current_reusable_node = reusable_node;
|
|
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
if (ts_parser__condense_stack(self)) {
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("condense");
|
2016-05-29 22:36:47 -07:00
|
|
|
LOG_STACK();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-29 17:31:43 -08:00
|
|
|
ts_tree_release(lookahead);
|
|
|
|
|
|
2016-06-22 14:10:54 -07:00
|
|
|
if (version == 0)
|
|
|
|
|
break;
|
2016-06-23 13:41:38 -07:00
|
|
|
else
|
|
|
|
|
self->is_split = (version > 1);
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2016-01-29 17:31:43 -08:00
|
|
|
|
2016-06-22 22:36:11 -07:00
|
|
|
LOG("done");
|
2016-06-22 21:04:35 -07:00
|
|
|
LOG_TREE();
|
2016-06-22 14:10:54 -07:00
|
|
|
ts_stack_clear(self->stack);
|
|
|
|
|
ts_tree_assign_parents(self->finished_tree);
|
|
|
|
|
return self->finished_tree;
|
|
|
|
|
|
2016-01-29 17:31:43 -08:00
|
|
|
error:
|
|
|
|
|
return NULL;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|