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-07-10 13:14:52 -07:00
|
|
|
|
2014-07-30 23:40:02 -07:00
|
|
|
static int advance(TSLexer *lexer) {
|
2014-07-20 20:27:33 -07:00
|
|
|
if (lexer->position_in_chunk + 1 < lexer->chunk_size) {
|
|
|
|
|
lexer->position_in_chunk++;
|
2014-08-31 16:24:27 -07:00
|
|
|
return 1;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-08-31 16:24:27 -07:00
|
|
|
|
|
|
|
|
static const char *empty_chunk = "";
|
|
|
|
|
if (lexer->chunk == empty_chunk)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
lexer->chunk_start += lexer->chunk_size;
|
|
|
|
|
lexer->chunk = lexer->input.read_fn(lexer->input.data, &lexer->chunk_size);
|
|
|
|
|
lexer->position_in_chunk = 0;
|
|
|
|
|
if (lexer->chunk_size == 0)
|
|
|
|
|
lexer->chunk = empty_chunk;
|
|
|
|
|
|
2014-07-20 20:27:33 -07:00
|
|
|
return 1;
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-31 13:11:39 -07:00
|
|
|
static TSTree *accept(TSLexer *lexer, TSSymbol symbol, int is_hidden) {
|
2014-07-20 20:27:33 -07:00
|
|
|
size_t current_position = ts_lexer_position(lexer);
|
|
|
|
|
size_t size = current_position - lexer->token_start_position;
|
2014-08-28 13:22:06 -07:00
|
|
|
size_t padding = lexer->token_start_position - lexer->token_end_position;
|
2014-07-20 20:27:33 -07:00
|
|
|
lexer->token_end_position = current_position;
|
2014-08-28 13:22:06 -07:00
|
|
|
return ts_tree_make_leaf(symbol, size, padding, is_hidden);
|
2014-07-10 13:14:52 -07:00
|
|
|
}
|
2014-07-30 23:40:02 -07:00
|
|
|
|
2014-08-31 16:24:27 -07:00
|
|
|
/*
|
|
|
|
|
* The `advance` and `accept` methods are stored as fields on the Lexer so
|
|
|
|
|
* that generated parsers can call them without needing to be linked against
|
|
|
|
|
* this library.
|
|
|
|
|
*/
|
2014-07-30 23:40:02 -07:00
|
|
|
TSLexer ts_lexer_make() {
|
|
|
|
|
return (TSLexer) { .chunk = NULL,
|
|
|
|
|
.debug = 0,
|
|
|
|
|
.chunk_start = 0,
|
|
|
|
|
.chunk_size = 0,
|
|
|
|
|
.position_in_chunk = 0,
|
|
|
|
|
.token_start_position = 0,
|
|
|
|
|
.token_end_position = 0,
|
|
|
|
|
.advance_fn = advance,
|
2014-07-31 13:11:39 -07:00
|
|
|
.accept_fn = accept, };
|
2014-07-30 23:40:02 -07:00
|
|
|
}
|