2014-10-17 17:52:54 -07:00
|
|
|
#include <stdio.h>
|
2014-08-28 18:35:30 -07:00
|
|
|
#include "runtime/lexer.h"
|
2014-07-17 23:29:11 -07:00
|
|
|
#include "runtime/tree.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2015-12-28 10:40:53 -08:00
|
|
|
#include "runtime/utf16.h"
|
2014-09-13 00:15:24 -07:00
|
|
|
#include "utf8proc.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
#define LOG(...) \
|
|
|
|
|
if (self->logger.log) { \
|
|
|
|
|
snprintf(self->debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
|
|
|
|
self->logger.log(self->logger.payload, TSLogTypeLex, self->debug_buffer); \
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 13:33:37 -08:00
|
|
|
#define LOG_CHARACTER(message, character) \
|
|
|
|
|
LOG(character < 255 ? message " character:'%c'" : message " character:%d", character)
|
2014-10-22 12:54:46 -07:00
|
|
|
|
2015-12-28 10:40:53 -08:00
|
|
|
static const char empty_chunk[2] = { 0, 0 };
|
2014-09-13 00:15:24 -07:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
static void ts_lexer__get_chunk(Lexer *self) {
|
2015-10-14 21:52:13 -07:00
|
|
|
TSInput input = self->input;
|
2015-10-28 12:10:45 -07:00
|
|
|
if (!self->chunk ||
|
|
|
|
|
self->current_position.bytes != self->chunk_start + self->chunk_size)
|
2016-09-06 21:39:10 -07:00
|
|
|
input.seek(input.payload, self->current_position.chars,
|
2016-10-05 14:02:49 -07:00
|
|
|
self->current_position.bytes);
|
2014-10-17 16:20:01 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->chunk_start = self->current_position.bytes;
|
2016-09-06 21:39:10 -07:00
|
|
|
self->chunk = input.read(input.payload, &self->chunk_size);
|
2015-10-14 21:52:13 -07:00
|
|
|
if (!self->chunk_size)
|
|
|
|
|
self->chunk = empty_chunk;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
static void ts_lexer__get_lookahead(Lexer *self) {
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t position_in_chunk = self->current_position.bytes - self->chunk_start;
|
2015-12-28 10:40:53 -08:00
|
|
|
const uint8_t *chunk = (const uint8_t *)self->chunk + position_in_chunk;
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t size = self->chunk_size - position_in_chunk + 1;
|
2015-12-28 10:40:53 -08:00
|
|
|
|
|
|
|
|
if (self->input.encoding == TSInputEncodingUTF8)
|
2016-10-05 14:02:49 -07:00
|
|
|
self->lookahead_size =
|
|
|
|
|
utf8proc_iterate(chunk, size, &self->data.lookahead);
|
2015-12-28 10:40:53 -08:00
|
|
|
else
|
2016-10-05 14:02:49 -07:00
|
|
|
self->lookahead_size = utf16_iterate(chunk, size, &self->data.lookahead);
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-05 16:36:34 -08:00
|
|
|
static void ts_lexer__advance(void *payload, bool skip) {
|
2016-10-05 14:02:49 -07:00
|
|
|
Lexer *self = (Lexer *)payload;
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->chunk == empty_chunk)
|
2016-05-20 20:26:03 -07:00
|
|
|
return;
|
2014-10-03 15:44:49 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->lookahead_size) {
|
|
|
|
|
self->current_position.bytes += self->lookahead_size;
|
2015-12-04 20:20:29 -08:00
|
|
|
self->current_position.chars++;
|
2016-10-05 14:02:49 -07:00
|
|
|
if (self->data.lookahead == '\n') {
|
2016-09-09 21:11:02 -07:00
|
|
|
self->current_position.extent.row++;
|
|
|
|
|
self->current_position.extent.column = 0;
|
2015-11-30 12:56:10 -05:00
|
|
|
} else {
|
2016-09-09 21:11:02 -07:00
|
|
|
self->current_position.extent.column++;
|
2015-11-30 12:56:10 -05:00
|
|
|
}
|
2014-09-13 00:15:24 -07:00
|
|
|
}
|
2014-08-31 16:24:27 -07:00
|
|
|
|
2016-09-03 23:40:57 -07:00
|
|
|
if (skip) {
|
2016-12-09 13:33:37 -08:00
|
|
|
LOG_CHARACTER("skip", self->data.lookahead);
|
2016-09-03 23:40:57 -07:00
|
|
|
self->token_start_position = self->current_position;
|
|
|
|
|
} else {
|
2016-12-09 13:33:37 -08:00
|
|
|
LOG_CHARACTER("consume", self->data.lookahead);
|
2016-05-20 20:26:03 -07:00
|
|
|
}
|
2016-05-19 16:25:44 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->current_position.bytes >= self->chunk_start + self->chunk_size)
|
|
|
|
|
ts_lexer__get_chunk(self);
|
2014-08-31 16:24:27 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_lexer__get_lookahead(self);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-07-30 23:40:02 -07:00
|
|
|
|
2014-08-31 16:24:27 -07:00
|
|
|
/*
|
2016-05-20 20:26:03 -07:00
|
|
|
* 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.
|
2014-08-31 16:24:27 -07:00
|
|
|
*/
|
2014-10-17 16:20:01 -07:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
void ts_lexer_init(Lexer *self) {
|
|
|
|
|
*self = (Lexer){
|
2016-11-19 20:45:32 -08:00
|
|
|
.data = {
|
|
|
|
|
.advance = ts_lexer__advance,
|
|
|
|
|
.lookahead = 0,
|
|
|
|
|
.result_symbol = 0,
|
|
|
|
|
},
|
2015-09-05 22:29:17 -07:00
|
|
|
.chunk = NULL,
|
|
|
|
|
.chunk_start = 0,
|
2016-11-19 20:45:32 -08:00
|
|
|
.logger = {
|
|
|
|
|
.payload = NULL,
|
|
|
|
|
.log = NULL
|
|
|
|
|
},
|
2016-12-21 13:59:56 -08:00
|
|
|
.needs_to_restore_external_scanner = false,
|
|
|
|
|
.last_external_token_end_byte = 0,
|
2015-09-05 22:29:17 -07:00
|
|
|
};
|
2016-11-09 20:59:05 -08:00
|
|
|
ts_lexer_reset(self, length_zero());
|
2014-09-11 13:12:06 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline void ts_lexer__reset(Lexer *self, Length position) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->token_start_position = position;
|
|
|
|
|
self->current_position = position;
|
2015-11-18 16:34:50 -08:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
if (self->chunk && (position.bytes < self->chunk_start ||
|
|
|
|
|
position.bytes >= self->chunk_start + self->chunk_size)) {
|
2016-09-06 10:23:07 -07:00
|
|
|
self->chunk = 0;
|
|
|
|
|
self->chunk_start = 0;
|
|
|
|
|
self->chunk_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->lookahead_size = 0;
|
2016-10-05 14:02:49 -07:00
|
|
|
self->data.lookahead = 0;
|
2014-07-30 23:40:02 -07:00
|
|
|
}
|
2015-12-03 10:00:39 -08:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
void ts_lexer_set_input(Lexer *self, TSInput input) {
|
2015-12-03 10:00:39 -08:00
|
|
|
self->input = input;
|
2016-11-09 20:59:05 -08:00
|
|
|
ts_lexer__reset(self, length_zero());
|
2016-12-21 13:59:56 -08:00
|
|
|
self->needs_to_restore_external_scanner = false;
|
|
|
|
|
self->last_external_token_end_byte = 0;
|
2015-12-03 10:00:39 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
void ts_lexer_reset(Lexer *self, Length position) {
|
2016-12-21 13:59:56 -08:00
|
|
|
if (position.bytes > self->current_position.bytes) {
|
|
|
|
|
self->needs_to_restore_external_scanner = true;
|
|
|
|
|
self->last_external_token_end_byte = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
ts_lexer__reset(self, position);
|
2016-12-21 13:59:56 -08:00
|
|
|
} else if (position.bytes < self->current_position.bytes) {
|
|
|
|
|
if (position.bytes < self->last_external_token_end_byte) {
|
|
|
|
|
self->needs_to_restore_external_scanner = true;
|
|
|
|
|
self->last_external_token_end_byte = 0;
|
|
|
|
|
}
|
|
|
|
|
ts_lexer__reset(self, position);
|
|
|
|
|
}
|
2015-12-03 10:00:39 -08:00
|
|
|
}
|
2016-05-20 20:26:03 -07:00
|
|
|
|
2016-12-02 22:03:48 -08:00
|
|
|
void ts_lexer_start(Lexer *self) {
|
2016-05-20 20:26:03 -07:00
|
|
|
self->token_start_position = self->current_position;
|
2016-10-05 14:02:49 -07:00
|
|
|
self->data.result_symbol = 0;
|
2016-05-20 20:26:03 -07:00
|
|
|
|
|
|
|
|
if (!self->chunk)
|
|
|
|
|
ts_lexer__get_chunk(self);
|
|
|
|
|
if (!self->lookahead_size)
|
|
|
|
|
ts_lexer__get_lookahead(self);
|
|
|
|
|
}
|