tree-sitter/src/runtime/document.c

68 lines
1.9 KiB
C
Raw Normal View History

#include "tree_sitter/parser.h"
2014-07-17 23:29:11 -07:00
#include "runtime/node.h"
#include "runtime/parser.h"
2014-08-01 12:43:14 -07:00
#include "runtime/string_input.h"
2014-01-07 21:50:32 -08:00
2014-06-28 18:37:29 -07:00
struct TSDocument {
TSParser parser;
2014-07-20 20:27:33 -07:00
TSInput input;
TSNode *node;
2014-09-06 17:56:00 -07:00
int debug;
2014-01-07 21:50:32 -08:00
};
TSDocument *ts_document_make() { return calloc(sizeof(TSDocument), 1); }
2014-01-07 21:50:32 -08:00
2014-06-28 18:37:29 -07:00
void ts_document_free(TSDocument *document) {
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);
if (document->node)
ts_node_release(document->node);
2014-07-20 20:27:33 -07:00
free(document);
2014-02-20 18:38:31 -08:00
}
static void reparse(TSDocument *document, TSInputEdit *edit) {
if (document->input.read_fn && document->parser.language) {
2014-08-09 01:03:55 -07:00
const TSTree *tree =
ts_parser_parse(&document->parser, document->input, edit);
if (document->node)
ts_node_release(document->node);
2014-08-09 01:03:55 -07:00
document->node =
ts_node_make_root(tree, document->parser.language->symbol_names);
}
}
void ts_document_set_language(TSDocument *document, const TSLanguage *language) {
ts_parser_destroy(&document->parser);
document->parser = ts_parser_make(language);
2014-09-06 17:56:00 -07:00
document->parser.debug = document->debug;
reparse(document, NULL);
}
2014-09-06 17:56:00 -07:00
void ts_document_set_debug(TSDocument *document, int debug) {
document->debug = debug;
document->parser.debug = debug;
}
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;
reparse(document, NULL);
}
void ts_document_edit(TSDocument *document, TSInputEdit edit) {
reparse(document, &edit);
}
const char *ts_document_symbol_name(const TSDocument *document,
const TSTree *tree) {
return document->parser.language->symbol_names[tree->symbol];
}
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
TSNode *ts_document_root_node(const TSDocument *document) {
return document->node;
2014-07-17 23:29:11 -07:00
}