From d910a2d0e73a610c62968aad3d2146c6a4ccc1f0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 16 Aug 2015 10:51:34 -0700 Subject: [PATCH] Rename node position to offset --- include/tree_sitter/runtime.h | 2 +- spec/runtime/helpers/tree_helpers.cc | 6 ++-- src/runtime/node.c | 45 +++++++++++++++------------- src/runtime/node.h | 2 +- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 2bfea562..d197c118 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -39,7 +39,7 @@ typedef struct { typedef struct { const void *data; - TSLength position; + TSLength offset; } TSNode; typedef unsigned short TSSymbol; diff --git a/spec/runtime/helpers/tree_helpers.cc b/spec/runtime/helpers/tree_helpers.cc index 2e20b4c0..767d36a3 100644 --- a/spec/runtime/helpers/tree_helpers.cc +++ b/spec/runtime/helpers/tree_helpers.cc @@ -28,7 +28,7 @@ std::ostream &operator<<(std::ostream &stream, const TSTree *tree) { return stream << std::string(ts_tree_string(tree, symbol_names));; } -std::ostream &operator<<(std::ostream &stream, const TSNode ref) { - return stream << std::string("{") << (const TSTree *)ref.data << - std::string(", ") << std::to_string(ref.position.chars) << std::string("}"); +std::ostream &operator<<(std::ostream &stream, const TSNode node) { + return stream << std::string("{") << (const TSTree *)node.data << + std::string(", ") << std::to_string(ts_node_pos(node).chars) << std::string("}"); } diff --git a/src/runtime/node.c b/src/runtime/node.c index 3673292d..faf79811 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,38 +4,43 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength position) { - return (TSNode){.data = tree, .position = position }; +TSNode ts_node_make(const TSTree *tree, TSLength offset) { + return (TSNode){.data = tree, .offset = offset }; } -static inline const TSTree *get_tree(TSNode this) { +static inline const TSTree *ts_node__tree(TSNode this) { return this.data; } +static inline TSLength ts_node__offset(TSNode this) { + return this.offset; +} + TSLength ts_node_pos(TSNode this) { - return ts_length_add(this.position, get_tree(this)->padding); + return ts_length_add(ts_node__offset(this), ts_node__tree(this)->padding); } TSLength ts_node_size(TSNode this) { - return get_tree(this)->size; + return ts_node__tree(this)->size; } bool ts_node_eq(TSNode this, TSNode other) { - return ts_tree_eq(get_tree(this), get_tree(other)) && - ts_length_eq(this.position, other.position); + return ts_tree_eq(ts_node__tree(this), ts_node__tree(other)) && + ts_length_eq(ts_node__offset(this), ts_node__offset(other)); } const char *ts_node_name(TSNode this, const TSDocument *document) { - return document->parser.language->symbol_names[get_tree(this)->symbol]; + return document->parser.language->symbol_names[ts_node__tree(this)->symbol]; } const char *ts_node_string(TSNode this, const TSDocument *document) { - return ts_tree_string(get_tree(this), document->parser.language->symbol_names); + return ts_tree_string(ts_node__tree(this), + document->parser.language->symbol_names); } TSNode ts_node_parent(TSNode this) { - const TSTree *tree = get_tree(this); - TSLength position = this.position; + const TSTree *tree = ts_node__tree(this); + TSLength position = ts_node__offset(this); do { TSTree *parent = tree->context.parent; @@ -54,8 +59,8 @@ TSNode ts_node_parent(TSNode this) { } TSNode ts_node_prev_sibling(TSNode this) { - const TSTree *tree = get_tree(this); - TSLength position = this.position; + const TSTree *tree = ts_node__tree(this); + TSLength position = ts_node__offset(this); do { TSTree *parent = tree->context.parent; @@ -79,8 +84,8 @@ TSNode ts_node_prev_sibling(TSNode this) { } TSNode ts_node_next_sibling(TSNode this) { - const TSTree *tree = get_tree(this); - TSLength position = this.position; + const TSTree *tree = ts_node__tree(this); + TSLength position = ts_node__offset(this); do { TSTree *parent = tree->context.parent; @@ -105,12 +110,12 @@ TSNode ts_node_next_sibling(TSNode this) { } size_t ts_node_child_count(TSNode this) { - return get_tree(this)->visible_child_count; + return ts_node__tree(this)->visible_child_count; } TSNode ts_node_child(TSNode this, size_t child_index) { - const TSTree *tree = get_tree(this); - TSLength position = this.position; + const TSTree *tree = ts_node__tree(this); + TSLength position = ts_node__offset(this); bool did_descend = true; while (did_descend) { @@ -141,8 +146,8 @@ TSNode ts_node_child(TSNode this, size_t child_index) { } TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) { - const TSTree *tree = get_tree(this), *last_visible_tree = tree; - TSLength position = this.position, last_visible_position = position; + const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree; + TSLength position = ts_node__offset(this), last_visible_position = position; bool did_descend = true; while (did_descend) { diff --git a/src/runtime/node.h b/src/runtime/node.h index c9990df0..609164dc 100644 --- a/src/runtime/node.h +++ b/src/runtime/node.h @@ -8,7 +8,7 @@ TSNode ts_node_make(const TSTree *, TSLength); static inline TSNode ts_node_null() { - return (TSNode){.data = NULL, .position = ts_length_zero() }; + return (TSNode){.data = NULL, .offset = ts_length_zero() }; } #endif