Consolidate TSInput interface down to one function
This commit is contained in:
parent
2a507f0739
commit
b0b3b2e5f3
8 changed files with 39 additions and 65 deletions
|
|
@ -17,14 +17,13 @@
|
|||
static const char empty_chunk[2] = { 0, 0 };
|
||||
|
||||
static void ts_lexer__get_chunk(Lexer *self) {
|
||||
TSInput input = self->input;
|
||||
if (!self->chunk ||
|
||||
self->current_position.bytes != self->chunk_start + self->chunk_size) {
|
||||
input.seek(input.payload, self->current_position.bytes, self->current_position.extent);
|
||||
}
|
||||
|
||||
self->chunk_start = self->current_position.bytes;
|
||||
self->chunk = input.read(input.payload, &self->chunk_size);
|
||||
self->chunk = self->input.read(
|
||||
self->input.payload,
|
||||
self->current_position.bytes,
|
||||
self->current_position.extent,
|
||||
&self->chunk_size
|
||||
);
|
||||
if (!self->chunk_size) self->chunk = empty_chunk;
|
||||
}
|
||||
|
||||
|
|
@ -74,8 +73,9 @@ static void ts_lexer__advance(void *payload, bool skip) {
|
|||
LOG_CHARACTER("consume", self->data.lookahead);
|
||||
}
|
||||
|
||||
if (self->current_position.bytes >= self->chunk_start + self->chunk_size)
|
||||
if (self->current_position.bytes >= self->chunk_start + self->chunk_size) {
|
||||
ts_lexer__get_chunk(self);
|
||||
}
|
||||
|
||||
ts_lexer__get_lookahead(self);
|
||||
}
|
||||
|
|
@ -105,10 +105,8 @@ static uint32_t ts_lexer__get_column(void *payload) {
|
|||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* The lexer's advance method is stored as a struct field so that generated
|
||||
* parsers can call it without needing to be linked against this library.
|
||||
*/
|
||||
// The lexer's methods are stored as a struct field so that generated
|
||||
// parsers can call them without needing to be linked against this library.
|
||||
|
||||
void ts_lexer_init(Lexer *self) {
|
||||
*self = (Lexer){
|
||||
|
|
@ -163,11 +161,8 @@ void ts_lexer_start(Lexer *self) {
|
|||
self->token_start_position = self->current_position;
|
||||
self->token_end_position = LENGTH_UNDEFINED;
|
||||
self->data.result_symbol = 0;
|
||||
|
||||
if (!self->chunk)
|
||||
ts_lexer__get_chunk(self);
|
||||
if (!self->lookahead_size)
|
||||
ts_lexer__get_lookahead(self);
|
||||
if (!self->chunk) ts_lexer__get_chunk(self);
|
||||
if (!self->lookahead_size) ts_lexer__get_lookahead(self);
|
||||
}
|
||||
|
||||
void ts_lexer_advance_to_end(Lexer *self) {
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ struct TSParser {
|
|||
size_t operation_limit;
|
||||
volatile bool enabled;
|
||||
bool halt_on_error;
|
||||
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -1461,7 +1462,7 @@ TSTree *ts_parser_resume(TSParser *self) {
|
|||
self->finished_tree = NULL;
|
||||
ts_stack_clear(self->stack);
|
||||
ts_parser__set_cached_token(self, 0, NULL, NULL);
|
||||
ts_lexer_set_input(&self->lexer, (TSInput) { NULL, NULL, NULL, 0 });
|
||||
ts_lexer_set_input(&self->lexer, (TSInput) { NULL, NULL, 0 });
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -1475,5 +1476,5 @@ TSTree *ts_parser_parse_string(TSParser *self, const TSTree *old_tree,
|
|||
const char *string, uint32_t length) {
|
||||
TSStringInput input;
|
||||
ts_string_input_init(&input, string, length);
|
||||
return ts_parser_parse(self, old_tree, input.input);
|
||||
return ts_parser_parse(self, old_tree, ts_string_input_get(&input));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,32 +2,27 @@
|
|||
#include "runtime/string_input.h"
|
||||
#include <string.h>
|
||||
|
||||
static const char *ts_string_input__read(void *payload, uint32_t *bytes_read) {
|
||||
static const char *ts_string_input__read(void *payload, uint32_t byte_offset,
|
||||
TSPoint _, uint32_t *bytes_read) {
|
||||
TSStringInput *input = (TSStringInput *)payload;
|
||||
if (input->position >= input->length) {
|
||||
if (byte_offset >= input->length) {
|
||||
*bytes_read = 0;
|
||||
return "";
|
||||
} else {
|
||||
*bytes_read = input->length - byte_offset;
|
||||
return input->string + byte_offset;
|
||||
}
|
||||
uint32_t previous_position = input->position;
|
||||
input->position = input->length;
|
||||
*bytes_read = input->position - previous_position;
|
||||
return input->string + previous_position;
|
||||
}
|
||||
|
||||
static int ts_string_input__seek(void *payload, uint32_t byte, TSPoint _) {
|
||||
TSStringInput *input = (TSStringInput *)payload;
|
||||
input->position = byte;
|
||||
return (byte < input->length);
|
||||
}
|
||||
|
||||
void ts_string_input_init(TSStringInput *self, const char *string, uint32_t length) {
|
||||
self->string = string;
|
||||
self->position = 0;
|
||||
self->length = length;
|
||||
self->input = (TSInput) {
|
||||
}
|
||||
|
||||
TSInput ts_string_input_get(TSStringInput *self) {
|
||||
return (TSInput) {
|
||||
.payload = self,
|
||||
.read = ts_string_input__read,
|
||||
.seek = ts_string_input__seek,
|
||||
.encoding = TSInputEncodingUTF8,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,11 @@ extern "C" {
|
|||
|
||||
typedef struct {
|
||||
const char *string;
|
||||
uint32_t position;
|
||||
uint32_t length;
|
||||
TSInput input;
|
||||
} TSStringInput;
|
||||
|
||||
void ts_string_input_init(TSStringInput *, const char *, uint32_t);
|
||||
TSInput ts_string_input_get(TSStringInput *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue