2014-07-10 13:14:52 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#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-07-10 13:14:52 -07:00
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
/*
|
|
|
|
|
* Private
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-27 22:06:27 -07:00
|
|
|
#define DEBUG_PARSE(...) \
|
|
|
|
|
if (parser->debug) { \
|
2014-08-27 18:27:53 -07:00
|
|
|
fprintf(stderr, "PARSE " __VA_ARGS__); \
|
2014-08-27 22:06:27 -07:00
|
|
|
fprintf(stderr, "\n"); \
|
2014-08-27 18:27:53 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-31 21:17:32 -07:00
|
|
|
static TSParseAction action_for(const TSLanguage *lang, TSStateId state,
|
|
|
|
|
TSSymbol sym) {
|
|
|
|
|
return (lang->parse_table + (state * lang->symbol_count))[sym];
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
static void lex(TSParser *parser, TSStateId lex_state) {
|
|
|
|
|
parser->lookahead = parser->language->lex_fn(&parser->lexer, lex_state);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
static TSLength breakdown_stack(TSParser *parser, TSInputEdit *edit) {
|
2014-09-03 18:48:10 -07:00
|
|
|
if (!edit) {
|
|
|
|
|
ts_stack_shrink(&parser->stack, 0);
|
2014-09-26 16:31:30 -07:00
|
|
|
return ts_length_zero();
|
2014-09-03 18:48:10 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
TSStack *stack = &parser->stack;
|
2014-10-08 17:37:20 -07:00
|
|
|
TSLength position = ts_stack_total_tree_size(stack);
|
2014-07-20 20:27:33 -07:00
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
TSTree *node = ts_stack_top_node(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-09-26 16:15:07 -07:00
|
|
|
if (position.chars < edit->position && !children)
|
2014-07-20 21:43:27 -07:00
|
|
|
break;
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2014-10-05 16:56:29 -07:00
|
|
|
DEBUG_PARSE("POP %s", parser->language->symbol_names[node->symbol]);
|
2014-07-20 20:27:33 -07:00
|
|
|
stack->size--;
|
2014-09-26 16:15:07 -07:00
|
|
|
position = ts_length_sub(position, ts_tree_total_size(node));
|
2014-09-03 22:35:52 -07:00
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
for (size_t i = 0; i < child_count && position.chars < edit->position; i++) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSTree *child = children[i];
|
|
|
|
|
TSStateId state = ts_stack_top_state(stack);
|
2014-09-03 13:17:06 -07:00
|
|
|
TSParseAction action = action_for(parser->language, state, child->symbol);
|
2014-09-03 18:48:10 -07:00
|
|
|
TSStateId next_state =
|
|
|
|
|
action.type == TSParseActionTypeShift ? action.data.to_state : state;
|
2014-10-05 16:56:29 -07:00
|
|
|
|
|
|
|
|
DEBUG_PARSE("PUT BACK %s", parser->language->symbol_names[child->symbol]);
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_stack_push(stack, next_state, child);
|
2014-09-26 16:15:07 -07:00
|
|
|
position = ts_length_add(position, ts_tree_total_size(child));
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_tree_release(node);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-05 16:56:29 -07:00
|
|
|
DEBUG_PARSE("RESUME %lu", position.chars);
|
2014-07-20 20:27:33 -07:00
|
|
|
return position;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-08 16:49:52 -07:00
|
|
|
static void resize_error(TSParser *parser, TSTree *error) {
|
|
|
|
|
error->size =
|
|
|
|
|
ts_length_sub(ts_length_sub(parser->lexer.token_start_position,
|
2014-10-08 17:37:20 -07:00
|
|
|
ts_stack_total_tree_size(&parser->stack)),
|
2014-10-08 16:49:52 -07:00
|
|
|
error->padding);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
int hidden = parser->language->hidden_symbol_flags[symbol];
|
|
|
|
|
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-09-29 10:33:56 -07:00
|
|
|
action_for(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-08-31 21:17:32 -07:00
|
|
|
TSParseAction action_after_error = action_for(
|
|
|
|
|
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-08-27 18:27:53 -07:00
|
|
|
DEBUG_PARSE("RECOVER %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-09-05 18:38:17 -07:00
|
|
|
DEBUG_PARSE("LEX AGAIN");
|
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-09-02 07:41:29 -07:00
|
|
|
lex(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-09-02 07:41:29 -07:00
|
|
|
if (!ts_lexer_advance(&parser->lexer)) {
|
|
|
|
|
DEBUG_PARSE("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-08-09 01:03:55 -07:00
|
|
|
static TSTree *get_root(TSParser *parser) {
|
|
|
|
|
if (parser->stack.size == 0)
|
2014-10-03 15:44:21 -07:00
|
|
|
ts_stack_push(&parser->stack, 0,
|
|
|
|
|
ts_tree_make_error(ts_length_zero(), ts_length_zero(), 0));
|
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-08-09 01:03:55 -07:00
|
|
|
TSParser ts_parser_make(const TSLanguage *language) {
|
|
|
|
|
return (TSParser) { .lexer = ts_lexer_make(),
|
|
|
|
|
.stack = ts_stack_make(),
|
|
|
|
|
.debug = 0,
|
|
|
|
|
.language = language, };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parser_destroy(TSParser *parser) {
|
|
|
|
|
if (parser->lookahead)
|
|
|
|
|
ts_tree_release(parser->lookahead);
|
|
|
|
|
if (parser->next_lookahead)
|
|
|
|
|
ts_tree_release(parser->next_lookahead);
|
|
|
|
|
ts_stack_delete(&parser->stack);
|
|
|
|
|
}
|
|
|
|
|
|
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-05 16:56:50 -07:00
|
|
|
TSLength position = breakdown_stack(parser, edit);
|
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)
|
|
|
|
|
lex(parser, parser->language->lex_states[state]);
|
|
|
|
|
TSParseAction action = action_for(parser->language, state, parser->lookahead->symbol);
|
|
|
|
|
|
2014-08-09 01:03:55 -07:00
|
|
|
DEBUG_PARSE("LOOKAHEAD %s",
|
|
|
|
|
parser->language->symbol_names[parser->lookahead->symbol]);
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case TSParseActionTypeShift:
|
2014-08-27 18:41:48 -07:00
|
|
|
if (parser->lookahead->symbol == ts_builtin_sym_error) {
|
|
|
|
|
if (!handle_error(parser))
|
|
|
|
|
return get_root(parser);
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_PARSE("SHIFT %d", action.data.to_state);
|
|
|
|
|
shift(parser, action.data.to_state);
|
|
|
|
|
}
|
2014-08-09 01:03:55 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeShiftExtra:
|
|
|
|
|
DEBUG_PARSE("SHIFT EXTRA");
|
|
|
|
|
shift_extra(parser);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduce:
|
|
|
|
|
DEBUG_PARSE("REDUCE %s %d",
|
|
|
|
|
parser->language->symbol_names[action.data.symbol],
|
|
|
|
|
action.data.child_count);
|
|
|
|
|
reduce(parser, action.data.symbol, action.data.child_count);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeReduceExtra:
|
|
|
|
|
DEBUG_PARSE("REDUCE EXTRA");
|
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:
|
|
|
|
|
DEBUG_PARSE("ACCEPT");
|
|
|
|
|
return get_root(parser);
|
|
|
|
|
|
|
|
|
|
case TSParseActionTypeError:
|
2014-08-27 18:27:53 -07:00
|
|
|
DEBUG_PARSE("ERROR");
|
2014-08-09 01:03:55 -07:00
|
|
|
if (!handle_error(parser))
|
|
|
|
|
return get_root(parser);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|