Simplify handling of ubiquitous tokens during reduce

This commit is contained in:
Max Brunsfeld 2014-08-08 08:46:01 -07:00
parent 41c4e7cd8f
commit 7ba3953f7e
7 changed files with 14 additions and 31 deletions

View file

@ -118,7 +118,7 @@ void ts_parser_shift_extra(TSParser *parser) {
void ts_parser_reduce(TSParser *parser, TSSymbol symbol, size_t child_count) {
parser->next_lookahead = parser->lookahead;
parser->lookahead = ts_stack_reduce(&parser->stack, symbol, child_count,
parser->language->hidden_symbol_flags, 1);
parser->language->hidden_symbol_flags);
}
int ts_parser_reduce_extra(TSParser *parser, TSSymbol symbol) {
@ -177,19 +177,7 @@ int ts_parser_handle_error(TSParser *parser) {
}
TSTree *ts_parser_tree_root(TSParser *parser) {
TSStack *stack = &parser->stack;
size_t node_count = 0;
for (size_t i = 0; i < stack->size; i++) {
TSTree *node = stack->entries[i].node;
if (!parser->language->hidden_symbol_flags[node->symbol])
node_count++;
}
if (node_count > 1)
return ts_stack_reduce(stack, 2, stack->size,
parser->language->hidden_symbol_flags, 0);
else
return ts_stack_top_node(stack);
return ts_stack_top_node(&parser->stack);
}
TSParseAction ts_parser_next_action(TSParser *parser) {

View file

@ -52,14 +52,14 @@ size_t ts_stack_right_position(const TSStack *stack) {
}
TSTree *ts_stack_reduce(TSStack *stack, TSSymbol symbol, size_t child_count,
const int *hidden_symbol_flags, int dont_count_extras) {
const int *hidden_symbol_flags) {
// First, 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
// extra tokens, which don't count towards the child node count.
// ubiquitous tokens, which don't count.
for (size_t i = 0; i < child_count; i++) {
TSTree *child = stack->entries[stack->size - 1 - i].node;
if (dont_count_extras && ts_tree_is_extra(child))
if (ts_tree_is_extra(child))
child_count++;
}
@ -70,7 +70,6 @@ TSTree *ts_stack_reduce(TSStack *stack, TSSymbol symbol, size_t child_count,
TSTree *lookahead = ts_tree_make_node(symbol, child_count, children,
hidden_symbol_flags[symbol]);
ts_stack_shrink(stack, stack->size - child_count);
return lookahead;
}

View file

@ -18,9 +18,7 @@ typedef struct {
TSStack ts_stack_make();
void ts_stack_delete(TSStack *);
TSTree *ts_stack_reduce(TSStack *stack, TSSymbol symbol,
size_t immediate_child_count,
const int *hidden_symbol_flags, int gather_extras);
TSTree *ts_stack_reduce(TSStack *, TSSymbol, size_t, const int *hidden_symbols);
void ts_stack_shrink(TSStack *stack, size_t new_size);
void ts_stack_push(TSStack *stack, TSStateId state, TSTree *node);
TSStateId ts_stack_top_state(const TSStack *stack);