tree-sitter/src/runtime/document.cpp

38 lines
1,006 B
C++
Raw Normal View History

2014-02-18 09:07:00 -08:00
#include "tree_sitter/runtime.h"
2014-01-07 21:50:32 -08:00
struct TSDocument {
2014-01-08 18:35:16 -08:00
TSParseFn *parse_fn;
2014-01-07 21:50:32 -08:00
const char **symbol_names;
const char *text;
TSParseError error;
2014-01-07 21:50:32 -08:00
TSTree *tree;
};
TSDocument * TSDocumentMake() {
return new TSDocument();
2014-01-07 21:50:32 -08:00
}
2014-01-08 18:35:16 -08:00
void TSDocumentSetUp(TSDocument *document, TSParseConfig config) {
document->parse_fn = config.parse_fn;
document->symbol_names = config.symbol_names;
2014-01-07 21:50:32 -08:00
}
void TSDocumentSetText(TSDocument *document, const char *text) {
2014-01-08 18:35:16 -08:00
TSParseResult result = document->parse_fn(text);
document->text = text;
2014-01-08 18:35:16 -08:00
document->tree = result.tree;
document->error = result.error;
2014-01-07 21:50:32 -08:00
}
TSTree * TSDocumentTree(const TSDocument *document) {
return document->tree;
}
2014-01-08 18:35:16 -08:00
const char * TSDocumentToString(const TSDocument *document) {
if (document->error.expected_inputs != NULL) {
return TSParseErrorToString(&document->error, document->text, document->symbol_names);
} else {
2014-01-23 13:00:08 -08:00
return TSTreeToString(document->tree, document->symbol_names);
}
2014-01-07 21:50:32 -08:00
}