From e7209226624adb853194e19a307f20bc127316de Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 12 Nov 2015 12:24:05 -0500 Subject: [PATCH 01/40] Add source info to TSLexer --- include/tree_sitter/parser.h | 4 ++++ include/tree_sitter/runtime.h | 7 +++++++ src/runtime/lexer.c | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index bb29a627..9aa4939f 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -38,6 +38,10 @@ typedef struct TSLexer { TSLength token_end_position; TSLength token_start_position; + TSSourceInfo current_source_info; + TSSourceInfo token_end_source_info; + TSSourceInfo token_start_source_info; + size_t lookahead_size; int32_t lookahead; diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 1143ad0e..c358f437 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -35,9 +35,16 @@ typedef struct { size_t chars_removed; } TSInputEdit; +typedef struct { + size_t line; + size_t column; +} TSSourceInfo; + typedef struct { const void *data; TSLength offset; + TSSourceInfo start; + TSSourceInfo end; } TSNode; typedef unsigned short TSSymbol; diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index b2b21819..e2b67610 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -53,6 +53,10 @@ static void ts_lexer__start(TSLexer *self, TSStateId lex_state) { static void ts_lexer__start_token(TSLexer *self) { DEBUG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; + + DEBUG("start_token line:%lu", self->current_source_info.line); + DEBUG("start_token column:%lu", self->current_source_info.column); + self->token_start_source_info = self->current_source_info; } static bool ts_lexer__advance(TSLexer *self, TSStateId state) { From e60ab58187c7b8851f7eceb32789f993f6008dcb Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 12 Nov 2015 13:25:35 -0500 Subject: [PATCH 02/40] maybe increment line and column here? --- src/runtime/lexer.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index e2b67610..5fda417d 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -68,6 +68,13 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead_size) { self->current_position.bytes += self->lookahead_size; self->current_position.chars += 1; + + if (self->lookahead == '\n') { + self->current_source_info.line += 1; + self->current_source_info.column = 0; + } else { + self->current_source_info.column += 1; + } } if (self->current_position.bytes >= self->chunk_start + self->chunk_size) From bf666351e9740bd79913d4b9b9696016fa0aaa5c Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 12 Nov 2015 13:28:33 -0500 Subject: [PATCH 03/40] Set token_end_source_info --- src/runtime/lexer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 5fda417d..249beb66 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -92,6 +92,8 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, ts_length_sub(self->token_start_position, self->token_end_position); self->token_end_position = self->current_position; + self->token_end_source_info = self->current_source_info; + if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); return ts_tree_make_error(size, padding, self->lookahead); From 8058500c5be751555f3a914e38d979d4133c8112 Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 12 Nov 2015 15:32:53 -0500 Subject: [PATCH 04/40] 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); From fc49a3949a542fb9a7f13f76a3b41995010e3c35 Mon Sep 17 00:00:00 2001 From: joshvera Date: Fri, 13 Nov 2015 14:13:16 -0500 Subject: [PATCH 05/40] try resetting to 1 --- src/runtime/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 2e31accf..44c6a285 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -71,7 +71,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead == '\n') { self->current_source_info.line += 1; - self->current_source_info.column = 0; + self->current_source_info.column = 1; } else { self->current_source_info.column += 1; } From a85b7fe3c408304e00493db6a5b15fe9bb72bc81 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 16 Nov 2015 16:59:12 -0800 Subject: [PATCH 06/40] start column at 0 again --- src/runtime/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 44c6a285..2e31accf 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -71,7 +71,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead == '\n') { self->current_source_info.line += 1; - self->current_source_info.column = 1; + self->current_source_info.column = 0; } else { self->current_source_info.column += 1; } From b0f6bac3ab7745b62b8e2f153a3b69d9fabb5012 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 16:34:50 -0800 Subject: [PATCH 07/40] replace start and end with padding and size --- include/tree_sitter/parser.h | 6 +++--- include/tree_sitter/runtime.h | 5 ++--- spec/runtime/tree_spec.cc | 16 +++++++-------- src/runtime/document.c | 2 +- src/runtime/length.h | 34 +++++++++++++++++++++++++++---- src/runtime/lexer.c | 31 ++++++++++++++++------------ src/runtime/lexer.h | 2 +- src/runtime/node.c | 38 +++++++++++++++++++++++------------ src/runtime/node.h | 2 +- src/runtime/parser.c | 9 ++++++--- src/runtime/tree.c | 22 +++++++++++--------- src/runtime/tree.h | 17 ++++++++++------ 12 files changed, 119 insertions(+), 65 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 9aa4939f..722c5208 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -38,9 +38,9 @@ typedef struct TSLexer { TSLength token_end_position; TSLength token_start_position; - TSSourceInfo current_source_info; - TSSourceInfo token_end_source_info; - TSSourceInfo token_start_source_info; + TSPoint current_point; + TSPoint token_end_point; + TSPoint token_start_point; size_t lookahead_size; int32_t lookahead; diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index c358f437..fc041596 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -38,13 +38,12 @@ typedef struct { typedef struct { size_t line; size_t column; -} TSSourceInfo; +} TSPoint; typedef struct { const void *data; TSLength offset; - TSSourceInfo start; - TSSourceInfo end; + TSPoint point; } TSNode; typedef unsigned short TSSymbol; diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index 33e344e2..99125dbe 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}, 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); + tree1 = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, ts_point_zero(), ts_point_zero(), TSNodeTypeNamed); + tree2 = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, ts_point_zero(), ts_point_zero(), TSNodeTypeNamed); parent1 = ts_tree_make_node(dog, 2, tree_array({ tree1, tree2, @@ -55,8 +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(), + ts_point_zero(), + ts_point_zero(), 'z'); AssertThat(ts_tree_is_fragile_left(error_tree), IsTrue()); @@ -291,8 +291,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, - tree1->start_source_info, - tree1->end_source_info, + tree1->padding_point, + tree1->size_point, TSNodeTypeNamed); AssertThat(ts_tree_eq(tree1, different_tree), IsFalse()); @@ -304,8 +304,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, - tree1->start_source_info, - tree1->end_source_info, + tree1->padding_point, + tree1->size_point, TSNodeTypeNamed); TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({ diff --git a/src/runtime/document.c b/src/runtime/document.c index 091bda23..7042516b 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) { } TSNode ts_document_root_node(const TSDocument *self) { - TSNode result = ts_node_make(self->tree, ts_length_zero()); + TSNode result = ts_node_make(self->tree, ts_length_zero(), ts_point_zero()); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; diff --git a/src/runtime/length.h b/src/runtime/length.h index 482e49a1..562e626d 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -26,6 +26,32 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { return result; } +static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { + size_t line = point1.line + point2.line; + + size_t column; + if (point2.line == 0) { + column = point1.column + point2.column; + } else { + column = point2.column; + } + + return (TSPoint){ .line = line, .column = column }; +} + +static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { + size_t line, column; + if (point1.line == point2.line) { + line = 0; + column = point1.column - point2.column; + } else { + line = point1.line - point2.line; + column = point1.column; + } + + return (TSPoint){ .line = line, .column = column }; +} + static inline TSLength ts_length_zero() { TSLength result; result.bytes = result.chars = 0; @@ -36,12 +62,12 @@ 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 TSPoint ts_point_zero() { + return (TSPoint){ .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 TSPoint ts_point_make(size_t line, size_t column) { + return (TSPoint){ .line = line, .column = column }; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 2e31accf..50fddf88 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -54,9 +54,9 @@ static void ts_lexer__start_token(TSLexer *self) { DEBUG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; - DEBUG("start_token line:%lu", self->current_source_info.line); - DEBUG("start_token column:%lu", self->current_source_info.column); - self->token_start_source_info = self->current_source_info; + DEBUG("start_token line:%lu", self->current_point.line); + DEBUG("start_token column:%lu", self->current_point.column); + self->token_start_point = self->current_point; } static bool ts_lexer__advance(TSLexer *self, TSStateId state) { @@ -70,10 +70,10 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { self->current_position.chars += 1; if (self->lookahead == '\n') { - self->current_source_info.line += 1; - self->current_source_info.column = 0; + self->current_point.line += 1; + self->current_point.column = 0; } else { - self->current_source_info.column += 1; + self->current_point.column += 1; } } @@ -92,17 +92,17 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, ts_length_sub(self->token_start_position, self->token_end_position); self->token_end_position = self->current_position; - self->token_end_source_info = self->current_source_info; + self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); - return ts_tree_make_error(size, padding, self->token_start_source_info, - self->token_end_source_info, self->lookahead); + return ts_tree_make_error(size, padding, self->token_start_point, + self->token_end_point, self->lookahead); } else { DEBUG("accept_token sym:%s", symbol_name); return ts_tree_make_leaf(symbol, padding, size, - self->token_start_source_info, - self->token_end_source_info, node_type); + self->token_start_point, + self->token_end_point, node_type); } } @@ -121,14 +121,19 @@ TSLexer ts_lexer_make() { .chunk_start = 0, .debugger = ts_debugger_null(), }; - ts_lexer_reset(&result, ts_length_zero()); + ts_lexer_reset(&result, ts_length_zero(), ts_point_zero()); return result; } -void ts_lexer_reset(TSLexer *self, TSLength position) { +void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) { self->token_start_position = position; self->token_end_position = position; self->current_position = position; + + self->token_start_point = point; + self->token_end_point = point; + self->current_point = point; + self->chunk = 0; self->chunk_start = 0; self->chunk_size = 0; diff --git a/src/runtime/lexer.h b/src/runtime/lexer.h index b127d483..2c4b5812 100644 --- a/src/runtime/lexer.h +++ b/src/runtime/lexer.h @@ -8,7 +8,7 @@ extern "C" { #include "tree_sitter/parser.h" TSLexer ts_lexer_make(); -void ts_lexer_reset(TSLexer *, TSLength); +void ts_lexer_reset(TSLexer *, TSLength, TSPoint); #ifdef __cplusplus } diff --git a/src/runtime/node.c b/src/runtime/node.c index a4968b24..a36105c5 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,11 +4,8 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength 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 }; +TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint point) { + return (TSNode){.data = tree, .offset = offset, .point = point }; } /* @@ -16,7 +13,7 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset) { */ static inline TSNode ts_node__null() { - return ts_node_make(NULL, ts_length_zero()); + return ts_node_make(NULL, ts_length_zero(), ts_point_zero()); } static inline const TSTree *ts_node__tree(TSNode self) { @@ -27,10 +24,16 @@ static inline TSLength ts_node__offset(TSNode self) { return self.offset; } + +static inline TSPoint ts_node__point(TSNode self) { + return self.point; +} + static inline TSNode ts_node__child(TSNode self, size_t child_index, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); bool did_descend = true; while (did_descend) { @@ -41,7 +44,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, TSTree *child = tree->children[i]; if (child->options.type >= type) { if (index == child_index) - return ts_node_make(child, position); + return ts_node_make(child, position, point); index++; } else { size_t grandchild_index = child_index - index; @@ -66,10 +69,12 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { size_t index = tree->context.index; position = ts_length_sub(position, tree->context.offset); + point = ts_point_sub(point, tree->context.offset_point); tree = tree->context.parent; if (!tree) break; @@ -77,13 +82,14 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { for (size_t i = index - 1; i + 1 > 0; i--) { const TSTree *child = tree->children[i]; TSLength child_position = ts_length_add(position, child->context.offset); + TSPoint child_point = ts_point_add(point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position); + return ts_node_make(child, child_position, child_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position), + return ts_node__child(ts_node_make(child, child_position, child_point), grandchild_count - 1, type); } } while (!ts_tree_is_visible(tree)); @@ -94,6 +100,7 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { size_t index = tree->context.index; @@ -105,13 +112,14 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { for (size_t i = index + 1; i < tree->child_count; i++) { const TSTree *child = tree->children[i]; TSLength child_position = ts_length_add(position, child->context.offset); + TSPoint child_point = ts_point_add(point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position); + return ts_node_make(child, child_position, child_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position), 0, type); + return ts_node__child(ts_node_make(child, child_position, child_point), 0, type); } } while (!ts_tree_is_visible(tree)); @@ -122,6 +130,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, size_t max, TSNodeType type) { const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree; TSLength position = ts_node__offset(self), last_visible_position = position; + TSPoint point = ts_node__point(self), last_visible_point = point; bool did_descend = true; while (did_descend) { @@ -144,7 +153,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, } } - return ts_node_make(last_visible_tree, last_visible_position); + return ts_node_make(last_visible_tree, last_visible_position, last_visible_point); } /* @@ -188,15 +197,18 @@ bool ts_node_has_changes(TSNode self) { TSNode ts_node_parent(TSNode self) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { position = ts_length_sub(position, tree->context.offset); + point = ts_point_sub(point, tree->context.offset_point); + tree = tree->context.parent; if (!tree) return ts_node__null(); } while (!ts_tree_is_visible(tree)); - return ts_node_make(tree, position); + return ts_node_make(tree, position, point); } TSNode ts_node_child(TSNode self, size_t child_index) { diff --git a/src/runtime/node.h b/src/runtime/node.h index f1ea67b3..3b269887 100644 --- a/src/runtime/node.h +++ b/src/runtime/node.h @@ -5,6 +5,6 @@ #include "runtime/length.h" #include "runtime/tree.h" -TSNode ts_node_make(const TSTree *, TSLength); +TSNode ts_node_make(const TSTree *, TSLength, TSPoint); #endif diff --git a/src/runtime/parser.c b/src/runtime/parser.c index b338adaf..7e06ec76 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -119,18 +119,21 @@ static void ts_parser__get_next_lookahead(TSParser *self) { self->lookahead = self->reusable_subtree; TSLength size = ts_tree_total_size(self->lookahead); + TSPoint point_size = ts_tree_total_size_point(self->lookahead); DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(self->lookahead->symbol), size.chars, self->lookahead->options.extra); ts_lexer_reset(&self->lexer, - ts_length_add(self->lexer.current_position, size)); + ts_length_add(self->lexer.current_position, size), + ts_point_add(self->lexer.current_point, point_size)); ts_parser__pop_reusable_subtree(self); return; } TSLength position = self->lexer.current_position; + TSPoint point = self->lexer.current_point; for (size_t i = 0, count = ts_stack_head_count(self->stack); i < count; i++) { if (i > 0) { - ts_lexer_reset(&self->lexer, position); + ts_lexer_reset(&self->lexer, position, point); ts_tree_release(self->lookahead); } @@ -285,7 +288,7 @@ static void ts_parser__start(TSParser *self, TSInput input, } self->lexer.input = input; - ts_lexer_reset(&self->lexer, ts_length_zero()); + ts_lexer_reset(&self->lexer, ts_length_zero(), ts_point_zero()); ts_stack_clear(self->stack); self->reusable_subtree = previous_tree; diff --git a/src/runtime/tree.c b/src/runtime/tree.c index eeb9f9b9..16117adb 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -7,8 +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, + TSPoint padding_point, + TSPoint size_point, TSNodeType node_type) { TSTree *result = malloc(sizeof(TSTree)); *result = (TSTree){ @@ -20,8 +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, + .padding_point = padding_point, + .size_point = size_point, .options = {.type = node_type }, }; @@ -34,12 +34,12 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, } TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSSourceInfo start_source_info, - TSSourceInfo end_source_info, + TSPoint start_point, + TSPoint end_point, char lookahead_char) { TSTree *result = - ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_source_info, - end_source_info, TSNodeTypeNamed); + ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_point, + end_point, TSNodeTypeNamed); result->lookahead_char = lookahead_char; return result; } @@ -90,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(), ts_source_info_zero(), ts_source_info_zero(), node_type); + ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_point_zero(), ts_point_zero(), node_type); ts_tree__set_children(result, children, child_count); return result; } @@ -116,6 +116,10 @@ TSLength ts_tree_total_size(const TSTree *self) { return ts_length_add(self->padding, self->size); } +TSPoint ts_tree_total_size_point(const TSTree *self) { + return ts_point_add(self->padding_point, self->size_point); +} + bool ts_tree_eq(const TSTree *self, const TSTree *other) { if (self) { if (!other) diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 263f9bc2..9a51a022 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -13,6 +13,7 @@ struct TSTree { struct TSTree *parent; size_t index; TSLength offset; + TSPoint offset_point; } context; size_t child_count; size_t visible_child_count; @@ -23,9 +24,12 @@ struct TSTree { }; TSLength padding; TSLength size; + + TSPoint padding_point; + TSPoint size_point; + TSSymbol symbol; - TSSourceInfo start_source_info; - TSSourceInfo end_source_info; + struct { TSNodeType type : 2; bool extra : 1; @@ -36,18 +40,19 @@ struct TSTree { unsigned short int ref_count; }; -TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSSourceInfo, - TSSourceInfo, TSNodeType); +TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSPoint, + TSPoint, TSNodeType); TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSNodeType); TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSSourceInfo start_source_info, - TSSourceInfo end_source_info, char lookahead_char); + TSPoint padding_point, + TSPoint size_point, 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); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); TSLength ts_tree_total_size(const TSTree *tree); +TSPoint ts_tree_total_size_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); void ts_tree_edit(TSTree *, TSInputEdit); From 6485e27d70f17eb9bb4ba177dbdb7df5fc26bca4 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 16:45:10 -0800 Subject: [PATCH 08/40] add ts_node_end_point --- include/tree_sitter/runtime.h | 1 + src/runtime/node.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index fc041596..e7e8f899 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -52,6 +52,7 @@ typedef struct TSDocument TSDocument; TSLength ts_node_pos(TSNode); TSLength ts_node_size(TSNode); +TSPoint ts_node_end_point(TSNode); TSSymbol ts_node_symbol(TSNode); const char *ts_node_name(TSNode, const TSDocument *); const char *ts_node_string(TSNode, const TSDocument *); diff --git a/src/runtime/node.c b/src/runtime/node.c index a36105c5..d8fb80ff 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -168,6 +168,10 @@ TSLength ts_node_size(TSNode self) { return ts_node__tree(self)->size; } +TSLength ts_node_point(TSNode self) { + return ts_node__tree(self)->size_point; +} + TSSymbol ts_node_symbol(TSNode self) { return ts_node__tree(self)->symbol; } From eb2a18cf16292e48772c1d455835bbe2eb1708db Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 16:46:09 -0800 Subject: [PATCH 09/40] s/TSLength/TSPoint --- src/runtime/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index d8fb80ff..c93d4532 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -168,7 +168,7 @@ TSLength ts_node_size(TSNode self) { return ts_node__tree(self)->size; } -TSLength ts_node_point(TSNode self) { +TSPoint ts_node_point(TSNode self) { return ts_node__tree(self)->size_point; } From cf72f2f0aea4b1dcaf1d0de0d4d35c219370bfa9 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 16:47:27 -0800 Subject: [PATCH 10/40] ts_node_size_point --- include/tree_sitter/runtime.h | 2 +- src/runtime/node.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index e7e8f899..d499032b 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -52,7 +52,7 @@ typedef struct TSDocument TSDocument; TSLength ts_node_pos(TSNode); TSLength ts_node_size(TSNode); -TSPoint ts_node_end_point(TSNode); +TSPoint ts_node_size_point(TSNode); TSSymbol ts_node_symbol(TSNode); const char *ts_node_name(TSNode, const TSDocument *); const char *ts_node_string(TSNode, const TSDocument *); diff --git a/src/runtime/node.c b/src/runtime/node.c index c93d4532..af49e4f5 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -168,7 +168,7 @@ TSLength ts_node_size(TSNode self) { return ts_node__tree(self)->size; } -TSPoint ts_node_point(TSNode self) { +TSPoint ts_node_size_point(TSNode self) { return ts_node__tree(self)->size_point; } From 8446b657f08a55f22ca271082acf9064bc5e0658 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 17:53:38 -0800 Subject: [PATCH 11/40] Rename position to offset and point to offset_point --- include/tree_sitter/runtime.h | 2 +- src/runtime/node.c | 52 ++++++++++++++++++++--------------- src/runtime/tree.c | 1 + 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index d499032b..1c0b47e8 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -43,7 +43,7 @@ typedef struct { typedef struct { const void *data; TSLength offset; - TSPoint point; + TSPoint offset_point; } TSNode; typedef unsigned short TSSymbol; diff --git a/src/runtime/node.c b/src/runtime/node.c index af49e4f5..3ec0678f 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,8 +4,8 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint point) { - return (TSNode){.data = tree, .offset = offset, .point = point }; +TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint offset_point) { + return (TSNode){.data = tree, .offset = offset, .offset_point = offset_point }; } /* @@ -25,15 +25,15 @@ static inline TSLength ts_node__offset(TSNode self) { } -static inline TSPoint ts_node__point(TSNode self) { - return self.point; +static inline TSPoint ts_node__offset_point(TSNode self) { + return self.offset_point; } static inline TSNode ts_node__child(TSNode self, size_t child_index, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__point(self); + TSPoint point = ts_node__offset_point(self); bool did_descend = true; while (did_descend) { @@ -68,28 +68,28 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); - TSLength position = ts_node__offset(self); - TSPoint point = ts_node__point(self); + TSLength offset = ts_node__offset(self); + TSPoint offset_point = ts_node__offset_point(self); do { size_t index = tree->context.index; - position = ts_length_sub(position, tree->context.offset); - point = ts_point_sub(point, tree->context.offset_point); + offset = ts_length_sub(offset, tree->context.offset); + offset_point = ts_find_parent_offset_point(tree, point); tree = tree->context.parent; if (!tree) break; for (size_t i = index - 1; i + 1 > 0; i--) { const TSTree *child = tree->children[i]; - TSLength child_position = ts_length_add(position, child->context.offset); - TSPoint child_point = ts_point_add(point, child->context.offset_point); + TSLength child_offset = ts_length_add(offset, child->context.offset); + TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position, child_point); + return ts_node_make(child, child_offset, child_offset_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position, child_point), + return ts_node__child(ts_node_make(child, child_offset, child_offset_point), grandchild_count - 1, type); } } while (!ts_tree_is_visible(tree)); @@ -99,27 +99,27 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); - TSLength position = ts_node__offset(self); - TSPoint point = ts_node__point(self); + TSLength offset = ts_node__offset(self); + TSPoint offset_point = ts_node__offset_point(self); do { size_t index = tree->context.index; - position = ts_length_sub(position, tree->context.offset); + offset = ts_length_sub(offset, tree->context.offset); tree = tree->context.parent; if (!tree) break; for (size_t i = index + 1; i < tree->child_count; i++) { const TSTree *child = tree->children[i]; - TSLength child_position = ts_length_add(position, child->context.offset); - TSPoint child_point = ts_point_add(point, child->context.offset_point); + TSLength child_offset = ts_length_add(offset, child->context.offset); + TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position, child_point); + return ts_node_make(child, child_offset, child_offset_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position, child_point), 0, type); + return ts_node__child(ts_node_make(child, child_offset, child_offset_point), 0, type); } } while (!ts_tree_is_visible(tree)); @@ -130,7 +130,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, size_t max, TSNodeType type) { const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree; TSLength position = ts_node__offset(self), last_visible_position = position; - TSPoint point = ts_node__point(self), last_visible_point = point; + TSPoint point = ts_node__offset_point(self), last_visible_point = point; bool did_descend = true; while (did_descend) { @@ -172,6 +172,14 @@ TSPoint ts_node_size_point(TSNode self) { return ts_node__tree(self)->size_point; } +TSPoint ts_node_start_point(TSNode self) { + return ts_point_add(ts_node__offset_point(self), ts_node__tree(self)->padding_point); +} + +TSPoint ts_node_end_point(TSNode self) { + return ts_point_add(ts_node_start_point(self), ts_node_size_point(self)); +} + TSSymbol ts_node_symbol(TSNode self) { return ts_node__tree(self)->symbol; } @@ -201,7 +209,7 @@ bool ts_node_has_changes(TSNode self) { TSNode ts_node_parent(TSNode self) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__point(self); + TSPoint point = ts_node__offset_point(self); do { position = ts_length_sub(position, tree->context.offset); diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 16117adb..d7347ab6 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -55,6 +55,7 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, child->context.parent = self; child->context.index = i; child->context.offset = ts_tree_total_size(self); + child->context.end_point = ts_tree_end_point(self); if (i == 0) { self->padding = child->padding; From 2669933d06fd936bb9b2f65be1a2526f9fcfd37e Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 11:08:44 -0500 Subject: [PATCH 12/40] Implement ts_find_parent_offset_point --- src/runtime/node.c | 2 +- src/runtime/tree.c | 13 +++++++++++++ src/runtime/tree.h | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index 3ec0678f..8dbb255f 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -74,7 +74,7 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { do { size_t index = tree->context.index; offset = ts_length_sub(offset, tree->context.offset); - offset_point = ts_find_parent_offset_point(tree, point); + offset_point = ts_find_parent_offset_point(tree); tree = tree->context.parent; if (!tree) break; diff --git a/src/runtime/tree.c b/src/runtime/tree.c index d7347ab6..31c13815 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -113,6 +113,19 @@ void ts_tree_release(TSTree *self) { } } +TSPoint ts_find_parent_offset_point(const TSTree *self) { + TSPoint size_point = self->size_point; + const TSTree *parent = self; + TSPoint current_offset_point; + + do { + parent = parent->context.parent; + current_offset_point = parent->context.offset_point; + } while (current_offset_point.line == 0); + + return (TSPoint){ .line = size_point.line, .column = current_offset_point.column }; +} + TSLength ts_tree_total_size(const TSTree *self) { return ts_length_add(self->padding, self->size); } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 9a51a022..582b3381 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -51,6 +51,8 @@ void ts_tree_release(TSTree *tree); bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); + +TSPoint ts_find_parent_offset_point(const TSTree *self); TSLength ts_tree_total_size(const TSTree *tree); TSPoint ts_tree_total_size_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); From ad58b752e62df2fcbe384d203badb2ba1a5060e4 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 11:28:28 -0500 Subject: [PATCH 13/40] Rename ts_tree_total_size_point to ts_tree_offset_point --- src/runtime/parser.c | 4 ++-- src/runtime/tree.c | 4 ++-- src/runtime/tree.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 7e06ec76..bd477d3d 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -119,12 +119,12 @@ static void ts_parser__get_next_lookahead(TSParser *self) { self->lookahead = self->reusable_subtree; TSLength size = ts_tree_total_size(self->lookahead); - TSPoint point_size = ts_tree_total_size_point(self->lookahead); + TSPoint offset_point = ts_tree_offset_point(self->lookahead); DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(self->lookahead->symbol), size.chars, self->lookahead->options.extra); ts_lexer_reset(&self->lexer, ts_length_add(self->lexer.current_position, size), - ts_point_add(self->lexer.current_point, point_size)); + ts_point_add(self->lexer.current_point, offset_point)); ts_parser__pop_reusable_subtree(self); return; } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 31c13815..ed9a8b9d 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -55,7 +55,7 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, child->context.parent = self; child->context.index = i; child->context.offset = ts_tree_total_size(self); - child->context.end_point = ts_tree_end_point(self); + child->context.offset_point = ts_tree_offset_point(self); if (i == 0) { self->padding = child->padding; @@ -130,7 +130,7 @@ TSLength ts_tree_total_size(const TSTree *self) { return ts_length_add(self->padding, self->size); } -TSPoint ts_tree_total_size_point(const TSTree *self) { +TSPoint ts_tree_offset_point(const TSTree *self) { return ts_point_add(self->padding_point, self->size_point); } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 582b3381..cd83d0de 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -54,7 +54,7 @@ char *ts_tree_string(const TSTree *tree, const char **names, TSPoint ts_find_parent_offset_point(const TSTree *self); TSLength ts_tree_total_size(const TSTree *tree); -TSPoint ts_tree_total_size_point(const TSTree *self); +TSPoint ts_tree_offset_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); void ts_tree_edit(TSTree *, TSInputEdit); From 4663b9ce891a7c0964412c9587ed66a0f3ce5487 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 11:44:13 -0500 Subject: [PATCH 14/40] Add padding and size points to ts_tree_make_leaf in ts_lexer__accept --- src/runtime/lexer.c | 10 ++++++---- src/runtime/tree.c | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 50fddf88..09e567f6 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -92,17 +92,19 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, ts_length_sub(self->token_start_position, self->token_end_position); self->token_end_position = self->current_position; + TSPoint size_point = ts_point_sub(self->current_point, self ->token_start_point); + TSPoint padding_point = ts_point_sub(self->token_start_point, self ->token_end_point); self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); - return ts_tree_make_error(size, padding, self->token_start_point, - self->token_end_point, self->lookahead); + return ts_tree_make_error(size, padding, size_point, + padding_point, self->lookahead); } else { DEBUG("accept_token sym:%s", symbol_name); return ts_tree_make_leaf(symbol, padding, size, - self->token_start_point, - self->token_end_point, node_type); + padding_point, + size_point, node_type); } } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index ed9a8b9d..d51ac0e1 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -34,12 +34,12 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, } TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSPoint start_point, - TSPoint end_point, + TSPoint size_point, + TSPoint padding_point, char lookahead_char) { TSTree *result = - ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_point, - end_point, TSNodeTypeNamed); + ts_tree_make_leaf(ts_builtin_sym_error, padding, size, padding_point, + size_point, TSNodeTypeNamed); result->lookahead_char = lookahead_char; return result; } From 1687d66776c9e0f804a79d125daf8b2fa285e943 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 11:57:39 -0500 Subject: [PATCH 15/40] expose ts_node start and end point functions --- include/tree_sitter/runtime.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 1c0b47e8..69f5a1b1 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -53,6 +53,8 @@ typedef struct TSDocument TSDocument; TSLength ts_node_pos(TSNode); TSLength ts_node_size(TSNode); TSPoint ts_node_size_point(TSNode); +TSPoint ts_node_start_point(TSNode); +TSPoint ts_node_end_point(TSNode); TSSymbol ts_node_symbol(TSNode); const char *ts_node_name(TSNode, const TSDocument *); const char *ts_node_string(TSNode, const TSDocument *); From 3d9a44d880fd863841279648c0b394a5d9342221 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 13:36:19 -0500 Subject: [PATCH 16/40] Calculate the column and offset separately in TSNode --- include/tree_sitter/runtime.h | 4 +-- script/util/run_tests.sh | 6 ++--- spec/runtime/node_spec.cc | 6 +++++ src/runtime/document.c | 2 +- src/runtime/length.h | 22 ++++++++-------- src/runtime/lexer.c | 6 ++--- src/runtime/node.c | 48 +++++++++++++++++------------------ src/runtime/node.h | 2 +- src/runtime/tree.c | 20 ++++++++++----- src/runtime/tree.h | 2 +- 10 files changed, 66 insertions(+), 52 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 69f5a1b1..0eb769bf 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -36,14 +36,14 @@ typedef struct { } TSInputEdit; typedef struct { - size_t line; + size_t row; size_t column; } TSPoint; typedef struct { const void *data; TSLength offset; - TSPoint offset_point; + size_t row; } TSNode; typedef unsigned short TSSymbol; diff --git a/script/util/run_tests.sh b/script/util/run_tests.sh index 4e3759e4..df1f29db 100644 --- a/script/util/run_tests.sh +++ b/script/util/run_tests.sh @@ -68,10 +68,10 @@ function run_tests { ;; debug) - if which -s gdb; then - gdb $cmd -- "${args[@]}" - elif which -s lldb; then + if which -s lldb; then lldb $cmd -- "${args[@]}" + elif which -s gdb; then + gdb $cmd -- "${args[@]}" else echo "No debugger found" exit 1 diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index e58317c9..a85288d0 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -43,6 +43,12 @@ describe("Node", []() { AssertThat(ts_node_pos(array_node).bytes, Equals(2)); AssertThat(ts_node_size(array_node).bytes, Equals(25)); + AssertThat(ts_node_start_point(array_node).row, Equals(0)); + AssertThat(ts_node_start_point(array_node).column, Equals(2)); + + AssertThat(ts_node_end_point(array_node).row, Equals(0)); + AssertThat(ts_node_end_point(array_node).column, Equals(27)); + AssertThat(ts_node_pos(child1).bytes, Equals(3)); AssertThat(ts_node_size(child1).bytes, Equals(3)); diff --git a/src/runtime/document.c b/src/runtime/document.c index 7042516b..1afcb2f3 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) { } TSNode ts_document_root_node(const TSDocument *self) { - TSNode result = ts_node_make(self->tree, ts_length_zero(), ts_point_zero()); + TSNode result = ts_node_make(self->tree, ts_length_zero(), 0); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; diff --git a/src/runtime/length.h b/src/runtime/length.h index 562e626d..a3a574e6 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -27,29 +27,29 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { - size_t line = point1.line + point2.line; + size_t row = point1.row + point2.row; size_t column; - if (point2.line == 0) { + if (point2.row == 0) { column = point1.column + point2.column; } else { column = point2.column; } - return (TSPoint){ .line = line, .column = column }; + return (TSPoint){ .row = row, .column = column }; } static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { - size_t line, column; - if (point1.line == point2.line) { - line = 0; + size_t row, column; + if (point1.row == point2.row) { + row = 0; column = point1.column - point2.column; } else { - line = point1.line - point2.line; + row = point1.row - point2.row; column = point1.column; } - return (TSPoint){ .line = line, .column = column }; + return (TSPoint){ .row = row, .column = column }; } static inline TSLength ts_length_zero() { @@ -63,11 +63,11 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .line = 1, .column = 1 }; + return (TSPoint){ .row = 0, .column = 0 }; } -static inline TSPoint ts_point_make(size_t line, size_t column) { - return (TSPoint){ .line = line, .column = column }; +static inline TSPoint ts_point_make(size_t row, size_t column) { + return (TSPoint){ .row = row, .column = column }; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 09e567f6..5a6fcf1d 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -54,7 +54,7 @@ static void ts_lexer__start_token(TSLexer *self) { DEBUG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; - DEBUG("start_token line:%lu", self->current_point.line); + DEBUG("start_token row:%lu", self->current_point.row); DEBUG("start_token column:%lu", self->current_point.column); self->token_start_point = self->current_point; } @@ -70,7 +70,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { self->current_position.chars += 1; if (self->lookahead == '\n') { - self->current_point.line += 1; + self->current_point.row += 1; self->current_point.column = 0; } else { self->current_point.column += 1; @@ -93,7 +93,7 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, self->token_end_position = self->current_position; TSPoint size_point = ts_point_sub(self->current_point, self ->token_start_point); - TSPoint padding_point = ts_point_sub(self->token_start_point, self ->token_end_point); + TSPoint padding_point = ts_point_sub(self->token_start_point, self->token_end_point); self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { diff --git a/src/runtime/node.c b/src/runtime/node.c index 8dbb255f..ffff0ed4 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,8 +4,8 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint offset_point) { - return (TSNode){.data = tree, .offset = offset, .offset_point = offset_point }; +TSNode ts_node_make(const TSTree *tree, TSLength offset, size_t row) { + return (TSNode){.data = tree, .offset = offset, .row = row }; } /* @@ -13,7 +13,7 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint offset_point) { */ static inline TSNode ts_node__null() { - return ts_node_make(NULL, ts_length_zero(), ts_point_zero()); + return ts_node_make(NULL, ts_length_zero(), 0); } static inline const TSTree *ts_node__tree(TSNode self) { @@ -25,15 +25,11 @@ static inline TSLength ts_node__offset(TSNode self) { } -static inline TSPoint ts_node__offset_point(TSNode self) { - return self.offset_point; -} - static inline TSNode ts_node__child(TSNode self, size_t child_index, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__offset_point(self); + size_t offset_row = self.row; bool did_descend = true; while (did_descend) { @@ -44,7 +40,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, TSTree *child = tree->children[i]; if (child->options.type >= type) { if (index == child_index) - return ts_node_make(child, position, point); + return ts_node_make(child, position, offset_row); index++; } else { size_t grandchild_index = child_index - index; @@ -60,6 +56,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, index += grandchild_count; } position = ts_length_add(position, ts_tree_total_size(child)); + offset_row += child->padding_point.row + child->size_point.row; } } @@ -69,12 +66,12 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength offset = ts_node__offset(self); - TSPoint offset_point = ts_node__offset_point(self); + size_t offset_row = self.row; do { size_t index = tree->context.index; offset = ts_length_sub(offset, tree->context.offset); - offset_point = ts_find_parent_offset_point(tree); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) break; @@ -82,14 +79,14 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { for (size_t i = index - 1; i + 1 > 0; i--) { const TSTree *child = tree->children[i]; TSLength child_offset = ts_length_add(offset, child->context.offset); - TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); + size_t child_offset_row = offset_row + child->context.offset_point.row; if (child->options.type >= type) - return ts_node_make(child, child_offset, child_offset_point); + return ts_node_make(child, child_offset, child_offset_row); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_offset, child_offset_point), + return ts_node__child(ts_node_make(child, child_offset, child_offset_row), grandchild_count - 1, type); } } while (!ts_tree_is_visible(tree)); @@ -100,11 +97,12 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength offset = ts_node__offset(self); - TSPoint offset_point = ts_node__offset_point(self); + size_t offset_row = self.row; do { size_t index = tree->context.index; offset = ts_length_sub(offset, tree->context.offset); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) break; @@ -112,14 +110,14 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { for (size_t i = index + 1; i < tree->child_count; i++) { const TSTree *child = tree->children[i]; TSLength child_offset = ts_length_add(offset, child->context.offset); - TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); + size_t child_offset_row = offset_row + child->context.offset_point.row; if (child->options.type >= type) - return ts_node_make(child, child_offset, child_offset_point); + return ts_node_make(child, child_offset, child_offset_row); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_offset, child_offset_point), 0, type); + return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0, type); } } while (!ts_tree_is_visible(tree)); @@ -130,7 +128,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, size_t max, TSNodeType type) { const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree; TSLength position = ts_node__offset(self), last_visible_position = position; - TSPoint point = ts_node__offset_point(self), last_visible_point = point; + size_t offset_row = self.row, last_visible_row = offset_row; bool did_descend = true; while (did_descend) { @@ -145,15 +143,17 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, if (child->options.type >= type) { last_visible_tree = tree; last_visible_position = position; + last_visible_row = offset_row; } did_descend = true; break; } position = ts_length_add(position, ts_tree_total_size(child)); + offset_row += child->padding_point.row + child->size_point.row; } } - return ts_node_make(last_visible_tree, last_visible_position, last_visible_point); + return ts_node_make(last_visible_tree, last_visible_position, last_visible_row); } /* @@ -173,7 +173,7 @@ TSPoint ts_node_size_point(TSNode self) { } TSPoint ts_node_start_point(TSNode self) { - return ts_point_add(ts_node__offset_point(self), ts_node__tree(self)->padding_point); + return ts_point_make(self.row, ts_tree_offset_column(ts_node__tree(self))); } TSPoint ts_node_end_point(TSNode self) { @@ -209,18 +209,18 @@ bool ts_node_has_changes(TSNode self) { TSNode ts_node_parent(TSNode self) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__offset_point(self); + size_t offset_row = self.row; do { position = ts_length_sub(position, tree->context.offset); - point = ts_point_sub(point, tree->context.offset_point); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) return ts_node__null(); } while (!ts_tree_is_visible(tree)); - return ts_node_make(tree, position, point); + return ts_node_make(tree, position, offset_row); } TSNode ts_node_child(TSNode self, size_t child_index) { diff --git a/src/runtime/node.h b/src/runtime/node.h index 3b269887..2a169df8 100644 --- a/src/runtime/node.h +++ b/src/runtime/node.h @@ -5,6 +5,6 @@ #include "runtime/length.h" #include "runtime/tree.h" -TSNode ts_node_make(const TSTree *, TSLength, TSPoint); +TSNode ts_node_make(const TSTree *, TSLength, size_t); #endif diff --git a/src/runtime/tree.c b/src/runtime/tree.c index d51ac0e1..32547532 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -60,9 +60,12 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, if (i == 0) { self->padding = child->padding; self->size = child->size; + self->padding_point = child->padding_point; + self->size_point = child->size_point; } else { self->size = ts_length_add(ts_length_add(self->size, child->padding), child->size); + self->size_point = ts_point_add(ts_point_add(self->size_point, child->padding_point), child->size_point); } switch (child->options.type) { @@ -113,17 +116,22 @@ void ts_tree_release(TSTree *self) { } } -TSPoint ts_find_parent_offset_point(const TSTree *self) { - TSPoint size_point = self->size_point; +size_t ts_tree_offset_column(const TSTree *self) { const TSTree *parent = self; - TSPoint current_offset_point; + size_t column = self->padding_point.column; + + if (self->padding_point.row > 0) { + return column; + } do { parent = parent->context.parent; - current_offset_point = parent->context.offset_point; - } while (current_offset_point.line == 0); + if (!parent) break; - return (TSPoint){ .line = size_point.line, .column = current_offset_point.column }; + column += parent->context.offset_point.column; + } while (parent->context.offset_point.row == 0); + + return column; } TSLength ts_tree_total_size(const TSTree *self) { diff --git a/src/runtime/tree.h b/src/runtime/tree.h index cd83d0de..f373928b 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -52,7 +52,7 @@ bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); -TSPoint ts_find_parent_offset_point(const TSTree *self); +size_t ts_tree_offset_column(const TSTree *self); TSLength ts_tree_total_size(const TSTree *tree); TSPoint ts_tree_offset_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); From 11efff2442ce3dc430bbe80e9a864994598af2df Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 14:25:11 -0500 Subject: [PATCH 17/40] try starting from 1 --- src/runtime/length.h | 2 +- src/runtime/lexer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index a3a574e6..fe7f4b12 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -63,7 +63,7 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .row = 0, .column = 0 }; + return (TSPoint){ .row = 1, .column = 1 }; } static inline TSPoint ts_point_make(size_t row, size_t column) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 5a6fcf1d..27426f48 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -71,7 +71,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead == '\n') { self->current_point.row += 1; - self->current_point.column = 0; + self->current_point.column = 1; } else { self->current_point.column += 1; } From 7fab9e2c42fe1eaa75e77f927fc5072beb642675 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 14:28:52 -0500 Subject: [PATCH 18/40] start the root node from 1 --- src/runtime/document.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/document.c b/src/runtime/document.c index 1afcb2f3..92e9a894 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) { } TSNode ts_document_root_node(const TSDocument *self) { - TSNode result = ts_node_make(self->tree, ts_length_zero(), 0); + TSNode result = ts_node_make(self->tree, ts_length_zero(), 1); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; From 2dc63f2c30f74c21b4397dfde44d1d35425e50b1 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 12:16:49 -0500 Subject: [PATCH 19/40] Revert "start the root node from 1" This reverts commit 7fab9e2c42fe1eaa75e77f927fc5072beb642675. --- src/runtime/document.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/document.c b/src/runtime/document.c index 92e9a894..1afcb2f3 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) { } TSNode ts_document_root_node(const TSDocument *self) { - TSNode result = ts_node_make(self->tree, ts_length_zero(), 1); + TSNode result = ts_node_make(self->tree, ts_length_zero(), 0); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; From 4cbc4b8bcf1acc16b351f37bfb76af1b6ffaf5cd Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 12:16:58 -0500 Subject: [PATCH 20/40] Revert "try starting from 1" This reverts commit 11efff2442ce3dc430bbe80e9a864994598af2df. --- src/runtime/length.h | 2 +- src/runtime/lexer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index fe7f4b12..a3a574e6 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -63,7 +63,7 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .row = 1, .column = 1 }; + return (TSPoint){ .row = 0, .column = 0 }; } static inline TSPoint ts_point_make(size_t row, size_t column) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 27426f48..5a6fcf1d 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -71,7 +71,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead == '\n') { self->current_point.row += 1; - self->current_point.column = 1; + self->current_point.column = 0; } else { self->current_point.column += 1; } From 4af3b7d0fd3a1c47339064678dc3931ac37d9fb6 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 12:50:16 -0500 Subject: [PATCH 21/40] Add offset_point to LookaheadState --- src/runtime/lexer.c | 4 ++-- src/runtime/parser.c | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 061e6315..3bfbc745 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -54,8 +54,8 @@ static void ts_lexer__start_token(TSLexer *self) { LOG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; - DEBUG("start_token row:%lu", self->current_point.row); - DEBUG("start_token column:%lu", self->current_point.column); + LOG("start_token row:%lu", self->current_point.row); + LOG("start_token column:%lu", self->current_point.column); self->token_start_point = self->current_point; } diff --git a/src/runtime/parser.c b/src/runtime/parser.c index e16b8de5..6c792074 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -27,6 +27,7 @@ typedef struct { TSTree *reusable_subtree; size_t reusable_subtree_pos; TSLength position; + TSPoint offset_point; } LookaheadState; typedef enum { @@ -150,6 +151,7 @@ static ConsumeResult ts_parser__shift(TSParser *self, int head, LookaheadState *head_state = vector_get(&self->lookahead_states, head); head_state->position = ts_length_add(head_state->position, ts_tree_total_size(lookahead)); + head_state->offset_point = ts_point_add(head_state->offset_point, ts_tree_offset_point(lookahead)); if (ts_stack_push(self->stack, head, parse_state, lookahead)) { LOG("merge head:%d", head); vector_erase(&self->lookahead_states, head); @@ -271,6 +273,7 @@ static void ts_parser__reduce_error(TSParser *self, int head, child_count, false, true); reduced->size = ts_length_add(reduced->size, lookahead->padding); head_state->position = ts_length_add(head_state->position, lookahead->padding); + head_state->offset_point = ts_point_add(head_state->offset_point, lookahead->padding_point); lookahead->padding = ts_length_zero(); ts_tree_set_fragile_left(reduced); ts_tree_set_fragile_right(reduced); @@ -482,7 +485,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { for (;;) { TSTree *lookahead = NULL; TSLength position = ts_length_zero(); - TSPoint point = ts_point_zero(); + TSPoint offset_point = ts_point_zero(); for (int head = 0; head < ts_stack_head_count(self->stack);) { LookaheadState *state = vector_get(&self->lookahead_states, head); @@ -498,8 +501,8 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) { lookahead = reused_lookahead; } else { position = state->position; - point = state->current_point; - ts_lexer_reset(&self->lexer, position, point); + offset_point = state->offset_point; + ts_lexer_reset(&self->lexer, position, offset_point); TSStateId parse_state = ts_stack_top_state(self->stack, head); TSStateId lex_state = self->language->lex_states[parse_state]; lookahead = self->language->lex_fn(&self->lexer, lex_state); From 7633cbb836a42d7d8c77dc7a8373dfb6590a1313 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 12:56:10 -0500 Subject: [PATCH 22/40] indentation --- include/tree_sitter/runtime.h | 4 ++-- src/runtime/length.h | 34 +++++++++++++++++----------------- src/runtime/lexer.c | 20 ++++++++++---------- src/runtime/tree.c | 32 ++++++++++++++++---------------- src/runtime/tree.h | 10 +++++----- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 0eb769bf..86678caf 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -36,8 +36,8 @@ typedef struct { } TSInputEdit; typedef struct { - size_t row; - size_t column; + size_t row; + size_t column; } TSPoint; typedef struct { diff --git a/src/runtime/length.h b/src/runtime/length.h index a3a574e6..5171b0b9 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -27,27 +27,27 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { - size_t row = point1.row + point2.row; + size_t row = point1.row + point2.row; - size_t column; - if (point2.row == 0) { - column = point1.column + point2.column; - } else { - column = point2.column; - } + size_t column; + if (point2.row == 0) { + column = point1.column + point2.column; + } else { + column = point2.column; + } return (TSPoint){ .row = row, .column = column }; } static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { - size_t row, column; - if (point1.row == point2.row) { - row = 0; - column = point1.column - point2.column; - } else { - row = point1.row - point2.row; - column = point1.column; - } + size_t row, column; + if (point1.row == point2.row) { + row = 0; + column = point1.column - point2.column; + } else { + row = point1.row - point2.row; + column = point1.column; + } return (TSPoint){ .row = row, .column = column }; } @@ -63,11 +63,11 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .row = 0, .column = 0 }; + return (TSPoint){ .row = 1, .column = 1 }; } static inline TSPoint ts_point_make(size_t row, size_t column) { - return (TSPoint){ .row = row, .column = column }; + return (TSPoint){ .row = row, .column = column }; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 5a6fcf1d..f53120bc 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -69,12 +69,12 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { self->current_position.bytes += self->lookahead_size; self->current_position.chars += 1; - if (self->lookahead == '\n') { - self->current_point.row += 1; - self->current_point.column = 0; - } else { - self->current_point.column += 1; - } + if (self->lookahead == '\n') { + self->current_point.row += 1; + self->current_point.column = 1; + } else { + self->current_point.column += 1; + } } if (self->current_position.bytes >= self->chunk_start + self->chunk_size) @@ -94,17 +94,17 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, TSPoint size_point = ts_point_sub(self->current_point, self ->token_start_point); TSPoint padding_point = ts_point_sub(self->token_start_point, self->token_end_point); - self->token_end_point = self->current_point; + self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); return ts_tree_make_error(size, padding, size_point, - padding_point, self->lookahead); + padding_point, self->lookahead); } else { DEBUG("accept_token sym:%s", symbol_name); return ts_tree_make_leaf(symbol, padding, size, - padding_point, - size_point, node_type); + padding_point, + size_point, node_type); } } diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 32547532..a6b57f4d 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -7,8 +7,8 @@ #include "runtime/length.h" TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, - TSPoint padding_point, - TSPoint size_point, + TSPoint padding_point, + TSPoint size_point, TSNodeType node_type) { TSTree *result = malloc(sizeof(TSTree)); *result = (TSTree){ @@ -20,8 +20,8 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, .named_child_count = 0, .children = NULL, .padding = padding, - .padding_point = padding_point, - .size_point = size_point, + .padding_point = padding_point, + .size_point = size_point, .options = {.type = node_type }, }; @@ -34,12 +34,12 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, } TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSPoint size_point, - TSPoint padding_point, - char lookahead_char) { + TSPoint size_point, + TSPoint padding_point, + char lookahead_char) { TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, padding, size, padding_point, - size_point, TSNodeTypeNamed); + size_point, TSNodeTypeNamed); result->lookahead_char = lookahead_char; return result; } @@ -55,7 +55,7 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, child->context.parent = self; child->context.index = i; child->context.offset = ts_tree_total_size(self); - child->context.offset_point = ts_tree_offset_point(self); + child->context.offset_point = ts_tree_offset_point(self); if (i == 0) { self->padding = child->padding; @@ -117,21 +117,21 @@ void ts_tree_release(TSTree *self) { } size_t ts_tree_offset_column(const TSTree *self) { - const TSTree *parent = self; - size_t column = self->padding_point.column; + const TSTree *parent = self; + size_t column = self->padding_point.column; if (self->padding_point.row > 0) { return column; } - do { - parent = parent->context.parent; + do { + parent = parent->context.parent; if (!parent) break; - column += parent->context.offset_point.column; - } while (parent->context.offset_point.row == 0); + column += parent->context.offset_point.column; + } while (parent->context.offset_point.row == 0); - return column; + return column; } TSLength ts_tree_total_size(const TSTree *self) { diff --git a/src/runtime/tree.h b/src/runtime/tree.h index f373928b..9b3a31cc 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -25,8 +25,8 @@ struct TSTree { TSLength padding; TSLength size; - TSPoint padding_point; - TSPoint size_point; + TSPoint padding_point; + TSPoint size_point; TSSymbol symbol; @@ -41,11 +41,11 @@ struct TSTree { }; TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSPoint, - TSPoint, TSNodeType); + TSPoint, TSNodeType); TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSNodeType); TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSPoint padding_point, - TSPoint size_point, char lookahead_char); + TSPoint padding_point, + TSPoint size_point, 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); From cc77889d11c3c40c554d4a12c593959c592133e1 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 14:19:50 -0500 Subject: [PATCH 23/40] combine logs --- src/runtime/lexer.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index a355b094..d15f8d19 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -51,11 +51,9 @@ static void ts_lexer__start(TSLexer *self, TSStateId lex_state) { } static void ts_lexer__start_token(TSLexer *self) { - LOG("start_token chars:%lu", self->current_position.chars); - self->token_start_position = self->current_position; + LOG("start_token chars:%lu, rows:%lu, columns:%lu", self->current_position.chars, self->current_point.row, self->current_point.column); - LOG("start_token row:%lu", self->current_point.row); - LOG("start_token column:%lu", self->current_point.column); + self->token_start_position = self->current_position; self->token_start_point = self->current_point; } From dc3818987ceede785bc7b30416fe0c2f7b997bf5 Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 16:54:10 -0500 Subject: [PATCH 24/40] start points from zero --- src/runtime/length.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index 5171b0b9..62a51d09 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -63,7 +63,7 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .row = 1, .column = 1 }; + return (TSPoint){ .row = 0, .column = 0 }; } static inline TSPoint ts_point_make(size_t row, size_t column) { From 9da4aeaeffc73ffb9effd5cafaee0ea59dea556f Mon Sep 17 00:00:00 2001 From: joshvera Date: Mon, 30 Nov 2015 17:22:47 -0500 Subject: [PATCH 25/40] columns start at 0 for sanity's sake --- spec/runtime/node_spec.cc | 32 ++++++++++++++++++++------------ src/runtime/lexer.c | 2 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index a85288d0..68468076 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -12,7 +12,15 @@ describe("Node", []() { before_each([&]() { document = ts_document_make(); ts_document_set_language(document, ts_language_json()); - ts_document_set_input_string(document, " [123, false, {\"x\": null}]"); + ts_document_set_input_string(document,"\n" + "\n" + "[\n" + " 123,\n" + " false,\n" + " {\n" + " \"x\": null\n" + " }\n" + "]"); ts_document_parse(document); array_node = ts_document_root_node(document); @@ -41,13 +49,13 @@ describe("Node", []() { AssertThat(ts_node_name(child3, document), Equals("object")); AssertThat(ts_node_pos(array_node).bytes, Equals(2)); - AssertThat(ts_node_size(array_node).bytes, Equals(25)); + AssertThat(ts_node_size(array_node).bytes, Equals(41)); AssertThat(ts_node_start_point(array_node).row, Equals(0)); - AssertThat(ts_node_start_point(array_node).column, Equals(2)); + AssertThat(ts_node_start_point(array_node).column, Equals(0)); - AssertThat(ts_node_end_point(array_node).row, Equals(0)); - AssertThat(ts_node_end_point(array_node).column, Equals(27)); + AssertThat(ts_node_end_point(array_node).row, Equals(6)); + AssertThat(ts_node_end_point(array_node).column, Equals(0)); AssertThat(ts_node_pos(child1).bytes, Equals(3)); AssertThat(ts_node_size(child1).bytes, Equals(3)); @@ -105,13 +113,13 @@ describe("Node", []() { AssertThat(ts_node_pos(child1).bytes, Equals(2)); AssertThat(ts_node_size(child1).bytes, Equals(1)); - AssertThat(ts_node_pos(child3).bytes, Equals(6)); + AssertThat(ts_node_pos(child3).bytes, Equals(9)); AssertThat(ts_node_size(child3).bytes, Equals(1)); - AssertThat(ts_node_pos(child5).bytes, Equals(13)); + AssertThat(ts_node_pos(child5).bytes, Equals(18)); AssertThat(ts_node_size(child5).bytes, Equals(1)); - AssertThat(ts_node_pos(child7).bytes, Equals(26)); + AssertThat(ts_node_pos(child7).bytes, Equals(42)); AssertThat(ts_node_size(child7).bytes, Equals(1)); AssertThat(ts_node_child_count(child6), Equals(5)) @@ -218,15 +226,15 @@ describe("Node", []() { describe("find_for_range(start, end)", [&]() { describe("when there is a leaf node that spans the given range exactly", [&]() { it("returns that leaf node", [&]() { - TSNode leaf = ts_node_named_descendent_for_range(array_node, 16, 18); + TSNode leaf = ts_node_named_descendent_for_range(array_node, 28, 30); AssertThat(ts_node_name(leaf, document), Equals("string")); AssertThat(ts_node_size(leaf).bytes, Equals(3)); - AssertThat(ts_node_pos(leaf).bytes, Equals(16)); + AssertThat(ts_node_pos(leaf).bytes, Equals(28)); - leaf = ts_node_named_descendent_for_range(array_node, 3, 5); + leaf = ts_node_named_descendent_for_range(array_node, 7, 9); AssertThat(ts_node_name(leaf, document), Equals("number")); AssertThat(ts_node_size(leaf).bytes, Equals(3)); - AssertThat(ts_node_pos(leaf).bytes, Equals(3)); + AssertThat(ts_node_pos(leaf).bytes, Equals(7)); }); }); diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index d15f8d19..91af5222 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -69,7 +69,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { if (self->lookahead == '\n') { self->current_point.row += 1; - self->current_point.column = 1; + self->current_point.column = 0; } else { self->current_point.column += 1; } From cc4bd82e8a3faf7418d1ca268ed17282deb2815d Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:06:55 -0500 Subject: [PATCH 26/40] Fix failing tests using find --- spec/runtime/node_spec.cc | 64 ++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 68468076..391c78d3 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -8,11 +8,13 @@ START_TEST describe("Node", []() { TSDocument *document; TSNode array_node; + string input_string; before_each([&]() { document = ts_document_make(); ts_document_set_language(document, ts_language_json()); - ts_document_set_input_string(document,"\n" + + input_string = "\n" "\n" "[\n" " 123,\n" @@ -20,7 +22,9 @@ describe("Node", []() { " {\n" " \"x\": null\n" " }\n" - "]"); + "]"; + + ts_document_set_input_string(document, input_string.c_str()); ts_document_parse(document); array_node = ts_document_root_node(document); @@ -55,16 +59,17 @@ describe("Node", []() { AssertThat(ts_node_start_point(array_node).column, Equals(0)); AssertThat(ts_node_end_point(array_node).row, Equals(6)); - AssertThat(ts_node_end_point(array_node).column, Equals(0)); + AssertThat(ts_node_end_point(array_node).column, Equals(1)); - AssertThat(ts_node_pos(child1).bytes, Equals(3)); + + AssertThat(ts_node_pos(child1).bytes, Equals(input_string.find("123"))); AssertThat(ts_node_size(child1).bytes, Equals(3)); - AssertThat(ts_node_pos(child2).bytes, Equals(8)); + AssertThat(ts_node_pos(child2).bytes, Equals(input_string.find("false"))); AssertThat(ts_node_size(child2).bytes, Equals(5)); - AssertThat(ts_node_pos(child3).bytes, Equals(15)); - AssertThat(ts_node_size(child3).bytes, Equals(11)); + AssertThat(ts_node_pos(child3).bytes, Equals(input_string.find("{"))); + AssertThat(ts_node_size(child3).bytes, Equals(19)); AssertThat(ts_node_named_child_count(child3), Equals(2)); @@ -231,55 +236,66 @@ describe("Node", []() { AssertThat(ts_node_size(leaf).bytes, Equals(3)); AssertThat(ts_node_pos(leaf).bytes, Equals(28)); - leaf = ts_node_named_descendent_for_range(array_node, 7, 9); + size_t index = input_string.find("123"); + leaf = ts_node_named_descendent_for_range(array_node, index, index + 2); AssertThat(ts_node_name(leaf, document), Equals("number")); + AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); - AssertThat(ts_node_pos(leaf).bytes, Equals(7)); }); }); describe("when there is a leaf node that extends beyond the given range", [&]() { it("returns that leaf node", [&]() { - TSNode leaf = ts_node_named_descendent_for_range(array_node, 16, 17); + size_t index = input_string.find("\"x\""); + TSNode leaf = ts_node_named_descendent_for_range(array_node, index, index + 1); AssertThat(ts_node_name(leaf, document), Equals("string")); + AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); - AssertThat(ts_node_pos(leaf).bytes, Equals(16)); - leaf = ts_node_named_descendent_for_range(array_node, 17, 18); + leaf = ts_node_named_descendent_for_range(array_node, index + 1, index + 2); AssertThat(ts_node_name(leaf, document), Equals("string")); + AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); - AssertThat(ts_node_pos(leaf).bytes, Equals(16)); }); }); describe("when there is no leaf node that spans the given range", [&]() { it("returns the smallest node that does span the range", [&]() { - TSNode node = ts_node_named_descendent_for_range(array_node, 16, 19); + size_t index = input_string.find("\"x\""); + TSNode node = ts_node_named_descendent_for_range(array_node, index, index + 3); AssertThat(ts_node_name(node, document), Equals("object")); - AssertThat(ts_node_size(node).bytes, Equals(11)); - AssertThat(ts_node_pos(node).bytes, Equals(15)); + + size_t object_index = input_string.find("{"); + AssertThat(ts_node_pos(node).bytes, Equals(object_index)); + AssertThat(ts_node_size(node).bytes, Equals(19)); }); it("does not return invisible nodes (repeats)", [&]() { - TSNode node = ts_node_named_descendent_for_range(array_node, 6, 7); + size_t comma_index = input_string.find(","); + TSNode node = ts_node_named_descendent_for_range(array_node, comma_index, comma_index + 1); AssertThat(ts_node_name(node, document), Equals("array")); - AssertThat(ts_node_size(node).bytes, Equals(25)); - AssertThat(ts_node_pos(node).bytes, Equals(2)); + size_t array_index = input_string.find("["); + AssertThat(ts_node_pos(node).bytes, Equals(array_index)); + AssertThat(ts_node_size(node).bytes, Equals(41)); }); }); }); describe("find_concrete_for_range(start, end)", [&]() { it("returns the smallest concrete node that spans the given range", [&]() { - TSNode node1 = ts_node_descendent_for_range(array_node, 19, 19); + size_t colon_index = input_string.find(":"); + TSNode node1 = ts_node_descendent_for_range(array_node, colon_index, colon_index); AssertThat(ts_node_name(node1, document), Equals(":")); - AssertThat(ts_node_pos(node1).bytes, Equals(19)); + AssertThat(ts_node_pos(node1).bytes, Equals(colon_index)); AssertThat(ts_node_size(node1).bytes, Equals(1)); - TSNode node2 = ts_node_descendent_for_range(array_node, 18, 20); + size_t index = input_string.find("\":"); + TSNode node2 = ts_node_descendent_for_range(array_node, index, index + 2); AssertThat(ts_node_name(node2, document), Equals("object")); - AssertThat(ts_node_pos(node2).bytes, Equals(15)); - AssertThat(ts_node_size(node2).bytes, Equals(11)); + + size_t object_index = input_string.find("{"); + AssertThat(ts_node_pos(node2).bytes, Equals(object_index)); + AssertThat(ts_node_size(node2).bytes, Equals(19)); }); }); }); From 94a46d9ae197e6a9a8d14515b715f46078323e69 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:40:56 -0500 Subject: [PATCH 27/40] Add more start_point and end_point tests --- spec/runtime/node_spec.cc | 43 ++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 391c78d3..1f5bc8f2 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -55,22 +55,27 @@ describe("Node", []() { AssertThat(ts_node_pos(array_node).bytes, Equals(2)); AssertThat(ts_node_size(array_node).bytes, Equals(41)); - AssertThat(ts_node_start_point(array_node).row, Equals(0)); - AssertThat(ts_node_start_point(array_node).column, Equals(0)); - - AssertThat(ts_node_end_point(array_node).row, Equals(6)); - AssertThat(ts_node_end_point(array_node).column, Equals(1)); - + AssertThat(ts_node_start_point(array_node), Equals({ 2, 0 })); + AssertThat(ts_node_end_point(array_node), Equals({ 8, 1 })); AssertThat(ts_node_pos(child1).bytes, Equals(input_string.find("123"))); AssertThat(ts_node_size(child1).bytes, Equals(3)); + AssertThat(ts_node_start_point(child1), Equals({ 3, 2 })); + AssertThat(ts_node_end_point(child1), Equals({ 3, 5 })); + AssertThat(ts_node_pos(child2).bytes, Equals(input_string.find("false"))); AssertThat(ts_node_size(child2).bytes, Equals(5)); + AssertThat(ts_node_start_point(child2), Equals({ 4, 2 })); + AssertThat(ts_node_end_point(child2), Equals({ 4, 7 })); + AssertThat(ts_node_pos(child3).bytes, Equals(input_string.find("{"))); AssertThat(ts_node_size(child3).bytes, Equals(19)); + AssertThat(ts_node_start_point(child3), Equals({ 5, 2 })); + AssertThat(ts_node_end_point(child3), Equals({ 7, 3 })); + AssertThat(ts_node_named_child_count(child3), Equals(2)); TSNode grandchild1 = ts_node_named_child(child3, 0); @@ -79,6 +84,9 @@ describe("Node", []() { AssertThat(ts_node_name(grandchild1, document), Equals("string")); AssertThat(ts_node_name(grandchild2, document), Equals("null")); + AssertThat(ts_node_start_point(grandchild1), Equals({ 6, 4 })); + AssertThat(ts_node_end_point(grandchild1), Equals({ 6, 7 })); + AssertThat(ts_node_parent(child1), Equals(array_node)); AssertThat(ts_node_parent(child2), Equals(array_node)); AssertThat(ts_node_parent(child3), Equals(array_node)); @@ -86,7 +94,7 @@ describe("Node", []() { }); }); - describe("concrete_child_count(), concrete_child(i)", [&]() { + describe("child_count(), child(i)", [&]() { it("returns the child node at the given index, including anonymous nodes", [&]() { AssertThat(ts_node_child_count(array_node), Equals(7)); TSNode child1 = ts_node_child(array_node, 0); @@ -118,15 +126,27 @@ describe("Node", []() { AssertThat(ts_node_pos(child1).bytes, Equals(2)); AssertThat(ts_node_size(child1).bytes, Equals(1)); + AssertThat(ts_node_start_point(child1), Equals({ 2, 0 })); + AssertThat(ts_node_end_point(child1), Equals({ 2, 1 })); + AssertThat(ts_node_pos(child3).bytes, Equals(9)); AssertThat(ts_node_size(child3).bytes, Equals(1)); + AssertThat(ts_node_start_point(child3), Equals({ 3, 5 })); + AssertThat(ts_node_end_point(child3), Equals({ 3, 6 })); + AssertThat(ts_node_pos(child5).bytes, Equals(18)); AssertThat(ts_node_size(child5).bytes, Equals(1)); + AssertThat(ts_node_start_point(child5), Equals({ 4, 7 })); + AssertThat(ts_node_end_point(child5), Equals({ 4, 8 })); + AssertThat(ts_node_pos(child7).bytes, Equals(42)); AssertThat(ts_node_size(child7).bytes, Equals(1)); + AssertThat(ts_node_start_point(child7), Equals({ 8, 0 })); + AssertThat(ts_node_end_point(child7), Equals({ 8, 1 })); + AssertThat(ts_node_child_count(child6), Equals(5)) TSNode grandchild1 = ts_node_child(child6, 0); @@ -301,3 +321,12 @@ describe("Node", []() { }); END_TEST + +bool operator==(const TSPoint &left, const TSPoint &right) { + return left.row == right.row && left.column == right.column; +} + +std::ostream &operator<<(std::ostream &stream, const TSPoint &point) { + return stream << "{" << point.row << ", " << point.column << "}"; +} + From 3f9c8b76deb3af329cc146dc776c574cd5841b2c Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:41:16 -0500 Subject: [PATCH 28/40] Add padding_point.row to ts_node_start_point's row. --- src/runtime/node.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index ffff0ed4..9a795f7d 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -173,7 +173,8 @@ TSPoint ts_node_size_point(TSNode self) { } TSPoint ts_node_start_point(TSNode self) { - return ts_point_make(self.row, ts_tree_offset_column(ts_node__tree(self))); + TSTree *tree = ts_node__tree(self); + return ts_point_make(self.row + tree->padding_point.row, ts_tree_offset_column(tree)); } TSPoint ts_node_end_point(TSNode self) { From e674094f643a4333eb664f7936e22b8ae89f82ca Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:41:40 -0500 Subject: [PATCH 29/40] Sum the offset_point like offset in ts_tree_assign_parents --- src/runtime/tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 1b69692e..faa85820 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -46,16 +46,18 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, void ts_tree_assign_parents(TSTree *self) { TSLength offset = ts_length_zero(); + TSPoint offset_point = ts_point_zero(); for (size_t i = 0; i < self->child_count; i++) { TSTree *child = self->children[i]; if (child->context.parent != self) { child->context.parent = self; child->context.index = i; child->context.offset = offset; - child->context.offset_point = ts_tree_offset_point(self); + child->context.offset_point = offset_point; ts_tree_assign_parents(child); } offset = ts_length_add(offset, ts_tree_total_size(child)); + offset_point = ts_point_add(offset_point, ts_tree_total_size_point(child)); } } From 5e748fdf63beb87b3496d0e130331a165485896a Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:41:56 -0500 Subject: [PATCH 30/40] s/ts_tree_offset_point/ts_tree_total_size_point --- src/runtime/parser.c | 2 +- src/runtime/tree.c | 2 +- src/runtime/tree.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 6c792074..71d254c1 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -151,7 +151,7 @@ static ConsumeResult ts_parser__shift(TSParser *self, int head, LookaheadState *head_state = vector_get(&self->lookahead_states, head); head_state->position = ts_length_add(head_state->position, ts_tree_total_size(lookahead)); - head_state->offset_point = ts_point_add(head_state->offset_point, ts_tree_offset_point(lookahead)); + head_state->offset_point = ts_point_add(head_state->offset_point, ts_tree_total_size_point(lookahead)); if (ts_stack_push(self->stack, head, parse_state, lookahead)) { LOG("merge head:%d", head); vector_erase(&self->lookahead_states, head); diff --git a/src/runtime/tree.c b/src/runtime/tree.c index faa85820..dbc509f7 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -151,7 +151,7 @@ TSLength ts_tree_total_size(const TSTree *self) { return ts_length_add(self->padding, self->size); } -TSPoint ts_tree_offset_point(const TSTree *self) { +TSPoint ts_tree_total_size_point(const TSTree *self) { return ts_point_add(self->padding_point, self->size_point); } diff --git a/src/runtime/tree.h b/src/runtime/tree.h index aa794540..817ee64a 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -55,7 +55,7 @@ char *ts_tree_string(const TSTree *tree, const char **names, size_t ts_tree_offset_column(const TSTree *self); TSLength ts_tree_total_size(const TSTree *tree); -TSPoint ts_tree_offset_point(const TSTree *self); +TSPoint ts_tree_total_size_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); void ts_tree_assign_parents(TSTree *); void ts_tree_edit(TSTree *, TSInputEdit); From da8e48bf09ef441d0e11596c5fca4b257b245d2e Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:45:08 -0500 Subject: [PATCH 31/40] fix test names --- spec/runtime/node_spec.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 1f5bc8f2..989898fc 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -39,7 +39,7 @@ describe("Node", []() { ts_document_free(document); }); - describe("child_count(), child(i)", [&]() { + describe("named_child_count(), named_child(i)", [&]() { it("returns the named child node at the given index", [&]() { AssertThat(ts_node_named_child_count(array_node), Equals(3)); @@ -172,7 +172,7 @@ describe("Node", []() { }); }); - describe("next_concrete_sibling(), prev_concrete_sibling()", [&]() { + describe("next_sibling(), prev_sibling()", [&]() { it("returns the node's next and previous sibling, including anonymous nodes", [&]() { TSNode bracket_node1 = ts_node_child(array_node, 0); TSNode number_node = ts_node_child(array_node, 1); From 7ad82cf684f93694f43d6945d9f8ac125e5f57b2 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:45:12 -0500 Subject: [PATCH 32/40] add const --- src/runtime/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index 9a795f7d..ff9dc2ee 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -173,7 +173,7 @@ TSPoint ts_node_size_point(TSNode self) { } TSPoint ts_node_start_point(TSNode self) { - TSTree *tree = ts_node__tree(self); + const TSTree *tree = ts_node__tree(self); return ts_point_make(self.row + tree->padding_point.row, ts_tree_offset_column(tree)); } From 49531827076718f49c7c2cade976536578ad1aa9 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 16:45:20 -0500 Subject: [PATCH 33/40] add row to node equality --- src/runtime/node.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index ff9dc2ee..cbc720ce 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -196,7 +196,8 @@ const char *ts_node_string(TSNode self, const TSDocument *document) { bool ts_node_eq(TSNode self, TSNode other) { return ts_tree_eq(ts_node__tree(self), ts_node__tree(other)) && - ts_length_eq(ts_node__offset(self), ts_node__offset(other)); + ts_length_eq(ts_node__offset(self), ts_node__offset(other)) && + self.row == other.row; } bool ts_node_is_named(TSNode self) { From 14bc05b8ac7d772664f1877057ea1a6a8a52ea76 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:12:00 -0500 Subject: [PATCH 34/40] Fix some names and add more tests --- spec/runtime/node_spec.cc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 989898fc..76876035 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -224,7 +224,7 @@ describe("Node", []() { }); }); - describe("next_concrete_sibling(), prev_concrete_sibling()", [&]() { + describe("next_named_sibling(), prev_concrete_sibling()", [&]() { it("returns the node's next and previous siblings", [&]() { TSNode number_node = ts_node_named_child(array_node, 0); TSNode false_node = ts_node_named_child(array_node, 1); @@ -261,6 +261,8 @@ describe("Node", []() { AssertThat(ts_node_name(leaf, document), Equals("number")); AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); + AssertThat(ts_node_start_point(leaf), Equals({ 3, 2 })); + AssertThat(ts_node_end_point(leaf), Equals({ 3, 5 })); }); }); @@ -272,10 +274,16 @@ describe("Node", []() { AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); + AssertThat(ts_node_start_point(leaf), Equals({ 6, 4 })); + AssertThat(ts_node_end_point(leaf), Equals({ 6, 7 })); + leaf = ts_node_named_descendent_for_range(array_node, index + 1, index + 2); AssertThat(ts_node_name(leaf, document), Equals("string")); AssertThat(ts_node_pos(leaf).bytes, Equals(index)); AssertThat(ts_node_size(leaf).bytes, Equals(3)); + + AssertThat(ts_node_start_point(leaf), Equals({ 6, 4 })); + AssertThat(ts_node_end_point(leaf), Equals({ 6, 7 })); }); }); @@ -288,6 +296,9 @@ describe("Node", []() { size_t object_index = input_string.find("{"); AssertThat(ts_node_pos(node).bytes, Equals(object_index)); AssertThat(ts_node_size(node).bytes, Equals(19)); + + AssertThat(ts_node_start_point(node), Equals({ 5, 2 })); + AssertThat(ts_node_end_point(node), Equals({ 7, 3 })); }); it("does not return invisible nodes (repeats)", [&]() { @@ -297,17 +308,22 @@ describe("Node", []() { size_t array_index = input_string.find("["); AssertThat(ts_node_pos(node).bytes, Equals(array_index)); AssertThat(ts_node_size(node).bytes, Equals(41)); + + AssertThat(ts_node_start_point(node), Equals({ 2, 0 })); + AssertThat(ts_node_end_point(node), Equals({ 8, 1 })); }); }); }); - describe("find_concrete_for_range(start, end)", [&]() { + describe("descendent_for_range(start, end)", [&]() { it("returns the smallest concrete node that spans the given range", [&]() { size_t colon_index = input_string.find(":"); TSNode node1 = ts_node_descendent_for_range(array_node, colon_index, colon_index); AssertThat(ts_node_name(node1, document), Equals(":")); AssertThat(ts_node_pos(node1).bytes, Equals(colon_index)); AssertThat(ts_node_size(node1).bytes, Equals(1)); + AssertThat(ts_node_start_point(node1), Equals({ 6, 7 })); + AssertThat(ts_node_end_point(node1), Equals({ 6, 8 })); size_t index = input_string.find("\":"); TSNode node2 = ts_node_descendent_for_range(array_node, index, index + 2); @@ -316,6 +332,8 @@ describe("Node", []() { size_t object_index = input_string.find("{"); AssertThat(ts_node_pos(node2).bytes, Equals(object_index)); AssertThat(ts_node_size(node2).bytes, Equals(19)); + AssertThat(ts_node_start_point(node2), Equals({ 5, 2 })); + AssertThat(ts_node_end_point(node2), Equals({ 7, 3 })); }); }); }); From e52c38a68f0bd9c2ce5cb87e14cefd9e676683fc Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:12:32 -0500 Subject: [PATCH 35/40] Work correctly when node's offset point is zero --- src/runtime/tree.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/runtime/tree.c b/src/runtime/tree.c index dbc509f7..c78f8f84 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -130,19 +130,21 @@ void ts_tree_release(TSTree *self) { } size_t ts_tree_offset_column(const TSTree *self) { - const TSTree *parent = self; size_t column = self->padding_point.column; if (self->padding_point.row > 0) { return column; } + const TSTree *parent = self; + TSPoint offset_point; do { + offset_point = parent->context.offset_point; + column += offset_point.column; + parent = parent->context.parent; if (!parent) break; - - column += parent->context.offset_point.column; - } while (parent->context.offset_point.row == 0); + } while (offset_point.row == 0); return column; } From 2df2b58d3ed28d56f367667382f56773405e760b Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:28:52 -0500 Subject: [PATCH 36/40] Move point helpers elsewhere --- spec/runtime/helpers/point_helpers.cc | 24 ++++++++++++++++++++++++ spec/runtime/helpers/point_helpers.h | 8 ++++++++ 2 files changed, 32 insertions(+) create mode 100644 spec/runtime/helpers/point_helpers.cc create mode 100644 spec/runtime/helpers/point_helpers.h diff --git a/spec/runtime/helpers/point_helpers.cc b/spec/runtime/helpers/point_helpers.cc new file mode 100644 index 00000000..97a444a9 --- /dev/null +++ b/spec/runtime/helpers/point_helpers.cc @@ -0,0 +1,24 @@ +#include +#include +#include "runtime/length.h" + +using namespace std; + +bool operator==(const TSPoint &left, const TSPoint &right) { + return left.row == right.row && left.column == right.column; +} + +std::ostream &operator<<(std::ostream &stream, const TSPoint &point) { + return stream << "{" << point.row << ", " << point.column << "}"; +} + +bool operator<(const TSPoint &left, const TSPoint &right) { + if (left.row < right.row) return true; + if (left.row > right.row) return false; + + return left.column < right.column; +} + +bool operator>(const TSPoint &left, const TSPoint &right) { + return right < left; +} diff --git a/spec/runtime/helpers/point_helpers.h b/spec/runtime/helpers/point_helpers.h new file mode 100644 index 00000000..671fd4dc --- /dev/null +++ b/spec/runtime/helpers/point_helpers.h @@ -0,0 +1,8 @@ + +bool operator==(const TSPoint &left, const TSPoint &right); + +bool operator<(const TSPoint &left, const TSPoint &right); + +bool operator>(const TSPoint &left, const TSPoint &right); + +std::ostream &operator<<(std::ostream &stream, const TSPoint &point); From 883bb87e29c6a190a62fc065c2accaaf7a37b06a Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:29:10 -0500 Subject: [PATCH 37/40] add asserts for points in language_specs --- spec/runtime/language_specs.cc | 11 +++++++++++ spec/runtime/node_spec.cc | 10 +--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spec/runtime/language_specs.cc b/spec/runtime/language_specs.cc index 81bd54fa..57e9e49b 100644 --- a/spec/runtime/language_specs.cc +++ b/spec/runtime/language_specs.cc @@ -4,6 +4,7 @@ #include "runtime/helpers/read_test_entries.h" #include "runtime/helpers/spy_input.h" #include "runtime/helpers/log_debugger.h" +#include "runtime/helpers/point_helpers.h" extern "C" const TSLanguage *ts_language_javascript(); extern "C" const TSLanguage *ts_language_json(); @@ -23,6 +24,9 @@ void expect_a_consistent_tree(TSNode node, TSDocument *doc) { TSLength end = ts_length_add(start, ts_node_size(node)); size_t child_count = ts_node_child_count(node); + TSPoint start_point = ts_node_start_point(node); + TSPoint end_point = ts_node_end_point(node); + bool has_changes = ts_node_has_changes(node); bool some_child_has_changes = false; @@ -31,8 +35,15 @@ void expect_a_consistent_tree(TSNode node, TSDocument *doc) { TSLength child_start = ts_node_pos(child); TSLength child_end = ts_length_add(child_start, ts_node_size(child)); + TSPoint child_start_point = ts_node_start_point(child); + TSPoint child_end_point = ts_node_end_point(child); + AssertThat(child_start.chars, IsGreaterThan(start.chars) || Equals(start.chars)); AssertThat(child_end.chars, IsLessThan(end.chars) || Equals(end.chars)); + + AssertThat(child_start_point, IsGreaterThan(start_point) || Equals(start_point)); + AssertThat(child_end_point, IsLessThan(end_point) || Equals(end_point)); + if (ts_node_has_changes(child)) some_child_has_changes = true; } diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 76876035..ce5eb772 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -1,5 +1,6 @@ #include "runtime/runtime_spec_helper.h" #include "runtime/helpers/tree_helpers.h" +#include "runtime/helpers/point_helpers.h" extern "C" TSLanguage * ts_language_json(); @@ -339,12 +340,3 @@ describe("Node", []() { }); END_TEST - -bool operator==(const TSPoint &left, const TSPoint &right) { - return left.row == right.row && left.column == right.column; -} - -std::ostream &operator<<(std::ostream &stream, const TSPoint &point) { - return stream << "{" << point.row << ", " << point.column << "}"; -} - From debf5205fbfb5a1518843088a946660593b9ee2b Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:44:14 -0500 Subject: [PATCH 38/40] Don't use c-style structs --- src/runtime/length.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index 62a51d09..b3ad3d03 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -63,11 +63,16 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .row = 0, .column = 0 }; + TSPoint point; + point.row = point.column = 0; + return point; } static inline TSPoint ts_point_make(size_t row, size_t column) { - return (TSPoint){ .row = row, .column = column }; + TSPoint point; + point.row = row; + point.column = column; + return point; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { From 144aab22e607483efee2ef36f4d4d888fccc16fc Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:47:46 -0500 Subject: [PATCH 39/40] missed two --- src/runtime/length.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index b3ad3d03..dab2b86e 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -36,7 +36,7 @@ static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { column = point2.column; } - return (TSPoint){ .row = row, .column = column }; + return ts_point_make(row, column); } static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { @@ -49,7 +49,7 @@ static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { column = point1.column; } - return (TSPoint){ .row = row, .column = column }; + return ts_point_make(row, column); } static inline TSLength ts_length_zero() { From 06c790e16ee722feaac211c5a634c3f397a4fee2 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 2 Dec 2015 17:51:06 -0500 Subject: [PATCH 40/40] declare ts_point_make before using it --- src/runtime/length.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/runtime/length.h b/src/runtime/length.h index dab2b86e..96051869 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -26,6 +26,13 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { return result; } +static inline TSPoint ts_point_make(size_t row, size_t column) { + TSPoint point; + point.row = row; + point.column = column; + return point; +} + static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { size_t row = point1.row + point2.row; @@ -68,13 +75,6 @@ static inline TSPoint ts_point_zero() { return point; } -static inline TSPoint ts_point_make(size_t row, size_t column) { - TSPoint point; - point.row = row; - point.column = column; - return point; -} - static inline TSLength ts_length_make(size_t bytes, size_t chars) { TSLength result; result.bytes = bytes;