tree-sitter/src/runtime/document.c
Max Brunsfeld e23f11b7c4 Allow lexical debug mode to be enabled on documents
- `ts_document_set_debug(doc, 1)` implies parse debug mode
- `ts_document_set_debug(doc, > 1)` implies parse and lex debug mode
2014-09-11 13:12:06 -07:00

71 lines
2 KiB
C

#include "tree_sitter/parser.h"
#include "runtime/node.h"
#include "runtime/parser.h"
#include "runtime/string_input.h"
struct TSDocument {
TSParser parser;
TSInput input;
TSNode *node;
int debug;
};
TSDocument *ts_document_make() { return calloc(sizeof(TSDocument), 1); }
void ts_document_free(TSDocument *document) {
ts_parser_destroy(&document->parser);
if (document->input.release_fn)
document->input.release_fn(document->input.data);
if (document->node)
ts_node_release(document->node);
free(document);
}
static void reparse(TSDocument *document, TSInputEdit *edit) {
if (document->input.read_fn && document->parser.language) {
const TSTree *tree =
ts_parser_parse(&document->parser, document->input, edit);
if (document->node)
ts_node_release(document->node);
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);
ts_document_set_debug(document, document->debug);
reparse(document, NULL);
}
void ts_document_set_debug(TSDocument *document, int debug) {
document->debug = debug;
document->parser.debug = debug;
if (debug > 1)
document->parser.lexer.debug = 1;
else
document->parser.lexer.debug = 0;
}
void ts_document_set_input(TSDocument *document, TSInput input) {
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];
}
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) {
return document->node;
}