aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.h
blob: 0cbda7558f242b1038e15eff6b9f141dfadd9f8d (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef LEXER_H
#define LEXER_H

#include <stdio.h>
#include "list.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_QUASI)            \
    X(TOKEN_UNQUOTE)          \
    X(TOKEN_LAMBDA)           \
    X(TOKEN_DEFINE)           \
    X(TOKEN_QUOTE_FORM)       \
    X(TOKEN_IF)               \
    X(TOKEN_DEFMACRO)         \
    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;
};

struct toklist {
    struct token *tokens;
    size_t tokens_len;

    struct list_head list;
};

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;
};

#define lexer_get_line(lexer) ((lexer)->line)

lexer_t lexer_create(FILE *fp);
void lexer_destroy(lexer_t lexer);
int lexer_clear_line(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);
char *token_value_static_string(struct token *token);

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

struct toklist *toklist_alloc(struct token *tokens, size_t tokens_len);
void toklist_dealloc(struct toklist *toklist);

#ifdef DEBUG
void token_print(struct token *token);
void toklist_print(struct toklist *toklist);
#endif

#endif