Skip to content

Commit 3f39fa9

Browse files
committed
Add arena allocator to lexer
1 parent e4f4b71 commit 3f39fa9

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/herb.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "include/herb.h"
22
#include "include/io.h"
33
#include "include/lexer.h"
4+
#include "include/macros.h"
45
#include "include/parser.h"
56
#include "include/token.h"
7+
#include "include/util/hb_arena.h"
68
#include "include/util/hb_array.h"
79
#include "include/util/hb_buffer.h"
810
#include "include/version.h"
@@ -11,8 +13,11 @@
1113
#include <stdlib.h>
1214

1315
hb_array_T* herb_lex(const char* source) {
16+
hb_arena_T allocator;
17+
hb_arena_init(&allocator, KB(32));
18+
1419
lexer_T lexer = { 0 };
15-
lexer_init(&lexer, source);
20+
lexer_init(&lexer, &allocator, source);
1621

1722
token_T* token = NULL;
1823
hb_array_T* tokens = hb_array_init(128);
@@ -29,8 +34,11 @@ hb_array_T* herb_lex(const char* source) {
2934
AST_DOCUMENT_NODE_T* herb_parse(const char* source, parser_options_T* options) {
3035
if (!source) { source = ""; }
3136

37+
hb_arena_T allocator;
38+
hb_arena_init(&allocator, MB(512));
39+
3240
lexer_T lexer = { 0 };
33-
lexer_init(&lexer, source);
41+
lexer_init(&lexer, &allocator, source);
3442
parser_T parser = { 0 };
3543

3644
parser_options_T parser_options = HERB_DEFAULT_PARSER_OPTIONS;

src/include/lexer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
#include "lexer_struct.h"
55
#include "token_struct.h"
6+
#include "util/hb_arena.h"
67

7-
void lexer_init(lexer_T* lexer, const char* source);
8+
void lexer_init(lexer_T* lexer, hb_arena_T* allocator, const char* source);
89
token_T* lexer_next_token(lexer_T* lexer);
910
token_T* lexer_error(lexer_T* lexer, const char* message);
1011

src/include/lexer_struct.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef HERB_LEXER_STRUCT_H
22
#define HERB_LEXER_STRUCT_H
33

4+
#include "util/hb_arena.h"
45
#include "util/hb_string.h"
56

67
#include <stdbool.h>
@@ -14,6 +15,7 @@ typedef enum {
1415
} lexer_state_T;
1516

1617
typedef struct LEXER_STRUCT {
18+
hb_arena_T* allocator;
1719
hb_string_T source;
1820

1921
uint32_t current_line;

src/lexer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "include/token.h"
33
#include "include/utf8.h"
44
#include "include/util.h"
5+
#include "include/util/hb_arena.h"
56
#include "include/util/hb_buffer.h"
67
#include "include/util/hb_string.h"
78

@@ -31,7 +32,9 @@ static bool lexer_stalled(lexer_T* lexer) {
3132
return lexer->stalled;
3233
}
3334

34-
void lexer_init(lexer_T* lexer, const char* source) {
35+
void lexer_init(lexer_T* lexer, hb_arena_T* allocator, const char* source) {
36+
lexer->allocator = allocator;
37+
3538
if (source != NULL) {
3639
lexer->source = hb_string(source);
3740
} else {

0 commit comments

Comments
 (0)