From 8058500c5be751555f3a914e38d979d4133c8112 Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 12 Nov 2015 15:32:53 -0500 Subject: [PATCH] Add source info to TSTree --- spec/runtime/stack_spec.cc | 2 +- spec/runtime/tree_spec.cc | 20 +++++++++++++------- src/runtime/length.h | 8 ++++++++ src/runtime/lexer.c | 7 +++++-- src/runtime/node.c | 5 ++++- src/runtime/tree.c | 14 +++++++++++--- src/runtime/tree.h | 9 +++++++-- 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 05be07e8..319830c3 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -43,7 +43,7 @@ describe("Stack", [&]() { TSLength len = ts_length_make(2, 2); for (size_t i = 0; i < tree_count; i++) - trees[i] = ts_tree_make_leaf(ts_builtin_sym_start + i, len, len, TSNodeTypeNamed); + trees[i] = ts_tree_make_leaf(ts_builtin_sym_start + i, len, len, {1, 1}, {1, 2}, TSNodeTypeNamed); }); after_each([&]() { diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index c2629fb3..33e344e2 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -29,8 +29,8 @@ describe("Tree", []() { TSTree *tree1, *tree2, *parent1; before_each([&]() { - tree1 = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, TSNodeTypeNamed); - tree2 = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, TSNodeTypeNamed); + tree1 = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, ts_source_info_zero(), ts_source_info_zero(), TSNodeTypeNamed); + tree2 = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, ts_source_info_zero(), ts_source_info_zero(), TSNodeTypeNamed); parent1 = ts_tree_make_node(dog, 2, tree_array({ tree1, tree2, @@ -55,6 +55,8 @@ describe("Tree", []() { TSTree *error_tree = ts_tree_make_error( ts_length_zero(), ts_length_zero(), + ts_source_info_zero(), + ts_source_info_zero(), 'z'); AssertThat(ts_tree_is_fragile_left(error_tree), IsTrue()); @@ -145,9 +147,9 @@ describe("Tree", []() { before_each([&]() { tree = ts_tree_make_node(cat, 3, tree_array({ - ts_tree_make_leaf(dog, {2, 2}, {3, 3}, TSNodeTypeNamed), - ts_tree_make_leaf(eel, {2, 2}, {3, 3}, TSNodeTypeNamed), - ts_tree_make_leaf(fox, {2, 2}, {3, 3}, TSNodeTypeNamed), + ts_tree_make_leaf(dog, {2, 2}, {3, 3}, {1, 2}, {1, 3}, TSNodeTypeNamed), + ts_tree_make_leaf(eel, {2, 2}, {3, 3}, {1, 2}, {1, 3}, TSNodeTypeNamed), + ts_tree_make_leaf(fox, {2, 2}, {3, 3}, {1, 2}, {1, 3}, TSNodeTypeNamed), }), TSNodeTypeNamed); AssertThat(tree->padding, Equals({2, 2})); @@ -266,10 +268,10 @@ describe("Tree", []() { describe("equality", [&]() { it("returns true for identical trees", [&]() { - TSTree *tree1_copy = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, TSNodeTypeNamed); + TSTree *tree1_copy = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, {1, 1}, {1, 4}, TSNodeTypeNamed); AssertThat(ts_tree_eq(tree1, tree1_copy), IsTrue()); - TSTree *tree2_copy = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, TSNodeTypeNamed); + TSTree *tree2_copy = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, {1, 1}, {1, 3}, TSNodeTypeNamed); AssertThat(ts_tree_eq(tree2, tree2_copy), IsTrue()); TSTree *parent2 = ts_tree_make_node(dog, 2, tree_array({ @@ -289,6 +291,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, + tree1->start_source_info, + tree1->end_source_info, TSNodeTypeNamed); AssertThat(ts_tree_eq(tree1, different_tree), IsFalse()); @@ -300,6 +304,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, + tree1->start_source_info, + tree1->end_source_info, TSNodeTypeNamed); TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({ diff --git a/src/runtime/length.h b/src/runtime/length.h index 58e078e4..482e49a1 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -36,6 +36,14 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { return len1.bytes == len2.bytes && len1.chars == len2.chars; } +static inline TSSourceInfo ts_source_info_zero() { + return (TSSourceInfo){ .line = 1, .column = 1 }; +} + +static inline TSSourceInfo ts_source_info_make(size_t line, size_t column) { + return (TSSourceInfo){ .line = line, .column = column }; +} + static inline TSLength ts_length_make(size_t bytes, size_t chars) { TSLength result; result.bytes = bytes; diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 249beb66..2e31accf 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -96,10 +96,13 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); - return ts_tree_make_error(size, padding, self->lookahead); + return ts_tree_make_error(size, padding, self->token_start_source_info, + self->token_end_source_info, self->lookahead); } else { DEBUG("accept_token sym:%s", symbol_name); - return ts_tree_make_leaf(symbol, padding, size, node_type); + return ts_tree_make_leaf(symbol, padding, size, + self->token_start_source_info, + self->token_end_source_info, node_type); } } diff --git a/src/runtime/node.c b/src/runtime/node.c index f6919d2a..a4968b24 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -5,7 +5,10 @@ #include "runtime/document.h" TSNode ts_node_make(const TSTree *tree, TSLength offset) { - return (TSNode){.data = tree, .offset = offset }; + TSSourceInfo start = tree != NULL ? tree->start_source_info : ts_source_info_zero(); + TSSourceInfo end = tree != NULL ? tree->end_source_info : ts_source_info_zero(); + + return (TSNode){.data = tree, .offset = offset, .start = start, .end = end }; } /* diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 822e51f8..eeb9f9b9 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -7,6 +7,8 @@ #include "runtime/length.h" TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, + TSSourceInfo start_source_info, + TSSourceInfo end_source_info, TSNodeType node_type) { TSTree *result = malloc(sizeof(TSTree)); *result = (TSTree){ @@ -18,6 +20,8 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, .named_child_count = 0, .children = NULL, .padding = padding, + .start_source_info = start_source_info, + .end_source_info = end_source_info, .options = {.type = node_type }, }; @@ -29,9 +33,13 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, return result; } -TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) { +TSTree *ts_tree_make_error(TSLength size, TSLength padding, + TSSourceInfo start_source_info, + TSSourceInfo end_source_info, + char lookahead_char) { TSTree *result = - ts_tree_make_leaf(ts_builtin_sym_error, padding, size, TSNodeTypeNamed); + ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_source_info, + end_source_info, TSNodeTypeNamed); result->lookahead_char = lookahead_char; return result; } @@ -82,7 +90,7 @@ 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 *result = - ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), node_type); + ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_source_info_zero(), ts_source_info_zero(), node_type); ts_tree__set_children(result, children, child_count); return result; } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 88208831..263f9bc2 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -24,6 +24,8 @@ struct TSTree { TSLength padding; TSLength size; TSSymbol symbol; + TSSourceInfo start_source_info; + TSSourceInfo end_source_info; struct { TSNodeType type : 2; bool extra : 1; @@ -34,9 +36,12 @@ struct TSTree { unsigned short int ref_count; }; -TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSNodeType); +TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSSourceInfo, + TSSourceInfo, TSNodeType); TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSNodeType); -TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char); +TSTree *ts_tree_make_error(TSLength size, TSLength padding, + TSSourceInfo start_source_info, + TSSourceInfo end_source_info, char lookahead_char); void ts_tree_retain(TSTree *tree); void ts_tree_release(TSTree *tree); bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2);