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"
|
2015-11-18 08:47:15 -08:00
|
|
|
#include "runtime/vector.h"
|
2015-11-20 12:00:49 -08:00
|
|
|
#include "runtime/language.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
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
#define SYM_NAME(sym) self->language->symbol_names[sym]
|
2014-10-13 21:20:08 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
typedef struct {
|
|
|
|
|
TSTree *reusable_subtree;
|
|
|
|
|
size_t reusable_subtree_pos;
|
2015-11-20 12:55:01 -08:00
|
|
|
} LookaheadState;
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2015-11-05 21:21:15 -08:00
|
|
|
typedef enum {
|
|
|
|
|
ConsumeResultShifted,
|
|
|
|
|
ConsumeResultRemoved,
|
|
|
|
|
ConsumeResultFinished
|
|
|
|
|
} ConsumeResult;
|
|
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
/*
|
|
|
|
|
* Private
|
|
|
|
|
*/
|
2014-10-09 14:02:03 -07: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.
|
|
|
|
|
*/
|
2015-11-20 12:55:01 -08:00
|
|
|
static bool ts_parser__breakdown_reusable_subtree(LookaheadState *state) {
|
2015-09-13 19:47:45 -07:00
|
|
|
do {
|
2015-11-18 08:47:15 -08:00
|
|
|
if (state->reusable_subtree->symbol == ts_builtin_sym_error)
|
2015-09-13 19:47:45 -07:00
|
|
|
return false;
|
2015-11-18 08:47:15 -08:00
|
|
|
if (state->reusable_subtree->child_count == 0)
|
2015-09-13 19:47:45 -07:00
|
|
|
return false;
|
2015-11-18 08:47:15 -08:00
|
|
|
state->reusable_subtree = state->reusable_subtree->children[0];
|
|
|
|
|
} while (ts_tree_is_fragile(state->reusable_subtree));
|
2015-09-13 19:47:45 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
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.
|
|
|
|
|
*/
|
2015-11-20 12:55:01 -08:00
|
|
|
static void ts_parser__pop_reusable_subtree(LookaheadState *state) {
|
2015-12-04 20:20:29 -08:00
|
|
|
state->reusable_subtree_pos += ts_tree_total_chars(state->reusable_subtree);
|
2015-09-13 19:47:45 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
while (state->reusable_subtree) {
|
|
|
|
|
TSTree *parent = state->reusable_subtree->context.parent;
|
|
|
|
|
size_t next_index = state->reusable_subtree->context.index + 1;
|
2015-09-13 19:47:45 -07:00
|
|
|
if (parent && parent->child_count > next_index) {
|
2015-11-18 08:47:15 -08:00
|
|
|
state->reusable_subtree = parent->children[next_index];
|
2015-09-13 19:47:45 -07:00
|
|
|
return;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
2015-11-18 08:47:15 -08:00
|
|
|
state->reusable_subtree = 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-11-20 12:55:01 -08:00
|
|
|
if (!subtree || subtree->symbol == ts_builtin_sym_error)
|
2015-11-18 08:47:15 -08:00
|
|
|
return false;
|
|
|
|
|
TSStateId state = ts_stack_top_state(self->stack, head);
|
|
|
|
|
const TSParseAction *action =
|
2015-11-20 12:00:49 -08:00
|
|
|
ts_language_actions(self->language, state, subtree->symbol);
|
2015-11-18 08:47:15 -08:00
|
|
|
return action->type != TSParseActionTypeError;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2015-11-18 08:47:15 -08:00
|
|
|
static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) {
|
2015-11-20 12:55:01 -08:00
|
|
|
LookaheadState *state = vector_get(&self->lookahead_states, head);
|
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
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
while (state->reusable_subtree) {
|
2015-12-02 07:53:15 -08:00
|
|
|
if (state->reusable_subtree_pos > 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
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
if (state->reusable_subtree_pos < position.chars) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("past_reuse sym:%s", SYM_NAME(state->reusable_subtree->symbol));
|
2015-11-18 08:47:15 -08:00
|
|
|
ts_parser__pop_reusable_subtree(state);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
if (ts_tree_has_changes(state->reusable_subtree) ||
|
|
|
|
|
ts_tree_is_fragile(state->reusable_subtree) ||
|
|
|
|
|
ts_tree_is_extra(state->reusable_subtree) ||
|
|
|
|
|
(state->reusable_subtree->child_count > 0 &&
|
|
|
|
|
!ts_parser__can_reuse(self, head, state->reusable_subtree))) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("breakdown sym:%s", SYM_NAME(state->reusable_subtree->symbol));
|
2015-11-18 08:47:15 -08:00
|
|
|
if (!ts_parser__breakdown_reusable_subtree(state))
|
|
|
|
|
ts_parser__pop_reusable_subtree(state);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
TSTree *result = state->reusable_subtree;
|
|
|
|
|
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,
|
|
|
|
|
result->options.extra);
|
2015-11-18 08:47:15 -08:00
|
|
|
ts_parser__pop_reusable_subtree(state);
|
|
|
|
|
return result;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
2014-10-22 12:54:46 -07:00
|
|
|
|
2015-11-18 08:47:15 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
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) {
|
|
|
|
|
int result = ts_stack_split(self->stack, head);
|
2015-11-20 12:55:01 -08:00
|
|
|
assert(result == (int)self->lookahead_states.size);
|
2015-12-02 07:53:15 -08:00
|
|
|
LookaheadState lookahead_state =
|
2015-11-20 12:55:01 -08:00
|
|
|
*(LookaheadState *)vector_get(&self->lookahead_states, head);
|
2015-12-02 07:53:15 -08:00
|
|
|
vector_push(&self->lookahead_states, &lookahead_state);
|
2015-11-18 08:47:15 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
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) {
|
2015-11-20 12:55:01 -08:00
|
|
|
vector_erase(&self->lookahead_states, head);
|
2015-11-18 08:47:15 -08:00
|
|
|
ts_stack_remove_head(self->stack, head);
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 13:10:11 -08:00
|
|
|
static TSTree *ts_parser__select_tree(void *data, TSTree *left, TSTree *right) {
|
|
|
|
|
return ts_tree_compare(left, right) <= 0 ? left : right;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Actions
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-11 17:53:45 -08:00
|
|
|
static ConsumeResult ts_parser__shift(TSParser *self, int head,
|
2015-11-20 12:55:01 -08:00
|
|
|
TSStateId parse_state, TSTree *lookahead) {
|
|
|
|
|
if (ts_stack_push(self->stack, head, parse_state, lookahead)) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("merge head:%d", head);
|
2015-11-20 12:55:01 -08:00
|
|
|
vector_erase(&self->lookahead_states, head);
|
2015-11-05 21:21:15 -08:00
|
|
|
return ConsumeResultRemoved;
|
2015-11-18 08:47:15 -08:00
|
|
|
} else {
|
2015-11-05 21:21:15 -08:00
|
|
|
return ConsumeResultShifted;
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 12:55:01 -08:00
|
|
|
static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state,
|
|
|
|
|
TSTree *lookahead) {
|
|
|
|
|
ts_tree_set_extra(lookahead);
|
|
|
|
|
return ts_parser__shift(self, head, state, lookahead);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
static bool ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
2015-12-04 20:56:33 -08:00
|
|
|
int child_count, bool extra, bool count_extra) {
|
2015-11-20 00:01:53 -08:00
|
|
|
vector_clear(&self->reduce_parents);
|
2015-12-02 07:53:15 -08:00
|
|
|
const TSSymbolMetadata *all_metadata = self->language->symbol_metadata;
|
|
|
|
|
TSSymbolMetadata metadata = all_metadata[symbol];
|
2015-11-20 00:01:53 -08:00
|
|
|
Vector pop_results = ts_stack_pop(self->stack, head, child_count, count_extra);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
int last_head_index = -1;
|
2015-12-02 07:53:15 -08:00
|
|
|
size_t removed_heads = 0;
|
|
|
|
|
size_t revealed_heads = 0;
|
2015-05-27 10:53:02 -07:00
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
for (size_t i = 0; i < pop_results.size; i++) {
|
|
|
|
|
StackPopResult *pop_result = vector_get(&pop_results, i);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
/*
|
|
|
|
|
* If the same set of trees led to a previous stack head, reuse the parent
|
|
|
|
|
* tree that was added to that head.
|
|
|
|
|
*/
|
|
|
|
|
TSTree *parent = NULL;
|
2015-12-02 07:53:15 -08:00
|
|
|
size_t trailing_extra_count = 0;
|
2015-11-20 00:01:53 -08:00
|
|
|
for (size_t j = 0; j < i; j++) {
|
|
|
|
|
StackPopResult *prior_result = vector_get(&pop_results, j);
|
|
|
|
|
if (pop_result->trees == prior_result->trees) {
|
|
|
|
|
TSTree **existing_parent = vector_get(&self->reduce_parents, j);
|
|
|
|
|
parent = *existing_parent;
|
2015-12-02 07:53:15 -08:00
|
|
|
trailing_extra_count = pop_result->tree_count - parent->child_count;
|
2015-11-20 00:01:53 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
/*
|
|
|
|
|
* Otherwise, create a new parent node for this set of trees.
|
|
|
|
|
*/
|
2015-12-02 07:53:15 -08:00
|
|
|
if (!parent) {
|
|
|
|
|
for (size_t j = pop_result->tree_count - 1; j + 1 > 0; j--) {
|
|
|
|
|
if (pop_result->trees[j]->options.extra) {
|
|
|
|
|
trailing_extra_count++;
|
|
|
|
|
} else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:56:33 -08:00
|
|
|
parent =
|
|
|
|
|
ts_tree_make_node(symbol, pop_result->tree_count - trailing_extra_count,
|
|
|
|
|
pop_result->trees, metadata);
|
2015-12-02 07:53:15 -08:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
vector_push(&self->reduce_parents, &parent);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
/*
|
|
|
|
|
* If another path led to the same stack head, add this new parent tree
|
|
|
|
|
* as an alternative for that stack head.
|
|
|
|
|
*/
|
|
|
|
|
int new_head = pop_result->head_index - removed_heads;
|
|
|
|
|
if (pop_result->head_index == last_head_index) {
|
|
|
|
|
ts_stack_add_alternative(self->stack, new_head, parent);
|
|
|
|
|
continue;
|
2015-12-02 07:53:15 -08:00
|
|
|
} else {
|
|
|
|
|
revealed_heads++;
|
|
|
|
|
last_head_index = pop_result->head_index;
|
2015-11-20 00:01:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the stack has split in the process of popping, create a duplicate of
|
|
|
|
|
* the lookahead state for this head, for the new head.
|
|
|
|
|
*/
|
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) {
|
|
|
|
|
ts_stack_remove_head(self->stack, new_head);
|
|
|
|
|
free(pop_result->trees);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("split_during_reduce new_head:%d", new_head);
|
2015-12-04 20:56:33 -08:00
|
|
|
LookaheadState *lookahead_state =
|
|
|
|
|
vector_get(&self->lookahead_states, head);
|
2015-12-02 07:53:15 -08:00
|
|
|
vector_push(&self->lookahead_states, lookahead_state);
|
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);
|
|
|
|
|
if (extra) {
|
|
|
|
|
ts_tree_set_extra(parent);
|
|
|
|
|
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
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
/*
|
|
|
|
|
* If the given state already existed at a different head of the stack,
|
|
|
|
|
* then remove the lookahead state for the head.
|
|
|
|
|
*/
|
|
|
|
|
if (ts_stack_push(self->stack, new_head, state, parent)) {
|
2015-12-02 07:53:15 -08:00
|
|
|
LOG("merge_during_reduce head:%d", new_head);
|
2015-11-20 12:55:01 -08:00
|
|
|
vector_erase(&self->lookahead_states, new_head);
|
2015-11-20 00:01:53 -08:00
|
|
|
removed_heads++;
|
2015-12-02 07:53:15 -08:00
|
|
|
continue;
|
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++) {
|
|
|
|
|
size_t index = pop_result->tree_count - trailing_extra_count + j;
|
2015-12-04 20:56:33 -08:00
|
|
|
if (ts_stack_push(self->stack, new_head, state,
|
|
|
|
|
pop_result->trees[index])) {
|
2015-12-02 07:53:15 -08:00
|
|
|
vector_erase(&self->lookahead_states, new_head);
|
|
|
|
|
removed_heads++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2015-05-27 10:53:02 -07:00
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
return removed_heads < revealed_heads;
|
2015-05-27 10:53:02 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
static bool ts_parser__reduce_fragile(TSParser *self, int head, TSSymbol symbol,
|
2015-10-14 21:52:13 -07:00
|
|
|
size_t child_count) {
|
2015-12-02 07:53:15 -08:00
|
|
|
bool result = ts_parser__reduce(self, head, symbol, child_count, false, false);
|
|
|
|
|
if (result)
|
|
|
|
|
for (size_t i = 0; i < self->reduce_parents.size; i++) {
|
|
|
|
|
TSTree **parent = vector_get(&self->reduce_parents, i);
|
|
|
|
|
ts_tree_set_fragile_left(*parent);
|
|
|
|
|
ts_tree_set_fragile_right(*parent);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2015-06-15 15:24:15 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__reduce_error(TSParser *self, int head,
|
2015-11-20 12:55:01 -08:00
|
|
|
size_t child_count, TSTree *lookahead) {
|
2015-12-04 20:56:33 -08:00
|
|
|
bool result = ts_parser__reduce(self, head, ts_builtin_sym_error, child_count,
|
|
|
|
|
false, true);
|
2015-12-02 07:53:15 -08:00
|
|
|
if (result) {
|
|
|
|
|
TSTree **parent = vector_back(&self->reduce_parents);
|
|
|
|
|
StackEntry *stack_entry = ts_stack_head(self->stack, head);
|
2015-12-04 20:56:33 -08:00
|
|
|
stack_entry->position =
|
|
|
|
|
ts_length_add(stack_entry->position, lookahead->padding);
|
2015-12-02 07:53:15 -08:00
|
|
|
(*parent)->size = ts_length_add((*parent)->size, lookahead->padding);
|
|
|
|
|
lookahead->padding = ts_length_zero();
|
|
|
|
|
ts_tree_set_fragile_left(*parent);
|
|
|
|
|
ts_tree_set_fragile_right(*parent);
|
|
|
|
|
}
|
2015-02-21 10:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 12:55:01 -08:00
|
|
|
static bool ts_parser__handle_error(TSParser *self, int head, TSTree *lookahead) {
|
2015-06-18 15:04:03 -07:00
|
|
|
size_t error_token_count = 1;
|
2015-10-14 21:52:13 -07:00
|
|
|
StackEntry *entry_before_error = ts_stack_head(self->stack, head);
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
/*
|
2014-08-25 21:58:01 -07:00
|
|
|
* Unwind the parse stack until a state is found in which an error is
|
|
|
|
|
* expected and the current lookahead token is expected afterwards.
|
2014-07-20 20:27:33 -07:00
|
|
|
*/
|
2015-06-18 15:04:03 -07:00
|
|
|
int i = -1;
|
2015-11-11 16:56:58 -08:00
|
|
|
for (StackEntry *entry = entry_before_error; true;
|
2015-11-14 12:37:21 -08:00
|
|
|
entry = ts_stack_entry_next(entry, 0), i++) {
|
2015-11-11 16:56:58 -08:00
|
|
|
TSStateId stack_state = entry ? entry->state : 0;
|
2015-11-20 12:00:49 -08:00
|
|
|
TSParseAction action_on_error = ts_language_last_action(
|
2015-11-11 16:56:58 -08:00
|
|
|
self->language, stack_state, ts_builtin_sym_error);
|
2014-08-25 21:58:01 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
if (action_on_error.type == TSParseActionTypeShift) {
|
|
|
|
|
TSStateId state_after_error = action_on_error.data.to_state;
|
2015-11-20 12:00:49 -08:00
|
|
|
TSParseAction action_after_error = ts_language_last_action(
|
2015-11-20 12:55:01 -08:00
|
|
|
self->language, state_after_error, lookahead->symbol);
|
2014-08-25 21:58:01 -07:00
|
|
|
|
2014-09-02 07:41:29 -07:00
|
|
|
if (action_after_error.type != TSParseActionTypeError) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("recover state:%u, count:%lu", state_after_error,
|
|
|
|
|
error_token_count + i);
|
2015-11-20 12:55:01 -08:00
|
|
|
ts_parser__reduce_error(self, head, error_token_count + i, lookahead);
|
2015-07-08 17:34:21 -07:00
|
|
|
return true;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2015-11-11 16:56:58 -08:00
|
|
|
|
2015-11-11 17:53:45 -08:00
|
|
|
if (!entry)
|
|
|
|
|
break;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-09-02 07:41:29 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there is no state in the stack for which we can recover with the
|
2015-06-15 15:24:15 -07:00
|
|
|
* current lookahead token, advance to the next token.
|
2014-09-02 07:41:29 -07:00
|
|
|
*/
|
2015-11-20 12:55:01 -08:00
|
|
|
LOG("skip token:%s", SYM_NAME(lookahead->symbol));
|
|
|
|
|
ts_parser__shift(self, head, ts_stack_top_state(self->stack, head),
|
|
|
|
|
lookahead);
|
|
|
|
|
lookahead = self->language->lex_fn(&self->lexer, ts_lex_state_error);
|
2015-06-18 15:04:03 -07:00
|
|
|
error_token_count++;
|
2014-09-29 10:33:56 -07:00
|
|
|
|
|
|
|
|
/*
|
2015-06-15 15:24:15 -07:00
|
|
|
* If the end of input is reached, exit.
|
2014-09-29 10:33:56 -07:00
|
|
|
*/
|
2015-11-20 12:55:01 -08:00
|
|
|
if (lookahead->symbol == ts_builtin_sym_end) {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("fail_to_recover");
|
2015-11-20 12:55:01 -08:00
|
|
|
ts_parser__reduce_error(self, head, -1, lookahead);
|
2015-07-08 17:34:21 -07:00
|
|
|
return false;
|
2015-06-12 13:13:43 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__start(TSParser *self, TSInput input,
|
2015-09-18 23:20:06 -07:00
|
|
|
TSTree *previous_tree) {
|
|
|
|
|
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-02 07:53:15 -08:00
|
|
|
LookaheadState lookahead_state = {
|
2015-12-04 20:56:33 -08:00
|
|
|
.reusable_subtree = previous_tree, .reusable_subtree_pos = 0,
|
2015-11-18 08:47:15 -08:00
|
|
|
};
|
2015-11-20 12:55:01 -08:00
|
|
|
vector_clear(&self->lookahead_states);
|
2015-12-02 07:53:15 -08:00
|
|
|
vector_push(&self->lookahead_states, &lookahead_state);
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static TSTree *ts_parser__finish(TSParser *self) {
|
2015-11-20 00:01:53 -08:00
|
|
|
Vector pop_results = ts_stack_pop(self->stack, 0, -1, true);
|
|
|
|
|
StackPopResult *pop_result = vector_get(&pop_results, 0);
|
2015-08-22 10:48:34 -07:00
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
for (size_t i = 0; i < pop_result->tree_count; i++) {
|
|
|
|
|
if (!pop_result->trees[i]->options.extra) {
|
|
|
|
|
TSTree *root = pop_result->trees[i];
|
|
|
|
|
size_t leading_extra_count = i;
|
|
|
|
|
size_t trailing_extra_count = pop_result->tree_count - 1 - i;
|
2015-12-04 20:56:33 -08:00
|
|
|
TSTree **new_children =
|
|
|
|
|
malloc((root->child_count + leading_extra_count + trailing_extra_count) *
|
|
|
|
|
sizeof(TSTree *));
|
|
|
|
|
memcpy(new_children, pop_result->trees,
|
|
|
|
|
leading_extra_count * sizeof(TSTree *));
|
|
|
|
|
memcpy(new_children + leading_extra_count, root->children,
|
|
|
|
|
root->child_count * sizeof(TSTree *));
|
|
|
|
|
memcpy(new_children + leading_extra_count + root->child_count,
|
|
|
|
|
pop_result->trees + leading_extra_count + 1,
|
|
|
|
|
trailing_extra_count * sizeof(TSTree *));
|
|
|
|
|
size_t new_count =
|
|
|
|
|
root->child_count + leading_extra_count + trailing_extra_count;
|
2015-12-02 07:53:15 -08:00
|
|
|
ts_tree_set_children(root, new_count, new_children);
|
|
|
|
|
ts_tree_assign_parents(root);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-22 10:48:34 -07:00
|
|
|
|
2015-12-02 07:53:15 -08:00
|
|
|
return NULL;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-07 12:58:32 -07:00
|
|
|
/*
|
|
|
|
|
* Continue performing parse actions for the given head until the current
|
|
|
|
|
* lookahead symbol is consumed.
|
|
|
|
|
*/
|
2015-11-20 12:55:01 -08:00
|
|
|
static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head,
|
|
|
|
|
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-10-07 12:58:32 -07:00
|
|
|
const TSParseAction *next_action =
|
2015-11-20 12:55:01 -08:00
|
|
|
ts_language_actions(self->language, state, lookahead->symbol);
|
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.
|
|
|
|
|
*/
|
2015-12-02 07:53:15 -08:00
|
|
|
while (next_action) {
|
2015-10-07 12:58:32 -07:00
|
|
|
TSParseAction action = *next_action;
|
|
|
|
|
next_action++;
|
|
|
|
|
|
|
|
|
|
int current_head;
|
|
|
|
|
if (next_action->type == 0) {
|
|
|
|
|
current_head = head;
|
2015-12-02 07:53:15 -08:00
|
|
|
next_action = NULL;
|
2015-10-07 12:58:32 -07:00
|
|
|
} else {
|
2015-11-18 08:47:15 -08:00
|
|
|
current_head = ts_parser__split(self, head);
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("split_action from_head:%d, new_head:%d", head, current_head);
|
2015-10-07 12:58:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Remove this by making a separate symbol for errors returned from
|
|
|
|
|
// the lexer.
|
2015-11-20 12:55:01 -08:00
|
|
|
if (lookahead->symbol == ts_builtin_sym_error)
|
2015-10-07 12:58:32 -07:00
|
|
|
action.type = TSParseActionTypeError;
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case TSParseActionTypeError:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("error_sym");
|
2015-10-14 21:52:13 -07:00
|
|
|
if (ts_stack_head_count(self->stack) == 1) {
|
2015-11-20 12:55:01 -08:00
|
|
|
if (ts_parser__handle_error(self, current_head, lookahead))
|
|
|
|
|
return ConsumeResultShifted;
|
2015-10-07 12:58:32 -07:00
|
|
|
else
|
|
|
|
|
return ConsumeResultFinished;
|
|
|
|
|
} else {
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("bail current_head:%d", current_head);
|
2015-11-18 08:47:15 -08:00
|
|
|
ts_parser__remove_head(self, current_head);
|
2015-10-07 12:58:32 -07:00
|
|
|
return ConsumeResultRemoved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeShift:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("shift state:%u", action.data.to_state);
|
2015-11-20 12:55:01 -08:00
|
|
|
return ts_parser__shift(self, current_head, action.data.to_state,
|
|
|
|
|
lookahead);
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeShiftExtra:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("shift_extra");
|
2015-11-20 12:55:01 -08:00
|
|
|
return ts_parser__shift_extra(self, current_head, state, lookahead);
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-12-02 07:53:15 -08:00
|
|
|
if (!ts_parser__reduce(self, current_head, action.data.symbol,
|
|
|
|
|
action.data.child_count, false, false))
|
|
|
|
|
if (!next_action)
|
|
|
|
|
return ConsumeResultRemoved;
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceExtra:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
2015-12-04 20:56:33 -08:00
|
|
|
ts_parser__reduce(self, current_head, action.data.symbol, 1, true,
|
|
|
|
|
false);
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceFragile:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-12-02 07:53:15 -08:00
|
|
|
if (!ts_parser__reduce_fragile(self, current_head, action.data.symbol,
|
2015-12-04 20:56:33 -08:00
|
|
|
action.data.child_count))
|
2015-12-02 07:53:15 -08:00
|
|
|
if (!next_action)
|
|
|
|
|
return ConsumeResultRemoved;
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeAccept:
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("accept");
|
2015-10-07 12:58:32 -07:00
|
|
|
return ConsumeResultFinished;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
/*
|
|
|
|
|
* Public
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
TSParser ts_parser_make() {
|
2015-09-13 19:47:45 -07:00
|
|
|
return (TSParser){
|
|
|
|
|
.lexer = ts_lexer_make(),
|
2015-09-18 18:04:52 -07:00
|
|
|
.stack = ts_stack_new((TreeSelectionCallback){
|
2015-09-13 19:47:45 -07:00
|
|
|
NULL, ts_parser__select_tree,
|
|
|
|
|
}),
|
2015-11-20 12:55:01 -08:00
|
|
|
.lookahead_states = vector_new(sizeof(LookaheadState), 4),
|
2015-11-20 00:01:53 -08:00
|
|
|
.reduce_parents = vector_new(sizeof(TSTree *), 4),
|
2015-09-13 19:47:45 -07:00
|
|
|
};
|
2015-08-16 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_parser_destroy(TSParser *self) {
|
|
|
|
|
ts_stack_delete(self->stack);
|
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-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
2015-11-20 12:55:01 -08:00
|
|
|
TSTree *lookahead = NULL;
|
2015-12-02 07:53:15 -08:00
|
|
|
TSLength last_position = ts_length_zero();
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
for (int head = 0; head < ts_stack_head_count(self->stack);) {
|
2015-12-02 07:53:15 -08:00
|
|
|
StackEntry *entry = ts_stack_head(self->stack, head);
|
|
|
|
|
TSLength position = entry ? entry->position : ts_length_zero();
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2015-11-20 11:49:04 -08:00
|
|
|
LOG("process head:%d, head_count:%d, state:%d, pos:%lu", head,
|
|
|
|
|
ts_stack_head_count(self->stack),
|
2015-12-02 07:53:15 -08:00
|
|
|
ts_stack_top_state(self->stack, head), position.chars);
|
2015-11-18 08:47:15 -08:00
|
|
|
|
2015-11-20 13:10:11 -08:00
|
|
|
if (!ts_parser__can_reuse(self, head, lookahead) ||
|
2015-12-02 07:53:15 -08:00
|
|
|
!ts_length_eq(position, last_position)) {
|
2015-11-20 13:10:11 -08:00
|
|
|
TSTree *reused_lookahead = ts_parser__get_next_lookahead(self, head);
|
|
|
|
|
if (ts_parser__can_reuse(self, head, reused_lookahead)) {
|
|
|
|
|
lookahead = reused_lookahead;
|
|
|
|
|
} else {
|
2015-12-02 07:53:15 -08:00
|
|
|
last_position = position;
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_lexer_reset(&self->lexer, position);
|
2015-11-20 13:10:11 -08:00
|
|
|
TSStateId parse_state = ts_stack_top_state(self->stack, head);
|
|
|
|
|
TSStateId lex_state = self->language->lex_states[parse_state];
|
|
|
|
|
lookahead = self->language->lex_fn(&self->lexer, lex_state);
|
|
|
|
|
}
|
2015-11-18 08:47:15 -08:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-11-20 12:55:01 -08:00
|
|
|
LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol),
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_tree_total_chars(lookahead));
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-11-20 12:55:01 -08:00
|
|
|
switch (ts_parser__consume_lookahead(self, head, lookahead)) {
|
2015-10-07 12:58:32 -07:00
|
|
|
case ConsumeResultRemoved:
|
|
|
|
|
break;
|
|
|
|
|
case ConsumeResultShifted:
|
|
|
|
|
head++;
|
|
|
|
|
break;
|
|
|
|
|
case ConsumeResultFinished:
|
2015-10-14 21:52:13 -07:00
|
|
|
return ts_parser__finish(self);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|