Replace NodeType enum with SymbolMetadata bitfield
This will allow storing other metadata about symbols, like if they only appear as ubiquitous tokens
This commit is contained in:
parent
53424699e4
commit
f08554e958
17 changed files with 541 additions and 512 deletions
|
|
@ -83,7 +83,8 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
|||
}
|
||||
|
||||
static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
|
||||
TSNodeType node_type, const char *symbol_name) {
|
||||
TSSymbolMetadata metadata,
|
||||
const char *symbol_name) {
|
||||
TSLength size =
|
||||
ts_length_sub(self->current_position, self->token_start_position);
|
||||
TSLength padding =
|
||||
|
|
@ -99,7 +100,7 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
|
|||
return ts_tree_make_error(size, padding, size_point, padding_point, self->lookahead);
|
||||
} else {
|
||||
LOG("accept_token sym:%s", symbol_name);
|
||||
return ts_tree_make_leaf(symbol, padding, size, padding_point, size_point, node_type);
|
||||
return ts_tree_make_leaf(symbol, padding, size, padding_point, size_point, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,9 +24,17 @@ static inline TSLength ts_node__offset(TSNode self) {
|
|||
return self.offset;
|
||||
}
|
||||
|
||||
static inline bool ts_tree__is_relevant(const TSTree *tree, bool include_anonymous) {
|
||||
return include_anonymous ? tree->options.visible : tree->options.named;
|
||||
}
|
||||
|
||||
static inline size_t ts_tree__relevant_child_count(const TSTree *tree,
|
||||
bool include_anonymous) {
|
||||
return include_anonymous ? tree->visible_child_count : tree->named_child_count;
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
||||
TSNodeType type) {
|
||||
bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
|
|
@ -38,15 +46,14 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
|||
size_t index = 0;
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
TSTree *child = tree->children[i];
|
||||
if (child->options.type >= type) {
|
||||
if (ts_tree__is_relevant(child, include_anonymous)) {
|
||||
if (index == child_index)
|
||||
return ts_node_make(child, position, offset_row);
|
||||
index++;
|
||||
} else {
|
||||
size_t grandchild_index = child_index - index;
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
size_t grandchild_count =
|
||||
ts_tree__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_index < grandchild_count) {
|
||||
did_descend = true;
|
||||
tree = child;
|
||||
|
|
@ -63,7 +70,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) {
|
||||
static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
|
|
@ -80,21 +87,19 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) {
|
|||
const TSTree *child = tree->children[i];
|
||||
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
||||
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
||||
if (child->options.type >= type)
|
||||
if (ts_tree__is_relevant(child, include_anonymous))
|
||||
return ts_node_make(child, child_offset, child_offset_row);
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child(ts_node_make(child, child_offset, child_offset_row),
|
||||
grandchild_count - 1, type);
|
||||
grandchild_count - 1, include_anonymous);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
|
||||
static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
|
|
@ -111,13 +116,12 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
|
|||
const TSTree *child = tree->children[i];
|
||||
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
||||
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
||||
if (child->options.type >= type)
|
||||
if (ts_tree__is_relevant(child, include_anonymous))
|
||||
return ts_node_make(child, child_offset, child_offset_row);
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0, type);
|
||||
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0,
|
||||
include_anonymous);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
|
|
@ -125,9 +129,10 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
|
|||
}
|
||||
|
||||
static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
|
||||
size_t max, TSNodeType type) {
|
||||
size_t max,
|
||||
bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
|
||||
TSLength position = ts_node__offset(self), last_visible_position = position;
|
||||
TSLength offset = ts_node__offset(self), last_visible_position = offset;
|
||||
size_t offset_row = self.row, last_visible_row = offset_row;
|
||||
|
||||
bool did_descend = true;
|
||||
|
|
@ -136,19 +141,19 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
|
|||
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
const TSTree *child = tree->children[i];
|
||||
if (position.chars + child->padding.chars > min)
|
||||
if (offset.chars + child->padding.chars > min)
|
||||
break;
|
||||
if (position.chars + child->padding.chars + child->size.chars > max) {
|
||||
if (offset.chars + child->padding.chars + child->size.chars > max) {
|
||||
tree = child;
|
||||
if (child->options.type >= type) {
|
||||
if (ts_tree__is_relevant(child, include_anonymous)) {
|
||||
last_visible_tree = tree;
|
||||
last_visible_position = position;
|
||||
last_visible_position = offset;
|
||||
last_visible_row = offset_row;
|
||||
}
|
||||
did_descend = true;
|
||||
break;
|
||||
}
|
||||
position = ts_length_add(position, ts_tree_total_size(child));
|
||||
offset = ts_length_add(offset, ts_tree_total_size(child));
|
||||
offset_row += child->padding_point.row + child->size_point.row;
|
||||
}
|
||||
}
|
||||
|
|
@ -201,7 +206,7 @@ bool ts_node_eq(TSNode self, TSNode other) {
|
|||
}
|
||||
|
||||
bool ts_node_is_named(TSNode self) {
|
||||
return ts_node__tree(self)->options.type == TSNodeTypeNamed;
|
||||
return ts_node__tree(self)->options.named;
|
||||
}
|
||||
|
||||
bool ts_node_has_changes(TSNode self) {
|
||||
|
|
@ -210,11 +215,11 @@ bool ts_node_has_changes(TSNode self) {
|
|||
|
||||
TSNode ts_node_parent(TSNode self) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
|
||||
do {
|
||||
position = ts_length_sub(position, tree->context.offset);
|
||||
offset = ts_length_sub(offset, tree->context.offset);
|
||||
offset_row -= tree->context.offset_point.row;
|
||||
|
||||
tree = tree->context.parent;
|
||||
|
|
@ -222,15 +227,15 @@ TSNode ts_node_parent(TSNode self) {
|
|||
return ts_node__null();
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node_make(tree, position, offset_row);
|
||||
return ts_node_make(tree, offset, offset_row);
|
||||
}
|
||||
|
||||
TSNode ts_node_child(TSNode self, size_t child_index) {
|
||||
return ts_node__child(self, child_index, TSNodeTypeAnonymous);
|
||||
return ts_node__child(self, child_index, true);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_child(TSNode self, size_t child_index) {
|
||||
return ts_node__child(self, child_index, TSNodeTypeNamed);
|
||||
return ts_node__child(self, child_index, false);
|
||||
}
|
||||
|
||||
size_t ts_node_child_count(TSNode self) {
|
||||
|
|
@ -242,25 +247,25 @@ size_t ts_node_named_child_count(TSNode self) {
|
|||
}
|
||||
|
||||
TSNode ts_node_next_sibling(TSNode self) {
|
||||
return ts_node__next_sibling(self, TSNodeTypeAnonymous);
|
||||
return ts_node__next_sibling(self, true);
|
||||
}
|
||||
|
||||
TSNode ts_node_next_named_sibling(TSNode self) {
|
||||
return ts_node__next_sibling(self, TSNodeTypeNamed);
|
||||
return ts_node__next_sibling(self, false);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_sibling(TSNode self) {
|
||||
return ts_node__prev_sibling(self, TSNodeTypeAnonymous);
|
||||
return ts_node__prev_sibling(self, true);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_named_sibling(TSNode self) {
|
||||
return ts_node__prev_sibling(self, TSNodeTypeNamed);
|
||||
return ts_node__prev_sibling(self, false);
|
||||
}
|
||||
|
||||
TSNode ts_node_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, TSNodeTypeAnonymous);
|
||||
return ts_node__descendent_for_range(self, min, max, true);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, TSNodeTypeNamed);
|
||||
return ts_node__descendent_for_range(self, min, max, false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,7 +170,7 @@ static bool ts_parser__shift_extra(TSParser *self, int head, TSStateId state,
|
|||
static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
||||
int child_count, bool extra, bool count_extra) {
|
||||
vector_clear(&self->reduce_parents);
|
||||
TSNodeType node_type = self->language->node_types[symbol];
|
||||
TSSymbolMetadata metadata = self->language->symbol_metadata[symbol];
|
||||
Vector pop_results = ts_stack_pop(self->stack, head, child_count, count_extra);
|
||||
|
||||
int last_head_index = -1;
|
||||
|
|
@ -198,7 +198,7 @@ static TSTree *ts_parser__reduce(TSParser *self, int head, TSSymbol symbol,
|
|||
*/
|
||||
if (!parent)
|
||||
parent = ts_tree_make_node(symbol, pop_result->tree_count,
|
||||
pop_result->trees, node_type);
|
||||
pop_result->trees, metadata);
|
||||
vector_push(&self->reduce_parents, &parent);
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
||||
TSPoint padding_point,
|
||||
TSPoint size_point,
|
||||
TSNodeType node_type) {
|
||||
TSSymbolMetadata metadata) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
*result = (TSTree){
|
||||
.ref_count = 1,
|
||||
|
|
@ -22,7 +22,10 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
|||
.padding = padding,
|
||||
.padding_point = padding_point,
|
||||
.size_point = size_point,
|
||||
.options = {.type = node_type },
|
||||
.options =
|
||||
{
|
||||
.visible = metadata.visible, .named = metadata.named,
|
||||
},
|
||||
};
|
||||
|
||||
if (sym == ts_builtin_sym_error) {
|
||||
|
|
@ -39,7 +42,9 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding,
|
|||
char lookahead_char) {
|
||||
TSTree *result =
|
||||
ts_tree_make_leaf(ts_builtin_sym_error, padding, size, padding_point,
|
||||
size_point, TSNodeTypeNamed);
|
||||
size_point, (TSSymbolMetadata){
|
||||
.visible = true, .named = true,
|
||||
});
|
||||
result->lookahead_char = lookahead_char;
|
||||
return result;
|
||||
}
|
||||
|
|
@ -81,18 +86,13 @@ static void ts_tree__set_children(TSTree *self, TSTree **children,
|
|||
self->size_point = ts_point_add(ts_point_add(self->size_point, child->padding_point), child->size_point);
|
||||
}
|
||||
|
||||
switch (child->options.type) {
|
||||
case TSNodeTypeNamed:
|
||||
self->visible_child_count++;
|
||||
if (child->options.visible) {
|
||||
self->visible_child_count++;
|
||||
if (child->options.named)
|
||||
self->named_child_count++;
|
||||
break;
|
||||
case TSNodeTypeAnonymous:
|
||||
self->visible_child_count++;
|
||||
break;
|
||||
case TSNodeTypeHidden:
|
||||
self->visible_child_count += child->visible_child_count;
|
||||
self->named_child_count += child->named_child_count;
|
||||
break;
|
||||
} else {
|
||||
self->visible_child_count += child->visible_child_count;
|
||||
self->named_child_count += child->named_child_count;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,9 +105,9 @@ static void ts_tree__set_children(TSTree *self, TSTree **children,
|
|||
}
|
||||
|
||||
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
||||
TSTree **children, TSNodeType node_type) {
|
||||
TSTree **children, TSSymbolMetadata metadata) {
|
||||
TSTree *result =
|
||||
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_point_zero(), ts_point_zero(), node_type);
|
||||
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_point_zero(), ts_point_zero(), metadata);
|
||||
ts_tree__set_children(result, children, child_count);
|
||||
return result;
|
||||
}
|
||||
|
|
@ -167,6 +167,10 @@ bool ts_tree_eq(const TSTree *self, const TSTree *other) {
|
|||
|
||||
if (self->symbol != other->symbol)
|
||||
return false;
|
||||
if (self->options.visible != other->options.visible)
|
||||
return false;
|
||||
if (self->options.named != other->options.named)
|
||||
return false;
|
||||
if (self->symbol == ts_builtin_sym_error)
|
||||
return self->lookahead_char == other->lookahead_char;
|
||||
if (self->child_count != other->child_count)
|
||||
|
|
@ -217,16 +221,15 @@ static size_t write_lookahead_to_string(char *string, size_t limit,
|
|||
|
||||
static size_t ts_tree__write_to_string(const TSTree *self,
|
||||
const char **symbol_names, char *string,
|
||||
size_t limit, int is_root,
|
||||
size_t limit, bool is_root,
|
||||
bool include_anonymous) {
|
||||
if (!self)
|
||||
return snprintf(string, limit, "(NULL)");
|
||||
|
||||
char *cursor = string;
|
||||
char **writer = (limit > 0) ? &cursor : &string;
|
||||
TSNodeType min_node_type =
|
||||
include_anonymous ? TSNodeTypeAnonymous : TSNodeTypeNamed;
|
||||
int visible = self->options.type >= min_node_type || is_root;
|
||||
bool visible = is_root || (self->options.visible &&
|
||||
(include_anonymous || self->options.named));
|
||||
|
||||
if (visible && !is_root)
|
||||
cursor += snprintf(*writer, limit, " ");
|
||||
|
|
@ -242,8 +245,8 @@ static size_t ts_tree__write_to_string(const TSTree *self,
|
|||
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
TSTree *child = self->children[i];
|
||||
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit, 0,
|
||||
include_anonymous);
|
||||
cursor += ts_tree__write_to_string(child, symbol_names, *writer, limit,
|
||||
false, include_anonymous);
|
||||
}
|
||||
|
||||
if (visible)
|
||||
|
|
@ -255,10 +258,10 @@ static size_t ts_tree__write_to_string(const TSTree *self,
|
|||
char *ts_tree_string(const TSTree *self, const char **symbol_names,
|
||||
bool include_anonymous) {
|
||||
static char SCRATCH[1];
|
||||
size_t size = 1 + ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, 1,
|
||||
include_anonymous);
|
||||
size_t size = 1 + ts_tree__write_to_string(self, symbol_names, SCRATCH, 0,
|
||||
true, include_anonymous);
|
||||
char *result = malloc(size * sizeof(char));
|
||||
ts_tree__write_to_string(self, symbol_names, result, size, 1,
|
||||
ts_tree__write_to_string(self, symbol_names, result, size, true,
|
||||
include_anonymous);
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,8 @@ struct TSTree {
|
|||
TSSymbol symbol;
|
||||
|
||||
struct {
|
||||
TSNodeType type : 2;
|
||||
bool visible : 1;
|
||||
bool named : 1;
|
||||
bool extra : 1;
|
||||
bool fragile_left : 1;
|
||||
bool fragile_right : 1;
|
||||
|
|
@ -41,8 +42,8 @@ struct TSTree {
|
|||
};
|
||||
|
||||
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSPoint,
|
||||
TSPoint, TSNodeType);
|
||||
TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSNodeType);
|
||||
TSPoint, TSSymbolMetadata);
|
||||
TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSSymbolMetadata);
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding,
|
||||
TSPoint padding_point,
|
||||
TSPoint size_point, char lookahead_char);
|
||||
|
|
@ -65,7 +66,7 @@ static inline bool ts_tree_is_extra(const TSTree *tree) {
|
|||
}
|
||||
|
||||
static inline bool ts_tree_is_visible(const TSTree *tree) {
|
||||
return tree->options.type != TSNodeTypeHidden;
|
||||
return tree->options.visible;
|
||||
}
|
||||
|
||||
static inline void ts_tree_set_extra(TSTree *tree) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue