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:
Niranjan Hasabnis 2021-05-15 00:18:32 +00:00
parent 6abf77a20c
commit dffee22736
2 changed files with 23 additions and 0 deletions

View file

@ -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,