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__); \
|
2015-09-08 23:24:33 -07:00
|
|
|
parser->lexer.debugger.debug_fn(parser->lexer.debugger.payload, \
|
2014-10-17 17:52:54 -07:00
|
|
|
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
|
|
|
|
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-08-16 19:53:34 -07:00
|
|
|
static TSParseAction ts_language__action(const TSLanguage *language,
|
|
|
|
|
TSStateId state, TSSymbol sym) {
|
|
|
|
|
return ts_language__actions(language, state, sym)[0];
|
2014-10-08 16:49:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static TSLength ts_parser__break_down_left(TSParser *parser, TSInputEdit edit) {
|
2014-10-09 14:02:03 -07:00
|
|
|
ts_stack_shrink(&parser->right_stack, 0);
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
TSLength prev_size =
|
|
|
|
|
ts_tree_total_size(ts_parse_stack_top_tree(parser->stack, 0));
|
2014-10-09 14:02:03 -07:00
|
|
|
parser->total_chars =
|
2015-07-27 18:29:48 -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 (;;) {
|
2015-06-18 15:04:03 -07:00
|
|
|
ParseStackEntry *entry = ts_parse_stack_head(parser->stack, 0);
|
|
|
|
|
if (!entry)
|
2014-07-20 21:43:27 -07:00
|
|
|
break;
|
2015-06-18 15:04:03 -07:00
|
|
|
TSTree *node = entry->tree;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2015-08-14 16:11:37 -07:00
|
|
|
size_t child_count = node->child_count;
|
|
|
|
|
TSTree **children = node->children;
|
|
|
|
|
if (node->symbol == ts_builtin_sym_error)
|
|
|
|
|
child_count = 0;
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
if (left_subtree_end.chars < edit.position && !children &&
|
|
|
|
|
node->symbol != ts_builtin_sym_error)
|
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),
|
2015-06-18 15:04:03 -07:00
|
|
|
ts_parse_stack_top_state(parser->stack, 0));
|
|
|
|
|
ts_parse_stack_shrink(parser->stack, 0, 1);
|
2014-10-12 11:47:00 -07:00
|
|
|
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];
|
2015-06-18 15:04:03 -07:00
|
|
|
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
2015-08-16 19:53:34 -07:00
|
|
|
TSParseAction action =
|
|
|
|
|
ts_language__action(parser->language, state, child->symbol);
|
2014-09-03 18:48:10 -07:00
|
|
|
TSStateId next_state =
|
2015-07-27 18:29:48 -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);
|
2015-06-18 15:04:03 -07:00
|
|
|
ts_parse_stack_push(parser->stack, 0, next_state, child);
|
2014-10-12 11:47:00 -07:00
|
|
|
left_subtree_end =
|
2015-07-27 18:29:48 -07:00
|
|
|
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,
|
2015-06-18 15:04:03 -07:00
|
|
|
ts_parse_stack_top_state(parser->stack, 0));
|
2014-10-12 11:47:00 -07:00
|
|
|
return left_subtree_end;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static TSTree *ts_parser__break_down_right(TSParser *parser) {
|
2014-10-14 09:32:11 -07:00
|
|
|
TSStack *stack = &parser->right_stack;
|
|
|
|
|
TSLength current_position = parser->lexer.current_position;
|
2015-06-18 15:04:03 -07:00
|
|
|
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
2014-10-14 09:32:11 -07:00
|
|
|
|
|
|
|
|
size_t right_subtree_start =
|
2015-07-27 18:29:48 -07:00
|
|
|
parser->total_chars - ts_stack_total_tree_size(stack).chars;
|
2014-10-14 09:32:11 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
TSTree *node = ts_stack_top_node(stack);
|
|
|
|
|
if (!node)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (right_subtree_start > current_position.chars)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
TSParseAction action =
|
|
|
|
|
ts_language__action(parser->language, state, node->symbol);
|
2014-10-17 23:47:20 -07:00
|
|
|
bool is_usable = (action.type != TSParseActionTypeError) &&
|
2015-07-27 18:29:48 -07:00
|
|
|
!ts_tree_is_extra(node) && !ts_tree_is_empty(node) &&
|
2015-06-15 15:24:15 -07:00
|
|
|
!ts_tree_is_fragile_left(node) &&
|
|
|
|
|
!ts_tree_is_fragile_right(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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-14 16:11:37 -07:00
|
|
|
size_t child_count = node->child_count;
|
|
|
|
|
TSTree **children = node->children;
|
2014-10-14 09:32:11 -07:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static TSTree *ts_parser__next_node(TSParser *parser, TSStateId lex_state) {
|
2014-10-14 09:32:11 -07:00
|
|
|
TSTree *node;
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
if ((node = ts_parser__break_down_right(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 =
|
2015-07-27 18:29:48 -07:00
|
|
|
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 =
|
2015-07-27 18:29:48 -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-13 01:02:12 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Actions
|
|
|
|
|
*/
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static void ts_parser__shift(TSParser *parser, int head, TSStateId parse_state) {
|
2015-07-08 17:34:21 -07:00
|
|
|
ts_parse_stack_push(parser->stack, head, parse_state, parser->lookahead);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static void ts_parser__shift_extra(TSParser *parser, int head, TSStateId state) {
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_tree_set_extra(parser->lookahead);
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__shift(parser, head, state);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static TSTree *ts_parser__reduce(TSParser *parser, int head, TSSymbol symbol,
|
|
|
|
|
size_t child_count, bool extra,
|
|
|
|
|
bool count_extra) {
|
2015-09-05 22:29:17 -07:00
|
|
|
TSNodeType node_type = parser->language->node_types[symbol];
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseStackPopResultList pop_results =
|
|
|
|
|
ts_parse_stack_pop(parser->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++) {
|
|
|
|
|
ParseStackPopResult pop_result = pop_results.contents[i];
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
ts_parse_stack_add_alternative(parser->stack, pop_result.index, parent);
|
|
|
|
|
} else {
|
2015-07-27 18:29:48 -07:00
|
|
|
TSStateId top_state =
|
|
|
|
|
ts_parse_stack_top_state(parser->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-08-16 19:53:34 -07:00
|
|
|
state = ts_language__action(parser->language, top_state, symbol)
|
|
|
|
|
.data.to_state;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_parse_stack_push(parser->stack, pop_result.index, state, parent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
last_index = pop_result.index;
|
|
|
|
|
last_children = pop_result.trees;
|
|
|
|
|
}
|
2015-05-27 10:53:02 -07:00
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static void ts_parser__reduce_fragile(TSParser *parser, int head,
|
|
|
|
|
TSSymbol symbol, size_t child_count) {
|
2015-07-27 18:29:48 -07:00
|
|
|
TSTree *reduced =
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce(parser, 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-08-16 19:53:34 -07:00
|
|
|
static void ts_parser__reduce_error(TSParser *parser, int head,
|
|
|
|
|
size_t child_count) {
|
|
|
|
|
TSTree *reduced = ts_parser__reduce(parser, head, ts_builtin_sym_error,
|
|
|
|
|
child_count, false, true);
|
2015-06-15 15:24:15 -07:00
|
|
|
reduced->size = ts_length_add(reduced->size, parser->lookahead->padding);
|
|
|
|
|
parser->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-08-16 19:53:34 -07:00
|
|
|
static bool ts_parser__handle_error(TSParser *parser, int head) {
|
2015-06-18 15:04:03 -07:00
|
|
|
size_t error_token_count = 1;
|
2015-07-08 17:34:21 -07:00
|
|
|
ParseStackEntry *entry_before_error = ts_parse_stack_head(parser->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-07-27 18:29:48 -07:00
|
|
|
for (ParseStackEntry *entry = entry_before_error; entry != NULL;
|
2015-07-10 15:17:54 -07:00
|
|
|
entry = ts_parse_stack_entry_next(entry, head), i++) {
|
2015-06-18 15:04:03 -07:00
|
|
|
TSStateId stack_state = entry->state;
|
2015-08-16 19:53:34 -07:00
|
|
|
TSParseAction action_on_error = ts_language__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;
|
2015-08-16 19:53:34 -07:00
|
|
|
TSParseAction action_after_error = ts_language__action(
|
2015-07-27 18:29:48 -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) {
|
2015-07-27 18:29:48 -07:00
|
|
|
DEBUG("recover state:%u, count:%lu", state_after_error,
|
|
|
|
|
error_token_count + i);
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce_error(parser, 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
|
|
|
}
|
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-06-15 15:24:15 -07:00
|
|
|
DEBUG("skip token:%s", SYM_NAME(parser->lookahead->symbol));
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__shift(parser, head,
|
|
|
|
|
ts_parse_stack_top_state(parser->stack, head));
|
|
|
|
|
parser->lookahead = ts_parser__next_node(parser, 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-06-12 13:13:43 -07:00
|
|
|
if (parser->lookahead->symbol == ts_builtin_sym_end) {
|
|
|
|
|
DEBUG("fail_to_recover");
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce_error(parser, 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-08-16 19:53:34 -07:00
|
|
|
static TSTree *ts_parser__finish(TSParser *parser) {
|
2015-08-22 10:48:34 -07:00
|
|
|
ParseStackPopResult pop_result =
|
|
|
|
|
ts_parse_stack_pop(parser->stack, 0, -1, true).contents[0];
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
ts_parse_stack_push(parser->stack, 0, 0, root);
|
|
|
|
|
return root;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
typedef enum {
|
|
|
|
|
ParserNextResultNone,
|
|
|
|
|
ParserNextResultAdvanced,
|
|
|
|
|
ParserNextResultRemoved,
|
|
|
|
|
ParserNextResultFinished
|
|
|
|
|
} ParserNextResult;
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static ParserNextResult ts_parser__next(TSParser *parser, int head_to_advance) {
|
2015-07-08 17:34:21 -07:00
|
|
|
TSStateId state = ts_parse_stack_top_state(parser->stack, head_to_advance);
|
2015-07-27 18:29:48 -07:00
|
|
|
const TSParseAction *next_action =
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_language__actions(parser->language, state, parser->lookahead->symbol);
|
2015-07-08 17:34:21 -07:00
|
|
|
int head, next_head = head_to_advance;
|
|
|
|
|
|
|
|
|
|
ParserNextResult result = ParserNextResultNone;
|
|
|
|
|
|
|
|
|
|
while (next_action) {
|
|
|
|
|
TSParseAction action = *next_action;
|
|
|
|
|
head = next_head;
|
|
|
|
|
|
|
|
|
|
next_action++;
|
|
|
|
|
if (next_action->type == 0) {
|
|
|
|
|
next_action = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
next_head = ts_parse_stack_split(parser->stack, head);
|
|
|
|
|
DEBUG("split head:%d, created:%d", head, next_head);
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
DEBUG("iteration state:%d, head:%d", state, head);
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
// TODO: Remove this by making a separate symbol for errors returned from
|
|
|
|
|
// the lexer.
|
|
|
|
|
if (parser->lookahead->symbol == ts_builtin_sym_error)
|
|
|
|
|
action.type = TSParseActionTypeError;
|
2014-10-08 16:49:52 -07:00
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
switch (action.type) {
|
2015-07-08 17:34:21 -07:00
|
|
|
case TSParseActionTypeError:
|
|
|
|
|
DEBUG("error_sym");
|
|
|
|
|
if (ts_parse_stack_head_count(parser->stack) == 1) {
|
2015-08-16 19:53:34 -07:00
|
|
|
if (ts_parser__handle_error(parser, head))
|
2015-07-08 17:34:21 -07:00
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
return ParserNextResultFinished;
|
2014-08-27 18:41:48 -07:00
|
|
|
} else {
|
2015-07-08 17:34:21 -07:00
|
|
|
DEBUG("bail head:%d", head);
|
|
|
|
|
ts_parse_stack_remove_head(parser->stack, head);
|
|
|
|
|
return ParserNextResultRemoved;
|
2014-08-27 18:41:48 -07:00
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
case TSParseActionTypeShift:
|
|
|
|
|
DEBUG("shift state:%u", action.data.to_state);
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__shift(parser, head, action.data.to_state);
|
2015-07-08 17:34:21 -07:00
|
|
|
result = ParserNextResultAdvanced;
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeShiftExtra:
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("shift_extra");
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__shift_extra(parser, head, state);
|
2015-07-08 17:34:21 -07:00
|
|
|
result = ParserNextResultAdvanced;
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
2015-07-27 18:29:48 -07:00
|
|
|
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce(parser, head, action.data.symbol,
|
|
|
|
|
action.data.child_count, false, false);
|
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));
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce(parser, head, action.data.symbol, 1, true, false);
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
2015-02-21 10:41:23 -08:00
|
|
|
case TSParseActionTypeReduceFragile:
|
2015-07-27 18:29:48 -07:00
|
|
|
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol),
|
|
|
|
|
action.data.child_count);
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__reduce_fragile(parser, head, action.data.symbol,
|
|
|
|
|
action.data.child_count);
|
2015-02-21 10:41:23 -08:00
|
|
|
break;
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
case TSParseActionTypeAccept:
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("accept");
|
2015-07-08 17:34:21 -07:00
|
|
|
return ParserNextResultFinished;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
/*
|
|
|
|
|
* Public
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
TSTree *ts_parser_select_tree(void *data, TSTree *left, TSTree *right) {
|
|
|
|
|
return left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSParser ts_parser_make() {
|
|
|
|
|
return (TSParser){.lexer = ts_lexer_make(),
|
|
|
|
|
.stack = ts_parse_stack_new(
|
|
|
|
|
(TreeSelectionCallback){ NULL, ts_parser_select_tree }),
|
|
|
|
|
.right_stack = ts_stack_make() };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parser_destroy(TSParser *parser) {
|
|
|
|
|
ts_parse_stack_delete(parser->stack);
|
|
|
|
|
ts_stack_delete(&parser->right_stack);
|
|
|
|
|
if (parser->lookahead)
|
|
|
|
|
ts_tree_release(parser->lookahead);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-08 23:33:43 -07:00
|
|
|
TSDebugger ts_parser_debugger(const TSParser *parser) {
|
2015-08-16 19:53:34 -07:00
|
|
|
return parser->lexer.debugger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parser_set_debugger(TSParser *parser, TSDebugger debugger) {
|
|
|
|
|
parser->lexer.debugger = debugger;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 15:47:48 -07:00
|
|
|
TSTree *ts_parser_parse(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
2015-07-08 17:34:21 -07:00
|
|
|
TSLength position;
|
|
|
|
|
if (edit) {
|
|
|
|
|
DEBUG("edit pos:%lu, inserted:%lu, deleted:%lu", edit->position,
|
|
|
|
|
edit->chars_inserted, edit->chars_removed);
|
2015-08-16 19:53:34 -07:00
|
|
|
position = ts_parser__break_down_left(parser, *edit);
|
2015-07-08 17:34:21 -07:00
|
|
|
} else {
|
|
|
|
|
DEBUG("new_parse");
|
|
|
|
|
ts_parse_stack_clear(parser->stack);
|
|
|
|
|
position = ts_length_zero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser->lexer.input = input;
|
|
|
|
|
ts_lexer_reset(&parser->lexer, position);
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
2015-07-27 18:29:48 -07:00
|
|
|
parser->lookahead =
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_parser__next_node(parser, parser->language->lex_states[state]);
|
2015-07-08 17:34:21 -07:00
|
|
|
|
|
|
|
|
DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol));
|
|
|
|
|
DEBUG("head_count: %d", ts_parse_stack_head_count(parser->stack));
|
|
|
|
|
|
|
|
|
|
int head = 0;
|
|
|
|
|
while (head < ts_parse_stack_head_count(parser->stack)) {
|
|
|
|
|
bool removed = false, advanced = false;
|
|
|
|
|
|
|
|
|
|
while (!(advanced || removed)) {
|
2015-08-16 19:53:34 -07:00
|
|
|
switch (ts_parser__next(parser, head)) {
|
2015-07-08 17:34:21 -07:00
|
|
|
case ParserNextResultNone:
|
|
|
|
|
break;
|
|
|
|
|
case ParserNextResultRemoved:
|
|
|
|
|
removed = true;
|
|
|
|
|
break;
|
|
|
|
|
case ParserNextResultAdvanced:
|
|
|
|
|
advanced = true;
|
|
|
|
|
break;
|
|
|
|
|
case ParserNextResultFinished:
|
2015-08-16 19:53:34 -07:00
|
|
|
return ts_parser__finish(parser);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
if (!removed)
|
|
|
|
|
head++;
|
2014-08-09 01:03:55 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|