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>
|
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"
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
/*
|
2014-10-14 09:32:11 -07:00
|
|
|
* Debugging
|
2014-10-08 16:49:52 -07:00
|
|
|
*/
|
|
|
|
|
|
2015-11-20 11:49:04 -08: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); \
|
2014-08-27 18:27:53 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-23 09:45:27 -08:00
|
|
|
#define LOG_ACTION(...) \
|
|
|
|
|
LOG(__VA_ARGS__); \
|
2016-02-23 11:16:50 -08:00
|
|
|
if (self->print_debugging_graphs) { \
|
2016-02-23 09:45:27 -08:00
|
|
|
fprintf(stderr, "graph {\nlabel=\""); \
|
|
|
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
|
|
|
fprintf(stderr, "\"\n}\n\n"); \
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 09:55:25 -08:00
|
|
|
#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); \
|
2016-02-23 09:45:27 -08: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-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
|
|
|
struct ErrorRepair {
|
|
|
|
|
TSParseAction action;
|
2016-03-02 21:03:55 -08:00
|
|
|
TSStateId next_state;
|
|
|
|
|
size_t in_progress_state_count;
|
2016-03-07 20:06:46 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
ErrorRepairArray *repairs;
|
|
|
|
|
size_t count_above_error;
|
|
|
|
|
int best_repair_index;
|
|
|
|
|
TSParser *parser;
|
|
|
|
|
TSSymbol lookahead_symbol;
|
|
|
|
|
} ErrorRepairSession;
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
typedef enum {
|
2016-03-03 10:24:12 -08:00
|
|
|
ParseActionFailed,
|
2016-03-03 10:21:57 -08:00
|
|
|
ParseActionUpdated,
|
|
|
|
|
ParseActionRemoved,
|
2016-01-19 18:07:24 -08:00
|
|
|
} ParseActionResult;
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
typedef struct {
|
|
|
|
|
enum {
|
|
|
|
|
ReduceFailed,
|
|
|
|
|
ReduceSucceeded,
|
|
|
|
|
ReduceMerged,
|
|
|
|
|
ReduceStoppedAtError,
|
|
|
|
|
} status;
|
|
|
|
|
|
|
|
|
|
StackSlice partial_slice;
|
2016-03-03 10:21:57 -08:00
|
|
|
} ReduceResult;
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
typedef enum {
|
|
|
|
|
RepairFailed,
|
|
|
|
|
RepairSucceeded,
|
|
|
|
|
RepairMerged,
|
|
|
|
|
RepairNoneFound,
|
|
|
|
|
} RepairResult;
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
typedef enum {
|
|
|
|
|
BreakdownFailed,
|
|
|
|
|
BreakdownPerformed,
|
|
|
|
|
BreakdownAborted,
|
|
|
|
|
} BreakdownResult;
|
|
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
/*
|
|
|
|
|
* Private
|
|
|
|
|
*/
|
2014-10-09 14:02:03 -07:00
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
static StackSlice ts_parser__pop_one(TSParser *self, int head_index, int count) {
|
2016-03-07 20:06:46 -08:00
|
|
|
StackPopResult pop = ts_stack_pop_count(self->stack, head_index, count);
|
2016-03-10 11:42:10 -08:00
|
|
|
if (pop.status != StackPopSucceeded)
|
|
|
|
|
return (StackSlice){
|
2016-03-31 12:03:07 -07:00
|
|
|
.head_index = -1, .trees = array_new(),
|
2016-03-10 11:42:10 -08:00
|
|
|
};
|
2016-03-03 11:05:37 -08:00
|
|
|
assert(pop.slices.size > 0);
|
|
|
|
|
assert(pop.slices.contents[0].head_index == head_index);
|
2016-03-07 20:06:46 -08:00
|
|
|
for (size_t i = pop.slices.size - 1; i > 0; i--) {
|
|
|
|
|
ts_tree_array_delete(&pop.slices.contents[i].trees);
|
2016-03-03 11:05:37 -08:00
|
|
|
ts_stack_remove_head(self->stack, pop.slices.contents[i].head_index);
|
2016-03-02 21:03:55 -08:00
|
|
|
}
|
2016-03-03 11:05:37 -08:00
|
|
|
return pop.slices.contents[0];
|
2016-03-02 21:03:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
static BreakdownResult ts_parser__breakdown_top_of_stack(TSParser *self,
|
|
|
|
|
int head) {
|
2015-12-02 11:24:13 -08:00
|
|
|
TSTree *last_child = NULL;
|
2016-03-31 12:03:07 -07:00
|
|
|
bool did_break_down = false;
|
|
|
|
|
bool is_still_pending = false;
|
2015-12-06 21:10:47 -08:00
|
|
|
|
2015-12-02 11:24:13 -08:00
|
|
|
do {
|
2016-03-31 12:03:07 -07:00
|
|
|
StackPopResult pop = ts_stack_pop_pending(self->stack, head);
|
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;
|
|
|
|
|
is_still_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-03-03 10:16:10 -08:00
|
|
|
TreeArray removed_trees = slice.trees;
|
2016-02-23 17:35:50 -08:00
|
|
|
TSTree *parent = *array_front(&removed_trees);
|
2016-03-03 10:16:10 -08:00
|
|
|
int head_index = slice.head_index;
|
2016-02-23 17:35:50 -08:00
|
|
|
LOG("breakdown_pop sym:%s, size:%lu", SYM_NAME(parent->symbol),
|
|
|
|
|
ts_tree_total_size(parent).chars);
|
2015-12-06 21:10:47 -08:00
|
|
|
|
2016-03-03 10:20:05 -08:00
|
|
|
StackPushResult last_push = StackPushContinued;
|
2015-12-06 21:10:47 -08:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, head_index);
|
|
|
|
|
for (size_t j = 0; j < parent->child_count; j++) {
|
|
|
|
|
last_child = parent->children[j];
|
2016-03-31 12:03:07 -07:00
|
|
|
is_still_pending = last_child->child_count > 0;
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
if (last_child->symbol == ts_builtin_sym_error) {
|
|
|
|
|
state = ts_parse_state_error;
|
|
|
|
|
} else if (!last_child->extra) {
|
2015-12-17 12:48:55 -08:00
|
|
|
TSParseAction action =
|
|
|
|
|
ts_language_last_action(self->language, state, last_child->symbol);
|
2015-12-06 21:10:47 -08:00
|
|
|
assert(action.type == TSParseActionTypeShift);
|
|
|
|
|
state = action.data.to_state;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 12:48:55 -08:00
|
|
|
LOG("breakdown_push sym:%s, size:%lu", SYM_NAME(last_child->symbol),
|
|
|
|
|
ts_tree_total_size(last_child).chars);
|
2016-03-31 12:03:07 -07:00
|
|
|
last_push = ts_stack_push(self->stack, head_index, last_child,
|
|
|
|
|
is_still_pending, state);
|
2016-03-03 10:20:05 -08:00
|
|
|
if (last_push == StackPushFailed)
|
2016-01-21 14:07:38 -07:00
|
|
|
goto error;
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2015-12-02 11:24:13 -08:00
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
for (size_t j = 1, count = slice.trees.size; j < count; j++) {
|
|
|
|
|
TSTree *tree = slice.trees.contents[j];
|
2016-03-31 12:03:07 -07:00
|
|
|
last_push = ts_stack_push(self->stack, head_index, tree, false, state);
|
2016-03-03 10:20:05 -08:00
|
|
|
if (last_push == StackPushFailed)
|
2016-01-21 14:07:38 -07:00
|
|
|
goto error;
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2016-01-21 14:07:38 -07:00
|
|
|
|
|
|
|
|
if (i == 0)
|
2016-03-03 10:20:05 -08:00
|
|
|
assert(last_push != StackPushMerged);
|
2016-01-21 14:07:38 -07:00
|
|
|
else
|
2016-03-03 10:20:05 -08:00
|
|
|
assert(last_push == StackPushMerged);
|
2016-02-02 12:03:11 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
for (size_t j = 0, count = removed_trees.size; j < count; j++)
|
|
|
|
|
ts_tree_release(removed_trees.contents[j]);
|
|
|
|
|
array_delete(&removed_trees);
|
|
|
|
|
}
|
2016-03-31 12:03:07 -07:00
|
|
|
} while (last_child && is_still_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-01 21:17:23 -07:00
|
|
|
static void ts_parser__pop_reusable_subtree(ReusableNode *);
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2015-09-13 19:47:45 -07:00
|
|
|
/*
|
2015-10-14 21:52:13 -07:00
|
|
|
* Replace the parser's reusable_subtree with its first non-fragile descendant.
|
2015-09-13 19:47:45 -07:00
|
|
|
* Return true if a suitable descendant is found, false otherwise.
|
|
|
|
|
*/
|
2016-04-01 21:17:23 -07:00
|
|
|
static void ts_parser__breakdown_reusable_subtree(ReusableNode *reusable_node) {
|
2015-09-13 19:47:45 -07:00
|
|
|
do {
|
2016-04-01 21:17:23 -07:00
|
|
|
if (reusable_node->tree->symbol == ts_builtin_sym_error) {
|
|
|
|
|
ts_parser__pop_reusable_subtree(reusable_node);
|
2015-12-21 16:04:11 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
if (reusable_node->tree->child_count == 0) {
|
|
|
|
|
ts_parser__pop_reusable_subtree(reusable_node);
|
2015-12-21 16:04:11 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
reusable_node->tree = reusable_node->tree->children[0];
|
|
|
|
|
} while (ts_tree_is_fragile(reusable_node->tree));
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
2014-10-12 11:47:00 -07:00
|
|
|
|
2015-09-13 19:47:45 -07:00
|
|
|
/*
|
|
|
|
|
* Replace the parser's reusable_subtree with its largest right neighbor, or
|
|
|
|
|
* NULL if no right neighbor exists.
|
|
|
|
|
*/
|
2016-04-01 21:17:23 -07:00
|
|
|
static void ts_parser__pop_reusable_subtree(ReusableNode *reusable_node) {
|
|
|
|
|
reusable_node->char_index += ts_tree_total_chars(reusable_node->tree);
|
2015-09-13 19:47:45 -07:00
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
while (reusable_node->tree) {
|
|
|
|
|
TSTree *parent = reusable_node->tree->context.parent;
|
|
|
|
|
size_t next_index = reusable_node->tree->context.index + 1;
|
2015-09-13 19:47:45 -07:00
|
|
|
if (parent && parent->child_count > next_index) {
|
2016-04-01 21:17:23 -07:00
|
|
|
reusable_node->tree = parent->children[next_index];
|
2015-09-13 19:47:45 -07:00
|
|
|
return;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
2016-04-01 21:17:23 -07:00
|
|
|
reusable_node->tree = parent;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *subtree) {
|
2015-12-21 16:04:11 -08:00
|
|
|
if (subtree->symbol == ts_builtin_sym_error)
|
|
|
|
|
return false;
|
2015-12-22 13:59:04 -08:00
|
|
|
if (ts_tree_is_fragile(subtree)) {
|
2015-12-22 14:20:58 -08:00
|
|
|
if (subtree->parse_state != ts_stack_top_state(self->stack, head))
|
2015-12-22 13:59:04 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, head);
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2015-12-22 14:37:29 -08:00
|
|
|
if (subtree->lex_state != TS_TREE_STATE_INDEPENDENT) {
|
2015-12-21 16:04:11 -08:00
|
|
|
TSStateId lex_state = self->language->lex_states[state];
|
2015-12-22 14:20:58 -08:00
|
|
|
if (subtree->lex_state != lex_state)
|
2015-12-21 16:04:11 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-29 21:17:31 -08:00
|
|
|
const TSParseAction action =
|
|
|
|
|
ts_language_last_action(self->language, state, subtree->symbol);
|
2015-12-29 11:20:52 -08:00
|
|
|
if (action.type == TSParseActionTypeError || action.can_hide_split)
|
2015-12-21 16:04:11 -08:00
|
|
|
return false;
|
|
|
|
|
|
2015-12-29 11:20:52 -08:00
|
|
|
if (subtree->extra && !action.extra)
|
2015-12-21 16:04:11 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
|
|
|
|
|
2015-09-13 19:47:45 -07:00
|
|
|
/*
|
|
|
|
|
* Advance the parser's lookahead subtree. If there is a reusable subtree
|
|
|
|
|
* at the correct position in the parser's previous tree, use that. Otherwise,
|
|
|
|
|
* run the lexer.
|
|
|
|
|
*/
|
2016-04-01 21:17:23 -07:00
|
|
|
static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head, ReusableNode *reusable_node) {
|
2015-12-02 07:53:15 -08:00
|
|
|
TSLength position = ts_stack_top_position(self->stack, head);
|
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));
|
|
|
|
|
ts_parser__pop_reusable_subtree(reusable_node);
|
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
|
|
|
if (reusable_node->tree->has_changes) {
|
|
|
|
|
if (reusable_node->tree->child_count == 0)
|
2015-12-10 21:06:22 -08:00
|
|
|
ts_parser__breakdown_top_of_stack(self, head);
|
2015-12-21 16:04:11 -08:00
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
LOG("breakdown_changed sym:%s", SYM_NAME(reusable_node->tree->symbol));
|
|
|
|
|
ts_parser__breakdown_reusable_subtree(reusable_node);
|
2015-12-21 16:04:11 -08:00
|
|
|
continue;
|
2015-12-09 14:33:54 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-01 21:17:23 -07:00
|
|
|
if (!ts_parser__can_reuse(self, head, reusable_node->tree)) {
|
2015-12-24 22:05:54 -08:00
|
|
|
LOG("breakdown_unreusable sym:%s",
|
2016-04-01 21:17:23 -07:00
|
|
|
SYM_NAME(reusable_node->tree->symbol));
|
|
|
|
|
ts_parser__breakdown_reusable_subtree(reusable_node);
|
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);
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("reuse sym:%s size:%lu extra:%d", SYM_NAME(result->symbol), size.chars,
|
2015-12-22 14:20:58 -08:00
|
|
|
result->extra);
|
2016-04-01 21:17:23 -07:00
|
|
|
ts_parser__pop_reusable_subtree(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);
|
|
|
|
|
TSStateId parse_state = ts_stack_top_state(self->stack, head);
|
|
|
|
|
TSStateId lex_state = self->language->lex_states[parse_state];
|
|
|
|
|
LOG("lex state:%d", lex_state);
|
2015-12-30 09:37:40 -08:00
|
|
|
return self->language->lex_fn(&self->lexer, lex_state, false);
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2015-10-06 16:22:58 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
static int ts_parser__split(TSParser *self, int head) {
|
2016-04-01 21:17:23 -07:00
|
|
|
return ts_stack_split(self->stack, head);
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2015-10-06 16:22:58 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
static void ts_parser__remove_head(TSParser *self, int head) {
|
|
|
|
|
ts_stack_remove_head(self->stack, head);
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
static int ts_parser__select_tree(void *data, TSTree *left, TSTree *right) {
|
2016-03-02 09:55:25 -08:00
|
|
|
if (!left || left->symbol == ts_builtin_sym_error)
|
2016-02-08 12:08:15 -08:00
|
|
|
return 1;
|
2016-03-02 09:55:25 -08:00
|
|
|
if (!right || right->symbol == ts_builtin_sym_error)
|
2016-02-08 12:08:15 -08:00
|
|
|
return -1;
|
2015-12-24 22:04:20 -08:00
|
|
|
|
2015-12-08 12:25:41 -08:00
|
|
|
TSParser *self = data;
|
|
|
|
|
int comparison = ts_tree_compare(left, right);
|
2016-02-23 17:35:50 -08:00
|
|
|
switch (comparison) {
|
|
|
|
|
case -1:
|
|
|
|
|
LOG_ACTION("select tree:%s, over_tree:%s", SYM_NAME(left->symbol),
|
|
|
|
|
SYM_NAME(right->symbol));
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
LOG_ACTION("select tree:%s, over_tree:%s", SYM_NAME(right->symbol),
|
|
|
|
|
SYM_NAME(left->symbol));
|
|
|
|
|
break;
|
2015-12-08 12:25:41 -08:00
|
|
|
}
|
2016-02-23 17:35:50 -08:00
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
return comparison;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Actions
|
|
|
|
|
*/
|
|
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
static ParseActionResult ts_parser__shift(TSParser *self, int head,
|
2016-01-22 22:10:18 -07:00
|
|
|
TSStateId parse_state,
|
|
|
|
|
TSTree *lookahead) {
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_verifying = lookahead->child_count > 0;
|
|
|
|
|
switch (
|
|
|
|
|
ts_stack_push(self->stack, head, lookahead, is_verifying, parse_state)) {
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushFailed:
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionFailed;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushMerged:
|
2016-01-19 18:07:24 -08:00
|
|
|
LOG("merge head:%d", head);
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionRemoved;
|
2016-02-12 14:07:30 -08:00
|
|
|
default:
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionUpdated;
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-22 22:10:18 -07:00
|
|
|
static ParseActionResult ts_parser__shift_extra(TSParser *self, int head,
|
|
|
|
|
TSStateId state,
|
|
|
|
|
TSTree *lookahead) {
|
2015-12-15 22:28:50 -08:00
|
|
|
TSSymbolMetadata metadata = self->language->symbol_metadata[lookahead->symbol];
|
2016-01-19 18:07:24 -08:00
|
|
|
if (metadata.structural && ts_stack_head_count(self->stack) > 1) {
|
2016-02-02 13:13:32 -08:00
|
|
|
TSTree *copy = ts_tree_make_copy(lookahead);
|
|
|
|
|
if (!copy)
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionFailed;
|
2016-02-02 13:13:32 -08:00
|
|
|
copy->extra = true;
|
|
|
|
|
ParseActionResult result = ts_parser__shift(self, head, state, copy);
|
|
|
|
|
ts_tree_release(copy);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
lookahead->extra = true;
|
|
|
|
|
return ts_parser__shift(self, head, state, lookahead);
|
2016-01-19 18:07:24 -08:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
static ReduceResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
|
|
|
|
int child_count, bool extra, bool fragile) {
|
2016-03-07 20:06:46 -08:00
|
|
|
TSSymbolMetadata metadata =
|
|
|
|
|
ts_language_symbol_metadata(self->language, symbol);
|
|
|
|
|
StackPopResult pop = ts_stack_pop_count(self->stack, head, child_count);
|
2016-03-03 11:05:37 -08:00
|
|
|
if (!pop.slices.size)
|
2016-03-07 20:06:46 -08:00
|
|
|
goto error;
|
|
|
|
|
if (pop.status == StackPopStoppedAtError)
|
|
|
|
|
return (ReduceResult){
|
|
|
|
|
.status = ReduceStoppedAtError, .partial_slice = pop.slices.contents[0],
|
|
|
|
|
};
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
size_t removed_heads = 0;
|
2015-05-27 10:53:02 -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
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
size_t trailing_extra_count = 0;
|
2016-03-03 10:16:10 -08:00
|
|
|
for (size_t j = slice.trees.size - 1; j + 1 > 0; j--) {
|
|
|
|
|
if (slice.trees.contents[j]->extra)
|
2016-02-23 17:35:50 -08:00
|
|
|
trailing_extra_count++;
|
|
|
|
|
else
|
2015-11-20 00:01:53 -08:00
|
|
|
break;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
size_t popped_child_count = slice.trees.size - trailing_extra_count;
|
2016-03-07 20:06:46 -08:00
|
|
|
TSTree *parent = ts_tree_make_node(symbol, popped_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-03-07 20:06:46 -08:00
|
|
|
if (fragile || self->is_split || ts_stack_head_count(self->stack) > 1) {
|
|
|
|
|
parent->fragile_left = true;
|
|
|
|
|
parent->fragile_right = true;
|
|
|
|
|
parent->parse_state = TS_TREE_STATE_ERROR;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
int new_head = slice.head_index - removed_heads;
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
if (i > 0) {
|
2015-12-02 07:53:15 -08:00
|
|
|
if (symbol == ts_builtin_sym_error) {
|
2016-01-29 17:31:43 -08:00
|
|
|
removed_heads++;
|
2015-12-02 07:53:15 -08:00
|
|
|
ts_stack_remove_head(self->stack, new_head);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("split_during_reduce new_head:%d", new_head);
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
/*
|
|
|
|
|
* If the parent node is extra, then do not change the state when pushing
|
|
|
|
|
* it. Otherwise, proceed to the state given in the parse table for the
|
|
|
|
|
* new parent symbol.
|
|
|
|
|
*/
|
|
|
|
|
TSStateId state;
|
|
|
|
|
TSStateId top_state = ts_stack_top_state(self->stack, new_head);
|
2015-12-22 14:37:29 -08:00
|
|
|
if (parent->parse_state != TS_TREE_STATE_ERROR)
|
2015-12-22 14:20:58 -08:00
|
|
|
parent->parse_state = top_state;
|
2015-11-20 00:01:53 -08:00
|
|
|
if (extra) {
|
2015-12-22 14:20:58 -08:00
|
|
|
parent->extra = true;
|
2015-11-20 00:01:53 -08:00
|
|
|
state = top_state;
|
2015-07-08 17:34:21 -07:00
|
|
|
} else {
|
2015-11-20 11:49:04 -08:00
|
|
|
TSParseAction action =
|
2015-11-20 12:00:49 -08:00
|
|
|
ts_language_last_action(self->language, top_state, symbol);
|
2015-11-20 00:01:53 -08:00
|
|
|
if (child_count == -1) {
|
|
|
|
|
state = 0;
|
2015-07-08 17:34:21 -07:00
|
|
|
} else {
|
2015-11-20 00:01:53 -08:00
|
|
|
assert(action.type == TSParseActionTypeShift);
|
|
|
|
|
state = action.data.to_state;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
StackPushResult push =
|
|
|
|
|
ts_stack_push(self->stack, new_head, parent, false, state);
|
2016-03-07 20:06:46 -08:00
|
|
|
ts_tree_release(parent);
|
|
|
|
|
switch (push) {
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushFailed:
|
2016-01-29 17:31:43 -08:00
|
|
|
goto error;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushMerged:
|
2016-01-19 18:07:24 -08:00
|
|
|
LOG("merge_during_reduce head:%d", new_head);
|
|
|
|
|
removed_heads++;
|
|
|
|
|
continue;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushContinued:
|
2016-01-19 18:07:24 -08:00
|
|
|
break;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
if (trailing_extra_count > 0) {
|
|
|
|
|
for (size_t j = 0; j < trailing_extra_count; j++) {
|
2016-03-03 10:16:10 -08:00
|
|
|
size_t index = slice.trees.size - trailing_extra_count + j;
|
|
|
|
|
TSTree *tree = slice.trees.contents[index];
|
2016-03-31 12:03:07 -07:00
|
|
|
StackPushResult push =
|
|
|
|
|
ts_stack_push(self->stack, new_head, tree, false, state);
|
2016-03-07 20:06:46 -08:00
|
|
|
ts_tree_release(tree);
|
|
|
|
|
switch (push) {
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushFailed:
|
2016-03-07 20:06:46 -08:00
|
|
|
goto error;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushMerged:
|
2016-01-19 18:07:24 -08:00
|
|
|
removed_heads++;
|
2016-02-02 12:03:11 -08:00
|
|
|
break;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushContinued:
|
2016-01-19 18:07:24 -08:00
|
|
|
break;
|
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-03-03 11:05:37 -08:00
|
|
|
if (removed_heads < pop.slices.size)
|
2016-03-07 20:06:46 -08:00
|
|
|
return (ReduceResult){.status = ReduceSucceeded };
|
2016-01-19 18:07:24 -08:00
|
|
|
else
|
2016-03-07 20:06:46 -08:00
|
|
|
return (ReduceResult){.status = ReduceMerged };
|
2016-01-29 17:31:43 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-03-07 20:06:46 -08:00
|
|
|
return (ReduceResult){.status = ReduceFailed };
|
2015-06-15 15:24:15 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
static StackIterateAction ts_parser__error_repair_callback(void *payload,
|
|
|
|
|
TSStateId state,
|
2016-03-10 11:51:38 -08:00
|
|
|
size_t tree_count,
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_done,
|
|
|
|
|
bool is_pending) {
|
2016-03-07 20:06:46 -08:00
|
|
|
ErrorRepairSession *session = (ErrorRepairSession *)payload;
|
|
|
|
|
const TSParser *self = session->parser;
|
|
|
|
|
size_t count_above_error = session->count_above_error;
|
|
|
|
|
TSSymbol lookahead_symbol = session->lookahead_symbol;
|
|
|
|
|
|
|
|
|
|
if (session->best_repair_index >= 0)
|
|
|
|
|
return StackIterateAbort;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < session->repairs->size; i++) {
|
|
|
|
|
ErrorRepair *repair = &session->repairs->contents[i];
|
|
|
|
|
TSSymbol symbol = repair->action.data.symbol;
|
|
|
|
|
size_t child_count = repair->action.data.child_count;
|
|
|
|
|
|
2016-03-10 11:51:38 -08:00
|
|
|
if (tree_count + count_above_error >= child_count &&
|
2016-03-07 20:06:46 -08:00
|
|
|
repair->in_progress_state_count > 0) {
|
|
|
|
|
TSParseAction new_action =
|
|
|
|
|
ts_language_last_action(self->language, state, symbol);
|
|
|
|
|
if (new_action.type == TSParseActionTypeShift) {
|
|
|
|
|
repair->next_state = new_action.data.to_state;
|
|
|
|
|
if (ts_language_has_action(self->language, repair->next_state,
|
|
|
|
|
lookahead_symbol)) {
|
|
|
|
|
session->best_repair_index = i;
|
|
|
|
|
return StackIteratePop;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
if (ts_language_symbol_is_in_progress(self->language, state, symbol))
|
|
|
|
|
repair->in_progress_state_count++;
|
2016-03-10 11:57:33 -08:00
|
|
|
else
|
|
|
|
|
repair->in_progress_state_count = 0;
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2016-03-02 21:03:55 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
return StackIterateContinue;
|
2015-02-21 10:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
static RepairResult ts_parser__repair_error(TSParser *self, int head_index,
|
|
|
|
|
StackSlice partial_slice,
|
|
|
|
|
TSTree *lookahead,
|
|
|
|
|
const TSParseAction *actions,
|
|
|
|
|
size_t action_count) {
|
|
|
|
|
ErrorRepairSession session = {
|
|
|
|
|
.count_above_error = 0,
|
|
|
|
|
.best_repair_index = -1,
|
|
|
|
|
.repairs = &self->error_repairs,
|
|
|
|
|
.lookahead_symbol = lookahead->symbol,
|
|
|
|
|
.parser = self,
|
|
|
|
|
};
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
for (size_t i = 0; i < partial_slice.trees.size; i++)
|
|
|
|
|
if (!partial_slice.trees.contents[i]->extra)
|
|
|
|
|
session.count_above_error++;
|
|
|
|
|
|
|
|
|
|
array_clear(&self->error_repairs);
|
|
|
|
|
for (size_t i = 0; i < action_count; i++)
|
|
|
|
|
if (actions[i].type == TSParseActionTypeReduce &&
|
2016-03-10 11:56:10 -08:00
|
|
|
actions[i].data.child_count > session.count_above_error)
|
2016-03-07 20:06:46 -08:00
|
|
|
array_push(&self->error_repairs,
|
|
|
|
|
((ErrorRepair){
|
|
|
|
|
.action = actions[i], .in_progress_state_count = 0,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
StackPopResult result = ts_stack_pop_until(
|
|
|
|
|
self->stack, head_index, ts_parser__error_repair_callback, &session);
|
|
|
|
|
if (!result.slices.size) {
|
|
|
|
|
ts_tree_array_delete(&partial_slice.trees);
|
|
|
|
|
return RepairNoneFound;
|
2016-03-03 11:05:37 -08:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
ErrorRepair repair = self->error_repairs.contents[session.best_repair_index];
|
|
|
|
|
size_t count_needed_below =
|
|
|
|
|
repair.action.data.child_count - session.count_above_error;
|
|
|
|
|
TreeArray children_below = result.slices.contents[0].trees;
|
|
|
|
|
size_t count_skipped_below = children_below.size - count_needed_below;
|
|
|
|
|
TSSymbol symbol = repair.action.data.symbol;
|
2015-11-11 16:56:58 -08:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
LOG_ACTION(
|
|
|
|
|
"repair_found sym:%s, child_count:%d, match_count:%lu, skipped:%lu",
|
|
|
|
|
SYM_NAME(symbol), repair.action.data.child_count,
|
|
|
|
|
repair.in_progress_state_count, count_skipped_below);
|
2014-09-02 07:41:29 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
if (count_skipped_below > 1) {
|
|
|
|
|
TreeArray skipped_children = array_new();
|
|
|
|
|
if (!array_grow(&skipped_children, count_skipped_below))
|
|
|
|
|
goto error;
|
|
|
|
|
for (size_t i = count_needed_below; i < children_below.size; i++)
|
|
|
|
|
array_push(&skipped_children, children_below.contents[i]);
|
|
|
|
|
TSTree *error = ts_tree_make_node(
|
|
|
|
|
ts_builtin_sym_error, skipped_children.size, skipped_children.contents,
|
|
|
|
|
ts_language_symbol_metadata(self->language, ts_builtin_sym_error));
|
|
|
|
|
error->extra = true;
|
|
|
|
|
children_below.size = count_needed_below;
|
|
|
|
|
array_push(&children_below, error);
|
2016-03-02 21:03:55 -08:00
|
|
|
}
|
2014-09-29 10:33:56 -07:00
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
for (size_t i = 0; i < partial_slice.trees.size; i++)
|
|
|
|
|
array_push(&children_below, partial_slice.trees.contents[i]);
|
|
|
|
|
array_delete(&partial_slice);
|
|
|
|
|
TSTree *parent =
|
|
|
|
|
ts_tree_make_node(symbol, children_below.size, children_below.contents,
|
|
|
|
|
ts_language_symbol_metadata(self->language, symbol));
|
|
|
|
|
|
|
|
|
|
StackPushResult push_result =
|
2016-03-31 12:03:07 -07:00
|
|
|
ts_stack_push(self->stack, head_index, parent, false, repair.next_state);
|
2016-03-02 21:03:55 -08:00
|
|
|
ts_tree_release(parent);
|
|
|
|
|
switch (push_result) {
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushFailed:
|
2016-03-07 20:06:46 -08:00
|
|
|
return RepairFailed;
|
2016-03-03 10:20:05 -08:00
|
|
|
case StackPushMerged:
|
2016-03-07 20:06:46 -08:00
|
|
|
return RepairMerged;
|
2016-03-02 21:03:55 -08:00
|
|
|
default:
|
2016-03-07 20:06:46 -08:00
|
|
|
return RepairSucceeded;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2016-03-07 20:06:46 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
return RepairFailed;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
static ParseActionResult ts_parser__start(TSParser *self, TSInput input,
|
2016-01-22 22:10:18 -07:00
|
|
|
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-12-17 12:08:06 -08:00
|
|
|
ts_stack_set_tree_selection_callback(self->stack, self,
|
|
|
|
|
ts_parser__select_tree);
|
2015-09-18 23:20:06 -07:00
|
|
|
|
2015-12-24 22:04:20 -08:00
|
|
|
self->finished_tree = NULL;
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionUpdated;
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
static ParseActionResult ts_parser__accept(TSParser *self, int head) {
|
2016-03-10 11:57:33 -08:00
|
|
|
StackSlice slice = ts_parser__pop_one(self, head, -1);
|
|
|
|
|
if (!slice.trees.contents)
|
2016-02-04 11:15:46 -08:00
|
|
|
goto error;
|
2015-12-24 22:04:20 -08:00
|
|
|
|
2016-03-10 11:57:33 -08:00
|
|
|
TreeArray trees = slice.trees;
|
2016-02-21 22:31:04 -08:00
|
|
|
|
2016-03-10 11:57:33 -08:00
|
|
|
for (size_t i = trees.size - 1; i + 1 > 0; i--) {
|
|
|
|
|
if (!trees.contents[i]->extra) {
|
|
|
|
|
TSTree *root = trees.contents[i];
|
|
|
|
|
if (!array_splice(&trees, i, 1, root->child_count, root->children))
|
|
|
|
|
goto error;
|
|
|
|
|
ts_tree_set_children(root, trees.size, trees.contents);
|
|
|
|
|
if (!trees.size)
|
|
|
|
|
array_delete(&trees);
|
|
|
|
|
|
|
|
|
|
ts_parser__remove_head(self, slice.head_index);
|
|
|
|
|
int comparison = ts_parser__select_tree(self, self->finished_tree, root);
|
|
|
|
|
if (comparison > 0) {
|
|
|
|
|
ts_tree_release(self->finished_tree);
|
|
|
|
|
self->finished_tree = root;
|
|
|
|
|
} else {
|
|
|
|
|
ts_tree_release(root);
|
2015-12-24 22:04:20 -08:00
|
|
|
}
|
2016-03-10 11:57:33 -08:00
|
|
|
|
|
|
|
|
break;
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionRemoved;
|
2016-02-04 11:15:46 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-03-10 11:57:33 -08:00
|
|
|
for (size_t i = 0; i < slice.trees.size; i++)
|
|
|
|
|
ts_tree_release(slice.trees.contents[i]);
|
|
|
|
|
array_delete(&slice.trees);
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionFailed;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:06:46 -08:00
|
|
|
static ParseActionResult ts_parser__handle_error(TSParser *self, int head,
|
2016-03-10 11:57:33 -08:00
|
|
|
TSTree *invalid_tree) {
|
|
|
|
|
TreeArray invalid_trees = array_new();
|
|
|
|
|
TSTree *next_token = self->language->lex_fn(&self->lexer, 0, true);
|
|
|
|
|
ts_tree_retain(invalid_tree);
|
|
|
|
|
if (!array_push(&invalid_trees, invalid_tree))
|
2016-03-07 20:06:46 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-03-10 11:57:33 -08:00
|
|
|
for (;;) {
|
|
|
|
|
if (next_token->symbol == ts_builtin_sym_end) {
|
|
|
|
|
LOG_ACTION("fail_to_recover");
|
|
|
|
|
TSTree *error = ts_tree_make_node(
|
|
|
|
|
ts_builtin_sym_error, invalid_trees.size, invalid_trees.contents,
|
|
|
|
|
ts_language_symbol_metadata(self->language, ts_builtin_sym_error));
|
2016-03-31 12:03:07 -07:00
|
|
|
if (!ts_stack_push(self->stack, head, error, false, 0))
|
2016-03-10 11:57:33 -08:00
|
|
|
goto error;
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2016-03-10 11:57:33 -08:00
|
|
|
StackSlice slice = ts_parser__pop_one(self, head, -1);
|
|
|
|
|
if (!slice.trees.contents)
|
|
|
|
|
goto error;
|
|
|
|
|
TSTree *parent = ts_tree_make_node(
|
|
|
|
|
ts_builtin_sym_start, slice.trees.size, slice.trees.contents,
|
|
|
|
|
ts_language_symbol_metadata(self->language, ts_builtin_sym_start));
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
if (!ts_stack_push(self->stack, head, parent, false, 0))
|
2016-03-10 11:57:33 -08:00
|
|
|
goto error;
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2016-03-10 11:57:33 -08:00
|
|
|
ts_tree_release(parent);
|
|
|
|
|
ts_tree_release(error);
|
|
|
|
|
ts_tree_release(next_token);
|
|
|
|
|
return ts_parser__accept(self, head);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSLength position = self->lexer.current_position;
|
|
|
|
|
TSTree *following_token = self->language->lex_fn(&self->lexer, 0, true);
|
|
|
|
|
|
|
|
|
|
if (!ts_language_symbol_metadata(self->language, next_token->symbol).extra) {
|
|
|
|
|
TSParseAction action = ts_language_last_action(
|
|
|
|
|
self->language, ts_parse_state_error, next_token->symbol);
|
|
|
|
|
assert(action.type == TSParseActionTypeShift);
|
|
|
|
|
|
|
|
|
|
TSStateId next_state = action.data.to_state;
|
2016-03-31 12:03:07 -07:00
|
|
|
if (ts_language_has_action(self->language, next_state,
|
|
|
|
|
following_token->symbol)) {
|
2016-03-10 11:57:33 -08:00
|
|
|
LOG_ACTION("resume_without_context state:%d", next_state);
|
|
|
|
|
ts_lexer_reset(&self->lexer, position);
|
|
|
|
|
TSTree *error = ts_tree_make_node(
|
|
|
|
|
ts_builtin_sym_error, invalid_trees.size, invalid_trees.contents,
|
|
|
|
|
ts_language_symbol_metadata(self->language, ts_builtin_sym_error));
|
|
|
|
|
error->extra = true;
|
2016-03-31 12:03:07 -07:00
|
|
|
if (!ts_stack_push(self->stack, head, error, false, ts_parse_state_error))
|
2016-03-10 11:57:33 -08:00
|
|
|
goto error;
|
2016-03-31 12:03:07 -07:00
|
|
|
if (!ts_stack_push(self->stack, head, next_token, false, next_state))
|
2016-03-10 11:57:33 -08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
ts_tree_release(error);
|
|
|
|
|
ts_tree_release(next_token);
|
|
|
|
|
ts_tree_release(following_token);
|
|
|
|
|
|
|
|
|
|
return ParseActionUpdated;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!array_push(&invalid_trees, next_token))
|
|
|
|
|
goto error;
|
|
|
|
|
next_token = following_token;
|
|
|
|
|
}
|
2016-03-07 20:06:46 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
return ParseActionFailed;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-07 12:58:32 -07:00
|
|
|
/*
|
|
|
|
|
* Continue performing parse actions for the given head until the current
|
|
|
|
|
* lookahead symbol is consumed.
|
|
|
|
|
*/
|
2016-01-19 18:07:24 -08:00
|
|
|
|
|
|
|
|
static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head,
|
2016-01-22 22:10:18 -07:00
|
|
|
TSTree *lookahead) {
|
2015-10-07 12:58:32 -07:00
|
|
|
for (;;) {
|
2015-10-14 21:52:13 -07:00
|
|
|
TSStateId state = ts_stack_top_state(self->stack, head);
|
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
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there are multiple actions for the current state and lookahead symbol,
|
|
|
|
|
* split the stack so that each one can be performed. If there is a `SHIFT`
|
|
|
|
|
* action, it will always appear *last* in the list of actions. Perform it
|
|
|
|
|
* on the original stack head and return.
|
|
|
|
|
*/
|
2016-03-02 21:03:55 -08:00
|
|
|
bool repaired_error = false;
|
2016-03-10 11:56:10 -08:00
|
|
|
size_t child_count_above_error = -1;
|
2015-12-29 11:20:52 -08:00
|
|
|
for (size_t i = 0; i < action_count; i++) {
|
|
|
|
|
TSParseAction action = actions[i];
|
2015-10-07 12:58:32 -07:00
|
|
|
|
2016-03-10 11:56:10 -08:00
|
|
|
bool should_skip = repaired_error &&
|
2016-03-31 12:03:07 -07:00
|
|
|
action.type == TSParseActionTypeReduce &&
|
|
|
|
|
action.data.child_count > child_count_above_error;
|
2016-03-07 20:06:46 -08:00
|
|
|
|
2015-10-07 12:58:32 -07:00
|
|
|
int current_head;
|
2015-12-29 11:20:52 -08:00
|
|
|
if (i == action_count - 1) {
|
2016-03-07 20:06:46 -08:00
|
|
|
if (should_skip)
|
2016-03-02 21:03:55 -08:00
|
|
|
action.type = TSParseActionTypeError;
|
2015-10-07 12:58:32 -07:00
|
|
|
current_head = head;
|
|
|
|
|
} else {
|
2016-03-07 20:06:46 -08:00
|
|
|
if (should_skip)
|
2016-03-02 21:03:55 -08:00
|
|
|
continue;
|
2015-11-18 08:47:15 -08:00
|
|
|
current_head = ts_parser__split(self, head);
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("split_action from_head:%d, new_head:%d", head, current_head);
|
2015-10-07 12:58:32 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
LOG_STACK();
|
|
|
|
|
|
2015-10-07 12:58:32 -07:00
|
|
|
switch (action.type) {
|
2016-03-31 12:03:07 -07:00
|
|
|
case TSParseActionTypeError: {
|
|
|
|
|
switch (ts_parser__breakdown_top_of_stack(self, current_head)) {
|
|
|
|
|
case BreakdownFailed:
|
|
|
|
|
return ParseActionFailed;
|
|
|
|
|
case BreakdownPerformed:
|
|
|
|
|
return ParseActionRemoved;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2015-12-02 11:24:13 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (ts_stack_head_count(self->stack) == 1) {
|
2016-03-03 09:37:37 -08:00
|
|
|
LOG_ACTION("handle_error %s", SYM_NAME(lookahead->symbol));
|
2016-03-07 20:06:46 -08:00
|
|
|
return ts_parser__handle_error(self, current_head, lookahead);
|
2015-10-07 12:58:32 -07:00
|
|
|
} else {
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("bail current_head:%d", current_head);
|
2015-11-18 08:47:15 -08:00
|
|
|
ts_parser__remove_head(self, current_head);
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionRemoved;
|
2015-10-07 12:58:32 -07:00
|
|
|
}
|
2016-03-31 12:03:07 -07:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeShift:
|
2015-12-17 12:48:55 -08:00
|
|
|
if (action.extra) {
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("shift_extra");
|
2015-12-17 12:48:55 -08:00
|
|
|
return ts_parser__shift_extra(self, current_head, state, lookahead);
|
|
|
|
|
} else {
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("shift state:%u", action.data.to_state);
|
2016-01-19 18:07:24 -08:00
|
|
|
TSStateId state = action.data.to_state;
|
|
|
|
|
return ts_parser__shift(self, current_head, state, lookahead);
|
2015-12-17 12:48:55 -08:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
2015-12-17 12:48:55 -08:00
|
|
|
if (action.extra) {
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
2015-12-17 12:48:55 -08:00
|
|
|
ts_parser__reduce(self, current_head, action.data.symbol, 1, true,
|
2016-03-02 21:03:55 -08:00
|
|
|
false);
|
2015-12-17 12:48:55 -08:00
|
|
|
} else {
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("reduce sym:%s, child_count:%u, fragile:%s",
|
|
|
|
|
SYM_NAME(action.data.symbol), action.data.child_count,
|
|
|
|
|
BOOL_STRING(action.fragile));
|
2016-03-07 20:06:46 -08:00
|
|
|
ReduceResult result =
|
|
|
|
|
ts_parser__reduce(self, current_head, action.data.symbol,
|
|
|
|
|
action.data.child_count, false, action.fragile);
|
|
|
|
|
switch (result.status) {
|
2016-03-02 21:03:55 -08:00
|
|
|
case ReduceFailed:
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionFailed;
|
2016-03-02 21:03:55 -08:00
|
|
|
case ReduceMerged:
|
2016-01-19 18:07:24 -08:00
|
|
|
if (current_head == head)
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionRemoved;
|
2016-01-19 18:07:24 -08:00
|
|
|
break;
|
2016-03-02 21:03:55 -08:00
|
|
|
case ReduceSucceeded:
|
|
|
|
|
break;
|
|
|
|
|
case ReduceStoppedAtError:
|
|
|
|
|
repaired_error = true;
|
2016-03-10 11:56:10 -08:00
|
|
|
child_count_above_error = 0;
|
|
|
|
|
for (size_t j = 0; j < result.partial_slice.trees.size; j++)
|
|
|
|
|
if (!result.partial_slice.trees.contents[j]->extra)
|
|
|
|
|
child_count_above_error++;
|
|
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
LOG_ACTION("repair head:%d", current_head);
|
2016-03-07 20:06:46 -08:00
|
|
|
switch (ts_parser__repair_error(self, current_head,
|
|
|
|
|
result.partial_slice, lookahead,
|
2016-03-02 21:03:55 -08:00
|
|
|
actions, action_count)) {
|
2016-03-07 20:06:46 -08:00
|
|
|
case RepairFailed:
|
2016-03-03 10:21:57 -08:00
|
|
|
return ParseActionFailed;
|
2016-03-07 20:06:46 -08:00
|
|
|
case RepairNoneFound:
|
2016-03-02 21:03:55 -08:00
|
|
|
LOG_ACTION("repair_failed");
|
2016-03-07 20:06:46 -08:00
|
|
|
if (ts_stack_head_count(self->stack) == 1) {
|
|
|
|
|
LOG_ACTION("handle_error %s", SYM_NAME(lookahead->symbol));
|
|
|
|
|
return ts_parser__handle_error(self, current_head,
|
|
|
|
|
lookahead);
|
|
|
|
|
} else {
|
|
|
|
|
LOG_ACTION("bail head:%d, count:%d", current_head,
|
|
|
|
|
ts_stack_head_count(self->stack));
|
|
|
|
|
ts_parser__remove_head(self, current_head);
|
|
|
|
|
if (current_head == head)
|
|
|
|
|
return ParseActionRemoved;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RepairMerged:
|
|
|
|
|
if (current_head == head)
|
|
|
|
|
return ParseActionRemoved;
|
2016-03-02 21:03:55 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2015-12-17 12:48:55 -08:00
|
|
|
}
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeAccept:
|
2016-02-23 09:45:27 -08:00
|
|
|
LOG_ACTION("accept");
|
2016-01-19 18:07:24 -08:00
|
|
|
return ts_parser__accept(self, current_head);
|
2015-10-07 12:58:32 -07:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
/*
|
|
|
|
|
* Public
|
|
|
|
|
*/
|
|
|
|
|
|
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-03-07 20:06:46 -08:00
|
|
|
array_init(&self->error_repairs);
|
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-03-07 20:06:46 -08:00
|
|
|
if (!array_grow(&self->error_repairs, 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-03-07 20:06:46 -08:00
|
|
|
if (self->error_repairs.contents)
|
|
|
|
|
array_delete(&self->error_repairs);
|
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-03-07 20:06:46 -08:00
|
|
|
if (self->error_repairs.contents)
|
|
|
|
|
array_delete(&self->error_repairs);
|
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
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
|
|
|
|
|
ts_parser__start(self, input, previous_tree);
|
2015-12-26 16:49:23 -08:00
|
|
|
size_t max_position = 0;
|
2016-04-01 21:17:23 -07:00
|
|
|
ReusableNode current_reusable_node, next_reusable_node = {previous_tree, 0};
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
2016-04-01 21:17:23 -07:00
|
|
|
current_reusable_node = next_reusable_node;
|
2015-11-20 12:55:01 -08:00
|
|
|
TSTree *lookahead = NULL;
|
2016-02-23 00:09:42 -08:00
|
|
|
size_t last_position, position = 0;
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-12-09 14:33:54 -08:00
|
|
|
self->is_split = ts_stack_head_count(self->stack) > 1;
|
2015-12-24 10:21:42 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
for (int head = 0; head < ts_stack_head_count(self->stack);) {
|
2016-04-01 21:17:23 -07:00
|
|
|
ReusableNode reusable_node = current_reusable_node;
|
|
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
for (bool removed = false; !removed;) {
|
2015-12-26 16:49:23 -08:00
|
|
|
last_position = position;
|
2016-02-23 00:09:42 -08:00
|
|
|
size_t new_position = ts_stack_top_position(self->stack, head).chars;
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2016-02-23 00:09:42 -08:00
|
|
|
if (new_position > max_position) {
|
|
|
|
|
max_position = new_position;
|
2016-04-01 21:17:23 -07:00
|
|
|
next_reusable_node = reusable_node;
|
2015-12-26 16:49:23 -08:00
|
|
|
head++;
|
|
|
|
|
break;
|
2016-02-23 00:09:42 -08:00
|
|
|
} else if (new_position == max_position && head > 0) {
|
2015-12-26 16:49:23 -08:00
|
|
|
head++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 00:09:42 -08:00
|
|
|
position = new_position;
|
|
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
LOG_ACTION("process head:%d, head_count:%d, state:%d, pos:%lu", head,
|
|
|
|
|
ts_stack_head_count(self->stack),
|
|
|
|
|
ts_stack_top_state(self->stack, head), position);
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2016-02-23 09:45:27 -08:00
|
|
|
if (!lookahead || (position != last_position) ||
|
2016-02-05 11:18:22 -08:00
|
|
|
!ts_parser__can_reuse(self, head, lookahead)) {
|
2016-01-29 17:31:43 -08:00
|
|
|
ts_tree_release(lookahead);
|
2016-04-01 21:17:23 -07:00
|
|
|
lookahead = ts_parser__get_next_lookahead(self, head, &reusable_node);
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!lookahead)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-03-02 21:03:55 -08:00
|
|
|
LOG_ACTION("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol),
|
|
|
|
|
ts_tree_total_chars(lookahead));
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-01-19 18:07:24 -08:00
|
|
|
switch (ts_parser__consume_lookahead(self, head, lookahead)) {
|
2016-03-03 10:21:57 -08:00
|
|
|
case ParseActionFailed:
|
2016-02-04 11:15:46 -08:00
|
|
|
ts_tree_release(lookahead);
|
2016-01-29 17:31:43 -08:00
|
|
|
goto error;
|
2016-03-03 10:21:57 -08:00
|
|
|
case ParseActionRemoved:
|
2016-01-19 18:07:24 -08:00
|
|
|
removed = true;
|
|
|
|
|
break;
|
2016-03-03 10:21:57 -08:00
|
|
|
case ParseActionUpdated:
|
2016-01-19 18:07:24 -08:00
|
|
|
break;
|
|
|
|
|
}
|
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-01-29 17:31:43 -08:00
|
|
|
ts_tree_release(lookahead);
|
|
|
|
|
|
2015-12-24 22:04:20 -08:00
|
|
|
if (ts_stack_head_count(self->stack) == 0) {
|
2016-01-29 17:31:43 -08:00
|
|
|
ts_stack_clear(self->stack);
|
2015-12-24 22:04:20 -08:00
|
|
|
ts_tree_assign_parents(self->finished_tree);
|
|
|
|
|
return self->finished_tree;
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2016-01-29 17:31:43 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
return NULL;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|