forked from marcoroth/herb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathherb.c
More file actions
91 lines (64 loc) · 1.99 KB
/
herb.c
File metadata and controls
91 lines (64 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "include/herb.h"
#include "include/io.h"
#include "include/lexer.h"
#include "include/parser.h"
#include "include/token.h"
#include "include/util/hb_array.h"
#include "include/util/hb_buffer.h"
#include "include/version.h"
#include <prism.h>
#include <stdlib.h>
hb_array_T* herb_lex(const char* source) {
lexer_T lexer = { 0 };
lexer_init(&lexer, source);
token_T* token = NULL;
hb_array_T* tokens = hb_array_init(128);
while ((token = lexer_next_token(&lexer))->type != TOKEN_EOF) {
hb_array_append(tokens, token);
}
hb_array_append(tokens, token);
return tokens;
}
AST_DOCUMENT_NODE_T* herb_parse(const char* source, parser_options_T* options) {
if (!source) { source = ""; }
lexer_T lexer = { 0 };
lexer_init(&lexer, source);
parser_T parser = { 0 };
parser_options_T parser_options = HERB_DEFAULT_PARSER_OPTIONS;
if (options != NULL) { parser_options = *options; }
herb_parser_init(&parser, &lexer, parser_options);
AST_DOCUMENT_NODE_T* document = herb_parser_parse(&parser);
herb_parser_deinit(&parser);
return document;
}
hb_array_T* herb_lex_file(const char* path) {
char* source = herb_read_file(path);
hb_array_T* tokens = herb_lex(source);
free(source);
return tokens;
}
void herb_lex_to_buffer(const char* source, hb_buffer_T* output) {
hb_array_T* tokens = herb_lex(source);
for (size_t i = 0; i < hb_array_size(tokens); i++) {
token_T* token = hb_array_get(tokens, i);
hb_string_T type = token_to_string(token);
hb_buffer_append_string(output, type);
free(type.data);
hb_buffer_append(output, "\n");
}
herb_free_tokens(&tokens);
}
void herb_free_tokens(hb_array_T** tokens) {
if (!tokens || !*tokens) { return; }
for (size_t i = 0; i < hb_array_size(*tokens); i++) {
token_T* token = hb_array_get(*tokens, i);
if (token) { token_free(token); }
}
hb_array_free(tokens);
}
const char* herb_version(void) {
return HERB_VERSION;
}
const char* herb_prism_version(void) {
return PRISM_VERSION;
}