-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbufparser.cpp
657 lines (546 loc) · 21 KB
/
bufparser.cpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#include <util/buffmt.hpp>
#include <util/dlg.hpp>
#include <util/profiling.hpp>
#include <util/util.hpp>
#include <threadContext.hpp>
#include <dlg/output.h>
#include <fstream>
#include <unordered_set>
#include <string>
#include <string_view>
#include <algorithm>
#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/trace.hpp>
#include <tao/pegtl/contrib/analyze.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include <tao/pegtl/contrib/parse_tree_to_dot.hpp>
namespace pegtl = tao::pegtl;
namespace vil {
namespace syn {
// https://stackoverflow.com/questions/53427551/pegtl-how-to-skip-spaces-for-the-entire-grammar
struct LineComment : pegtl::seq<
pegtl::sor<
pegtl::string<'/', '/'>,
pegtl::one<'#'>>,
pegtl::until<
pegtl::eolf,
pegtl::utf8::any>> {};
struct InlineComment : pegtl::seq<
pegtl::string<'/', '*'>,
pegtl::until<
pegtl::string<'*', '/'>,
pegtl::utf8::any>> {};
struct Comment : pegtl::disable<pegtl::sor<LineComment, InlineComment>> {};
struct Separator : pegtl::sor<tao::pegtl::ascii::space, Comment> {};
struct Seps : tao::pegtl::star<Separator> {}; // Any separators, whitespace or comments
template<typename S, typename... R> using Interleaved = pegtl::seq<S, pegtl::seq<R, S>...>;
template<typename S, typename... R> using IfMustSep =
pegtl::if_must<pegtl::pad<S, Separator>, pegtl::pad<R, Separator>...>;
struct Plus : pegtl::one<'+'> {};
struct Minus : pegtl::one<'-'> {};
struct Mult : pegtl::one<'*'> {};
struct Divide : pegtl::one<'/'> {};
struct Comma : pegtl::one<','> {};
struct Dot : pegtl::one<'.'> {};
struct Semicolon : pegtl::one<';'> {};
struct Colon : pegtl::one<':'> {};
struct Eof : pegtl::eof {};
struct Identifier : pegtl::seq<pegtl::alpha, pegtl::star<pegtl::alnum>> {};
struct Type : pegtl::seq<Identifier> {};
struct Number : pegtl::plus<pegtl::digit> {};
struct ArrayQualifierClose : pegtl::one<']'> {};
struct ArrayQualifierNumber : pegtl::seq<Number> {};
struct ArrayQualifier : IfMustSep<
pegtl::one<'['>,
ArrayQualifierNumber,
ArrayQualifierClose
> {};
struct ArrayRuntimeQualifier : Interleaved<Seps,
pegtl::one<'['>,
pegtl::one<']'>
> {};
struct ArrayQualifiers : pegtl::seq<
pegtl::pad_opt<ArrayRuntimeQualifier, Separator>, // must be first
pegtl::star<pegtl::pad<ArrayQualifier, Separator>>
> {};
struct ValueDecl : IfMustSep<
Type,
Identifier,
ArrayQualifiers,
Semicolon
> {};
struct StructMemberList : pegtl::plus<ValueDecl> {};
struct StructOpen : pegtl::one<'{'> {};
struct StructClose : pegtl::one<'}'> {};
struct StructSemicolon : Semicolon {};
struct StructName : pegtl::seq<Identifier> {};
struct StructDecl : IfMustSep<
pegtl::keyword<'s', 't', 'r', 'u', 'c', 't'>,
StructName,
StructOpen,
StructMemberList,
StructClose,
StructSemicolon
> {};
struct StructDecls : pegtl::star<pegtl::pad<StructDecl, Separator>> {};
struct ValueDecls : pegtl::star<ValueDecl> {};
struct Grammar : Interleaved<Seps, StructDecls, ValueDecls> {};
struct Whole : pegtl::must<Grammar, Eof> {};
// transform/select
// Just keeps the node as it was
struct Keep : pegtl::parse_tree::apply<Keep> {
template<typename Node, typename... States>
static void transform(std::unique_ptr<Node>&, States&&...) {
}
};
// Keeps the node but discards all children
struct DiscardChildren : pegtl::parse_tree::apply<DiscardChildren> {
template<typename Node, typename... States>
static void transform(std::unique_ptr<Node>& n, States&&...) {
n->children.clear();
}
};
// Completely discards the node and all its children
struct Discard : pegtl::parse_tree::apply<Discard> {
template<typename Node, typename... States>
static void transform(std::unique_ptr< Node >& n, States&&...) {
n.reset();
}
};
// Discards the node if it does not have any children.
// Folds the node if it only has one child (i.e. replaces by child).
struct FoldDiscard : pegtl::parse_tree::apply<FoldDiscard> {
template<typename Node, typename... States>
static void transform(std::unique_ptr< Node >& n, States&&...) {
if(n->children.size() == 1) {
n = std::move(n->children.front());
} else if(n->children.empty()) {
n.reset();
}
}
};
// Discards node only if empty
struct DiscardIfEmpty : pegtl::parse_tree::apply<DiscardIfEmpty> {
template<typename Node, typename... States>
static void transform(std::unique_ptr< Node >& n, States&&...) {
if(n->children.empty()) {
n.reset();
}
}
};
template<typename Rule> struct selector : FoldDiscard {};
template<> struct selector<Identifier> : DiscardChildren {};
template<> struct selector<ValueDecls> : Keep {};
template<> struct selector<StructDecls> : Keep {};
template<> struct selector<StructDecl> : Keep {};
template<> struct selector<StructMemberList> : Keep {};
template<> struct selector<ValueDecl> : Keep {};
template<> struct selector<Type> : DiscardChildren {};
template<> struct selector<Seps> : Discard {};
template<> struct selector<ArrayQualifier> : FoldDiscard {};
template<> struct selector<ArrayRuntimeQualifier> : Keep {};
template<> struct selector<Number> : Keep {};
template<> struct selector<ArrayQualifiers> : pegtl::parse_tree::apply<selector<ArrayQualifiers>> {
template<typename Node, typename... States>
static void transform(std::unique_ptr< Node >& n, States&&...) {
if(n->children.empty()) {
n.reset();
} else if(n->children.back()->children.size() > 1) {
// get rid of extra star rule for list of static qualifiers
auto staticQuals = std::move(n->children.back());
n->children.pop_back();
for(auto& qual : staticQuals->children) {
n->emplace_back(std::move(qual));
}
}
}
};
// error messages
template<typename> inline constexpr const char* error_message = nullptr;
template<> inline constexpr const char* error_message<StructOpen> = "Expected '{' after beginning of struct declaration";
template<> inline constexpr const char* error_message<StructClose> = "Expected '}' to close struct declaration";
template<> inline constexpr const char* error_message<StructMemberList> = "Expected a non-empty list of struct members";
template<> inline constexpr const char* error_message<StructSemicolon> = "Expected a semicolon after a struct declaration";
template<> inline constexpr const char* error_message<StructName> = "Expected identifier as struct name";
template<> inline constexpr const char* error_message<Identifier> = "Expected identifier";
template<> inline constexpr const char* error_message<ArrayQualifierClose> = "Expected a ']' to close the array qualifier";
template<> inline constexpr const char* error_message<ArrayQualifierNumber> = "Expected a number literal as array size";
template<> inline constexpr const char* error_message<Semicolon> = "Expected ';'";
template<> inline constexpr const char* error_message<Eof> = "Expected end of file";
// Those should never fail but we need them to avoid static asserts in pegtl
template<> inline constexpr const char* error_message<Seps> = "??Seps";
template<> inline constexpr const char* error_message<Grammar> = "??Grammar";
template<> inline constexpr const char* error_message<ArrayQualifiers> = "??ArrayQualifiers";
template<typename T> inline constexpr const char* error_message<pegtl::pad<T, Separator>> = error_message<T>;
struct error {
template<typename Rule> static constexpr auto message = error_message<Rule>;
template<typename Rule> static constexpr auto raise_on_failure = false;
};
template<typename Rule> using control = tao::pegtl::must_if<error>::control<Rule>;
} // namespace syn
// tree parser
const Type* parseBuiltin(std::string_view str) {
constexpr auto createAtomPair = [](std::string_view name, Type::BaseType type,
u32 width, u32 vec, u32 col) {
Type ret;
ret.type = type;
ret.width = width;
ret.vecsize = vec;
ret.columns = col;
ret.deco.name = name; // static string so it's ok to set it here
if(col > 1u) {
// TODO: would need to consider buffer layout
ret.deco.matrixStride = vec * width / 8;
}
return std::pair(name, ret);
};
static const std::unordered_map<std::string_view, Type> builtins = {
// base types
createAtomPair("float", Type::typeFloat, 32, 1, 1),
createAtomPair("f32", Type::typeFloat, 32, 1, 1),
createAtomPair("vec1", Type::typeFloat, 32, 1, 1),
createAtomPair("float1", Type::typeFloat, 32, 1, 1),
createAtomPair("uint", Type::typeUint, 32, 1, 1),
createAtomPair("u32", Type::typeUint, 32, 1, 1),
createAtomPair("uvec1", Type::typeUint, 32, 1, 1),
createAtomPair("uint1", Type::typeUint, 32, 1, 1),
createAtomPair("int", Type::typeInt, 32, 1, 1),
createAtomPair("i32", Type::typeInt, 32, 1, 1),
createAtomPair("ivec1", Type::typeInt, 32, 1, 1),
createAtomPair("int1", Type::typeInt, 32, 1, 1),
createAtomPair("half", Type::typeFloat, 16, 1, 1),
createAtomPair("f16", Type::typeFloat, 16, 1, 1),
createAtomPair("f16vec1", Type::typeFloat, 16, 1, 1),
createAtomPair("half1", Type::typeFloat, 16, 1, 1),
createAtomPair("float16_t", Type::typeFloat, 16, 1, 1),
createAtomPair("double", Type::typeFloat, 64, 1, 1),
createAtomPair("f64", Type::typeFloat, 64, 1, 1),
createAtomPair("dvec1", Type::typeFloat, 64, 1, 1),
createAtomPair("double1", Type::typeFloat, 64, 1, 1),
createAtomPair("u64", Type::typeUint, 64, 1, 1),
createAtomPair("uint64_t", Type::typeUint, 64, 1, 1),
createAtomPair("u16", Type::typeUint, 16, 1, 1),
createAtomPair("ushort", Type::typeUint, 16, 1, 1),
createAtomPair("u8", Type::typeUint, 8, 1, 1),
createAtomPair("ubyte", Type::typeUint, 8, 1, 1),
createAtomPair("i64", Type::typeInt, 64, 1, 1),
createAtomPair("int64_t", Type::typeInt, 64, 1, 1),
createAtomPair("i16", Type::typeInt, 16, 1, 1),
createAtomPair("short", Type::typeInt, 16, 1, 1),
createAtomPair("i8", Type::typeInt, 8, 1, 1),
createAtomPair("byte", Type::typeInt, 8, 1, 1),
createAtomPair("bool", Type::typeBool, 32, 1, 1),
// vectors
createAtomPair("vec2", Type::typeFloat, 32, 2, 1),
createAtomPair("float2", Type::typeFloat, 32, 2, 1),
createAtomPair("vec3", Type::typeFloat, 32, 3, 1),
createAtomPair("float3", Type::typeFloat, 32, 3, 1),
createAtomPair("vec4", Type::typeFloat, 32, 4, 1),
createAtomPair("float4", Type::typeFloat, 32, 4, 1),
createAtomPair("uvec2", Type::typeUint, 32, 2, 1),
createAtomPair("uint2", Type::typeUint, 32, 2, 1),
createAtomPair("uvec3", Type::typeUint, 32, 3, 1),
createAtomPair("uint3", Type::typeUint, 32, 3, 1),
createAtomPair("uvec4", Type::typeUint, 32, 4, 1),
createAtomPair("uint4", Type::typeUint, 32, 4, 1),
createAtomPair("u32vec2", Type::typeUint, 32, 2, 1),
createAtomPair("u32int2", Type::typeUint, 32, 2, 1),
createAtomPair("u32vec3", Type::typeUint, 32, 3, 1),
createAtomPair("u32int3", Type::typeUint, 32, 3, 1),
createAtomPair("u32vec4", Type::typeUint, 32, 4, 1),
createAtomPair("u32int4", Type::typeUint, 32, 4, 1),
createAtomPair("u8vec2", Type::typeUint, 8, 2, 1),
createAtomPair("u8vec3", Type::typeUint, 8, 3, 1),
createAtomPair("u8vec4", Type::typeUint, 8, 4, 1),
createAtomPair("i8vec2", Type::typeInt, 8, 2, 1),
createAtomPair("i8vec3", Type::typeInt, 8, 3, 1),
createAtomPair("i8vec4", Type::typeInt, 8, 4, 1),
createAtomPair("bvec2", Type::typeBool, 32, 2, 1),
createAtomPair("bvec3", Type::typeBool, 32, 3, 1),
createAtomPair("bvec4", Type::typeBool, 32, 4, 1),
createAtomPair("u16vec2", Type::typeUint, 16, 2, 1),
createAtomPair("ushort2", Type::typeUint, 16, 2, 1),
createAtomPair("u16vec3", Type::typeUint, 16, 3, 1),
createAtomPair("ushort3", Type::typeUint, 16, 3, 1),
createAtomPair("u16vec4", Type::typeUint, 16, 4, 1),
createAtomPair("ushort4", Type::typeUint, 16, 4, 1),
createAtomPair("u64vec2", Type::typeUint, 64, 2, 1),
createAtomPair("uint64_t2", Type::typeUint, 64, 2, 1),
createAtomPair("u64vec3", Type::typeUint, 64, 3, 1),
createAtomPair("uint64_t3", Type::typeUint, 64, 3, 1),
createAtomPair("u64vec4", Type::typeUint, 64, 4, 1),
createAtomPair("uint64_t4", Type::typeUint, 64, 4, 1),
createAtomPair("ivec2", Type::typeInt, 32, 2, 1),
createAtomPair("int2", Type::typeInt, 32, 2, 1),
createAtomPair("ivec3", Type::typeInt, 32, 3, 1),
createAtomPair("int3", Type::typeInt, 32, 3, 1),
createAtomPair("ivec4", Type::typeInt, 32, 4, 1),
createAtomPair("int4", Type::typeInt, 32, 4, 1),
createAtomPair("i16vec2", Type::typeInt, 16, 2, 1),
createAtomPair("short2", Type::typeInt, 16, 2, 1),
createAtomPair("i16vec3", Type::typeInt, 16, 3, 1),
createAtomPair("short3", Type::typeInt, 16, 3, 1),
createAtomPair("i16vec4", Type::typeInt, 16, 4, 1),
createAtomPair("short4", Type::typeInt, 16, 4, 1),
createAtomPair("f16vec2", Type::typeFloat, 16, 2, 1),
createAtomPair("half2", Type::typeFloat, 16, 2, 1),
createAtomPair("f16vec3", Type::typeFloat, 16, 3, 1),
createAtomPair("half3", Type::typeFloat, 16, 3, 1),
createAtomPair("f16vec4", Type::typeFloat, 16, 4, 1),
createAtomPair("half4", Type::typeFloat, 16, 4, 1),
createAtomPair("f64vec2", Type::typeFloat, 64, 2, 1),
createAtomPair("dvec2", Type::typeFloat, 64, 2, 1),
createAtomPair("double2", Type::typeFloat, 64, 2, 1),
createAtomPair("f64vec3", Type::typeFloat, 64, 3, 1),
createAtomPair("dvec3", Type::typeFloat, 64, 3, 1),
createAtomPair("double3", Type::typeFloat, 64, 3, 1),
createAtomPair("f64vec4", Type::typeFloat, 64, 4, 1),
createAtomPair("dvec4", Type::typeFloat, 64, 4, 1),
createAtomPair("double4", Type::typeFloat, 64, 4, 1),
};
auto it = builtins.find(str);
if(it == builtins.end()) {
return nullptr;
}
return &it->second;
}
struct TreeParser {
using ParseTreeNode = tao::pegtl::parse_tree::node;
struct InternalError : std::exception {
const ParseTreeNode* node;
std::string assertion;
InternalError(const ParseTreeNode& n, std::string a) :
node(&n), assertion(std::move(a)) {}
};
struct SemanticError : std::exception {
const ParseTreeNode* node;
std::string message;
SemanticError(const ParseTreeNode& n, std::string a) :
node(&n), message(std::move(a)) {}
};
#define passert(x, node) if(!(x)) { throw InternalError(node, #x); }
template<typename T>
void checkType(const ParseTreeNode& node) const {
if(!node.is_type<T>()) {
auto msg = dlg::format("node.is_type<{}>()", pegtl::demangle<T>);
throw InternalError(node, msg);
}
}
const Type* parseType(const ParseTreeNode& node) const {
checkType<syn::Type>(node);
auto name = node.string_view();
auto it = structs_.find(name);
if(it != structs_.end()) {
return it->second;
}
auto t = parseBuiltin(name);
if(t == nullptr) {
auto msg = dlg::format("Invalid type '{}'", name);
throw SemanticError(node, msg);
}
return t;
}
const Type* applyArrayQualifiers(const ParseTreeNode& quals, const Type& in) {
checkType<syn::ArrayQualifiers>(quals);
passert(!quals.children.empty(), quals);
auto& dst = alloc_.construct<Type>();
dst = in;
dst.deco.arrayStride = size(in, bufferLayout_);
dst.array = alloc_.alloc<u32>(quals.children.size());
for(auto [i, qual] : enumerate(quals.children)) {
if(qual->is_type<syn::ArrayRuntimeQualifier>()) {
passert(i == 0u, *qual);
dst.array[i] = 0;
} else {
checkType<syn::Number>(*qual);
// initialization here only done because auf shitty msvc
// compiler warnings, breaks my heart :(
u32 dim {};
auto success = stoi(qual->string(), dim);
passert(success, *qual);
dst.array[i] = dim;
}
}
return &dst;
}
void parseValueDecl(const ParseTreeNode& decl, Type::Member& dst, unsigned& offset) {
checkType<syn::ValueDecl>(decl);
passert(decl.children.size() >= 2u, decl);
passert(decl.children.size() <= 3u, decl);
dst.type = parseType(*decl.children[0]);
dst.name = copy(alloc_, decl.children[1]->string_view());
offset = align(offset, align(*dst.type, bufferLayout_));
dst.offset = offset;
if(decl.children.size() >= 3) {
dst.type = applyArrayQualifiers(*decl.children[2], *dst.type);
}
offset += size(*dst.type, bufferLayout_);
}
void parseStruct(const ParseTreeNode& decl) {
checkType<syn::StructDecl>(decl);
auto& t = alloc_.construct<Type>();
t.type = Type::typeStruct;
passert(decl.children.size() == 2u, decl);
t.deco.name = copy(alloc_, decl.children[0]->string_view());
auto offset = 0u;
auto& members = decl.children[1]->children;
t.members = alloc_.alloc<Type::Member>(members.size());
for(auto [i, member] : enumerate(members)) {
parseValueDecl(*member, t.members[i], offset);
auto& type = *t.members[i].type;
if(!type.array.empty() && type.array.front() == 0u) {
throw SemanticError(decl, "Runtime array not allowed in struct");
}
}
structs_.emplace(t.deco.name, &t);
}
void parseStructDecls(const ParseTreeNode& decls) {
checkType<syn::StructDecls>(decls);
for(auto& decl : decls.children) {
parseStruct(*decl);
}
}
void parseValueDecls(const ParseTreeNode& decls) {
checkType<syn::ValueDecls>(decls);
auto* sdst = alloc_.allocRaw<Type>();
sdst->type = Type::typeStruct;
sdst->deco.name = "main";
auto offset = 0u;
auto runtimeArray = false;
auto& members = decls.children;
sdst->members = alloc_.alloc<Type::Member>(members.size());
for(auto [i, member] : enumerate(decls.children)) {
if(runtimeArray) {
throw SemanticError(*member, "No values allowed after runtime-array");
}
parseValueDecl(*member, sdst->members[i], offset);
auto& type = *sdst->members[i].type;
if(!type.array.empty() && type.array.front() == 0u) {
runtimeArray = true;
}
}
main_ = sdst;
}
void parseModule(const ParseTreeNode& mod) {
checkType<syn::Grammar>(mod);
parseStructDecls(*mod.children[0]);
parseValueDecls(*mod.children[1]);
}
LinAllocator& alloc_;
// TODO: use linear allocator here as well?
// Should probably just use a vector (or map/linked list with
// LinearAllocator), we don't have so many types that we need an
// unordered map
std::unordered_map<std::string_view, const Type*> structs_ {};
const Type* main_ {};
BufferLayout bufferLayout_ {BufferLayout::std430}; // TODO
};
ParseTypeResult parseType(std::string_view str, LinAllocator& alloc) {
ZoneScoped;
pegtl::string_input in {std::string(str), "input"};
std::unique_ptr<tao::pegtl::parse_tree::node> root;
try {
root = tao::pegtl::parse_tree::parse<syn::Whole, syn::selector,
pegtl::nothing, syn::control>(in);
} catch(const pegtl::parse_error& error) {
ParseTypeResult res;
auto pos = error.positions()[0];
auto& err = res.error.emplace();
err.message = error.message();
err.loc.line = pos.line;
err.loc.col = pos.column;
err.loc.lineContent = in.line_at(pos);
return res;
}
dlg_assert(root);
TreeParser parser{alloc};
try {
parser.parseModule(*root->children[0]);
} catch(const TreeParser::InternalError& err) {
auto node_printer = [&](std::ostream& os, auto& n) {
auto type = n.is_root() ? "ROOT" : n.type;
using pegtl::parse_tree::internal::escape;
os << " x" << &n << " [ label=\"";
escape( os, type );
if( n.has_content() ) {
os << "\\n\\\"";
escape( os, n.string_view() );
os << "\\\"";
}
if (&n == err.node) {
os << "\\nInternal Error: \\\"";
escape( os, err.assertion);
os << "\\\"";
}
os << "\"";
if (&n == err.node) {
os << " color=red ";
}
os << "]\n";
};
auto msg = dlg::format("Internal buftype compiler error. Assertion '{}' failed.",
err.assertion);
// Print dot file for internal errors
// Make sure to never print the errror for the same input twice since
// that might be problematic with continuous editing.
static thread_local std::unordered_set<std::string> seen;
if(seen.insert(std::string(str)).second) {
char name[100];
std::time_t now = std::time(0);
std::strftime(name, sizeof(name), "vil_err_%Y_%m_%d_%H_%M_%S.dot", localtime(&now));
auto of = std::ofstream(name);
pegtl::parse_tree::print_dot(of, *root, node_printer);
of.close();
msg += dlg::format(" See {}.", name);
}
dlg_error("{}", msg);
ParseTypeResult res;
auto& dstErr = res.error.emplace();
auto pos = err.node->begin();
dstErr.loc.line = pos.line;
dstErr.loc.col = pos.column;
dstErr.loc.lineContent = in.line_at(pos);
dstErr.message = msg;
return res;
} catch(const TreeParser::SemanticError& err) {
ParseTypeResult res;
auto& dstErr = res.error.emplace();
auto pos = err.node->begin();
dstErr.loc.line = pos.line;
dstErr.loc.col = pos.column;
dstErr.loc.lineContent = in.line_at(pos);
dstErr.message = err.message;
return res;
}
ParseTypeResult res;
res.type = parser.main_;
return res;
}
// Will simply output any errors to console.
const Type* unwrap(const ParseTypeResult& res) {
if(res.type) {
dlg_assert(!res.error);
return res.type;
}
dlg_assert(res.error);
auto& err = *res.error;
char buf[12];
dlg_escape_sequence(dlg_default_output_styles[dlg_level_error], buf);
std::string msg = buf;
msg += dlg::format("input:{}:{}: {}\n", err.loc.line, err.loc.col, err.message);
// TODO: make it work with tabs
auto& line = err.loc.lineContent;
auto tabCount = std::count(line.begin(), line.end(), '\t');
msg += line;
msg += "\n";
// hard to say what tab size is... eh. Maybe just replace it?
auto col = err.loc.col + tabCount * (4 - 1);
for(auto i = 1u; i < col; ++i) {
msg += " ";
}
msg += "^\n";
msg += dlg_reset_sequence;
std::cerr << msg;
return nullptr;
}
} // namespace vil