From ff0c8a98b82fd7c1292a63435679542f68a08847 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 11 Mar 2014 08:30:19 -0700 Subject: [PATCH] Consolidate reading of input chunks in parser --- include/tree_sitter/parser.h | 22 +++++++++++----------- spec/runtime/helpers/spy_reader.cc | 4 ++-- spec/runtime/parser_spec.cc | 6 +++--- src/compiler/prepared_grammar.h | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index a9d211ec..3257e007 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -38,12 +38,11 @@ typedef struct { typedef struct { ts_input input; - const char *chunk; size_t chunk_start; size_t chunk_size; size_t position_in_chunk; - + size_t token_end_position; size_t token_start_position; @@ -56,29 +55,30 @@ typedef struct { static void ts_lex(ts_parser *parser); static const ts_symbol * ts_recover(ts_state state, ts_state *to_state, size_t *count); +static void ts_parser_advance(ts_parser *); static ts_parser ts_parser_make(ts_input input) { - size_t bytes_read = 0; - const char *chunk = input.read_fn(input.data, &bytes_read); ts_parser result = { .input = input, + .chunk = NULL, + .chunk_start = 0, + .chunk_size = 0, + .position_in_chunk = 0, + .token_start_position = 0, .token_end_position = 0, - .chunk = chunk, - .chunk_size = bytes_read, - .chunk_start = 0, - .position_in_chunk = 0, - .lookahead_node = NULL, .prev_lookahead_node = NULL, .lex_state = 0, .stack = calloc(INITIAL_STACK_SIZE, sizeof(ts_stack_entry)), .stack_size = 0, }; + + ts_parser_advance(&result); return result; } - + static size_t ts_parser_position(const ts_parser *parser) { return parser->chunk_start + parser->position_in_chunk; } @@ -157,7 +157,7 @@ static void ts_parser_reduce(ts_parser *parser, ts_symbol symbol, int immediate_ static const char empty_chunk[1] = { '\0' }; static void ts_parser_advance(ts_parser *parser) { - if (parser->position_in_chunk < parser->chunk_size - 1) { + if (parser->position_in_chunk + 1 < parser->chunk_size) { parser->position_in_chunk++; } else { parser->chunk_start += parser->chunk_size; diff --git a/spec/runtime/helpers/spy_reader.cc b/spec/runtime/helpers/spy_reader.cc index f376ecde..194be372 100644 --- a/spec/runtime/helpers/spy_reader.cc +++ b/spec/runtime/helpers/spy_reader.cc @@ -15,13 +15,13 @@ static const char * spy_read(void *data, size_t *bytes_read) { } static int spy_seek(void *data, size_t position) { - SpyReader *reader = static_cast(data); + SpyReader *reader = static_cast(data); reader->position = position; return 0; } static void spy_release(void *data) { - SpyReader *reader = static_cast(data); + SpyReader *reader = static_cast(data); delete reader; } diff --git a/spec/runtime/parser_spec.cc b/spec/runtime/parser_spec.cc index d247eb87..15cf1af2 100644 --- a/spec/runtime/parser_spec.cc +++ b/spec/runtime/parser_spec.cc @@ -7,12 +7,12 @@ START_TEST describe("reading from an input", [&]() { ts_document *doc; - + before_each([&]() { doc = ts_document_make(); ts_document_set_parser(doc, ts_parse_config_json); }); - + after_each([&]() { ts_document_free(doc); }); @@ -20,7 +20,7 @@ describe("reading from an input", [&]() { it("reads the entire input", [&]() { SpyReader reader("\"ok go do it!\"", 3); ts_document_set_input(doc, reader.input); - + AssertThat(string(ts_document_string(doc)), Equals("(value (string))")); AssertThat(reader.chunks_read, Equals(vector({ "\"ok", diff --git a/src/compiler/prepared_grammar.h b/src/compiler/prepared_grammar.h index f3a26b80..ee2804fa 100644 --- a/src/compiler/prepared_grammar.h +++ b/src/compiler/prepared_grammar.h @@ -9,7 +9,7 @@ namespace tree_sitter { class PreparedGrammar : public Grammar { - public: + public: PreparedGrammar(std::string start_rule_name, const std::map &rules, const std::map &aux_rules);