Hide TSTree, expose TSNode

This commit is contained in:
Max Brunsfeld 2014-07-17 23:29:11 -07:00
parent 02904085c2
commit b3385f20c8
13 changed files with 333 additions and 122 deletions

View file

@ -1,6 +1,7 @@
#include "tree_sitter/runtime.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
#include "runtime/node.h"
#include <string.h>
struct TSDocument {
@ -11,11 +12,16 @@ struct TSDocument {
};
TSDocument * ts_document_make() {
return malloc(sizeof(TSDocument));
TSDocument *document = malloc(sizeof(TSDocument));
*document = (TSDocument) {
.input = (TSInput) {}
};
return document;
}
void ts_document_free(TSDocument *document) {
ts_parser_free(document->parser);
if (document->parser)
ts_parser_free(document->parser);
if (document->input.release_fn)
document->input.release_fn(document->input.data);
free(document);
@ -87,3 +93,29 @@ TSInput ts_string_input_make(const char *string) {
void ts_document_set_input_string(TSDocument *document, const char *text) {
ts_document_set_input(document, ts_string_input_make(text));
}
TSNode * ts_document_root_node(const TSDocument *document) {
const TSTree *tree = document->tree;
size_t position = 0;
while (ts_tree_is_wrapper(tree)) {
position = tree->offset;
tree = tree->children[0];
}
TSNode *result = malloc(sizeof(TSNode));
*result = (TSNode) {
.ref_count = 1,
.position = position,
.content = tree,
.parent = NULL,
.config = &document->parser->config,
};
return result;
}
TSNode * ts_document_get_node(const TSDocument *document, size_t pos) {
TSNode *root = ts_document_root_node(document);
TSNode *result = ts_node_leaf_at_pos(root, pos);
ts_node_release(root);
return result;
}

View file

@ -1,5 +1,5 @@
#include "tree_sitter/runtime.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
TSLexer ts_lexer_make() {
return (TSLexer) {

95
src/runtime/node.c Normal file
View file

@ -0,0 +1,95 @@
#include "runtime/node.h"
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
TSNode * ts_node_make(TSTree *tree, TSNode *parent, size_t position, TSParserConfig *config) {
if (parent) ts_node_retain(parent);
TSNode *result = malloc(sizeof(TSNode));
*result = (TSNode) {
.ref_count = 1,
.parent = parent,
.content = tree,
.position = position,
.config = config
};
return result;
}
void ts_node_retain(TSNode *node) {
node->ref_count++;
}
void ts_node_release(TSNode *node) {
node->ref_count--;
if (node->ref_count == 0) {
if (node->parent) ts_node_release(node->parent);
free(node);
}
}
size_t ts_node_pos(const TSNode *node) {
return node->position;
}
size_t ts_node_size(const TSNode *node) {
return node->content->size;
}
const char * ts_node_name(const TSNode *node) {
return node->config->symbol_names[node->content->symbol];
}
TSTree * ts_tree_child(const TSTree *tree, size_t goal_index, size_t *offset, size_t *children_seen) {
*offset = 0;
*children_seen = 0;
size_t child_count;
TSTree **children = ts_tree_children(tree, &child_count);
if (!children) {
*offset = tree->offset + tree->size;
}
for (size_t i = 0; i < child_count; i++) {
size_t delta_index = 0, delta_offset = 0;
TSTree *child = children[i];
TSTree *result = NULL;
if (ts_tree_is_visible(child)) {
delta_offset = child->offset;
if (*children_seen == goal_index) {
result = child;
} else {
delta_index = 1;
delta_offset += child->size;
}
} else {
result = ts_tree_child(child, (goal_index - *children_seen), &delta_offset, &delta_index);
}
*offset += delta_offset;
*children_seen += delta_index;
if (result) {
return result;
}
}
return NULL;
}
const char * ts_node_string(const TSNode *node) {
return ts_tree_string(node->content, node->config->symbol_names);
}
TSNode * ts_node_child(TSNode *parent, size_t goal_index) {
size_t offset, index;
TSTree *child = ts_tree_child(parent->content, goal_index, &offset, &index);
if (child)
return ts_node_make(child, parent, offset, parent->config);
else
return NULL;
}
TSNode * ts_node_leaf_at_pos(TSNode *parent, size_t child_index) {
return NULL;
}

17
src/runtime/node.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef RUNTIME_NODE_H_
#define RUNTIME_NODE_H_
#include "tree_sitter/parser.h"
struct TSNode {
size_t ref_count;
size_t position;
size_t index;
const TSTree *content;
struct TSNode *parent;
TSParserConfig *config;
};
TSNode * ts_node_make(TSTree *tree, TSNode *parent, size_t position, TSParserConfig *config);
#endif

View file

@ -1,6 +1,6 @@
#include "tree_sitter/runtime.h"
#include <string.h>
#include <stdio.h>
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
static TSTree * ts_tree_make(TSSymbol symbol, size_t size, size_t offset, int is_hidden) {

View file

@ -1,8 +1,18 @@
#ifndef RUNTIME_TREE_H_
#define RUNTIME_TREE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "tree_sitter/runtime.h"
typedef enum {
TSTreeOptionsHidden = 1,
TSTreeOptionsExtra = 2,
TSTreeOptionsWrapper = 4,
} TSTreeOptions;
struct TSTree {
TSSymbol symbol;
TSTreeOptions options;
@ -38,4 +48,19 @@ static inline int ts_tree_is_wrapper(const TSTree *tree) {
return (tree->options & TSTreeOptionsWrapper);
}
TSTree * ts_tree_make_leaf(TSSymbol symbol, size_t size, size_t offset, int is_hidden);
TSTree * ts_tree_make_node(TSSymbol symbol, size_t child_count, TSTree **children, int is_hidden);
TSTree * ts_tree_make_error(char lookahead_char, size_t expected_input_count, const TSSymbol *expected_inputs, size_t size, size_t offset);
void ts_tree_retain(TSTree *tree);
void ts_tree_release(TSTree *tree);
int ts_tree_equals(const TSTree *tree1, const TSTree *tree2);
char * ts_tree_string(const TSTree *tree, const char **names);
char * ts_tree_error_string(const TSTree *tree, const char **names);
TSTree ** ts_tree_children(const TSTree *tree, size_t *count);
size_t ts_tree_total_size(const TSTree *tree);
#ifdef __cplusplus
}
#endif
#endif // RUNTIME_TREE_H_