From 26ac5788b6c0ac787645759aab9a5b05c3f45693 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 26 Sep 2014 16:31:30 -0700 Subject: [PATCH] Don't use struct literal syntax for TSLength --- spec/runtime/stack_spec.cc | 5 +++-- spec/runtime/tree_spec.cc | 33 +++++++++++++++++---------------- src/runtime/length.h | 27 ++++++++++++++++++--------- src/runtime/lexer.c | 6 +++--- src/runtime/parser.c | 2 +- src/runtime/stack.c | 2 +- 6 files changed, 43 insertions(+), 32 deletions(-) diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 255d2203..4d5c1e72 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -1,4 +1,5 @@ #include "runtime/runtime_spec_helper.h" +#include "runtime/length.h" #include "runtime/tree.h" #include "runtime/stack.h" @@ -29,8 +30,8 @@ describe("stacks", [&]() { before_each([&]() { node1 = ts_tree_make_leaf( sym1, - (TSLength) { 0, 0 }, - (TSLength) { 1, 1 }, + ts_length_make(0, 0), + ts_length_make(1, 1), 0); ts_stack_push(&stack, 5, node1); diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index ca38cedf..0ba3e66d 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -1,5 +1,6 @@ #include "runtime/runtime_spec_helper.h" #include "runtime/tree.h" +#include "runtime/length.h" START_TEST @@ -17,14 +18,14 @@ describe("Tree", []() { before_each([&]() { tree1 = ts_tree_make_leaf( cat, - (TSLength) { .bytes = 5, .chars = 4 }, - (TSLength) { .bytes = 2, .chars = 1 }, + ts_length_make(5, 4), + ts_length_make(2, 1), 0); tree2 = ts_tree_make_leaf( cat, - (TSLength) { .bytes = 3, .chars = 3 }, - (TSLength) { .bytes = 1, .chars = 1 }, + ts_length_make(3, 3), + ts_length_make(1, 1), 0); parent1 = ts_tree_make_node( @@ -77,8 +78,8 @@ describe("Tree", []() { parent1->options = TSTreeOptionsHidden; tree3 = ts_tree_make_leaf( cat, - (TSLength) { .bytes = 8, .chars = 6 }, - (TSLength) { .bytes = 5, .chars = 3 }, + ts_length_make(8, 6), + ts_length_make(5, 3), 0); grandparent = ts_tree_make_node(pig, 2, tree_array({ parent1, @@ -119,16 +120,16 @@ describe("Tree", []() { it("returns true for identical trees", [&]() { TSTree *tree1_copy = ts_tree_make_leaf( cat, - (TSLength) { .bytes = 5, .chars = 4 }, - (TSLength) { .bytes = 2, .chars = 1 }, + ts_length_make(5, 4), + ts_length_make(2, 1), 0); AssertThat(ts_tree_equals(tree1, tree1_copy), Equals(1)); TSTree *tree2_copy = ts_tree_make_leaf( cat, - (TSLength) { .bytes = 3, .chars = 3 }, - (TSLength) { .bytes = 1, .chars = 1 }, + ts_length_make(3, 3), + ts_length_make(1, 1), 0); AssertThat(ts_tree_equals(tree2, tree2_copy), Equals(1)); @@ -146,9 +147,9 @@ describe("Tree", []() { it("returns false for trees with different symbols", [&]() { TSTree *different_tree = ts_tree_make_leaf( - pig, - (TSLength) { .bytes = 5, .chars = 4 }, - (TSLength) { .bytes = 2, .chars = 1 }, + tree1->symbol + 1, + tree1->size, + tree1->padding, 0); AssertThat(ts_tree_equals(tree1, different_tree), Equals(0)); @@ -157,9 +158,9 @@ describe("Tree", []() { it("returns false for trees with different children", [&]() { TSTree *different_tree = ts_tree_make_leaf( - pig, - (TSLength) { .bytes = 5, .chars = 4 }, - (TSLength) { .bytes = 2, .chars = 1 }, + tree1->symbol + 1, + tree1->size, + tree1->padding, 0); TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({ diff --git a/src/runtime/length.h b/src/runtime/length.h index c5c11db7..e00e4679 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -5,25 +5,34 @@ #include static inline TSLength ts_length_add(TSLength len1, TSLength len2) { - return (TSLength) { - .bytes = len1.bytes + len2.bytes, - .chars = len1.chars + len2.chars, - }; + TSLength result; + result.bytes = len1.bytes + len2.bytes; + result.chars = len1.chars + len2.chars; + return result; } static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { - return (TSLength) { - .bytes = len1.bytes - len2.bytes, - .chars = len1.chars - len2.chars, - }; + TSLength result; + result.bytes = len1.bytes - len2.bytes; + result.chars = len1.chars - len2.chars; + return result; } static inline TSLength ts_length_zero() { - return (TSLength) { 0, 0 }; + TSLength result; + result.bytes = result.chars = 0; + return result; } static inline bool ts_length_eq(TSLength len1, TSLength len2) { return len1.bytes == len2.bytes && len1.chars == len2.chars; } +static inline TSLength ts_length_make(size_t bytes, size_t chars) { + TSLength result; + result.bytes = bytes; + result.chars = chars; + return result; +} + #endif diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index b2544b83..db6469f6 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -68,9 +68,9 @@ void ts_lexer_reset(TSLexer *lexer) { lexer->chunk = NULL; lexer->chunk_start = 0; lexer->chunk_size = 0; - lexer->current_position = (TSLength) {}; - lexer->token_start_position = (TSLength) {}; - lexer->token_end_position = (TSLength) {}; + lexer->current_position = ts_length_zero(), + lexer->token_start_position = ts_length_zero(), + lexer->token_end_position = ts_length_zero(), lexer->lookahead = 0; lexer->lookahead_size = 0; } diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 001502b7..2b09fbe7 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -21,7 +21,7 @@ static TSParseAction action_for(const TSLanguage *lang, TSStateId state, static TSLength breakdown_stack(TSParser *parser, TSInputEdit *edit) { if (!edit) { ts_stack_shrink(&parser->stack, 0); - return (TSLength) {}; + return ts_length_zero(); } TSStack *stack = &parser->stack; diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 6795c00d..5958fc8e 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -48,7 +48,7 @@ void ts_stack_shrink(TSStack *stack, size_t new_size) { } TSLength ts_stack_right_position(const TSStack *stack) { - TSLength result = {}; + TSLength result = ts_length_zero(); for (size_t i = 0; i < stack->size; i++) { TSTree *node = stack->entries[i].node; result = ts_length_add(result, ts_tree_total_size(node));