Rename node accessor methods
Instead of child() vs concrete_child(), next_sibling() vs next_concrete_sibling(), etc, the default is switched: child() refers to the concrete syntax tree, and named_child() refers to the AST. Because the AST is abstract through exclusion of some nodes, the names are clearer if the qualifier goes on the AST operations
This commit is contained in:
parent
245daffbc4
commit
7ee5eaa16a
16 changed files with 475 additions and 476 deletions
|
|
@ -65,6 +65,6 @@ void ts_document_set_input_string(TSDocument *document, const char *text) {
|
|||
TSNode ts_document_root_node(const TSDocument *document) {
|
||||
TSNode result = ts_node_make(document->tree, ts_length_zero());
|
||||
while (result.data && !ts_tree_is_visible(result.data))
|
||||
result = ts_node_child(result, 0);
|
||||
result = ts_node_named_child(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,14 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset) {
|
|||
return (TSNode){.data = tree, .offset = offset };
|
||||
}
|
||||
|
||||
/*
|
||||
* Private
|
||||
*/
|
||||
|
||||
static inline TSNode ts_node__null() {
|
||||
return ts_node_make(NULL, ts_length_zero());
|
||||
}
|
||||
|
||||
static inline const TSTree *ts_node__tree(TSNode this) {
|
||||
return this.data;
|
||||
}
|
||||
|
|
@ -16,54 +24,8 @@ static inline TSLength ts_node__offset(TSNode this) {
|
|||
return this.offset;
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__null() {
|
||||
return ts_node_make(NULL, ts_length_zero());
|
||||
}
|
||||
|
||||
TSLength ts_node_pos(TSNode this) {
|
||||
return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding);
|
||||
}
|
||||
|
||||
TSLength ts_node_size(TSNode this) {
|
||||
return ts_node__tree(this)->size;
|
||||
}
|
||||
|
||||
bool ts_node_eq(TSNode this, TSNode other) {
|
||||
return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) &&
|
||||
ts_length_eq(ts_node__offset(this), ts_node__offset(other));
|
||||
}
|
||||
|
||||
const char *ts_node_name(TSNode this, const TSDocument *document) {
|
||||
return document->parser.language->symbol_names[ts_node__tree(this)->symbol];
|
||||
}
|
||||
|
||||
const char *ts_node_string(TSNode this, const TSDocument *document) {
|
||||
return ts_tree_string(ts_node__tree(this),
|
||||
document->parser.language->symbol_names);
|
||||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode this) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
|
||||
do {
|
||||
TSTree *parent = tree->context.parent;
|
||||
if (!parent)
|
||||
return ts_node__null();
|
||||
|
||||
for (size_t i = 0; i < tree->context.index; i++) {
|
||||
TSTree *child = parent->children[i];
|
||||
position = ts_length_sub(position, ts_tree_total_size(child));
|
||||
}
|
||||
|
||||
tree = parent;
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node_make(tree, position);
|
||||
}
|
||||
|
||||
static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
|
||||
TSNodeType type) {
|
||||
static inline TSNode ts_node__child(TSNode this, size_t child_index,
|
||||
TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
|
||||
|
|
@ -80,7 +42,7 @@ static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
|
|||
index++;
|
||||
} else {
|
||||
size_t grandchild_index = child_index - index;
|
||||
size_t grandchild_count = (type == TSNodeTypeNormal)
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
if (grandchild_index < grandchild_count) {
|
||||
|
|
@ -98,7 +60,7 @@ static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
static TSNode ts_node__prev_sibling_with_type(TSNode this, TSNodeType type) {
|
||||
static inline TSNode ts_node__prev_sibling(TSNode this, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
|
||||
|
|
@ -114,19 +76,19 @@ static TSNode ts_node__prev_sibling_with_type(TSNode this, TSNodeType type) {
|
|||
TSLength child_position = ts_length_add(position, child->context.offset);
|
||||
if (child->options.type >= type)
|
||||
return ts_node_make(child, child_position);
|
||||
size_t grandchild_count = (type == TSNodeTypeNormal)
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child_with_type(ts_node_make(child, child_position),
|
||||
grandchild_count - 1, type);
|
||||
return ts_node__child(ts_node_make(child, child_position),
|
||||
grandchild_count - 1, type);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node__null();
|
||||
}
|
||||
|
||||
static TSNode ts_node__next_sibling_with_type(TSNode this, TSNodeType type) {
|
||||
static inline TSNode ts_node__next_sibling(TSNode this, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
|
||||
|
|
@ -142,19 +104,19 @@ static TSNode ts_node__next_sibling_with_type(TSNode this, TSNodeType type) {
|
|||
TSLength child_position = ts_length_add(position, child->context.offset);
|
||||
if (child->options.type >= type)
|
||||
return ts_node_make(child, child_position);
|
||||
size_t grandchild_count = (type == TSNodeTypeNormal)
|
||||
size_t grandchild_count = (type == TSNodeTypeNamed)
|
||||
? child->named_child_count
|
||||
: child->visible_child_count;
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child_with_type(ts_node_make(child, child_position), 0,
|
||||
type);
|
||||
return ts_node__child(ts_node_make(child, child_position), 0, type);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node__null();
|
||||
}
|
||||
|
||||
static TSNode ts_node__find_for_range_with_type(TSNode this, size_t min, size_t max, TSNodeType type) {
|
||||
static inline TSNode ts_node__descendent_for_range(TSNode this, size_t min,
|
||||
size_t max, TSNodeType type) {
|
||||
const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree;
|
||||
TSLength position = ts_node__offset(this), last_visible_position = position;
|
||||
|
||||
|
|
@ -182,50 +144,96 @@ static TSNode ts_node__find_for_range_with_type(TSNode this, size_t min, size_t
|
|||
return ts_node_make(last_visible_tree, last_visible_position);
|
||||
}
|
||||
|
||||
TSNode ts_node_next_sibling(TSNode this) {
|
||||
return ts_node__next_sibling_with_type(this, TSNodeTypeNormal);
|
||||
/*
|
||||
* Public
|
||||
*/
|
||||
|
||||
TSLength ts_node_pos(TSNode this) {
|
||||
return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling_with_type(this, TSNodeTypeNormal);
|
||||
TSLength ts_node_size(TSNode this) {
|
||||
return ts_node__tree(this)->size;
|
||||
}
|
||||
|
||||
TSNode ts_node_next_concrete_sibling(TSNode this) {
|
||||
return ts_node__next_sibling_with_type(this, TSNodeTypeConcrete);
|
||||
TSSymbol ts_node_symbol(TSNode this) {
|
||||
return ts_node__tree(this)->symbol;
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_concrete_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling_with_type(this, TSNodeTypeConcrete);
|
||||
const char *ts_node_name(TSNode this, const TSDocument *document) {
|
||||
return document->parser.language->symbol_names[ts_node__tree(this)->symbol];
|
||||
}
|
||||
|
||||
size_t ts_node_concrete_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->visible_child_count;
|
||||
const char *ts_node_string(TSNode this, const TSDocument *document) {
|
||||
return ts_tree_string(ts_node__tree(this),
|
||||
document->parser.language->symbol_names);
|
||||
}
|
||||
|
||||
bool ts_node_is_concrete(TSNode this) {
|
||||
return ts_node__tree(this)->options.type == TSNodeTypeConcrete;
|
||||
bool ts_node_eq(TSNode this, TSNode other) {
|
||||
return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) &&
|
||||
ts_length_eq(ts_node__offset(this), ts_node__offset(other));
|
||||
}
|
||||
|
||||
size_t ts_node_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->named_child_count;
|
||||
bool ts_node_is_named(TSNode this) {
|
||||
return ts_node__tree(this)->options.type == TSNodeTypeNamed;
|
||||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode this) {
|
||||
const TSTree *tree = ts_node__tree(this);
|
||||
TSLength position = ts_node__offset(this);
|
||||
|
||||
do {
|
||||
TSTree *parent = tree->context.parent;
|
||||
if (!parent)
|
||||
return ts_node__null();
|
||||
|
||||
for (size_t i = 0; i < tree->context.index; i++) {
|
||||
TSTree *child = parent->children[i];
|
||||
position = ts_length_sub(position, ts_tree_total_size(child));
|
||||
}
|
||||
|
||||
tree = parent;
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
|
||||
return ts_node_make(tree, position);
|
||||
}
|
||||
|
||||
TSNode ts_node_child(TSNode this, size_t child_index) {
|
||||
return ts_node__child_with_type(this, child_index, TSNodeTypeNormal);
|
||||
return ts_node__child(this, child_index, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_concrete_child(TSNode this, size_t child_index) {
|
||||
return ts_node__child_with_type(this, child_index, TSNodeTypeConcrete);
|
||||
TSNode ts_node_named_child(TSNode this, size_t child_index) {
|
||||
return ts_node__child(this, child_index, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeNormal);
|
||||
size_t ts_node_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->visible_child_count;
|
||||
}
|
||||
|
||||
TSNode ts_node_find_concrete_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeConcrete);
|
||||
size_t ts_node_named_child_count(TSNode this) {
|
||||
return ts_node__tree(this)->named_child_count;
|
||||
}
|
||||
|
||||
TSNode ts_node_find_for_pos(TSNode this, size_t position) {
|
||||
return ts_node_find_for_range(this, position, position);
|
||||
TSNode ts_node_next_sibling(TSNode this) {
|
||||
return ts_node__next_sibling(this, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_next_named_sibling(TSNode this) {
|
||||
return ts_node__next_sibling(this, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling(this, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_prev_named_sibling(TSNode this) {
|
||||
return ts_node__prev_sibling(this, TSNodeTypeNamed);
|
||||
}
|
||||
|
||||
TSNode ts_node_descendent_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(this, min, max, TSNodeTypeAnonymous);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_descendent_for_range(TSNode this, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(this, min, max, TSNodeTypeNamed);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
|
|||
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
||||
TSTree *result =
|
||||
ts_tree_make_leaf(ts_builtin_sym_error, size, padding, TSNodeTypeNormal);
|
||||
ts_tree_make_leaf(ts_builtin_sym_error, size, padding, TSNodeTypeNamed);
|
||||
result->lookahead_char = lookahead_char;
|
||||
return result;
|
||||
}
|
||||
|
|
@ -56,11 +56,11 @@ static void ts_tree__set_children(TSTree *this, TSTree **children,
|
|||
}
|
||||
|
||||
switch (child->options.type) {
|
||||
case TSNodeTypeNormal:
|
||||
case TSNodeTypeNamed:
|
||||
this->visible_child_count++;
|
||||
this->named_child_count++;
|
||||
break;
|
||||
case TSNodeTypeConcrete:
|
||||
case TSNodeTypeAnonymous:
|
||||
this->visible_child_count++;
|
||||
break;
|
||||
case TSNodeTypeHidden:
|
||||
|
|
@ -149,7 +149,7 @@ static size_t ts_tree__write_to_string(const TSTree *tree,
|
|||
|
||||
char *cursor = string;
|
||||
char **writer = (limit > 0) ? &cursor : &string;
|
||||
int visible = tree->options.type == TSNodeTypeNormal || is_root;
|
||||
int visible = tree->options.type == TSNodeTypeNamed || is_root;
|
||||
|
||||
if (visible && !is_root)
|
||||
cursor += snprintf(*writer, limit, " ");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue