2014-02-18 09:07:00 -08:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
2014-07-14 21:11:15 -07:00
|
|
|
#include "runtime/tree.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/node.h"
|
2014-07-30 23:40:02 -07:00
|
|
|
#include "runtime/parser.h"
|
2014-03-01 22:43:25 -08:00
|
|
|
#include <string.h>
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
struct TSDocument {
|
2014-07-30 23:40:02 -07:00
|
|
|
TSParser parser;
|
2014-07-20 20:27:33 -07:00
|
|
|
const TSTree *tree;
|
|
|
|
|
TSInput input;
|
2014-01-07 21:50:32 -08:00
|
|
|
};
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSDocument *ts_document_make() {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSDocument *document = malloc(sizeof(TSDocument));
|
2014-07-29 12:59:18 -07:00
|
|
|
*document = (TSDocument) {
|
|
|
|
|
.input = (TSInput) { .data = NULL, .read_fn = NULL, .seek_fn = NULL },
|
|
|
|
|
.tree = NULL
|
|
|
|
|
};
|
2014-07-20 20:27:33 -07:00
|
|
|
return document;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_free(TSDocument *document) {
|
2014-07-30 23:40:02 -07:00
|
|
|
ts_parser_destroy(&document->parser);
|
2014-07-20 20:27:33 -07:00
|
|
|
if (document->input.release_fn)
|
|
|
|
|
document->input.release_fn(document->input.data);
|
|
|
|
|
free(document);
|
2014-02-20 18:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:11:39 -07:00
|
|
|
void ts_document_set_language(TSDocument *document,
|
|
|
|
|
const TSLanguage *language) {
|
2014-07-30 23:40:02 -07:00
|
|
|
ts_parser_destroy(&document->parser);
|
|
|
|
|
document->parser = ts_parser_make(language);
|
2014-08-01 12:34:40 -07:00
|
|
|
if (document->input.read_fn)
|
|
|
|
|
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const TSTree *ts_document_tree(const TSDocument *document) {
|
2014-07-20 20:27:33 -07:00
|
|
|
return document->tree;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const char *ts_document_string(const TSDocument *document) {
|
2014-07-31 13:11:39 -07:00
|
|
|
return ts_tree_string(document->tree,
|
|
|
|
|
document->parser.language->symbol_names);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:04 -07:00
|
|
|
void ts_document_set_input(TSDocument *document, TSInput input) {
|
2014-07-20 20:27:33 -07:00
|
|
|
document->input = input;
|
2014-08-01 12:34:40 -07:00
|
|
|
if (document->parser.language)
|
|
|
|
|
document->tree = ts_parser_parse(&document->parser, document->input, NULL);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:47 -07:00
|
|
|
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
|
2014-07-30 23:40:02 -07:00
|
|
|
document->tree = ts_parser_parse(&document->parser, document->input, &edit);
|
2014-03-12 13:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const char *ts_document_symbol_name(const TSDocument *document,
|
|
|
|
|
const TSTree *tree) {
|
2014-07-30 23:40:02 -07:00
|
|
|
return document->parser.language->symbol_names[tree->symbol];
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
|
|
|
|
|
2014-03-01 22:43:25 -08:00
|
|
|
typedef struct {
|
2014-07-20 20:27:33 -07:00
|
|
|
const char *string;
|
|
|
|
|
size_t position;
|
|
|
|
|
size_t length;
|
2014-06-28 19:29:44 -07:00
|
|
|
} TSStringInput;
|
2014-03-01 22:43:25 -08:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
const char *ts_string_input_read(void *d, size_t *bytes_read) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSStringInput *data = (TSStringInput *)d;
|
|
|
|
|
if (data->position >= data->length) {
|
|
|
|
|
*bytes_read = 0;
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
size_t previous_position = data->position;
|
|
|
|
|
data->position = data->length;
|
|
|
|
|
*bytes_read = data->position - previous_position;
|
|
|
|
|
return data->string + previous_position;
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ts_string_input_seek(void *d, size_t position) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSStringInput *data = (TSStringInput *)d;
|
|
|
|
|
data->position = position;
|
|
|
|
|
return (position < data->length);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:56:04 -07:00
|
|
|
TSInput ts_string_input_make(const char *string) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSStringInput *data = malloc(sizeof(TSStringInput));
|
|
|
|
|
data->string = string;
|
|
|
|
|
data->position = 0;
|
|
|
|
|
data->length = strlen(string);
|
2014-07-20 21:43:27 -07:00
|
|
|
return (TSInput) { .data = (void *)data,
|
|
|
|
|
.read_fn = ts_string_input_read,
|
|
|
|
|
.seek_fn = ts_string_input_seek,
|
|
|
|
|
.release_fn = free };
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 18:37:29 -07:00
|
|
|
void ts_document_set_input_string(TSDocument *document, const char *text) {
|
2014-07-20 20:27:33 -07:00
|
|
|
ts_document_set_input(document, ts_string_input_make(text));
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
2014-07-17 23:29:11 -07:00
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_document_root_node(const TSDocument *document) {
|
2014-08-01 12:34:40 -07:00
|
|
|
if (document->tree)
|
|
|
|
|
return ts_node_make_root(document->tree,
|
|
|
|
|
document->parser.language->symbol_names);
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
TSNode *ts_document_get_node(const TSDocument *document, size_t pos) {
|
2014-07-20 20:27:33 -07:00
|
|
|
TSNode *root = ts_document_root_node(document);
|
|
|
|
|
TSNode *result = ts_node_leaf_at_pos(root, pos);
|
|
|
|
|
ts_node_release(root);
|
|
|
|
|
return result;
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|