2015-09-19 13:19:49 -07:00
|
|
|
#include "runtime/parser.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"
|
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-10-14 21:52:13 -07:00
|
|
|
#define DEBUG(...) \
|
|
|
|
|
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-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-07-08 17:34:21 -07:00
|
|
|
static const TSParseAction ERROR_ACTIONS[2] = {
|
2015-07-27 18:29:48 -07:00
|
|
|
{.type = TSParseActionTypeError }, {.type = 0 }
|
2015-05-19 10:46:01 -07:00
|
|
|
};
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static const TSParseAction *ts_language__actions(const TSLanguage *language,
|
|
|
|
|
TSStateId state, TSSymbol sym) {
|
2015-07-27 18:29:48 -07:00
|
|
|
const TSParseAction *actions =
|
|
|
|
|
(language->parse_table + (state * language->symbol_count))[sym];
|
2015-07-08 17:34:21 -07:00
|
|
|
return actions ? actions : ERROR_ACTIONS;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 17:53:45 -08:00
|
|
|
static TSParseAction ts_language__last_action(const TSLanguage *language,
|
|
|
|
|
TSStateId state, TSSymbol sym) {
|
|
|
|
|
const TSParseAction *action = ts_language__actions(language, state, sym);
|
|
|
|
|
while ((action + 1)->type)
|
|
|
|
|
action++;
|
|
|
|
|
return *action;
|
2014-10-08 16:49:52 -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-10-14 21:52:13 -07:00
|
|
|
static bool ts_parser__breakdown_reusable_subtree(TSParser *self) {
|
2015-09-13 19:47:45 -07:00
|
|
|
do {
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->reusable_subtree->symbol == ts_builtin_sym_error)
|
2015-09-13 19:47:45 -07:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->reusable_subtree->child_count == 0)
|
2015-09-13 19:47:45 -07:00
|
|
|
return false;
|
2015-10-14 21:52:13 -07:00
|
|
|
self->reusable_subtree = self->reusable_subtree->children[0];
|
|
|
|
|
} while (ts_tree_is_fragile(self->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-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__pop_reusable_subtree(TSParser *self) {
|
|
|
|
|
self->reusable_subtree_pos += ts_tree_total_size(self->reusable_subtree).chars;
|
2015-09-13 19:47:45 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
while (self->reusable_subtree) {
|
|
|
|
|
TSTree *parent = self->reusable_subtree->context.parent;
|
|
|
|
|
size_t next_index = self->reusable_subtree->context.index + 1;
|
2015-09-13 19:47:45 -07:00
|
|
|
if (parent && parent->child_count > next_index) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->reusable_subtree = parent->children[next_index];
|
2015-09-13 19:47:45 -07:00
|
|
|
return;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
2015-10-14 21:52:13 -07:00
|
|
|
self->reusable_subtree = parent;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07: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.
|
|
|
|
|
*/
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__get_next_lookahead(TSParser *self) {
|
|
|
|
|
while (self->reusable_subtree) {
|
|
|
|
|
if (self->reusable_subtree_pos > self->lexer.current_position.chars) {
|
2015-09-13 19:47:45 -07:00
|
|
|
break;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->reusable_subtree_pos < self->lexer.current_position.chars) {
|
|
|
|
|
DEBUG("past_reuse sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
|
|
|
|
ts_parser__pop_reusable_subtree(self);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (ts_tree_has_changes(self->reusable_subtree) ||
|
|
|
|
|
ts_tree_is_fragile(self->reusable_subtree) ||
|
|
|
|
|
ts_tree_is_extra(self->reusable_subtree)) {
|
|
|
|
|
DEBUG("breakdown sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
|
|
|
|
if (!ts_parser__breakdown_reusable_subtree(self))
|
|
|
|
|
ts_parser__pop_reusable_subtree(self);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSStateId top_state = ts_stack_top_state(self->stack, 0);
|
|
|
|
|
TSSymbol symbol = self->reusable_subtree->symbol;
|
2015-11-11 17:53:45 -08:00
|
|
|
if (ts_language__last_action(self->language, top_state, symbol).type ==
|
2015-09-13 19:47:45 -07:00
|
|
|
TSParseActionTypeError) {
|
2015-10-14 21:52:13 -07:00
|
|
|
DEBUG("cant_reuse sym:%s", SYM_NAME(self->reusable_subtree->symbol));
|
|
|
|
|
ts_parser__pop_reusable_subtree(self);
|
2015-09-13 19:47:45 -07:00
|
|
|
continue;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->lookahead = self->reusable_subtree;
|
|
|
|
|
TSLength size = ts_tree_total_size(self->lookahead);
|
2015-11-25 11:28:28 -05:00
|
|
|
TSPoint offset_point = ts_tree_offset_point(self->lookahead);
|
2015-10-14 21:52:13 -07:00
|
|
|
DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(self->lookahead->symbol),
|
|
|
|
|
size.chars, self->lookahead->options.extra);
|
|
|
|
|
ts_lexer_reset(&self->lexer,
|
2015-11-18 16:34:50 -08:00
|
|
|
ts_length_add(self->lexer.current_position, size),
|
2015-11-25 11:28:28 -05:00
|
|
|
ts_point_add(self->lexer.current_point, offset_point));
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__pop_reusable_subtree(self);
|
2015-09-13 19:47:45 -07:00
|
|
|
return;
|
2014-10-14 09:32:11 -07:00
|
|
|
}
|
2014-10-22 12:54:46 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSLength position = self->lexer.current_position;
|
2015-11-18 16:34:50 -08:00
|
|
|
TSPoint point = self->lexer.current_point;
|
2015-10-14 21:52:13 -07:00
|
|
|
for (size_t i = 0, count = ts_stack_head_count(self->stack); i < count; i++) {
|
2015-10-06 16:22:58 -07:00
|
|
|
if (i > 0) {
|
2015-11-18 16:34:50 -08:00
|
|
|
ts_lexer_reset(&self->lexer, position, point);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_tree_release(self->lookahead);
|
2015-10-06 16:22:58 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
TSStateId parse_state = ts_stack_top_state(self->stack, i);
|
|
|
|
|
TSStateId lex_state = self->language->lex_states[parse_state];
|
2015-10-06 16:22:58 -07:00
|
|
|
DEBUG("lex state:%d", lex_state);
|
2015-10-14 21:52:13 -07:00
|
|
|
self->lookahead = self->language->lex_fn(&self->lexer, lex_state);
|
2015-10-06 16:22:58 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->lookahead->symbol != ts_builtin_sym_error)
|
2015-10-06 16:22:58 -07:00
|
|
|
break;
|
|
|
|
|
}
|
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,
|
|
|
|
|
TSStateId parse_state) {
|
2015-11-05 21:21:15 -08:00
|
|
|
if (ts_stack_push(self->stack, head, parse_state, self->lookahead))
|
|
|
|
|
return ConsumeResultRemoved;
|
|
|
|
|
else
|
|
|
|
|
return ConsumeResultShifted;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-05 21:21:15 -08:00
|
|
|
static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state) {
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_tree_set_extra(self->lookahead);
|
2015-11-05 21:21:15 -08:00
|
|
|
return ts_parser__shift(self, head, state);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
2015-08-16 19:53:34 -07:00
|
|
|
size_t child_count, bool extra,
|
|
|
|
|
bool count_extra) {
|
2015-10-14 21:52:13 -07:00
|
|
|
TSNodeType node_type = self->language->node_types[symbol];
|
2015-09-18 18:04:52 -07:00
|
|
|
StackPopResultList pop_results =
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_pop(self->stack, head, child_count, count_extra);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
TSTree *parent = NULL;
|
|
|
|
|
TSTree **last_children = NULL;
|
|
|
|
|
int last_index = -1;
|
2015-05-27 10:53:02 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
for (int i = 0; i < pop_results.size; i++) {
|
2015-09-18 18:04:52 -07:00
|
|
|
StackPopResult pop_result = pop_results.contents[i];
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
if (pop_result.trees != last_children) {
|
2015-07-27 18:29:48 -07:00
|
|
|
parent = ts_tree_make_node(symbol, pop_result.tree_count,
|
2015-09-05 22:29:17 -07:00
|
|
|
pop_result.trees, node_type);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pop_result.index == last_index) {
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_add_alternative(self->stack, pop_result.index, parent);
|
2015-07-08 17:34:21 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
TSStateId top_state = ts_stack_top_state(self->stack, pop_result.index);
|
2015-07-08 17:34:21 -07:00
|
|
|
TSStateId state;
|
|
|
|
|
|
|
|
|
|
if (extra) {
|
|
|
|
|
ts_tree_set_extra(parent);
|
|
|
|
|
state = top_state;
|
|
|
|
|
} else {
|
2015-11-11 17:53:45 -08:00
|
|
|
state = ts_language__last_action(self->language, top_state, symbol)
|
|
|
|
|
.data.to_state;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_push(self->stack, pop_result.index, state, parent);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
last_index = pop_result.index;
|
|
|
|
|
last_children = pop_result.trees;
|
|
|
|
|
}
|
2015-05-27 10:53:02 -07:00
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__reduce_fragile(TSParser *self, int head, TSSymbol symbol,
|
|
|
|
|
size_t child_count) {
|
2015-07-27 18:29:48 -07:00
|
|
|
TSTree *reduced =
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce(self, head, symbol, child_count, false, false);
|
2015-06-15 15:24:15 -07:00
|
|
|
ts_tree_set_fragile_left(reduced);
|
|
|
|
|
ts_tree_set_fragile_right(reduced);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_parser__reduce_error(TSParser *self, int head,
|
2015-08-16 19:53:34 -07:00
|
|
|
size_t child_count) {
|
2015-10-14 21:52:13 -07:00
|
|
|
TSTree *reduced = ts_parser__reduce(self, head, ts_builtin_sym_error,
|
2015-08-16 19:53:34 -07:00
|
|
|
child_count, false, true);
|
2015-10-14 21:52:13 -07:00
|
|
|
reduced->size = ts_length_add(reduced->size, self->lookahead->padding);
|
|
|
|
|
self->lookahead->padding = ts_length_zero();
|
2015-05-27 10:53:02 -07:00
|
|
|
ts_tree_set_fragile_left(reduced);
|
|
|
|
|
ts_tree_set_fragile_right(reduced);
|
2015-02-21 10:41:23 -08:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static bool ts_parser__handle_error(TSParser *self, int head) {
|
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-09-18 18:04:52 -07:00
|
|
|
entry = ts_stack_entry_next(entry, head), i++) {
|
2015-11-11 16:56:58 -08:00
|
|
|
TSStateId stack_state = entry ? entry->state : 0;
|
2015-11-11 17:53:45 -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-11 17:53:45 -08:00
|
|
|
TSParseAction action_after_error = ts_language__last_action(
|
2015-10-14 21:52:13 -07:00
|
|
|
self->language, state_after_error, self->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-07-27 18:29:48 -07:00
|
|
|
DEBUG("recover state:%u, count:%lu", state_after_error,
|
|
|
|
|
error_token_count + i);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce_error(self, head, error_token_count + i);
|
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-10-14 21:52:13 -07:00
|
|
|
DEBUG("skip token:%s", SYM_NAME(self->lookahead->symbol));
|
|
|
|
|
ts_parser__shift(self, head, ts_stack_top_state(self->stack, head));
|
|
|
|
|
self->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-10-14 21:52:13 -07:00
|
|
|
if (self->lookahead->symbol == ts_builtin_sym_end) {
|
2015-06-12 13:13:43 -07:00
|
|
|
DEBUG("fail_to_recover");
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce_error(self, head, error_token_count - 1);
|
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-09-13 19:47:45 -07:00
|
|
|
DEBUG("parse_after_edit");
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG("new_parse");
|
|
|
|
|
}
|
2015-09-18 23:20:06 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->lexer.input = input;
|
2015-11-18 16:34:50 -08:00
|
|
|
ts_lexer_reset(&self->lexer, ts_length_zero(), ts_point_zero());
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_clear(self->stack);
|
2015-09-18 23:20:06 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->reusable_subtree = previous_tree;
|
|
|
|
|
self->reusable_subtree_pos = 0;
|
|
|
|
|
self->lookahead = NULL;
|
2015-09-13 19:47:45 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static TSTree *ts_parser__finish(TSParser *self) {
|
|
|
|
|
StackPopResult pop_result = ts_stack_pop(self->stack, 0, -1, true).contents[0];
|
2015-08-22 10:48:34 -07:00
|
|
|
|
|
|
|
|
TSTree **trees = pop_result.trees;
|
|
|
|
|
size_t extra_count = pop_result.tree_count - 1;
|
|
|
|
|
TSTree *root = trees[extra_count];
|
|
|
|
|
|
|
|
|
|
ts_tree_prepend_children(root, extra_count, trees);
|
|
|
|
|
return root;
|
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-10-14 21:52:13 -07:00
|
|
|
static ConsumeResult ts_parser__consume_lookahead(TSParser *self, int head) {
|
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-10-14 21:52:13 -07:00
|
|
|
ts_language__actions(self->language, state, self->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.
|
|
|
|
|
*/
|
|
|
|
|
while (next_action->type != 0) {
|
|
|
|
|
TSParseAction action = *next_action;
|
|
|
|
|
next_action++;
|
|
|
|
|
|
|
|
|
|
int current_head;
|
|
|
|
|
if (next_action->type == 0) {
|
|
|
|
|
current_head = head;
|
|
|
|
|
DEBUG("action current_head:%d, state:%d", current_head, state);
|
|
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
current_head = ts_stack_split(self->stack, head);
|
2015-10-07 12:58:32 -07:00
|
|
|
DEBUG("split_action from_head:%d, current_head:%d, state:%d", head,
|
|
|
|
|
current_head, state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Remove this by making a separate symbol for errors returned from
|
|
|
|
|
// the lexer.
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->lookahead->symbol == ts_builtin_sym_error)
|
2015-10-07 12:58:32 -07:00
|
|
|
action.type = TSParseActionTypeError;
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case TSParseActionTypeError:
|
|
|
|
|
DEBUG("error_sym");
|
2015-10-14 21:52:13 -07:00
|
|
|
if (ts_stack_head_count(self->stack) == 1) {
|
|
|
|
|
if (ts_parser__handle_error(self, current_head))
|
2015-10-07 12:58:32 -07:00
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
return ConsumeResultFinished;
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG("bail current_head:%d", current_head);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_stack_remove_head(self->stack, current_head);
|
2015-10-07 12:58:32 -07:00
|
|
|
return ConsumeResultRemoved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeShift:
|
|
|
|
|
DEBUG("shift state:%u", action.data.to_state);
|
2015-11-05 21:21:15 -08:00
|
|
|
return ts_parser__shift(self, current_head, action.data.to_state);
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeShiftExtra:
|
|
|
|
|
DEBUG("shift_extra");
|
2015-11-05 21:21:15 -08:00
|
|
|
return ts_parser__shift_extra(self, current_head, state);
|
2015-10-07 12:58:32 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
|
|
|
|
DEBUG("reduce sym:%s, child_count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce(self, current_head, action.data.symbol,
|
2015-10-07 12:58:32 -07:00
|
|
|
action.data.child_count, false, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceExtra:
|
|
|
|
|
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce(self, current_head, action.data.symbol, 1, true,
|
2015-10-07 12:58:32 -07:00
|
|
|
false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceFragile:
|
|
|
|
|
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_parser__reduce_fragile(self, current_head, action.data.symbol,
|
2015-10-07 12:58:32 -07:00
|
|
|
action.data.child_count);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeAccept:
|
|
|
|
|
DEBUG("accept");
|
|
|
|
|
return ConsumeResultFinished;
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 16:54:00 -08:00
|
|
|
static int ts_tree__compare(TSTree *left, TSTree *right) {
|
|
|
|
|
if (left->symbol < right->symbol) return -1;
|
|
|
|
|
if (right->symbol < left->symbol) return 1;
|
|
|
|
|
if (left->child_count < right->child_count) return -1;
|
|
|
|
|
if (right->child_count < left->child_count) return 1;
|
|
|
|
|
for (size_t i = 0; i < left->child_count; i++) {
|
|
|
|
|
TSTree *left_child = left->children[i];
|
|
|
|
|
TSTree *right_child = right->children[i];
|
|
|
|
|
switch (ts_tree__compare(left_child, right_child)) {
|
|
|
|
|
case -1:
|
|
|
|
|
return -1;
|
|
|
|
|
case 1:
|
|
|
|
|
return 1;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-13 19:47:45 -07:00
|
|
|
static TSTree *ts_parser__select_tree(void *data, TSTree *left, TSTree *right) {
|
2015-11-11 16:54:00 -08:00
|
|
|
if (ts_tree__compare(left, right) <= 0)
|
|
|
|
|
return left;
|
|
|
|
|
else
|
|
|
|
|
return right;
|
2015-09-13 19:47:45 -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,
|
|
|
|
|
}),
|
|
|
|
|
.lookahead = NULL,
|
|
|
|
|
};
|
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);
|
|
|
|
|
if (self->lookahead)
|
|
|
|
|
ts_tree_release(self->lookahead);
|
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-10-14 21:52:13 -07:00
|
|
|
ts_parser__get_next_lookahead(self);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2015-09-20 23:41:40 -07:00
|
|
|
DEBUG("lookahead sym:%s, pos:%lu, head_count:%d",
|
2015-10-14 21:52:13 -07:00
|
|
|
SYM_NAME(self->lookahead->symbol), self->lexer.current_position.chars,
|
|
|
|
|
ts_stack_head_count(self->stack));
|
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);) {
|
|
|
|
|
switch (ts_parser__consume_lookahead(self, head)) {
|
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
|
|
|
}
|