2014-07-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/node.h"
|
2015-08-22 10:48:34 -07:00
|
|
|
#include "runtime/tree.h"
|
2015-07-31 15:47:48 -07:00
|
|
|
#include "runtime/length.h"
|
2014-07-30 23:40:02 -07:00
|
|
|
#include "runtime/parser.h"
|
2014-08-01 12:43:14 -07:00
|
|
|
#include "runtime/string_input.h"
|
2015-07-31 15:47:48 -07:00
|
|
|
#include "runtime/document.h"
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2014-10-13 01:02:12 -07:00
|
|
|
TSDocument *ts_document_make() {
|
|
|
|
|
TSDocument *document = calloc(sizeof(TSDocument), 1);
|
|
|
|
|
document->parser = ts_parser_make();
|
|
|
|
|
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);
|
2015-07-31 15:47:48 -07:00
|
|
|
if (document->tree)
|
|
|
|
|
ts_tree_release(document->tree);
|
2014-07-20 20:27:33 -07:00
|
|
|
free(document);
|
2014-02-20 18:38:31 -08:00
|
|
|
}
|
|
|
|
|
|
2015-08-16 19:53:34 -07:00
|
|
|
static void ts_document__reparse(TSDocument *document, TSInputEdit *edit) {
|
2014-08-08 12:58:39 -07:00
|
|
|
if (document->input.read_fn && document->parser.language) {
|
2015-07-31 15:47:48 -07:00
|
|
|
TSTree *tree = ts_parser_parse(&document->parser, document->input, edit);
|
|
|
|
|
if (document->tree)
|
|
|
|
|
ts_tree_release(document->tree);
|
|
|
|
|
document->tree = tree;
|
|
|
|
|
ts_tree_retain(tree);
|
2014-08-08 12:58:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-09 13:15:40 -07:00
|
|
|
void ts_document_set_language(TSDocument *document, const TSLanguage *language) {
|
2014-10-13 01:02:12 -07:00
|
|
|
document->parser.language = language;
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_document__reparse(document, NULL);
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 17:52:54 -07:00
|
|
|
TSDebugger ts_document_get_debugger(const TSDocument *document) {
|
|
|
|
|
return ts_parser_get_debugger(&document->parser);
|
2014-10-13 01:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 17:52:54 -07:00
|
|
|
void ts_document_set_debugger(TSDocument *document, TSDebugger debugger) {
|
|
|
|
|
ts_parser_set_debugger(&document->parser, debugger);
|
2014-09-06 17:56:00 -07: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;
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_document__reparse(document, 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) {
|
2015-08-16 19:53:34 -07:00
|
|
|
ts_document__reparse(document, &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-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
|
|
|
|
2015-07-31 15:47:48 -07:00
|
|
|
TSNode ts_document_root_node(const TSDocument *document) {
|
2015-08-22 10:48:34 -07:00
|
|
|
TSTree *tree = document->tree;
|
|
|
|
|
while (tree && ts_tree_is_singleton(tree))
|
|
|
|
|
tree = tree->children[0];
|
|
|
|
|
return ts_node_make(tree, ts_length_zero());
|
2014-07-17 23:29:11 -07:00
|
|
|
}
|