Introduce RENAME rule type
This commit is contained in:
parent
0b94e9d814
commit
b3a72954ff
26 changed files with 516 additions and 246 deletions
|
|
@ -3,7 +3,8 @@
|
|||
#include "runtime/error_costs.h"
|
||||
|
||||
static const TSParseAction SHIFT_ERROR = {
|
||||
.type = TSParseActionTypeShift, .params = {.to_state = ERROR_STATE}
|
||||
.type = TSParseActionTypeShift,
|
||||
.to_state = ERROR_STATE,
|
||||
};
|
||||
|
||||
void ts_language_table_entry(const TSLanguage *self, TSStateId state,
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ static inline TSStateId ts_language_next_state(const TSLanguage *self,
|
|||
if (count > 0) {
|
||||
TSParseAction action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift || action.type == TSParseActionTypeRecover) {
|
||||
return action.params.to_state;
|
||||
return action.to_state;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -288,7 +288,8 @@ void ts_symbol_iterator_next(TSSymbolIterator *self) {
|
|||
}
|
||||
|
||||
const char *ts_node_type(TSNode self, const TSDocument *document) {
|
||||
TSSymbol symbol = ts_node__tree(self)->symbol;
|
||||
const Tree *tree = ts_node__tree(self);
|
||||
TSSymbol symbol = tree->context.rename_symbol ? tree->context.rename_symbol : tree->symbol;
|
||||
return ts_language_symbol_name(document->parser.language, symbol);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -558,8 +558,8 @@ static bool parser__switch_children(Parser *self, Tree *tree,
|
|||
|
||||
static StackPopResult parser__reduce(Parser *self, StackVersion version,
|
||||
TSSymbol symbol, unsigned count,
|
||||
bool fragile, int dynamic_precedence,
|
||||
bool allow_skipping) {
|
||||
int dynamic_precedence, unsigned short rename_sequence_id,
|
||||
bool fragile, bool allow_skipping) {
|
||||
uint32_t initial_version_count = ts_stack_version_count(self->stack);
|
||||
|
||||
StackPopResult pop = ts_stack_pop_count(self->stack, version, count);
|
||||
|
|
@ -603,6 +603,7 @@ static StackPopResult parser__reduce(Parser *self, StackVersion version,
|
|||
}
|
||||
|
||||
parent->dynamic_precedence += dynamic_precedence;
|
||||
parent->rename_sequence_id = rename_sequence_id;
|
||||
|
||||
TSStateId state = ts_stack_top_state(self->stack, slice.version);
|
||||
TSStateId next_state = ts_language_next_state(language, state, symbol);
|
||||
|
|
@ -699,12 +700,12 @@ static const TSParseAction *parser__reductions_after_sequence(
|
|||
(*count)--;
|
||||
}
|
||||
|
||||
while (*count > 0 && actions[0].params.child_count < child_count) {
|
||||
while (*count > 0 && actions[0].child_count < child_count) {
|
||||
actions++;
|
||||
(*count)--;
|
||||
}
|
||||
|
||||
while (*count > 0 && actions[*count - 1].params.child_count > child_count) {
|
||||
while (*count > 0 && actions[*count - 1].child_count > child_count) {
|
||||
(*count)--;
|
||||
}
|
||||
|
||||
|
|
@ -756,7 +757,7 @@ static StackIterateAction parser__repair_error_callback(void *payload, TSStateId
|
|||
}
|
||||
|
||||
for (uint32_t j = 0; j < repair_reduction_count; j++) {
|
||||
if (repair_reductions[j].params.symbol == repair->symbol) {
|
||||
if (repair_reductions[j].symbol == repair->symbol) {
|
||||
result |= StackIteratePop;
|
||||
session->found_repair = true;
|
||||
session->best_repair = *repair;
|
||||
|
|
@ -788,8 +789,8 @@ static bool parser__repair_error(Parser *self, StackSlice slice,
|
|||
array_clear(&self->reduce_actions);
|
||||
for (uint32_t i = 0; i < entry.action_count; i++) {
|
||||
if (entry.actions[i].type == TSParseActionTypeReduce) {
|
||||
TSSymbol symbol = entry.actions[i].params.symbol;
|
||||
uint32_t child_count = entry.actions[i].params.child_count;
|
||||
TSSymbol symbol = entry.actions[i].symbol;
|
||||
uint32_t child_count = entry.actions[i].child_count;
|
||||
if ((child_count > session.tree_count_above_error) ||
|
||||
(child_count == session.tree_count_above_error &&
|
||||
!ts_language_symbol_metadata(self->language, symbol).visible))
|
||||
|
|
@ -942,11 +943,12 @@ static bool parser__do_potential_reductions(Parser *self, StackVersion version)
|
|||
has_shift_action = true;
|
||||
break;
|
||||
case TSParseActionTypeReduce:
|
||||
if (action.params.child_count > 0)
|
||||
if (action.child_count > 0)
|
||||
ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction){
|
||||
.symbol = action.params.symbol,
|
||||
.count = action.params.child_count,
|
||||
.dynamic_precedence = action.params.dynamic_precedence
|
||||
.symbol = action.symbol,
|
||||
.count = action.child_count,
|
||||
.dynamic_precedence = action.dynamic_precedence,
|
||||
.rename_sequence_id = action.rename_sequence_id,
|
||||
});
|
||||
default:
|
||||
break;
|
||||
|
|
@ -958,8 +960,9 @@ static bool parser__do_potential_reductions(Parser *self, StackVersion version)
|
|||
for (uint32_t i = 0; i < self->reduce_actions.size; i++) {
|
||||
ReduceAction action = self->reduce_actions.contents[i];
|
||||
StackPopResult reduction = parser__reduce(
|
||||
self, version, action.symbol, action.count, true,
|
||||
action.dynamic_precedence, false
|
||||
self, version, action.symbol, action.count,
|
||||
action.dynamic_precedence, action.rename_sequence_id,
|
||||
true, false
|
||||
);
|
||||
if (reduction.stopped_at_error) {
|
||||
ts_tree_array_delete(&reduction.slices.contents[0].trees);
|
||||
|
|
@ -1168,7 +1171,7 @@ static void parser__advance(Parser *self, StackVersion version,
|
|||
next_state = state;
|
||||
LOG("shift_extra");
|
||||
} else {
|
||||
next_state = action.params.to_state;
|
||||
next_state = action.to_state;
|
||||
LOG("shift state:%u", next_state);
|
||||
}
|
||||
|
||||
|
|
@ -1195,18 +1198,14 @@ static void parser__advance(Parser *self, StackVersion version,
|
|||
}
|
||||
|
||||
case TSParseActionTypeReduce: {
|
||||
if (reduction_stopped_at_error)
|
||||
continue;
|
||||
if (reduction_stopped_at_error) continue;
|
||||
|
||||
unsigned child_count = action.params.child_count;
|
||||
TSSymbol symbol = action.params.symbol;
|
||||
unsigned dynamic_precedence = action.params.dynamic_precedence;
|
||||
bool fragile = action.fragile;
|
||||
|
||||
LOG("reduce sym:%s, child_count:%u", SYM_NAME(symbol), child_count);
|
||||
|
||||
StackPopResult reduction =
|
||||
parser__reduce(self, version, symbol, child_count, fragile, dynamic_precedence, true);
|
||||
LOG("reduce sym:%s, child_count:%u", SYM_NAME(action.symbol), action.child_count);
|
||||
StackPopResult reduction = parser__reduce(
|
||||
self, version, action.symbol, action.child_count,
|
||||
action.dynamic_precedence, action.rename_sequence_id,
|
||||
action.fragile, true
|
||||
);
|
||||
StackSlice slice = *array_front(&reduction.slices);
|
||||
if (reduction.stopped_at_error) {
|
||||
reduction_stopped_at_error = true;
|
||||
|
|
@ -1237,7 +1236,7 @@ static void parser__advance(Parser *self, StackVersion version,
|
|||
ts_tree_retain(lookahead);
|
||||
}
|
||||
|
||||
parser__recover(self, version, action.params.to_state, lookahead);
|
||||
parser__recover(self, version, action.to_state, lookahead);
|
||||
if (lookahead == reusable_node->tree) {
|
||||
reusable_node_pop(reusable_node);
|
||||
}
|
||||
|
|
@ -1355,6 +1354,6 @@ Tree *parser_parse(Parser *self, TSInput input, Tree *old_tree, bool halt_on_err
|
|||
LOG_TREE();
|
||||
ts_stack_clear(self->stack);
|
||||
parser__clear_cached_token(self);
|
||||
ts_tree_assign_parents(self->finished_tree, &self->tree_path1);
|
||||
ts_tree_assign_parents(self->finished_tree, &self->tree_path1, self->language);
|
||||
return self->finished_tree;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ typedef struct {
|
|||
uint32_t count;
|
||||
TSSymbol symbol;
|
||||
int dynamic_precedence;
|
||||
unsigned short rename_sequence_id;
|
||||
} ReduceAction;
|
||||
|
||||
typedef Array(ReduceAction) ReduceActionSet;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ Tree *ts_tree_make_leaf(TSSymbol sym, Length padding, Length size,
|
|||
.visible_child_count = 0,
|
||||
.named_child_count = 0,
|
||||
.children = NULL,
|
||||
.rename_sequence_id = 0,
|
||||
.padding = padding,
|
||||
.visible = metadata.visible,
|
||||
.named = metadata.named,
|
||||
|
|
@ -120,18 +121,23 @@ Tree *ts_tree_make_copy(Tree *self) {
|
|||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_assign_parents(Tree *self, TreePath *path) {
|
||||
void ts_tree_assign_parents(Tree *self, TreePath *path, const TSLanguage *language) {
|
||||
array_clear(path);
|
||||
array_push(path, ((TreePathEntry){self, length_zero(), 0}));
|
||||
while (path->size > 0) {
|
||||
Tree *tree = array_pop(path).tree;
|
||||
Length offset = length_zero();
|
||||
const TSSymbol *rename_symbols = language->rename_sequences +
|
||||
tree->rename_sequence_id * language->max_rename_sequence_length;
|
||||
for (uint32_t i = 0; i < tree->child_count; i++) {
|
||||
Tree *child = tree->children[i];
|
||||
if (child->context.parent != tree || child->context.index != i) {
|
||||
child->context.parent = tree;
|
||||
child->context.index = i;
|
||||
child->context.offset = offset;
|
||||
if (tree->rename_sequence_id && rename_symbols[i] != 0) {
|
||||
child->context.rename_symbol = rename_symbols[i];
|
||||
}
|
||||
array_push(path, ((TreePathEntry){child, length_zero(), 0}));
|
||||
}
|
||||
offset = length_add(offset, ts_tree_total_size(child));
|
||||
|
|
@ -472,36 +478,32 @@ static size_t ts_tree__write_to_string(const Tree *self,
|
|||
const TSLanguage *language, char *string,
|
||||
size_t limit, bool is_root,
|
||||
bool include_all) {
|
||||
if (!self)
|
||||
return snprintf(string, limit, "(NULL)");
|
||||
if (!self) return snprintf(string, limit, "(NULL)");
|
||||
|
||||
char *cursor = string;
|
||||
char **writer = (limit > 0) ? &cursor : &string;
|
||||
bool visible = include_all || is_root || (self->visible && self->named);
|
||||
|
||||
if (visible && !is_root)
|
||||
if (visible && !is_root) {
|
||||
cursor += snprintf(*writer, limit, " ");
|
||||
}
|
||||
|
||||
if (visible) {
|
||||
if (self->symbol == ts_builtin_sym_error && self->child_count == 0 &&
|
||||
self->size.chars > 0) {
|
||||
if (self->symbol == ts_builtin_sym_error && self->child_count == 0 && self->size.chars > 0) {
|
||||
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
|
||||
cursor +=
|
||||
ts_tree__write_char_to_string(*writer, limit, self->lookahead_char);
|
||||
cursor += ts_tree__write_char_to_string(*writer, limit, self->lookahead_char);
|
||||
} else {
|
||||
cursor += snprintf(*writer, limit, "(%s",
|
||||
ts_language_symbol_name(language, self->symbol));
|
||||
TSSymbol symbol = self->context.rename_symbol ? self->context.rename_symbol : self->symbol;
|
||||
cursor += snprintf(*writer, limit, "(%s", ts_language_symbol_name(language, symbol));
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < self->child_count; i++) {
|
||||
Tree *child = self->children[i];
|
||||
cursor += ts_tree__write_to_string(child, language, *writer, limit, false,
|
||||
include_all);
|
||||
cursor += ts_tree__write_to_string(child, language, *writer, limit, false, include_all);
|
||||
}
|
||||
|
||||
if (visible)
|
||||
cursor += snprintf(*writer, limit, ")");
|
||||
if (visible) cursor += snprintf(*writer, limit, ")");
|
||||
|
||||
return cursor - string;
|
||||
}
|
||||
|
|
@ -518,8 +520,8 @@ char *ts_tree_string(const Tree *self, const TSLanguage *language,
|
|||
|
||||
void ts_tree__print_dot_graph(const Tree *self, uint32_t byte_offset,
|
||||
const TSLanguage *language, FILE *f) {
|
||||
fprintf(f, "tree_%p [label=\"%s\"", self,
|
||||
ts_language_symbol_name(language, self->symbol));
|
||||
TSSymbol symbol = self->context.rename_symbol ? self->context.rename_symbol : self->symbol;
|
||||
fprintf(f, "tree_%p [label=\"%s\"", self, ts_language_symbol_name(language, symbol));
|
||||
|
||||
if (self->child_count == 0)
|
||||
fprintf(f, ", shape=plaintext");
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ typedef struct Tree {
|
|||
struct Tree *parent;
|
||||
uint32_t index;
|
||||
Length offset;
|
||||
TSSymbol rename_symbol;
|
||||
} context;
|
||||
|
||||
uint32_t child_count;
|
||||
|
|
@ -26,6 +27,7 @@ typedef struct Tree {
|
|||
struct {
|
||||
uint32_t visible_child_count;
|
||||
uint32_t named_child_count;
|
||||
unsigned short rename_sequence_id;
|
||||
struct Tree **children;
|
||||
};
|
||||
TSExternalTokenState external_token_state;
|
||||
|
|
@ -85,7 +87,7 @@ int ts_tree_compare(const Tree *tree1, const Tree *tree2);
|
|||
uint32_t ts_tree_start_column(const Tree *self);
|
||||
uint32_t ts_tree_end_column(const Tree *self);
|
||||
void ts_tree_set_children(Tree *, uint32_t, Tree **);
|
||||
void ts_tree_assign_parents(Tree *, TreePath *);
|
||||
void ts_tree_assign_parents(Tree *, TreePath *, const TSLanguage *);
|
||||
void ts_tree_edit(Tree *, const TSInputEdit *edit);
|
||||
char *ts_tree_string(const Tree *, const TSLanguage *, bool include_all);
|
||||
void ts_tree_print_dot_graph(const Tree *, const TSLanguage *, FILE *);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue