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

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

#include <stddef.h>
#include "compiler.h"

enum Token {
    T_EOL, /**< End of line */
    /* Expressions */
    T_UpperIdent,
    T_LowerIdent,
    T_Integer,
    T_Version,
    T_String,
    T_SYM_Dot,
    T_SYM_ExclMark,
    T_SYM_SingleEqual,
    T_SYM_PlusEqual,
    T_SYM_MinusEqual,
    T_SYM_AsteriskEqual,
    T_SYM_SlashEqual,
    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_entry,
    T_KW_enum,
    T_KW_func,
    T_KW_constructor,
    T_KW_giveme,
    T_KW_ignore,
    T_KW_record,
    T_KW_templates,
    T_KW_trait,
    T_KW_usetype,
    T_KW_versions,
    /* Builtin types */
    T_KW_bool,
    T_KW_byte,
    T_KW_int,
    T_KW_long,
    /* Qualifiers */
    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.
       These can additionally contain T_KW_return */
    T_KW_calledfrom,
    T_KW_code,
    T_KW_export,
    T_KW_io,
    T_KW_local,
    T_KW_modifies,
    T_KW_reads,
    T_KW_section,
    T_KW_sets,
    T_KW_since,
    /* `usetype` lines */
    T_KW_apihash,
    /* 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,
    T_KW_this
};

#define FIRST_QUALIFIER T_KW_aliased
#define LAST_QUALIFIER  T_KW_unsigned
#define NUM_QUALIFIERS  (LAST_QUALIFIER+1 - FIRST_QUALIFIER)
/* Please keep in sync with Q_* in compiler.h */
#define TOKEN_CASES_QUALIFIERS \
    case T_KW_aliased:      \
    case T_KW_volatile:     \
    case T_KW_wrapping:     \
    case T_KW_signed:       \
    case T_KW_unsigned:


struct LexemeInfo {
    size_t len;

    const char *string;
    SlulInt num;
};

extern bool tokenize_numbers_as_versions;

void tokenizer_init(FILE *f);
bool tokenizer_next_line(void);
bool tokenizer_line_is_indented(void);
enum Token tokenize(struct LexemeInfo *li_out);
enum Token lookahead_token(void);
/** Unreads a token (but never unreads the line itself) */
void unread_token(void);
/** Unreads a full line */
void unread_line(void);
void expect(struct LexemeInfo *li_out, enum Token expected,
            const char *errmsg);
void expect_next_line(void);
bool expect_next_line_or_eof(void);

enum IdentKind classify_ident(const struct LexemeInfo *li);

NORETURN void error_token(const char *s, const struct LexemeInfo *li);

#endif