aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.h
blob: c2e4637f3a24a735691a85158655ae094771a655 (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 <stdio.h>

#define TOKEN_TYPES(X)        \
    X(TOKEN_LP)               \
    X(TOKEN_RP)               \
    X(TOKEN_ID)               \
    X(TOKEN_STR)              \
    X(TOKEN_INT)              \
    X(TOKEN_DOT)              \
    X(TOKEN_QUOTE)            \
    X(TOKEN_UNQUOTE)          \
    X(TOKEN_LAMBDA)           \
    X(TOKEN_DEFINE)           \
    X(TOKEN_QUOTE_FORM)       \
    X(TOKEN_NONE)

#define TO_ENUM(type) type,
#define TO_STRING(type) #type,

extern const char * const token_type_string[];

struct token {
    enum token_type {
        TOKEN_TYPES(TO_ENUM)
    } type;
    
    union {
        char *id;
        char *str;
        int   num;
    } value;
};

typedef struct lexer * lexer_t;
#define LEXER_EMPTY NULL

struct lexer {
    FILE *fp;
    size_t line;
    
    char str[256];
    size_t str_idx;

    struct token token;
    char acc[256];
    size_t acc_idx;
};

lexer_t lexer_create(FILE *fp);
void lexer_destroy(lexer_t lexer);
int lexer_token_next(lexer_t lexer, struct token *token);

int token_value_string(struct token *token, size_t buf_sz, char *buf);

void token_clone(struct token *dest, struct token *src);
void token_dealloc(struct token *token);

#endif