aboutsummaryrefslogtreecommitdiff
path: root/include/tokenizer.h
blob: e79f0703cf5bb4d86278792ac9817222f4371ada (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
#ifndef AMALGAM_TOKENIZER_H
#define AMALGAM_TOKENIZER_H

#include "std/buffer_view.h"
#include "std/misc.h"

#define TOKENIZER_OK 0
/* General error */
#define TOKENIZER_ERR -1
#define TOKENIZER_UNEXPECTED_TOKEN -2

typedef enum {
    TOK_NONE,
    TOK_END_OF_FILE,
    TOK_IDENTIFIER,
    TOK_CONST,
    TOK_VAR,
    TOK_STRING,
    TOK_EQUALS,
    TOK_OPEN_PAREN,
    TOK_CLOSING_PAREN,
    TOK_OPEN_BRACE,
    TOK_CLOSING_BRACE,
    TOK_IMPORT
} Token;

typedef struct {
    BufferView code;
    int index;
    int prev_index;
    int line;
    BufferView code_name;
    
    union {
        BufferView identifier;
        BufferView string;
    } value;
} Tokenizer;

CHECK_RESULT int tokenizer_init(Tokenizer *self, BufferView code, BufferView code_name);

CHECK_RESULT int tokenizer_next(Tokenizer *self, Token *token);
CHECK_RESULT int tokenizer_accept(Tokenizer *self, Token expected_token);
/* 
   @result is set to 0 if the next token is equal to @expected_token,
   otherwise @result is set to 1
*/
CHECK_RESULT int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result);
void tokenizer_print_error(Tokenizer *self, const char *fmt, ...);

#endif