aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/token.h
blob: d3a906a149e0ee811ff211ba6b02da838e8c335f (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

/*
 * Declarations for the tokenizer in the bootstrap compiler.
 *
 * Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+
 */
#ifndef SLUL_TOKEN_H
#define SLUL_TOKEN_H

#include <stddef.h>

enum Token {
    T_EOL, /**< End of line */
    /* Expressions */
    T_UpperIdent,
    T_LowerIdent,
    T_Integer,
    T_String,
    T_SYM_Dot,
    T_SYM_LArrow,
    T_SYM_RArrow,
    T_SYM_SingleEqual,
    T_SYM_DoubleEqual,
    T_SYM_NotEqual,
    T_SYM_Less,
    T_SYM_Greater,
    T_SYM_LessEqual,
    T_SYM_GreaterEqual,
    T_SYM_Plus,
    T_SYM_Minus,
    T_SYM_Asterisk,
    T_SYM_Slash,
    T_SYM_LParen,
    T_SYM_RParen,
    T_SYM_LBracket,
    T_SYM_RBracket,
    /* Common stuff */
    T_KW_end,
    /* Toplevels */
    T_KW_class,
    T_KW_enum,
    T_KW_func,
    T_KW_record,
    T_KW_templates,
    T_KW_trait,
    /* Builtin types */
    T_KW_bool,
    T_KW_byte,
    T_KW_int,
    T_KW_long,
    /* Qualifiers */
    T_KW_var,
    T_KW_aliased,
    T_KW_volatile,
    T_KW_wrapping,
    T_KW_signed,
    T_KW_unsigned,
    /* Generics */
    T_KW_of,
    T_KW_to,
    T_KW_from,
    /* Function sections */
    T_KW_calledfrom,
    T_KW_code,
    T_KW_export,
    T_KW_io,
    T_KW_local,
    T_KW_modifies,
    T_KW_reads,
    T_KW_returns,
    T_KW_section,
    T_KW_sets,
    /* Statements */
    T_KW_assert,
    T_KW_break,
    T_KW_case,
    T_KW_continue,
    T_KW_default,
    T_KW_elif,
    T_KW_else,
    T_KW_for,
    T_KW_if,
    T_KW_in,
    T_KW_loopend,
    T_KW_loopempty,
    T_KW_return,
    T_KW_switch,
    T_KW_while,
    /* Operators */
    T_KW_and,
    T_KW_mod,
    T_KW_not,
    T_KW_or,
    /* Special values */
    T_KW_false,
    T_KW_true,
    T_KW_none
};

#define FIRST_QUALIFIER T_KW_var
#define LAST_QUALIFIER  T_KW_unsigned
#define NUM_QUALIFIERS  (LAST_QUALIFIER+1 - FIRST_QUALIFIER)
#define TOKEN_CASES_QUALIFIERS \
    case T_KW_var:          \
    case T_KW_aliased:      \
    case T_KW_volatile:     \
    case T_KW_wrapping:     \
    case T_KW_signed:       \
    case T_KW_unsigned:


#define TOKEN_CASES_VALUE_START \
    case T_LowerIdent:      \
    case T_Integer:         \
    case T_String:          \
    case T_SYM_LParen:      \
    case T_SYM_RParen:      \
    case T_SYM_LBracket:    \
    case T_KW_not:          \
    case T_KW_false:        \
    case T_KW_true:         \
    case T_KW_none:

/* Type qualifiers */
#define Q_VAR        0x01
#define Q_ALIASED    0x02
#define Q_VOLATILE   0x04
#define Q_SIGNED     0x08
#define Q_UNSIGNED   0x10
#define Q_WRAPPING   0x20


struct LexemeInfo {
    size_t len;

    const char *string;
    /* Negative numbers are created with the unary minus operator */
    uint64_t num;
};

void tokenizer_init(FILE *f);
bool tokenizer_next_line(void);
bool tokenizer_line_is_indented(void);
enum Token tokenize(struct LexemeInfo *li_out);
void unread_token(void);
void expect(struct LexemeInfo *li_out, enum Token expected,
            const char *errmsg);
void expect_next_line(void);

#endif