Don't store text on documents
This commit is contained in:
parent
6ed6aa05cc
commit
ceee7eefd0
5 changed files with 21 additions and 13 deletions
|
|
@ -123,9 +123,9 @@ static void TSParserReduce(ts_parser *parser, ts_symbol symbol, int immediate_ch
|
||||||
static void TSParserError(ts_parser *parser, size_t count, const char **expected_inputs) {
|
static void TSParserError(ts_parser *parser, size_t count, const char **expected_inputs) {
|
||||||
ts_error *error = &parser->result.error;
|
ts_error *error = &parser->result.error;
|
||||||
error->position = parser->position;
|
error->position = parser->position;
|
||||||
|
error->lookahead_char = TSParserLookaheadChar(parser);
|
||||||
error->expected_input_count = count;
|
error->expected_input_count = count;
|
||||||
error->expected_inputs = expected_inputs;
|
error->expected_inputs = expected_inputs;
|
||||||
error->lookahead_sym = TSParserLookaheadSym(parser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int TSParserHasError(const ts_parser *parser) {
|
static int TSParserHasError(const ts_parser *parser) {
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,15 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char **expected_inputs;
|
|
||||||
size_t expected_input_count;
|
|
||||||
size_t position;
|
size_t position;
|
||||||
long lookahead_sym;
|
char lookahead_char;
|
||||||
|
size_t expected_input_count;
|
||||||
|
const char **expected_inputs;
|
||||||
} ts_error;
|
} ts_error;
|
||||||
|
|
||||||
const char * ts_error_string(const ts_error *error, const char *input_string, const char **symbol_names);
|
const char * ts_error_string(const ts_error *error);
|
||||||
|
|
||||||
typedef size_t ts_symbol;
|
typedef size_t ts_symbol;
|
||||||
|
|
||||||
|
|
@ -46,6 +46,7 @@ typedef struct {
|
||||||
typedef struct ts_document ts_document;
|
typedef struct ts_document ts_document;
|
||||||
|
|
||||||
ts_document * ts_document_make();
|
ts_document * ts_document_make();
|
||||||
|
void ts_document_free(ts_document *);
|
||||||
void ts_document_set_parser(ts_document *document, ts_parse_config config);
|
void ts_document_set_parser(ts_document *document, ts_parse_config config);
|
||||||
void ts_document_set_text(ts_document *document, const char *text);
|
void ts_document_set_text(ts_document *document, const char *text);
|
||||||
ts_tree * ts_document_tree(const ts_document *document);
|
ts_tree * ts_document_tree(const ts_document *document);
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ describe("json", []() {
|
||||||
ts_document_set_parser(document, ts_parse_config_json);
|
ts_document_set_parser(document, ts_parse_config_json);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
after_each([&]() {
|
||||||
|
ts_document_free(document);
|
||||||
|
});
|
||||||
|
|
||||||
it("parses strings", [&]() {
|
it("parses strings", [&]() {
|
||||||
ts_document_set_text(document, "\"\"");
|
ts_document_set_text(document, "\"\"");
|
||||||
AssertThat(string(ts_document_string(document)), Equals("(value (string))"));
|
AssertThat(string(ts_document_string(document)), Equals("(value (string))"));
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
struct ts_document {
|
struct ts_document {
|
||||||
ts_parse_fn *parse_fn;
|
ts_parse_fn *parse_fn;
|
||||||
const char **symbol_names;
|
const char **symbol_names;
|
||||||
const char *text;
|
|
||||||
ts_error error;
|
ts_error error;
|
||||||
ts_tree *tree;
|
ts_tree *tree;
|
||||||
};
|
};
|
||||||
|
|
@ -12,6 +11,10 @@ ts_document * ts_document_make() {
|
||||||
return new ts_document();
|
return new ts_document();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ts_document_free(ts_document *document) {
|
||||||
|
delete document;
|
||||||
|
}
|
||||||
|
|
||||||
void ts_document_set_parser(ts_document *document, ts_parse_config config) {
|
void ts_document_set_parser(ts_document *document, ts_parse_config config) {
|
||||||
document->parse_fn = config.parse_fn;
|
document->parse_fn = config.parse_fn;
|
||||||
document->symbol_names = config.symbol_names;
|
document->symbol_names = config.symbol_names;
|
||||||
|
|
@ -19,7 +22,6 @@ void ts_document_set_parser(ts_document *document, ts_parse_config config) {
|
||||||
|
|
||||||
void ts_document_set_text(ts_document *document, const char *text) {
|
void ts_document_set_text(ts_document *document, const char *text) {
|
||||||
ts_parse_result result = document->parse_fn(text);
|
ts_parse_result result = document->parse_fn(text);
|
||||||
document->text = text;
|
|
||||||
document->tree = result.tree;
|
document->tree = result.tree;
|
||||||
document->error = result.error;
|
document->error = result.error;
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +32,7 @@ ts_tree * ts_document_tree(const ts_document *document) {
|
||||||
|
|
||||||
const char * ts_document_string(const ts_document *document) {
|
const char * ts_document_string(const ts_document *document) {
|
||||||
if (document->error.expected_inputs != NULL) {
|
if (document->error.expected_inputs != NULL) {
|
||||||
return ts_error_string(&document->error, document->text, document->symbol_names);
|
return ts_error_string(&document->error);
|
||||||
} else {
|
} else {
|
||||||
return ts_tree_string(document->tree, document->symbol_names);
|
return ts_tree_string(document->tree, document->symbol_names);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,11 @@
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
const char * ts_error_string(const ts_error *error, const char *input_string, const char **symbol_names) {
|
const char * ts_error_string(const ts_error *error) {
|
||||||
string result = string("Unexpected character '") + input_string[error->position] + "'. Expected: ";
|
string result = string("Unexpected character '") + error->lookahead_char + "'. Expected:";
|
||||||
for (int i = 0; i < error->expected_input_count; i++)
|
for (int i = 0; i < error->expected_input_count; i++) {
|
||||||
result += string(error->expected_inputs[i]) + " ";
|
result += string(" ") + error->expected_inputs[i];
|
||||||
|
}
|
||||||
|
|
||||||
char *stuff = (char *)malloc(result.size() * sizeof(char));
|
char *stuff = (char *)malloc(result.size() * sizeof(char));
|
||||||
strcpy(stuff, result.c_str());
|
strcpy(stuff, result.c_str());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue