-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
404 lines (353 loc) · 16.5 KB
/
parser.cpp
File metadata and controls
404 lines (353 loc) · 16.5 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include "module.hpp"
#include "diagnostics.hpp"
#include "parser.hpp"
#include "interface.hpp"
#include "file_manager.hpp"
#include <variant>
using namespace std::literals;
diagnose::source_location get_location(doir::module& mod, const peg::SemanticValues &vs) {
diagnose::source_location out;
auto sv = vs.sv();
out.file = vs.path;
out.start_byte = sv.data() - mod.source.data();
out.end_byte = out.start_byte + sv.size() - 1;
return out;
}
struct call_info {
bool flatten = false, Inline = false, tail = false;
doir::interned_string function;
doir::lookup::function_inputs inputs;
};
using assignment_value_t = std::variant<long double, doir::interned_string, call_info, ecrs::entity_t, std::shared_ptr<struct function_type_t>>;
struct function_type_t {
struct parameter {
doir::interned_string name;
doir::lookup::lookup type;
std::optional<assignment_value_t> value = {};
};
std::vector<parameter> inputs;
std::optional<doir::lookup::lookup> return_type = {};
ecrs::entity_t push_function_type(doir::block_builder& builder, doir::interned_string ident = "_"){
std::vector<doir::lookup::lookup> inputs; inputs.reserve(this->inputs.size());
for(auto& i: this->inputs)
inputs.push_back(i.type);
std::vector<doir::interned_string> names; names.reserve(this->inputs.size());
for(auto& i: this->inputs)
names.push_back(i.name);
return builder.push_function_type(ident, inputs, return_type, names);
}
doir::function_builder push_function(doir::block_builder& builder, doir::interned_string name) {
auto out = builder.push_function(name, push_function_type(builder));
for(size_t i = 0; i < inputs.size(); ++i)
if(inputs[i].value && std::holds_alternative<long double>(*inputs[i].value)) {
if(inputs[i].type.resolved())
out.push_number_parameter(i, inputs[i].name, inputs[i].type.entity(), std::get<long double>(*inputs[i].value));
else out.push_number_parameter(i, inputs[i].name, inputs[i].type.name(), std::get<long double>(*inputs[i].value));
} else if(inputs[i].value && std::holds_alternative<doir::interned_string>(*inputs[i].value)) {
if(inputs[i].type.resolved())
out.push_string_parameter(i, inputs[i].name, inputs[i].type.entity(), std::get<doir::interned_string>(*inputs[i].value));
else out.push_string_parameter(i, inputs[i].name, inputs[i].type.name(), std::get<doir::interned_string>(*inputs[i].value));
} else {
if(inputs[i].type.resolved())
out.push_valueless_parameter(i, inputs[i].name, inputs[i].type.entity());
else out.push_valueless_parameter(i, inputs[i].name, inputs[i].type.name());
}
return out;
}
ecrs::entity_t push_valueless_function(doir::block_builder& builder, doir::interned_string name) {
return builder.push_valueless_function(name, push_function_type(builder));
}
};
peg::parser doir::initialize_parser(std::vector<doir::block_builder>& blocks, bool guarantee_source_location /*= false */) {
auto grammar =
#include "grammar.peg"
;
peg::parser parser(grammar);
parser.set_logger([&](size_t line, size_t col, std::string_view msg) {
auto& mod = *blocks.back().mod;
auto working_file = mod.working_file.value();
// auto working_file = "../grammar.peg"; mod.source = doir::file_manager::singleton().get_file_string(working_file);
diagnostics().register_source(working_file, mod.source);
auto message_segments = diagnose::split(msg, ',');
diagnose::diagnostic diagnostic;
diagnostic.kind = diagnose::diagnostic::error;
diagnostic.message = std::string(message_segments[1].substr(1));
diagnostic.location = {.file = working_file, .start = {line, col}, .end = {line, col + 1}};
if(message_segments.size() == 3) {
diagnose::diagnostic::annotation annotation;
annotation.message = "expecting"s + diagnose::ansi::magenta + std::string(message_segments[2].substr(10));
annotation.position = diagnostic.location.start;
diagnostic.annotations.push_back(annotation);
}
diagnostics().push(diagnostic);
});
// auto ok = parser.load_grammar(grammar);
// assert(ok);
auto& mod = *blocks.back().mod;
auto compiler_interned = mod.interner.intern("compiler");
auto type_interned = mod.interner.intern("type");
auto namespace_interned = mod.interner.intern("namespace");
auto alias_interned = mod.interner.intern("alias");
parser["assignment"] = [&, guarantee_source_location, compiler_interned, type_interned, namespace_interned, alias_interned]
(const peg::SemanticValues &vs)
{
if(vs.choice() == 0) { // Changing languages is not supported in the prototype!
auto& diag = push_diagnostic(diagnostic_type::LanguageChangeNotSupported, get_location(mod, vs), mod.source, *mod.working_file);
return ecrs::invalid_entity;
}
bool Export = false;
if(vs.tokens.size())
if(vs.token(0) == "export")
Export = true;
auto ident = std::any_cast<interned_string>(vs[0 + Export]);
auto type = vs[3 + Export];
std::optional<diagnose::source_location::detailed> location = {};
std::optional<assignment_value_t> value = {};
auto dbg = vs.size() - Export;
switch(vs.size() - Export) {
break; case 7: // valueless
{ /* do nothing */}
break; case 9: // sourceinfo
location = std::any_cast<diagnose::source_location::detailed>(vs[5 + Export]);
break; case 12: // value and sourceinfo
location = std::any_cast<diagnose::source_location::detailed>(vs[8 + Export]);
case 10: // value
if(vs[7 + Export].type() == typeid(interned_string))
value = call_info{.function = alias_interned, .inputs = {{std::any_cast<interned_string>(vs[7 + Export])}}};
else if(vs[7 + Export].type() == typeid(function_type_t))
value = std::make_unique<function_type_t>(std::any_cast<function_type_t>(vs[7 + Export])); // Function type
else value = std::any_cast<assignment_value_t>(vs[7 + Export]);
}
auto invalid_function_type_error = []() {
throw std::runtime_error("TODO: Cannot store this value in a function register");
};
auto& mod = *blocks.back().mod;
ecrs::entity_t e;
if(!value) {
if(type.type() == typeid(doir::lookup::type_of))
e = blocks.back().push_valueless(ident, std::any_cast<doir::lookup::type_of>(type).name());
else {
auto ft = std::any_cast<function_type_t>(type);
e = ft.push_valueless_function(blocks.back(), ident);
}
} else if(std::holds_alternative<long double>(*value)) {
if(type.type() == typeid(doir::lookup::type_of))
e = blocks.back().push_number(ident, std::any_cast<doir::lookup::type_of>(type).name(), std::get<long double>(*value));
else invalid_function_type_error();
} else if(std::holds_alternative<interned_string>(*value)) {
if(type.type() == typeid(doir::lookup::type_of)) {
auto name = std::any_cast<doir::lookup::type_of>(type).name();
if(name == alias_interned)
e = blocks.back().push_alias(ident, std::get<interned_string>(*value));
else e = blocks.back().push_string(ident, name, std::get<interned_string>(*value));
} else invalid_function_type_error();
} else if(std::holds_alternative<call_info>(*value)) {
auto call = std::get<call_info>(*value);
if(type.type() == typeid(doir::lookup::type_of)) {
auto name = std::any_cast<doir::lookup::type_of>(type).name();
if(name == alias_interned && call.function == alias_interned)
e = blocks.back().push_alias(ident, call.inputs[0].name());
else if(call.function == alias_interned)
throw std::runtime_error("TODO: Cannot directly copy registers, instead call copy or move");
else e = blocks.back().push_call(ident, name, call.function, call.inputs);
} else e = blocks.back().push_call(ident, std::any_cast<function_type_t>(type).push_function_type(blocks.back()), call.function, call.inputs);
if(call.Inline || call.flatten || call.tail) {
auto f = (doir::flags::Inline * call.Inline) | (doir::flags::Flatten * call.flatten) | (doir::flags::Tail * call.tail);
blocks.back().mod->add_component<doir::flags>(e).as_underlying() = f;
}
} else if(std::holds_alternative<ecrs::entity_t>(*value)) {
block_builder builder;
if(type.type() == typeid(doir::lookup::type_of)) {
auto type_name = std::any_cast<doir::lookup::type_of>(type).name();
if(type_name == type_interned) {
builder = blocks.back().push_type(ident);
} else if(type_name == namespace_interned) {
builder = blocks.back().push_namespace(ident);
if(ident == compiler_interned) {
mod.get_component<doir::name>(builder.block) = {mod.interner.intern("compiler_ignored")};
auto& diag = push_diagnostic(diagnostic_type::CompilerNamespaceReserved, get_location(mod, vs), mod.source, *mod.working_file);
diag.push_annotation_at_start({
.message = "Use of reserved "s + doir::ansi::type + "compiler" + diagnose::ansi::reset + " namespace has been ignored",
.color = doir::ansi::type
});
return builder.end();
}
} else if(type_name == alias_interned) {
auto& diag = push_diagnostic(diagnostic_type::AliasNotAllowed, get_location(mod, vs), mod.source, *mod.working_file);
diag.additional_note = "Aliases aren't allowed to reference blocks";
} else
builder = blocks.back().push_subblock(ident, interned_string(type_name));
} else {
auto ft = std::any_cast<function_type_t>(type);
builder = ft.push_function(blocks.back(), ident);
}
e = builder.block;
block_builder source = {std::get<ecrs::entity_t>(*value), builder.mod};
builder.move_exisiting(source);
} else if(std::holds_alternative<std::shared_ptr<struct function_type_t>>(*value)) {
if(type.type() != typeid(doir::lookup::type_of)) {
throw std::runtime_error("TODO: Function types can only be assigned to registers of type `type`");
}
auto type_name = std::any_cast<doir::lookup::type_of>(type).name();
if(type_name != type_interned) {
throw std::runtime_error("TODO: Function types can only be assigned to registers of type `type`");
}
auto& function = std::get<std::shared_ptr<struct function_type_t>>(*value);
function->push_function_type(blocks.back(), ident);
}
if(location) mod.add_component<diagnose::source_location::detailed>(e) = *location;
else if(guarantee_source_location) mod.add_component<diagnose::source_location>(e) = get_location(*blocks.back().mod, vs);
if(Export) blocks.back().mod->get_or_add_component<doir::flags>(e) = {doir::flags::Export};
return e;
};
parser["deducible_type"] = [](const peg::SemanticValues &vs) {
auto dbg = vs.size();
if(vs.tokens.size())
throw std::runtime_error("Deducible types not yet supported!");
return vs[0];
};
parser["parameter"] = [&](const peg::SemanticValues &vs) -> function_type_t::parameter {
function_type_t::parameter out;
out.name = std::any_cast<interned_string>(vs[0]);
auto dbg = vs.size();
if(vs[3].type() == typeid(doir::lookup::type_of))
out.type = std::any_cast<doir::lookup::type_of>(vs[3]);
else out.type = std::any_cast<function_type_t>(vs[3]).push_function_type(blocks.back());
if(vs.size() == 6)
out.value = std::any_cast<assignment_value_t>(vs[5]);
return out;
};
parser["FunctionType"] = [](const peg::SemanticValues &vs) -> function_type_t {
bool has_return_type = vs.tokens.size();
function_type_t out;
for(size_t i = 1, size = vs.size() - 3 * has_return_type; i < size; i += 2)
out.inputs.emplace_back(std::any_cast<function_type_t::parameter>(vs[i]));
if(has_return_type) {
size_t i = vs.size() - 1;
if(vs[i].type() == typeid(doir::lookup::type_of))
out.return_type = std::any_cast<doir::lookup::type_of>(vs[i]);
else out.return_type = std::any_cast<ecrs::entity_t>(vs[i]);
}
return out;
};
parser["Type"] = [](const peg::SemanticValues &vs) -> std::any {
switch(vs.choice()) {
break; case 0: // FunctionType
return vs[0];
break; case 1: { // Identifier
auto ident = std::any_cast<interned_string>(vs[0]);
return doir::lookup::type_of(ident);
}
}
throw std::runtime_error("Type Unreachable");
};
parser["Block"] = [&](const peg::SemanticValues &vs) -> assignment_value_t {
auto out = blocks.back().end();
blocks.pop_back();
return out;
};
parser["block_start"] = [&](const peg::SemanticValues &vs) {
auto mod = blocks.back().mod;
auto& builder = blocks.emplace_back(mod->add_entity(), mod);
mod->add_component<doir::block>(builder.block);
};
parser["function_call"] = [&](const peg::SemanticValues &vs) -> assignment_value_t {
bool has_modifier = vs.tokens.size();
call_info out;
if(has_modifier) {
if(vs.token(0) == "flatten")
out.flatten = true;
else if(vs.token(0) == "inline")
out.Inline = true;
else if(vs.token() == "tail")
out.tail = true;
}
out.function = std::any_cast<interned_string>(vs[0 + has_modifier]);
auto dbg = vs.size();
for(size_t i = 3 + has_modifier; i + 3 <= vs.size(); i += 3)
out.inputs.emplace_back(std::any_cast<interned_string>(vs[i]));
return out;
};
parser["SourceInfo"] = [&](const peg::SemanticValues &vs) {
diagnose::source_location::detailed out;
out.file = vs.token(0);
out.start.line = peg::token_to_number_<size_t>(vs.token(1));
switch (vs.tokens.size()) {
break; case 3: // no ends
out.end.line = out.start.line;
out.start.column = peg::token_to_number_<size_t>(vs.token(2));
out.end.column = out.start.column + 1;
break; case 4: // line end or column end
if(vs.token(2).starts_with("-")) { // end line
out.end.line = peg::token_to_number_<size_t>(vs.token(2).substr(1));
out.start.column = peg::token_to_number_<size_t>(vs.token(3));
out.end.column = out.start.column + 1;
} else { // end column
out.end.line = out.start.line;
out.start.column = peg::token_to_number_<size_t>(vs.token(2));
out.end.column = peg::token_to_number_<size_t>(vs.token(3).substr(1));
}
break; case 5: // line end and column end
out.end.line = peg::token_to_number_<size_t>(vs.token(2).substr(1));
out.start.column = peg::token_to_number_<size_t>(vs.token(3));
out.end.column = peg::token_to_number_<size_t>(vs.token(4).substr(1));
}
auto& mod = *blocks.back().mod;
if(out.end.line < out.start.line || out.end.column < out.start.column) {
auto& diag = push_diagnostic(diagnostic_type::NumberingOutOfOrder, get_location(mod, vs), mod.source, *mod.working_file);
auto offset = vs.sv().find(":");
if( !(out.end.line < out.start.line) ) offset = vs.sv().find(":", offset + 1);
diag.push_annotation({
.position = {diag.location.start.line, diag.location.start.column + offset + 1},
.message = "The "s + doir::ansi::info + "start" + diagnose::ansi::reset + " of a source location must come before its " + doir::ansi::info + "end" + diagnose::ansi::reset,
.color = doir::ansi::info
});
}
if(out.start.column == 0 && out.end.column == 1 && out.start.line != 0) try {
std::vector<std::string_view> lines = diagnose::split(doir::file_manager::singleton().get_file_string(out.file), '\n');
out.start.column = 1;
out.end.column = lines[out.start.line - 1].size() + 1;
} catch(std::system_error) {
auto& diag = push_diagnostic(diagnostic_type::FileDoesNotExist, get_location(mod, vs), mod.source, *mod.working_file);
diag.push_annotation_at_start({
.message = "Attempted to load file "s + doir::ansi::file + "`" + std::string(out.file) + "`" + diagnose::ansi::reset,
.color = doir::ansi::file,
});
diag.additional_note = "Calculating the end of line with column=0 requires loading a file and scanning its lines.";
} else if(out.start.column == 0 || out.start.line == 0) {
auto& diag = push_diagnostic(diagnostic_type::NumberingStartsAt1, get_location(mod, vs), mod.source, *mod.working_file);
bool line = out.start.line == 0;
auto offset = vs.sv().find(":");
if(!line) offset = vs.sv().find(":", offset + 1);
diag.push_annotation({
.position = {diag.location.start.line, diag.location.start.column + offset + 1},
.message = std::string("Source location ") + doir::ansi::info + (line ? "lines" : "columns") + diagnose::ansi::reset + " start at 0, not 1",
.color = doir::ansi::info,
});
}
return out;
};
parser["Identifier"] = [&](const peg::SemanticValues &vs) {
auto out = mod.interner.intern(vs.token(0));
verify::identifier_structure(diagnostics(), *blocks.back().mod, out);
return out;
};
parser["Constant"] = [&](const peg::SemanticValues &vs) -> assignment_value_t {
switch (vs.choice()) {
break; case 0: case 1: {
auto out = vs.token_to_number<long double>();
if(out == 0)
return (long double)std::stoi(vs.token_to_string(0), nullptr, 0);
else return out;
}
break; case 2: case 3: {
auto unescaped = unescape_python_string(vs.token(0));
return mod.interner.intern(unescaped);
}
}
throw std::runtime_error("Constant Unreachable");
};
return parser;
};