-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.c
54 lines (47 loc) · 1.13 KB
/
parse.c
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
#include "parse.h"
#include "std.h"
#include "token.h"
struct token* parse_token(struct parse_context* ctx)
{
if (ctx->token_index >= ctx->num_tokens)
return NULL;
ctx->current_token = &ctx->tokens[ctx->token_index];
return ctx->current_token;
}
struct token* parse_advance(struct parse_context* ctx)
{
struct token* t = parse_token(ctx);
++ctx->token_index;
return t;
}
void parse_initialize(struct parse_context* ctx)
{
ctx->current_token = NULL;
ctx->num_tokens = 0;
ctx->token_index = 0;
ctx->tokens = NULL;
}
int parse_string(struct parse_context* ctx, const char* str, int flags)
{
// TODO: handle errors
parse(str, &ctx->tokens, &ctx->num_tokens, flags);
return 0;
}
void parse_cleanup(struct parse_context* ctx)
{
free(ctx->tokens);
}
int parse_accept(struct parse_context* ctx, int type)
{
struct token* old_token = ctx->current_token;
struct token* tk = parse_token(ctx);
if (!tk || tk->type != type)
{
ctx->current_token = old_token;
// debug_printf("tk->type %s (%d) != type %s (%d)\n", token_type_to_string(tk->type), tk->type,
// token_type_to_string(type), type);
return 1;
}
++ctx->token_index;
return 0;
}