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"
|
|
|
|
|
#include "runtime/stack.h"
|
|
|
|
|
#include "runtime/parser.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2014-10-14 22:50:24 -07:00
|
|
|
#include "runtime/debugger.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
|
|
|
*/
|
|
|
|
|
|
2014-10-17 17:52:54 -07:00
|
|
|
#define DEBUG(...) \
|
|
|
|
|
if (parser->lexer.debugger.debug_fn) { \
|
2014-10-13 01:02:12 -07:00
|
|
|
snprintf(parser->lexer.debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
2014-10-17 17:52:54 -07:00
|
|
|
parser->lexer.debugger.debug_fn(parser->lexer.debugger.data, \
|
|
|
|
|
TSDebugTypeParse, \
|
|
|
|
|
parser->lexer.debug_buffer); \
|
2014-08-27 18:27:53 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 21:20:08 -07:00
|
|
|
#define SYM_NAME(sym) parser->language->symbol_names[sym]
|
|
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
/*
|
|
|
|
|
* Private
|
|
|
|
|
*/
|
2014-10-09 14:02:03 -07:00
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
static TSParseAction get_action(const TSLanguage *language, TSStateId state,
|
|
|
|
|
TSSymbol sym) {
|
|
|
|
|
return (language->parse_table + (state * language->symbol_count))[sym];
|
2014-10-08 16:49:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-14 10:31:18 -07:00
|
|
|
static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
2014-10-09 14:02:03 -07:00
|
|
|
ts_stack_shrink(&parser->right_stack, 0);
|
|
|
|
|
|
2014-10-12 11:47:00 -07:00
|
|
|
TSLength prev_size = ts_stack_total_tree_size(&parser->stack);
|
2014-10-09 14:02:03 -07:00
|
|
|
parser->total_chars =
|
2014-10-14 10:31:18 -07:00
|
|
|
prev_size.chars + edit.chars_inserted - edit.chars_removed;
|
2014-10-12 11:47:00 -07:00
|
|
|
TSLength left_subtree_end = prev_size;
|
|
|
|
|
size_t right_subtree_start = parser->total_chars;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
2014-10-12 11:47:00 -07:00
|
|
|
TSTree *node = ts_stack_top_node(&parser->stack);
|
2014-07-20 21:43:27 -07:00
|
|
|
if (!node)
|
|
|
|
|
break;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
size_t child_count;
|
|
|
|
|
TSTree **children = ts_tree_children(node, &child_count);
|
2014-10-14 10:31:18 -07:00
|
|
|
if (left_subtree_end.chars < edit.position && !children)
|
2014-07-20 21:43:27 -07:00
|
|
|
break;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("pop_left sym:%s, state:%u", SYM_NAME(node->symbol),
|
2014-10-22 12:54:46 -07:00
|
|
|
ts_stack_top_state(&parser->stack));
|
2014-10-12 11:47:00 -07:00
|
|
|
parser->stack.size--;
|
|
|
|
|
left_subtree_end = ts_length_sub(left_subtree_end, ts_tree_total_size(node));
|
2014-09-03 22:35:52 -07:00
|
|
|
|
2014-10-09 14:02:03 -07:00
|
|
|
size_t i = 0;
|
2014-10-14 10:31:18 -07:00
|
|
|
for (; i < child_count && left_subtree_end.chars < edit.position; i++) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSTree *child = children[i];
|
2014-10-12 11:47:00 -07:00
|
|
|
TSStateId state = ts_stack_top_state(&parser->stack);
|
2014-10-14 09:32:11 -07:00
|
|
|
TSParseAction action = get_action(parser->language, state, child->symbol);
|
2014-09-03 18:48:10 -07:00
|
|
|
TSStateId next_state =
|
2014-10-22 18:44:52 -07:00
|
|
|
ts_tree_is_extra(child) ? state : action.data.to_state;
|
2014-10-05 16:56:29 -07:00
|
|
|
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("push_left sym:%s, state:%u", SYM_NAME(child->symbol), next_state);
|
2014-10-12 11:47:00 -07:00
|
|
|
ts_stack_push(&parser->stack, next_state, child);
|
|
|
|
|
left_subtree_end =
|
|
|
|
|
ts_length_add(left_subtree_end, ts_tree_total_size(child));
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-10 12:10:23 -07:00
|
|
|
for (size_t j = child_count - 1; j + 1 > i; j--) {
|
2014-10-09 14:02:03 -07:00
|
|
|
TSTree *child = children[j];
|
2014-10-12 11:47:00 -07:00
|
|
|
right_subtree_start -= ts_tree_total_size(child).chars;
|
2014-10-14 10:31:18 -07:00
|
|
|
if (right_subtree_start < edit.position + edit.chars_inserted)
|
2014-10-12 11:47:00 -07:00
|
|
|
break;
|
|
|
|
|
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("push_right sym:%s", SYM_NAME(child->symbol));
|
2014-10-09 14:02:03 -07:00
|
|
|
ts_stack_push(&parser->right_stack, 0, child);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_tree_release(node);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("reuse_left chars:%lu, state:%u", left_subtree_end.chars,
|
2014-10-22 12:54:46 -07:00
|
|
|
ts_stack_top_state(&parser->stack));
|
2014-10-12 11:47:00 -07:00
|
|
|
return left_subtree_end;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
static TSTree *break_down_right_stack(TSParser *parser) {
|
|
|
|
|
TSStack *stack = &parser->right_stack;
|
|
|
|
|
TSLength current_position = parser->lexer.current_position;
|
2014-10-09 14:02:03 -07:00
|
|
|
TSStateId state = ts_stack_top_state(&parser->stack);
|
2014-10-14 09:32:11 -07:00
|
|
|
|
|
|
|
|
size_t right_subtree_start =
|
|
|
|
|
parser->total_chars - ts_stack_total_tree_size(stack).chars;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
TSTree *node = ts_stack_top_node(stack);
|
|
|
|
|
if (!node)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (right_subtree_start > current_position.chars)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
TSParseAction action = get_action(parser->language, state, node->symbol);
|
2014-10-17 23:47:20 -07:00
|
|
|
bool is_usable = (action.type != TSParseActionTypeError) &&
|
2014-11-05 18:39:50 -08:00
|
|
|
!ts_tree_has_error(node) && !ts_tree_is_extra(node);
|
2014-10-14 09:32:11 -07:00
|
|
|
if (is_usable && right_subtree_start == current_position.chars) {
|
|
|
|
|
ts_stack_shrink(&parser->right_stack, parser->right_stack.size - 1);
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t child_count;
|
|
|
|
|
TSTree **children = ts_tree_children(node, &child_count);
|
|
|
|
|
|
2014-10-22 12:54:46 -07:00
|
|
|
DEBUG("pop_right sym:%s", SYM_NAME(node->symbol));
|
2014-10-14 09:32:11 -07:00
|
|
|
stack->size--;
|
|
|
|
|
right_subtree_start += ts_tree_total_size(node).chars;
|
|
|
|
|
|
|
|
|
|
for (size_t i = child_count - 1; i + 1 > 0; i--) {
|
|
|
|
|
if (right_subtree_start <= current_position.chars)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
TSTree *child = children[i];
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("push_right sym:%s", SYM_NAME(child->symbol));
|
2014-10-14 09:32:11 -07:00
|
|
|
ts_stack_push(stack, 0, child);
|
|
|
|
|
right_subtree_start -= ts_tree_total_size(child).chars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_tree_release(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) {
|
|
|
|
|
TSTree *node;
|
|
|
|
|
|
|
|
|
|
if ((node = break_down_right_stack(parser))) {
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("reuse sym:%s, is_extra:%u, size:%lu", SYM_NAME(node->symbol),
|
2014-10-22 12:54:46 -07:00
|
|
|
ts_tree_is_extra(node), ts_tree_total_size(node).chars);
|
2014-10-14 09:32:11 -07:00
|
|
|
|
2014-10-17 23:47:20 -07:00
|
|
|
parser->lexer.token_start_position =
|
|
|
|
|
ts_length_add(parser->lexer.current_position, node->padding);
|
2014-10-17 23:20:36 -07:00
|
|
|
parser->lexer.token_end_position = parser->lexer.current_position =
|
2014-10-17 23:47:20 -07:00
|
|
|
ts_length_add(parser->lexer.token_start_position, node->size);
|
2014-10-22 12:54:46 -07:00
|
|
|
|
|
|
|
|
parser->lexer.lookahead = 0;
|
|
|
|
|
parser->lexer.lookahead_size = 0;
|
2014-10-22 18:44:52 -07:00
|
|
|
parser->lexer.advance_fn(&parser->lexer, 0);
|
2014-10-09 14:02:03 -07:00
|
|
|
} else {
|
2014-10-14 09:32:11 -07:00
|
|
|
node = parser->language->lex_fn(&parser->lexer, lex_state);
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
2014-10-14 09:32:11 -07:00
|
|
|
|
|
|
|
|
return node;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
static void resize_error(TSParser *parser, TSTree *error) {
|
2014-10-14 09:32:11 -07:00
|
|
|
TSLength distance = ts_length_sub(parser->lexer.token_start_position,
|
|
|
|
|
ts_stack_total_tree_size(&parser->stack));
|
|
|
|
|
error->size = ts_length_sub(distance, error->padding);
|
2014-10-08 16:49:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Actions
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
static void shift(TSParser *parser, TSStateId parse_state) {
|
2014-07-20 20:27:33 -07:00
|
|
|
if (ts_tree_is_extra(parser->lookahead))
|
|
|
|
|
parse_state = ts_stack_top_state(&parser->stack);
|
2014-10-13 01:02:12 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_stack_push(&parser->stack, parse_state, parser->lookahead);
|
|
|
|
|
parser->lookahead = parser->next_lookahead;
|
|
|
|
|
parser->next_lookahead = NULL;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
static void shift_extra(TSParser *parser) {
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_tree_set_extra(parser->lookahead);
|
2014-08-09 01:03:55 -07:00
|
|
|
shift(parser, 0);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
static void reduce(TSParser *parser, TSSymbol symbol, size_t child_count) {
|
|
|
|
|
TSStack *stack = &parser->stack;
|
2014-07-20 20:27:33 -07:00
|
|
|
parser->next_lookahead = parser->lookahead;
|
2014-08-09 01:03:55 -07:00
|
|
|
|
2014-08-25 23:02:43 -07:00
|
|
|
/*
|
|
|
|
|
* Walk down the stack to determine which symbols will be reduced.
|
|
|
|
|
* The child node count is known ahead of time, but some children
|
|
|
|
|
* may be ubiquitous tokens, which don't count.
|
|
|
|
|
*/
|
2014-09-02 07:41:29 -07:00
|
|
|
for (size_t i = 0; i < child_count; i++) {
|
|
|
|
|
if (child_count == stack->size)
|
|
|
|
|
break;
|
2014-08-09 01:03:55 -07:00
|
|
|
TSTree *child = stack->entries[stack->size - 1 - i].node;
|
|
|
|
|
if (ts_tree_is_extra(child))
|
|
|
|
|
child_count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t start_index = stack->size - child_count;
|
|
|
|
|
TSTree **children = calloc(child_count, sizeof(TSTree *));
|
|
|
|
|
for (size_t i = 0; i < child_count; i++)
|
|
|
|
|
children[i] = stack->entries[start_index + i].node;
|
|
|
|
|
|
2014-10-14 09:32:11 -07:00
|
|
|
bool hidden = parser->language->hidden_symbol_flags[symbol];
|
2014-08-09 01:03:55 -07:00
|
|
|
parser->lookahead = ts_tree_make_node(symbol, child_count, children, hidden);
|
|
|
|
|
ts_stack_shrink(stack, stack->size - child_count);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
static void reduce_extra(TSParser *parser, TSSymbol symbol) {
|
|
|
|
|
reduce(parser, symbol, 1);
|
|
|
|
|
ts_tree_set_extra(parser->lookahead);
|
2014-10-05 16:56:50 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
static int handle_error(TSParser *parser) {
|
2014-08-26 23:22:18 -07:00
|
|
|
TSTree *error = parser->lookahead;
|
|
|
|
|
ts_tree_retain(error);
|
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
|
|
|
*/
|
2014-09-29 10:16:24 -07:00
|
|
|
TS_STACK_FROM_TOP(parser->stack, entry) {
|
2014-09-29 10:33:56 -07:00
|
|
|
TSStateId stack_state = entry->state;
|
2014-08-25 21:58:01 -07:00
|
|
|
TSParseAction action_on_error =
|
2014-10-14 09:32:11 -07:00
|
|
|
get_action(parser->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;
|
2014-10-14 09:32:11 -07:00
|
|
|
TSParseAction action_after_error = get_action(
|
2014-08-31 21:17:32 -07:00
|
|
|
parser->language, state_after_error, parser->lookahead->symbol);
|
2014-08-25 21:58:01 -07:00
|
|
|
|
2014-09-02 07:41:29 -07:00
|
|
|
if (action_after_error.type != TSParseActionTypeError) {
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("recover state:%u", state_after_error);
|
2014-09-29 10:33:56 -07:00
|
|
|
|
2014-09-29 10:16:24 -07:00
|
|
|
ts_stack_shrink(&parser->stack, entry - parser->stack.entries + 1);
|
2014-09-29 10:33:56 -07:00
|
|
|
parser->lookahead->padding = ts_length_zero();
|
2014-10-05 16:56:50 -07:00
|
|
|
|
|
|
|
|
resize_error(parser, error);
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_stack_push(&parser->stack, state_after_error, error);
|
2014-08-26 23:22:18 -07:00
|
|
|
ts_tree_release(error);
|
2014-07-20 20:27:33 -07:00
|
|
|
return 1;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
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
|
|
|
|
|
* current lookahead token, advance to the next token. If no characters
|
|
|
|
|
* were consumed, advance the lexer to the next character.
|
|
|
|
|
*/
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("skip_token");
|
2014-09-29 10:33:56 -07:00
|
|
|
TSLength prev_position = parser->lexer.current_position;
|
2014-10-08 16:49:52 -07:00
|
|
|
if (parser->lookahead)
|
|
|
|
|
ts_tree_release(parser->lookahead);
|
2014-10-14 09:32:11 -07:00
|
|
|
parser->lookahead = get_next_node(parser, ts_lex_state_error);
|
2014-09-29 10:33:56 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the current lookahead character cannot be the start of any token,
|
|
|
|
|
* just skip it. If the end of input is reached, exit.
|
|
|
|
|
*/
|
2014-09-26 16:15:07 -07:00
|
|
|
if (ts_length_eq(parser->lexer.current_position, prev_position))
|
2014-10-17 16:20:01 -07:00
|
|
|
if (!parser->lexer.advance_fn(&parser->lexer, 0)) {
|
|
|
|
|
DEBUG("fail_to_recover");
|
2014-10-05 16:56:50 -07:00
|
|
|
|
|
|
|
|
resize_error(parser, error);
|
2014-09-02 07:41:29 -07:00
|
|
|
ts_stack_push(&parser->stack, 0, error);
|
|
|
|
|
ts_tree_release(error);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
static TSTree *finish(TSParser *parser) {
|
2014-10-14 09:32:11 -07:00
|
|
|
if (parser->stack.size == 0) {
|
|
|
|
|
TSTree *err = ts_tree_make_error(ts_length_zero(), ts_length_zero(), 0);
|
|
|
|
|
ts_stack_push(&parser->stack, 0, err);
|
|
|
|
|
}
|
2014-08-08 23:58:59 -07:00
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
reduce(parser, ts_builtin_sym_document, parser->stack.size);
|
|
|
|
|
parser->lookahead->options = 0;
|
|
|
|
|
shift(parser, 0);
|
|
|
|
|
return parser->stack.entries[0].node;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
/*
|
|
|
|
|
* Public
|
|
|
|
|
*/
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
TSParser ts_parser_make() {
|
2014-08-09 01:03:55 -07:00
|
|
|
return (TSParser) { .lexer = ts_lexer_make(),
|
|
|
|
|
.stack = ts_stack_make(),
|
2014-10-17 17:52:54 -07:00
|
|
|
.right_stack = ts_stack_make() };
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parser_destroy(TSParser *parser) {
|
2014-10-13 01:02:12 -07:00
|
|
|
ts_stack_delete(&parser->stack);
|
|
|
|
|
ts_stack_delete(&parser->right_stack);
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
if (parser->lookahead)
|
|
|
|
|
ts_tree_release(parser->lookahead);
|
|
|
|
|
if (parser->next_lookahead)
|
|
|
|
|
ts_tree_release(parser->next_lookahead);
|
2014-10-13 01:02:12 -07:00
|
|
|
|
|
|
|
|
if (parser->lexer.debugger.release_fn)
|
|
|
|
|
parser->lexer.debugger.release_fn(parser->lexer.debugger.data);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-17 17:52:54 -07:00
|
|
|
TSDebugger ts_parser_get_debugger(const TSParser *parser) {
|
|
|
|
|
return parser->lexer.debugger;
|
2014-10-13 01:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 17:52:54 -07:00
|
|
|
void ts_parser_set_debugger(TSParser *parser, TSDebugger debugger) {
|
2014-10-18 18:36:39 -07:00
|
|
|
if (parser->lexer.debugger.release_fn)
|
|
|
|
|
parser->lexer.debugger.release_fn(parser->lexer.debugger.data);
|
2014-10-13 01:02:12 -07:00
|
|
|
parser->lexer.debugger = debugger;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const TSTree *ts_parser_parse(TSParser *parser, TSInput input,
|
|
|
|
|
TSInputEdit *edit) {
|
2014-08-09 01:03:55 -07:00
|
|
|
parser->lookahead = NULL;
|
|
|
|
|
parser->next_lookahead = NULL;
|
2014-10-14 10:31:18 -07:00
|
|
|
|
|
|
|
|
TSLength position;
|
|
|
|
|
if (edit) {
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("edit pos:%lu, inserted:%lu, deleted:%lu", edit->position,
|
2014-10-17 17:52:54 -07:00
|
|
|
edit->chars_inserted, edit->chars_removed);
|
2014-10-14 10:31:18 -07:00
|
|
|
position = break_down_left_stack(parser, *edit);
|
|
|
|
|
} else {
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("new_parse");
|
2014-10-14 10:31:18 -07:00
|
|
|
ts_stack_shrink(&parser->stack, 0);
|
|
|
|
|
position = ts_length_zero();
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
|
2014-10-05 16:56:50 -07:00
|
|
|
parser->lexer.input = input;
|
|
|
|
|
ts_lexer_reset(&parser->lexer, position);
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
for (;;) {
|
2014-10-08 16:49:52 -07:00
|
|
|
TSStateId state = ts_stack_top_state(&parser->stack);
|
|
|
|
|
if (!parser->lookahead)
|
2014-10-14 09:32:11 -07:00
|
|
|
parser->lookahead =
|
|
|
|
|
get_next_node(parser, parser->language->lex_states[state]);
|
2014-10-09 14:02:03 -07:00
|
|
|
TSParseAction action =
|
2014-10-14 09:32:11 -07:00
|
|
|
get_action(parser->language, state, parser->lookahead->symbol);
|
2014-10-08 16:49:52 -07:00
|
|
|
|
2014-10-22 12:54:46 -07:00
|
|
|
DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol));
|
2014-08-09 01:03:55 -07:00
|
|
|
switch (action.type) {
|
|
|
|
|
case TSParseActionTypeShift:
|
2014-08-27 18:41:48 -07:00
|
|
|
if (parser->lookahead->symbol == ts_builtin_sym_error) {
|
2014-10-22 12:54:46 -07:00
|
|
|
DEBUG("error_sym");
|
2014-08-27 18:41:48 -07:00
|
|
|
if (!handle_error(parser))
|
2014-10-13 01:02:12 -07:00
|
|
|
return finish(parser);
|
2014-08-27 18:41:48 -07:00
|
|
|
} else {
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("shift state:%u", action.data.to_state);
|
2014-08-27 18:41:48 -07:00
|
|
|
shift(parser, action.data.to_state);
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeShiftExtra:
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("shift_extra");
|
2014-08-09 01:03:55 -07:00
|
|
|
shift_extra(parser);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
|
|
|
|
reduce(parser, action.data.symbol, action.data.child_count);
|
2014-10-29 18:16:34 -07:00
|
|
|
DEBUG("reduce sym:%s, count:%u, state:%u", SYM_NAME(action.data.symbol),
|
2014-10-22 12:54:46 -07:00
|
|
|
action.data.child_count, ts_stack_top_state(&parser->stack));
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceExtra:
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
2014-10-08 16:49:52 -07:00
|
|
|
reduce_extra(parser, action.data.symbol);
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeAccept:
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("accept");
|
2014-10-13 01:02:12 -07:00
|
|
|
return finish(parser);
|
2014-08-09 01:03:55 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeError:
|
2014-10-22 12:54:46 -07:00
|
|
|
DEBUG("error_sym");
|
2014-08-09 01:03:55 -07:00
|
|
|
if (!handle_error(parser))
|
2014-10-13 01:02:12 -07:00
|
|
|
return finish(parser);
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|