blob: e928430dd2064ff89dffc43dbd1a1ec366331ca1 (
plain)
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
|
#ifndef LEXER_H
#define LEXER_H
#include "value.h"
#ifndef LEXER_IDEN_CAP
#define LEXER_IDEN_CAP 512
#endif
#ifndef LEXER_TOK_CAP
#define LEXER_TOK_CAP 8192
#endif
typedef struct lexer *lexer_t;
typedef struct toklist toklist_t;
struct token {
enum token_enum {
TOKEN_PARENTHS_OPEN, TOKEN_PARENTHS_CLOSE,
TOKEN_SPECIAL_QUOTE, TOKEN_VALUE,
TOKEN_TOKENS // number of token types
} type;
value_t value;
};
struct toklist {
struct token tokens[LEXER_TOK_CAP];
size_t ntokens;
};
struct lexer {
char iden[LEXER_IDEN_CAP]; // identifier
size_t iden_sz;
bool inside_string;
int scope_depth;
};
// returns a lexer on success and NULL on fail
lexer_t lexer_create();
// deallocate a lexer
void lexer_destroy(lexer_t lexer);
// reset to its default state without destroying it
void lexer_reset(lexer_t lexer);
void toklist_reset(toklist_t *toklist);
// self explanatory
void toklist_print(toklist_t *toklist);
// turn the given non-null-terminated string str of lenght len
// into into tokens
// returns 0 on success
int lexer_tokenize(lexer_t lexer, toklist_t *toklist, char *str, size_t len);
// checks whether the lexer has finished (temp buffers like iden are empty)
// returns 1 on finished, 0 on not finished
bool lexer_has_finished(lexer_t lexer);
#endif
|