Make separate header files for stack and lexer
This commit is contained in:
parent
e4be585c43
commit
ccc1b41f2a
4 changed files with 96 additions and 99 deletions
|
|
@ -8,6 +8,8 @@ extern "C" {
|
|||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include "tree_sitter/runtime.h"
|
||||
#include "tree_sitter/parser/stack.h"
|
||||
#include "tree_sitter/parser/lexer.h"
|
||||
|
||||
//#define TS_DEBUG_PARSE
|
||||
//#define TS_DEBUG_LEX
|
||||
|
|
@ -75,7 +77,7 @@ ts_parser constructor_name() { \
|
|||
ts_lex_states, \
|
||||
hidden_symbol_flags, \
|
||||
ubiquitous_symbol_flags \
|
||||
) \
|
||||
), \
|
||||
}; \
|
||||
}
|
||||
|
||||
|
|
@ -89,90 +91,6 @@ ts_parser constructor_name() { \
|
|||
{ .type = ts_parse_action_type_accept }
|
||||
|
||||
|
||||
/*
|
||||
* Stack
|
||||
*/
|
||||
typedef unsigned short ts_state_id;
|
||||
typedef struct {
|
||||
size_t size;
|
||||
struct {
|
||||
ts_tree *node;
|
||||
ts_state_id state;
|
||||
} *entries;
|
||||
} ts_stack;
|
||||
|
||||
ts_stack ts_stack_make();
|
||||
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, size_t immediate_child_count, const int *hidden_symbol_flags, const int *ubiquitous_symbol_flags);
|
||||
void ts_stack_shrink(ts_stack *stack, size_t new_size);
|
||||
void ts_stack_push(ts_stack *stack, ts_state_id state, ts_tree *node);
|
||||
ts_state_id ts_stack_top_state(const ts_stack *stack);
|
||||
ts_tree * ts_stack_top_node(const ts_stack *stack);
|
||||
size_t ts_stack_right_position(const ts_stack *stack);
|
||||
|
||||
|
||||
/*
|
||||
* Lexer
|
||||
*/
|
||||
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;
|
||||
} ts_lexer;
|
||||
|
||||
static ts_lexer ts_lexer_make() {
|
||||
ts_lexer result = {
|
||||
.chunk = NULL,
|
||||
.chunk_start = 0,
|
||||
.chunk_size = 0,
|
||||
.position_in_chunk = 0,
|
||||
.token_start_position = 0,
|
||||
.token_end_position = 0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t ts_lexer_position(const ts_lexer *lexer) {
|
||||
return lexer->chunk_start + lexer->position_in_chunk;
|
||||
}
|
||||
|
||||
static char ts_lexer_lookahead_char(const ts_lexer *lexer) {
|
||||
return lexer->chunk[lexer->position_in_chunk];
|
||||
}
|
||||
|
||||
static void ts_lexer_advance(ts_lexer *lexer) {
|
||||
static const char empty_chunk[1] = "";
|
||||
if (lexer->position_in_chunk + 1 < lexer->chunk_size) {
|
||||
lexer->position_in_chunk++;
|
||||
} else {
|
||||
lexer->chunk_start += lexer->chunk_size;
|
||||
lexer->chunk = lexer->input.read_fn(lexer->input.data, &lexer->chunk_size);
|
||||
if (lexer->chunk_size == 0) {
|
||||
lexer->chunk = empty_chunk;
|
||||
lexer->chunk_size = 1;
|
||||
}
|
||||
lexer->position_in_chunk = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void ts_lexer_start_token(ts_lexer *lexer) {
|
||||
lexer->token_start_position = ts_lexer_position(lexer);
|
||||
}
|
||||
|
||||
static ts_tree * ts_lexer_build_node(ts_lexer *lexer, ts_symbol symbol) {
|
||||
size_t current_position = ts_lexer_position(lexer);
|
||||
size_t size = current_position - lexer->token_start_position;
|
||||
size_t offset = lexer->token_start_position - lexer->token_end_position;
|
||||
lexer->token_end_position = current_position;
|
||||
return ts_tree_make_leaf(symbol, size, offset);
|
||||
}
|
||||
|
||||
#define ts_lex_state_error 0
|
||||
|
||||
|
||||
/*
|
||||
* Parse Table components
|
||||
*/
|
||||
|
|
|
|||
65
include/tree_sitter/parser/lexer.h
Normal file
65
include/tree_sitter/parser/lexer.h
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#ifndef TREE_SITTER_PARSER_LEXER_H_
|
||||
#define TREE_SITTER_PARSER_LEXER_H_
|
||||
|
||||
#include "tree_sitter/runtime.h"
|
||||
|
||||
#define ts_lex_state_error 0
|
||||
|
||||
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;
|
||||
} ts_lexer;
|
||||
|
||||
static ts_lexer ts_lexer_make() {
|
||||
ts_lexer result = {
|
||||
.chunk = NULL,
|
||||
.chunk_start = 0,
|
||||
.chunk_size = 0,
|
||||
.position_in_chunk = 0,
|
||||
.token_start_position = 0,
|
||||
.token_end_position = 0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t ts_lexer_position(const ts_lexer *lexer) {
|
||||
return lexer->chunk_start + lexer->position_in_chunk;
|
||||
}
|
||||
|
||||
static char ts_lexer_lookahead_char(const ts_lexer *lexer) {
|
||||
return lexer->chunk[lexer->position_in_chunk];
|
||||
}
|
||||
|
||||
static void ts_lexer_advance(ts_lexer *lexer) {
|
||||
static const char empty_chunk[1] = "";
|
||||
if (lexer->position_in_chunk + 1 < lexer->chunk_size) {
|
||||
lexer->position_in_chunk++;
|
||||
} else {
|
||||
lexer->chunk_start += lexer->chunk_size;
|
||||
lexer->chunk = lexer->input.read_fn(lexer->input.data, &lexer->chunk_size);
|
||||
if (lexer->chunk_size == 0) {
|
||||
lexer->chunk = empty_chunk;
|
||||
lexer->chunk_size = 1;
|
||||
}
|
||||
lexer->position_in_chunk = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void ts_lexer_start_token(ts_lexer *lexer) {
|
||||
lexer->token_start_position = ts_lexer_position(lexer);
|
||||
}
|
||||
|
||||
static ts_tree * ts_lexer_build_node(ts_lexer *lexer, ts_symbol symbol) {
|
||||
size_t current_position = ts_lexer_position(lexer);
|
||||
size_t size = current_position - lexer->token_start_position;
|
||||
size_t offset = lexer->token_start_position - lexer->token_end_position;
|
||||
lexer->token_end_position = current_position;
|
||||
return ts_tree_make_leaf(symbol, size, offset);
|
||||
}
|
||||
|
||||
#endif
|
||||
23
include/tree_sitter/parser/stack.h
Normal file
23
include/tree_sitter/parser/stack.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef TREE_SITTER_PARSER_STACK_H_
|
||||
#define TREE_SITTER_PARSER_STACK_H_
|
||||
|
||||
#include "tree_sitter/runtime.h"
|
||||
|
||||
typedef unsigned short ts_state_id;
|
||||
typedef struct {
|
||||
size_t size;
|
||||
struct {
|
||||
ts_tree *node;
|
||||
ts_state_id state;
|
||||
} *entries;
|
||||
} ts_stack;
|
||||
|
||||
ts_stack ts_stack_make();
|
||||
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, size_t immediate_child_count, const int *hidden_symbol_flags, const int *ubiquitous_symbol_flags);
|
||||
void ts_stack_shrink(ts_stack *stack, size_t new_size);
|
||||
void ts_stack_push(ts_stack *stack, ts_state_id state, ts_tree *node);
|
||||
ts_state_id ts_stack_top_state(const ts_stack *stack);
|
||||
ts_tree * ts_stack_top_node(const ts_stack *stack);
|
||||
size_t ts_stack_right_position(const ts_stack *stack);
|
||||
|
||||
#endif
|
||||
|
|
@ -1,18 +1,9 @@
|
|||
#include "tree_sitter/runtime.h"
|
||||
#include "tree_sitter/parser/stack.h"
|
||||
#include <string.h>
|
||||
|
||||
typedef int state_id;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
struct {
|
||||
ts_tree *node;
|
||||
state_id state;
|
||||
} *entries;
|
||||
} ts_stack;
|
||||
|
||||
static int INITIAL_STACK_SIZE = 100;
|
||||
static int INITIAL_STATE = 0;
|
||||
static size_t INITIAL_STACK_SIZE = 100;
|
||||
static ts_state_id INITIAL_STATE = 0;
|
||||
|
||||
ts_stack ts_stack_make() {
|
||||
ts_stack result = {
|
||||
|
|
@ -22,7 +13,7 @@ ts_stack ts_stack_make() {
|
|||
return result;
|
||||
}
|
||||
|
||||
state_id ts_stack_top_state(const ts_stack *stack) {
|
||||
ts_state_id ts_stack_top_state(const ts_stack *stack) {
|
||||
if (stack->size == 0) return INITIAL_STATE;
|
||||
return stack->entries[stack->size - 1].state;
|
||||
}
|
||||
|
|
@ -32,7 +23,7 @@ ts_tree * ts_stack_top_node(const ts_stack *stack) {
|
|||
return stack->entries[stack->size - 1].node;
|
||||
}
|
||||
|
||||
void ts_stack_push(ts_stack *stack, state_id state, ts_tree *node) {
|
||||
void ts_stack_push(ts_stack *stack, ts_state_id state, ts_tree *node) {
|
||||
stack->entries[stack->size].state = state;
|
||||
stack->entries[stack->size].node = node;
|
||||
stack->size++;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue