Refactor parser and stack
This commit is contained in:
parent
1e79ed794b
commit
4327f3ed26
7 changed files with 108 additions and 183 deletions
|
|
@ -6,10 +6,6 @@
|
|||
#include "runtime/stack.h"
|
||||
#include "runtime/parser.h"
|
||||
|
||||
/*
|
||||
* Private
|
||||
*/
|
||||
|
||||
static const TSParseAction *actions_for_state(const TSLanguage *language,
|
||||
TSStateId state) {
|
||||
return language->parse_table + (state * language->symbol_count);
|
||||
|
|
@ -69,40 +65,7 @@ static TSSymbol *expected_symbols(TSParser *parser, size_t *count) {
|
|||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Public
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void ts_parser_start(TSParser *parser, TSInput input, TSInputEdit *edit) {
|
||||
if (!edit)
|
||||
ts_stack_shrink(&parser->stack, 0);
|
||||
parser->lookahead = NULL;
|
||||
parser->next_lookahead = NULL;
|
||||
|
||||
size_t position = breakdown_stack(parser, edit);
|
||||
input.seek_fn(input.data, position);
|
||||
|
||||
parser->lexer = ts_lexer_make();
|
||||
parser->lexer.input = input;
|
||||
ts_lexer_advance(&parser->lexer);
|
||||
}
|
||||
|
||||
void ts_parser_shift(TSParser *parser, TSStateId parse_state) {
|
||||
static void shift(TSParser *parser, TSStateId parse_state) {
|
||||
if (ts_tree_is_extra(parser->lookahead))
|
||||
parse_state = ts_stack_top_state(&parser->stack);
|
||||
ts_stack_push(&parser->stack, parse_state, parser->lookahead);
|
||||
|
|
@ -110,21 +73,40 @@ void ts_parser_shift(TSParser *parser, TSStateId parse_state) {
|
|||
parser->next_lookahead = NULL;
|
||||
}
|
||||
|
||||
void ts_parser_shift_extra(TSParser *parser) {
|
||||
static void shift_extra(TSParser *parser) {
|
||||
ts_tree_set_extra(parser->lookahead);
|
||||
ts_parser_shift(parser, 0);
|
||||
shift(parser, 0);
|
||||
}
|
||||
|
||||
void ts_parser_reduce(TSParser *parser, TSSymbol symbol, size_t child_count) {
|
||||
static void reduce(TSParser *parser, TSSymbol symbol, size_t child_count) {
|
||||
TSStack *stack = &parser->stack;
|
||||
parser->next_lookahead = parser->lookahead;
|
||||
parser->lookahead = ts_stack_reduce(&parser->stack, symbol, child_count,
|
||||
parser->language->hidden_symbol_flags);
|
||||
|
||||
// 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.
|
||||
for (size_t i = 0; i < child_count; i++) {
|
||||
if (child_count == stack->size)
|
||||
break;
|
||||
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);
|
||||
}
|
||||
|
||||
int ts_parser_reduce_extra(TSParser *parser, TSSymbol symbol) {
|
||||
static int reduce_extra(TSParser *parser, TSSymbol symbol) {
|
||||
TSTree *top_node = ts_stack_top_node(&parser->stack);
|
||||
if (top_node->symbol == symbol && !ts_tree_is_extra(top_node)) {
|
||||
ts_parser_reduce(parser, symbol, 1);
|
||||
reduce(parser, symbol, 1);
|
||||
ts_tree_set_extra(parser->lookahead);
|
||||
return 1;
|
||||
} else {
|
||||
|
|
@ -132,7 +114,7 @@ int ts_parser_reduce_extra(TSParser *parser, TSSymbol symbol) {
|
|||
}
|
||||
}
|
||||
|
||||
int ts_parser_handle_error(TSParser *parser) {
|
||||
static int handle_error(TSParser *parser) {
|
||||
size_t count = 0;
|
||||
const TSSymbol *inputs = expected_symbols(parser, &count);
|
||||
TSTree *error = ts_tree_make_error(ts_lexer_lookahead_char(&parser->lexer),
|
||||
|
|
@ -176,19 +158,17 @@ int ts_parser_handle_error(TSParser *parser) {
|
|||
}
|
||||
}
|
||||
|
||||
TSTree *ts_parser_tree_root(TSParser *parser) {
|
||||
TSStack *stack = &parser->stack;
|
||||
if (stack->size == 0)
|
||||
static TSTree *get_root(TSParser *parser) {
|
||||
if (parser->stack.size == 0)
|
||||
return NULL;
|
||||
|
||||
TSTree *tree = ts_stack_reduce(stack, ts_builtin_sym_document,
|
||||
stack->size, parser->language->hidden_symbol_flags);
|
||||
tree->options = 0;
|
||||
ts_stack_push(stack, 0, tree);
|
||||
return tree;
|
||||
reduce(parser, ts_builtin_sym_document, parser->stack.size);
|
||||
parser->lookahead->options = 0;
|
||||
shift(parser, 0);
|
||||
return parser->stack.entries[0].node;
|
||||
}
|
||||
|
||||
TSParseAction ts_parser_next_action(TSParser *parser) {
|
||||
static TSParseAction next_action(TSParser *parser) {
|
||||
TSStateId state = ts_stack_top_state(&parser->stack);
|
||||
if (!parser->lookahead)
|
||||
parser->lookahead = parser->language->lex_fn(
|
||||
|
|
@ -196,59 +176,82 @@ TSParseAction ts_parser_next_action(TSParser *parser) {
|
|||
return actions_for_state(parser->language, state)[parser->lookahead->symbol];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#define DEBUG_PARSE(...) \
|
||||
if (parser->debug) { \
|
||||
fprintf(stderr, "\n" __VA_ARGS__); \
|
||||
}
|
||||
|
||||
TSTree *ts_parser_step(TSParser *parser) {
|
||||
TSParseAction action = ts_parser_next_action(parser);
|
||||
DEBUG_PARSE("LOOKAHEAD %s",
|
||||
parser->language->symbol_names[parser->lookahead->symbol]);
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG_PARSE("SHIFT %d", action.data.to_state);
|
||||
ts_parser_shift(parser, action.data.to_state);
|
||||
return NULL;
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG_PARSE("SHIFT EXTRA");
|
||||
ts_parser_shift_extra(parser);
|
||||
return NULL;
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG_PARSE("REDUCE %s %d",
|
||||
parser->language->symbol_names[action.data.symbol],
|
||||
action.data.child_count);
|
||||
ts_parser_reduce(parser, action.data.symbol, action.data.child_count);
|
||||
return NULL;
|
||||
case TSParseActionTypeReduceExtra:
|
||||
if (!ts_parser_reduce_extra(parser, action.data.symbol))
|
||||
goto error;
|
||||
DEBUG_PARSE("REDUCE EXTRA");
|
||||
return NULL;
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG_PARSE("ACCEPT");
|
||||
return ts_parser_tree_root(parser);
|
||||
case TSParseActionTypeError:
|
||||
goto error;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
error:
|
||||
DEBUG_PARSE("ERROR");
|
||||
if (!ts_parser_handle_error(parser))
|
||||
return ts_parser_tree_root(parser);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const TSTree *ts_parser_parse(TSParser *parser, TSInput input,
|
||||
TSInputEdit *edit) {
|
||||
ts_parser_start(parser, input, edit);
|
||||
if (!edit)
|
||||
ts_stack_shrink(&parser->stack, 0);
|
||||
|
||||
parser->lookahead = NULL;
|
||||
parser->next_lookahead = NULL;
|
||||
parser->lexer = ts_lexer_make();
|
||||
parser->lexer.input = input;
|
||||
|
||||
input.seek_fn(input.data, breakdown_stack(parser, edit));
|
||||
ts_lexer_advance(&parser->lexer);
|
||||
|
||||
for (;;) {
|
||||
const TSTree *tree = ts_parser_step(parser);
|
||||
if (tree)
|
||||
return tree;
|
||||
TSParseAction action = next_action(parser);
|
||||
DEBUG_PARSE("LOOKAHEAD %s",
|
||||
parser->language->symbol_names[parser->lookahead->symbol]);
|
||||
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG_PARSE("SHIFT %d", action.data.to_state);
|
||||
shift(parser, action.data.to_state);
|
||||
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:
|
||||
if (!reduce_extra(parser, action.data.symbol)) {
|
||||
DEBUG_PARSE("ERROR");
|
||||
if (!handle_error(parser))
|
||||
return get_root(parser);
|
||||
}
|
||||
DEBUG_PARSE("REDUCE EXTRA");
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG_PARSE("ACCEPT");
|
||||
return get_root(parser);
|
||||
|
||||
case TSParseActionTypeError:
|
||||
if (!handle_error(parser))
|
||||
return get_root(parser);
|
||||
break;
|
||||
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue