Adding API to get field name of a TSNode
This PR adds an API to get name of the field of TSNode's child.
It uses same set of arguments as that of ts_node_child, but returns
field name if it is found, otherwise it returns NULL.
This API is useful to implement custom printing of S-expressions such
as following:
"(binary_expression
(binary_expression_left (identifier))
(binary_expression_operator ("+"))
(binary_expression_right (identifier)
)"
Currently, ts_node_string does not allow any customization for printing.
This commit is contained in:
parent
6abf77a20c
commit
dffee22736
2 changed files with 23 additions and 0 deletions
|
|
@ -487,6 +487,12 @@ TSNode ts_node_parent(TSNode);
|
|||
*/
|
||||
TSNode ts_node_child(TSNode, uint32_t);
|
||||
|
||||
/**
|
||||
* Get the field_name for node's child at the given index, where zero represents
|
||||
* the first child. Returns NULL, if no field is found.
|
||||
*/
|
||||
const char *ts_node_child_field_name(TSNode, uint32_t);
|
||||
|
||||
/**
|
||||
* Get the node's number of children.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -578,6 +578,23 @@ recur:
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
const char *ts_node_child_field_name(TSNode self, uint32_t child_index) {
|
||||
const TSFieldMapEntry *field_map, *field_map_end;
|
||||
ts_language_field_map(
|
||||
self.tree->language,
|
||||
ts_node__subtree(self).ptr->production_id,
|
||||
&field_map,
|
||||
&field_map_end
|
||||
);
|
||||
|
||||
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
|
||||
if (i->child_index == child_index) {
|
||||
return self.tree->language->field_names[i->field_id];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TSNode ts_node_child_by_field_name(
|
||||
TSNode self,
|
||||
const char *name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue