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
|
#ifndef LEX_H
#define LEX_H
#include "sv.h"
#include <stddef.h>
#define TOKEN_EOF (struct token){ \
.type = T_EOF, \
.me = (struct string_view){.buf = NULL, .len = 0} \
}
#define TOKEN_EQUAL_SIGN (struct token){ \
.type = T_EQUAL_SIGN, \
.me = (struct string_view){.buf = "=", .len = 1} \
}
#define TOKEN_SEMICOLON (struct token){ \
.type = T_SEMICOL, \
.me = (struct string_view){.buf = ";", .len = 1} \
}
#define TOKEN_SCOPE_START (struct token){ \
.type = T_SCOPESTART, \
.me = (struct string_view){.buf = "{", .len = 1} \
}
#define TOKEN_SCOPE_END (struct token){ \
.type = T_SCOPEEND, \
.me = (struct string_view){.buf = "}", .len = 1} \
}
#define T_TYPE (uint64_t)0xFF000000
#define T_ERR (uint32_t)0
#define T_ID (uint32_t)1
#define T_STRING (uint32_t)2
#define T_SEMICOL (uint32_t)3
#define T_NUMBER (uint32_t)4
#define T_EQUAL_SIGN (uint32_t)5
#define T_SCOPESTART (uint32_t)6
#define T_SCOPEEND (uint32_t)7
#define T_TYPE_G64 (uint64_t)T_TYPE | (uint32_t)8
#define T_TYPE_G32 (uint64_t)T_TYPE | (uint32_t)9
#define T_TYPE_G16 (uint64_t)T_TYPE | (uint32_t)10
#define T_TYPE_G8 (uint64_t)T_TYPE | (uint32_t)11
#define T_EOF (uint32_t)12
struct token {
uint64_t type;
struct string_view me;
};
struct lex_config {
char *src;
struct string_pool *sp;
size_t size;
size_t pos;
};
int lex_keyword_lookup(struct string_view sv, uint64_t *res);
int lex_is_char(char c);
int lex_is_digit(char c);
int lex_within_bounds(struct lex_config *lc);
int lex_within_obounds(struct lex_config *lc, size_t o);
char lex_peek(struct lex_config *lc, size_t o);
char lex_advance(struct lex_config *lc);
int lex_is_space(char c);
void lex_skip_whitespace(struct lex_config *lc);
struct token lex_word(struct lex_config *lc);
struct token lex_number(struct lex_config *lc);
struct token lex_opcode(struct lex_config *lc);
struct token lex_next(struct lex_config *lc);
#endif
|