aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/token.h
blob: bbd80665d795583a268381000e1b7ac32f6daba9 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef DUCC_TOKEN_H
#define DUCC_TOKEN_H

#include "io.h"
#include "json.h"

typedef enum {
    TokenKind_eof,

    // Only preprocessing phase.
    TokenKind_hash,
    TokenKind_hashhash,
    TokenKind_whitespace,
    TokenKind_removed,
    TokenKind_newline,
    TokenKind_other,
    TokenKind_character_constant,
    TokenKind_header_name,
    TokenKind_pp_directive_define,
    TokenKind_pp_directive_elif,
    TokenKind_pp_directive_elifdef,
    TokenKind_pp_directive_elifndef,
    TokenKind_pp_directive_else,
    TokenKind_pp_directive_embed,
    TokenKind_pp_directive_endif,
    TokenKind_pp_directive_error,
    TokenKind_pp_directive_if,
    TokenKind_pp_directive_ifdef,
    TokenKind_pp_directive_ifndef,
    TokenKind_pp_directive_include,
    TokenKind_pp_directive_line,
    TokenKind_pp_directive_non_directive,
    TokenKind_pp_directive_nop,
    TokenKind_pp_directive_pragma,
    TokenKind_pp_directive_undef,
    TokenKind_pp_directive_warning,
    TokenKind_pp_operator_defined,
    TokenKind_pp_operator___has_c_attribute,
    TokenKind_pp_operator___has_embed,
    TokenKind_pp_operator___has_include,

    // C23: 6.4.1
    TokenKind_keyword_alignas,
    TokenKind_keyword_alignof,
    TokenKind_keyword_auto,
    TokenKind_keyword_bool,
    TokenKind_keyword_break,
    TokenKind_keyword_case,
    TokenKind_keyword_char,
    TokenKind_keyword_const,
    TokenKind_keyword_constexpr,
    TokenKind_keyword_continue,
    TokenKind_keyword_default,
    TokenKind_keyword_do,
    TokenKind_keyword_double,
    TokenKind_keyword_else,
    TokenKind_keyword_enum,
    TokenKind_keyword_extern,
    TokenKind_keyword_false,
    TokenKind_keyword_float,
    TokenKind_keyword_for,
    TokenKind_keyword_goto,
    TokenKind_keyword_if,
    TokenKind_keyword_inline,
    TokenKind_keyword_int,
    TokenKind_keyword_long,
    TokenKind_keyword_nullptr,
    TokenKind_keyword_register,
    TokenKind_keyword_restrict,
    TokenKind_keyword_return,
    TokenKind_keyword_short,
    TokenKind_keyword_signed,
    TokenKind_keyword_sizeof,
    TokenKind_keyword_static,
    TokenKind_keyword_static_assert,
    TokenKind_keyword_struct,
    TokenKind_keyword_switch,
    TokenKind_keyword_thread_local,
    TokenKind_keyword_true,
    TokenKind_keyword_typedef,
    TokenKind_keyword_typeof,
    TokenKind_keyword_typeof_unqual,
    TokenKind_keyword_union,
    TokenKind_keyword_unsigned,
    TokenKind_keyword_void,
    TokenKind_keyword_volatile,
    TokenKind_keyword_while,
    TokenKind_keyword__Atomic,
    TokenKind_keyword__BitInt,
    TokenKind_keyword__Complex,
    TokenKind_keyword__Decimal128,
    TokenKind_keyword__Decimal32,
    TokenKind_keyword__Decimal64,
    TokenKind_keyword__Generic,
    TokenKind_keyword__Imaginary,
    TokenKind_keyword__Noreturn,

    TokenKind_and,
    TokenKind_andand,
    TokenKind_arrow,
    TokenKind_assign,
    TokenKind_assign_add,
    TokenKind_assign_and,
    TokenKind_assign_div,
    TokenKind_assign_lshift,
    TokenKind_assign_mod,
    TokenKind_assign_mul,
    TokenKind_assign_or,
    TokenKind_assign_rshift,
    TokenKind_assign_sub,
    TokenKind_assign_xor,
    TokenKind_brace_l,
    TokenKind_brace_r,
    TokenKind_bracket_l,
    TokenKind_bracket_r,
    TokenKind_colon,
    TokenKind_comma,
    TokenKind_dot,
    TokenKind_ellipsis,
    TokenKind_eq,
    TokenKind_ge,
    TokenKind_gt,
    TokenKind_ident,
    TokenKind_le,
    TokenKind_literal_int,
    TokenKind_literal_str,
    TokenKind_lshift,
    TokenKind_lt,
    TokenKind_minus,
    TokenKind_minusminus,
    TokenKind_ne,
    TokenKind_not,
    TokenKind_or,
    TokenKind_oror,
    TokenKind_paren_l,
    TokenKind_paren_r,
    TokenKind_percent,
    TokenKind_plus,
    TokenKind_plusplus,
    TokenKind_question,
    TokenKind_rshift,
    TokenKind_semicolon,
    TokenKind_slash,
    TokenKind_star,
    TokenKind_tilde,
    TokenKind_xor,
} TokenKind;

const char* token_kind_stringify(TokenKind k);
bool is_pp_directive(TokenKind k);

// TokenValue is externally tagged by Token's kind.
typedef union {
    const char* string;
    int integer;
} TokenValue;

typedef struct {
    TokenKind kind;
    TokenValue value;
    SourceLocation loc;
} Token;

const char* token_stringify(Token* tok);
void token_build_json(JsonBuilder* builder, Token* tok);

typedef struct {
    size_t len;
    size_t capacity;
    Token* data;
} TokenArray;

void tokens_init(TokenArray* tokens, size_t capacity);
void tokens_reserve(TokenArray* tokens, size_t size);
Token* tokens_push_new(TokenArray* tokens);
Token* tokens_pop(TokenArray* tokens);
void tokens_build_json(JsonBuilder* builder, TokenArray* tokens);

#endif