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-10 13:14:52 -07:00
|
|
|
#include "tree_sitter/parser.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"
|
2014-10-14 22:50:24 -07:00
|
|
|
#include "runtime/debugger.h"
|
2014-09-13 00:15:24 -07:00
|
|
|
#include "utf8proc.h"
|
2014-07-10 13:14:52 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
#define DEBUG(...) \
|
|
|
|
|
if (self->debugger.debug_fn) { \
|
|
|
|
|
snprintf(self->debug_buffer, TS_DEBUG_BUFFER_SIZE, __VA_ARGS__); \
|
|
|
|
|
self->debugger.debug_fn(self->debugger.payload, TSDebugTypeLex, \
|
|
|
|
|
self->debug_buffer); \
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
#define DEBUG_LOOKAHEAD() \
|
|
|
|
|
DEBUG((0 < self->lookahead && self->lookahead < 256) ? "lookahead char:'%c'" \
|
|
|
|
|
: "lookahead char:%d", \
|
|
|
|
|
self->lookahead);
|
2014-10-22 12:54:46 -07:00
|
|
|
|
2014-10-03 15:44:49 -07:00
|
|
|
static const char *empty_chunk = "";
|
2014-09-13 00:15:24 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_lexer__get_chunk(TSLexer *self) {
|
|
|
|
|
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)
|
2015-10-14 21:52:13 -07:00
|
|
|
input.seek_fn(input.payload, self->current_position);
|
2014-10-17 16:20:01 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->chunk_start = self->current_position.bytes;
|
|
|
|
|
self->chunk = input.read_fn(input.payload, &self->chunk_size);
|
|
|
|
|
if (!self->chunk_size)
|
|
|
|
|
self->chunk = empty_chunk;
|
2014-10-09 14:02:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_lexer__get_lookahead(TSLexer *self) {
|
|
|
|
|
size_t position_in_chunk = self->current_position.bytes - self->chunk_start;
|
|
|
|
|
self->lookahead_size = utf8proc_iterate(
|
|
|
|
|
(const uint8_t *)self->chunk + position_in_chunk,
|
|
|
|
|
self->chunk_size - position_in_chunk + 1, &self->lookahead);
|
2014-10-22 12:54:46 -07:00
|
|
|
DEBUG_LOOKAHEAD();
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_lexer__start(TSLexer *self, TSStateId lex_state) {
|
2015-10-26 12:47:54 -07:00
|
|
|
DEBUG("start_lex state:%d, pos:%lu", lex_state, self->current_position.chars);
|
2015-06-12 13:13:43 -07:00
|
|
|
DEBUG_LOOKAHEAD();
|
2015-09-13 19:47:45 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (!self->chunk)
|
|
|
|
|
ts_lexer__get_chunk(self);
|
|
|
|
|
if (!self->lookahead_size)
|
|
|
|
|
ts_lexer__get_lookahead(self);
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void ts_lexer__start_token(TSLexer *self) {
|
|
|
|
|
DEBUG("start_token chars:%lu", self->current_position.chars);
|
|
|
|
|
self->token_start_position = self->current_position;
|
2014-10-17 16:20:01 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
2014-10-17 16:20:01 -07:00
|
|
|
DEBUG("advance state:%d", state);
|
2014-09-13 00:15:24 -07:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self->chunk == empty_chunk)
|
2014-10-03 16:06:08 -07:00
|
|
|
return false;
|
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;
|
|
|
|
|
self->current_position.chars += 1;
|
2014-09-13 00:15:24 -07:00
|
|
|
}
|
2014-08-31 16:24:27 -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-10-03 16:06:08 -07:00
|
|
|
return true;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
|
2015-09-05 22:29:17 -07:00
|
|
|
TSNodeType node_type, const char *symbol_name) {
|
2014-10-03 15:44:21 -07:00
|
|
|
TSLength size =
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_length_sub(self->current_position, self->token_start_position);
|
2014-10-03 15:44:21 -07:00
|
|
|
TSLength padding =
|
2015-10-14 21:52:13 -07:00
|
|
|
ts_length_sub(self->token_start_position, self->token_end_position);
|
|
|
|
|
self->token_end_position = self->current_position;
|
2014-10-22 12:54:46 -07:00
|
|
|
|
|
|
|
|
if (symbol == ts_builtin_sym_error) {
|
|
|
|
|
DEBUG("error_char");
|
2015-10-14 21:52:13 -07:00
|
|
|
return ts_tree_make_error(size, padding, self->lookahead);
|
2014-10-22 12:54:46 -07:00
|
|
|
} else {
|
|
|
|
|
DEBUG("accept_token sym:%s", symbol_name);
|
2015-09-15 16:00:16 -07:00
|
|
|
return ts_tree_make_leaf(symbol, padding, size, node_type);
|
2014-10-22 12:54:46 -07:00
|
|
|
}
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-07-30 23:40:02 -07:00
|
|
|
|
2014-08-31 16:24:27 -07:00
|
|
|
/*
|
2014-10-17 16:20:01 -07:00
|
|
|
* The lexer's methods are stored as struct fields so that generated parsers
|
|
|
|
|
* can call them without needing to be linked against this library.
|
2014-08-31 16:24:27 -07:00
|
|
|
*/
|
2014-10-17 16:20:01 -07:00
|
|
|
|
2014-07-30 23:40:02 -07:00
|
|
|
TSLexer ts_lexer_make() {
|
2015-09-05 22:29:17 -07:00
|
|
|
TSLexer result = (TSLexer){
|
|
|
|
|
.start_fn = ts_lexer__start,
|
|
|
|
|
.start_token_fn = ts_lexer__start_token,
|
|
|
|
|
.advance_fn = ts_lexer__advance,
|
|
|
|
|
.accept_fn = ts_lexer__accept,
|
|
|
|
|
.chunk = NULL,
|
|
|
|
|
.chunk_start = 0,
|
|
|
|
|
.debugger = ts_debugger_null(),
|
|
|
|
|
};
|
2015-09-13 19:47:45 -07:00
|
|
|
ts_lexer_reset(&result, ts_length_zero());
|
2014-09-11 13:12:06 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_lexer_reset(TSLexer *self, TSLength position) {
|
2015-11-18 08:47:15 -08:00
|
|
|
if (ts_length_eq(position, self->current_position))
|
|
|
|
|
return;
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
self->token_start_position = position;
|
|
|
|
|
self->token_end_position = position;
|
|
|
|
|
self->current_position = position;
|
|
|
|
|
self->chunk = 0;
|
2015-10-22 11:42:38 -07:00
|
|
|
self->chunk_start = 0;
|
2015-10-14 21:52:13 -07:00
|
|
|
self->chunk_size = 0;
|
|
|
|
|
self->lookahead_size = 0;
|
|
|
|
|
self->lookahead = 0;
|
2014-07-30 23:40:02 -07:00
|
|
|
}
|