Consolidate reading of input chunks in parser

This commit is contained in:
Max Brunsfeld 2014-03-11 08:30:19 -07:00
parent 42e9a264f3
commit ff0c8a98b8
4 changed files with 17 additions and 17 deletions

View file

@ -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;

View file

@ -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<SpyReader *>(data);
SpyReader *reader = static_cast<SpyReader *>(data);
reader->position = position;
return 0;
}
static void spy_release(void *data) {
SpyReader *reader = static_cast<SpyReader *>(data);
SpyReader *reader = static_cast<SpyReader *>(data);
delete reader;
}

View file

@ -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<string>({
"\"ok",

View file

@ -9,7 +9,7 @@
namespace tree_sitter {
class PreparedGrammar : public Grammar {
public:
public:
PreparedGrammar(std::string start_rule_name,
const std::map<const std::string, const rules::rule_ptr> &rules,
const std::map<const std::string, const rules::rule_ptr> &aux_rules);