-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlexer.h
236 lines (196 loc) · 4.94 KB
/
lexer.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "platform-utils.h"
#include "src-file.h"
#include <string>
namespace tolk {
enum TokenType {
tok_empty,
tok_fun,
tok_get,
tok_type,
tok_enum,
tok_struct,
tok_operator,
tok_infix,
tok_global,
tok_const,
tok_var,
tok_val,
tok_redef,
tok_mutate,
tok_self,
tok_annotation_at,
tok_colon,
tok_asm,
tok_builtin,
tok_int_const,
tok_string_const,
tok_true,
tok_false,
tok_null,
tok_identifier,
tok_dot,
tok_plus,
tok_set_plus,
tok_minus,
tok_set_minus,
tok_mul,
tok_set_mul,
tok_div,
tok_set_div,
tok_mod,
tok_set_mod,
tok_lshift,
tok_set_lshift,
tok_rshift,
tok_set_rshift,
tok_rshiftR,
tok_rshiftC,
tok_bitwise_and,
tok_set_bitwise_and,
tok_bitwise_or,
tok_set_bitwise_or,
tok_bitwise_xor,
tok_set_bitwise_xor,
tok_bitwise_not,
tok_question,
tok_comma,
tok_semicolon,
tok_oppar,
tok_clpar,
tok_opbracket,
tok_clbracket,
tok_opbrace,
tok_clbrace,
tok_assign,
tok_underscore,
tok_lt,
tok_gt,
tok_logical_not,
tok_logical_and,
tok_logical_or,
tok_eq,
tok_neq,
tok_leq,
tok_geq,
tok_spaceship,
tok_divR,
tok_divC,
tok_return,
tok_repeat,
tok_do,
tok_while,
tok_break,
tok_continue,
tok_try,
tok_catch,
tok_throw,
tok_assert,
tok_if,
tok_else,
tok_arrow,
tok_as,
tok_tolk,
tok_semver,
tok_import,
tok_export,
tok_eof
};
// All tolk language is parsed into tokens.
// Lexer::next() returns a Token.
struct Token {
TokenType type = tok_empty;
std::string_view str_val;
Token() = default;
Token(TokenType type, std::string_view str_val): type(type), str_val(str_val) {}
};
// Lexer::next() is a method to be used externally (while parsing tolk file to AST).
// It's streaming: `next()` parses a token on demand.
// For comments, see lexer.cpp, a comment above Lexer constructor.
class Lexer {
Token tokens_circularbuf[8]{};
int last_token_idx = -1;
int cur_token_idx = -1;
Token cur_token; // = tokens_circularbuf[cur_token_idx & 7]
const SrcFile* file;
const char *p_start, *p_end, *p_next;
SrcLocation location;
void update_location() {
location.char_offset = static_cast<int>(p_next - p_start);
}
public:
struct SavedPositionForLookahead {
const char* p_next = nullptr;
int cur_token_idx = 0;
Token cur_token;
};
explicit Lexer(const SrcFile* file);
explicit Lexer(std::string_view text);
Lexer(const Lexer&) = delete;
Lexer &operator=(const Lexer&) = delete;
void add_token(TokenType type, std::string_view str) {
tokens_circularbuf[++last_token_idx & 7] = Token(type, str);
}
void skip_spaces() {
while (std::isspace(*p_next)) {
++p_next;
}
}
void skip_line() {
while (p_next < p_end && *p_next != '\n' && *p_next != '\r') {
++p_next;
}
while (*p_next == '\n' || *p_next == '\r') {
++p_next;
}
}
void skip_chars(int n) {
p_next += n;
}
bool is_eof() const {
return p_next >= p_end;
}
char char_at() const { return *p_next; }
char char_at(int shift) const { return *(p_next + shift); }
const char* c_str() const { return p_next; }
TokenType tok() const { return cur_token.type; }
std::string_view cur_str() const { return cur_token.str_val; }
SrcLocation cur_location() const { return location; }
const SrcFile* cur_file() const { return file; }
void next();
void next_special(TokenType parse_next_as, const char* str_expected);
SavedPositionForLookahead save_parsing_position() const;
void restore_position(SavedPositionForLookahead saved);
void check(TokenType next_tok, const char* str_expected) const {
if (cur_token.type != next_tok) {
unexpected(str_expected); // unlikely path, not inlined
}
}
void expect(TokenType next_tok, const char* str_expected) {
if (cur_token.type != next_tok) {
unexpected(str_expected);
}
next();
}
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
void unexpected(const char* str_expected) const;
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
void error(const std::string& err_msg) const;
};
void lexer_init();
// todo #ifdef TOLK_PROFILING
void lexer_measure_performance(const AllRegisteredSrcFiles& files_to_just_parse);
} // namespace tolk