Skip to content

Commit 4217b53

Browse files
committed
token_table(): return field name, if any
1 parent 77959ee commit 4217b53

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

src/tree-sitter.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ SEXP token_table(SEXP input, SEXP rlanguage, SEXP rranges) {
144144
TSNode root = ts_tree_root_node(tree);
145145
uint32_t num_nodes = ts_node_descendant_count(root);
146146
const char *nms[] = {
147-
"id", "parent", "type", "code", "start_byte", "end_byte",
147+
"id", "parent", "field_name", "type", "code", "start_byte", "end_byte",
148148
"start_row", "start_column", "end_row", "end_column", ""
149149
};
150150
SEXP res = PROTECT(Rf_mkNamed(VECSXP, nms));
@@ -153,21 +153,23 @@ SEXP token_table(SEXP input, SEXP rlanguage, SEXP rranges) {
153153
SET_VECTOR_ELT(res, 1, Rf_allocVector(INTSXP, num_nodes));
154154
SEXP res_parent = VECTOR_ELT(res, 1);
155155
SET_VECTOR_ELT(res, 2, Rf_allocVector(STRSXP, num_nodes));
156-
SEXP res_type = VECTOR_ELT(res, 2);
156+
SEXP res_field_name = VECTOR_ELT(res, 2);
157157
SET_VECTOR_ELT(res, 3, Rf_allocVector(STRSXP, num_nodes));
158-
SEXP res_code = VECTOR_ELT(res, 3);
159-
SET_VECTOR_ELT(res, 4, Rf_allocVector(INTSXP, num_nodes));
160-
SEXP res_start_byte = VECTOR_ELT(res, 4);
158+
SEXP res_type = VECTOR_ELT(res, 3);
159+
SET_VECTOR_ELT(res, 4, Rf_allocVector(STRSXP, num_nodes));
160+
SEXP res_code = VECTOR_ELT(res, 4);
161161
SET_VECTOR_ELT(res, 5, Rf_allocVector(INTSXP, num_nodes));
162-
SEXP res_end_byte = VECTOR_ELT(res, 5);
162+
SEXP res_start_byte = VECTOR_ELT(res, 5);
163163
SET_VECTOR_ELT(res, 6, Rf_allocVector(INTSXP, num_nodes));
164-
SEXP res_start_row = VECTOR_ELT(res, 6);
164+
SEXP res_end_byte = VECTOR_ELT(res, 6);
165165
SET_VECTOR_ELT(res, 7, Rf_allocVector(INTSXP, num_nodes));
166-
SEXP res_start_column = VECTOR_ELT(res, 7);
166+
SEXP res_start_row = VECTOR_ELT(res, 7);
167167
SET_VECTOR_ELT(res, 8, Rf_allocVector(INTSXP, num_nodes));
168-
SEXP res_end_row = VECTOR_ELT(res, 8);
168+
SEXP res_start_column = VECTOR_ELT(res, 8);
169169
SET_VECTOR_ELT(res, 9, Rf_allocVector(INTSXP, num_nodes));
170-
SEXP res_end_column = VECTOR_ELT(res, 9);
170+
SEXP res_end_row = VECTOR_ELT(res, 9);
171+
SET_VECTOR_ELT(res, 10, Rf_allocVector(INTSXP, num_nodes));
172+
SEXP res_end_column = VECTOR_ELT(res, 10);
171173

172174
TSTreeCursor cursor = ts_tree_cursor_new(root);
173175
r_call_on_exit((cleanup_fn_t) ts_tree_cursor_delete, &cursor);
@@ -180,6 +182,9 @@ SEXP token_table(SEXP input, SEXP rlanguage, SEXP rranges) {
180182
TSNode crnt = ts_tree_cursor_current_node(&cursor);
181183
INTEGER(res_id)[idx] = idx + 1;
182184
INTEGER(res_parent)[idx] = parent + 1;
185+
const char *field_name = ts_tree_cursor_current_field_name(&cursor);
186+
SET_STRING_ELT(res_field_name, idx,
187+
field_name ? Rf_mkChar(field_name) : R_NaString);
183188
const char *type = ts_node_type(crnt);
184189
SET_STRING_ELT(res_type, idx, Rf_mkChar(type));
185190
uint32_t sb = ts_node_start_byte(crnt);

0 commit comments

Comments
 (0)