-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcodegen.zig
More file actions
1099 lines (983 loc) · 44.8 KB
/
codegen.zig
File metadata and controls
1099 lines (983 loc) · 44.8 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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This program is executed from the build system to generate type bindings for the LSP specification.
const std = @import("std");
const MetaModel = @import("MetaModel.zig");
pub fn main(init: std.process.Init.Minimal) !u8 {
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
defer _ = debug_allocator.deinit();
const gpa = debug_allocator.allocator();
var threaded: std.Io.Threaded = .init_single_threaded;
const io = threaded.io();
var arg_it = try init.args.iterateAllocator(gpa);
defer arg_it.deinit();
_ = arg_it.skip(); // skip self exe
const out_file_path = try gpa.dupe(u8, arg_it.next() orelse std.process.fatal("second argument must be the output path to the generated zig code", .{}));
defer gpa.free(out_file_path);
var parsed_meta_model = try std.json.parseFromSlice(MetaModel, gpa, @embedFile("meta-model"), .{});
defer parsed_meta_model.deinit();
const source = try renderMetaModel(gpa, &parsed_meta_model.value);
defer gpa.free(source);
var zig_tree: std.zig.Ast = try .parse(gpa, source, .zig);
defer zig_tree.deinit(gpa);
std.Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(out_file_path) orelse ".") catch {};
var out_file = try std.Io.Dir.cwd().createFile(io, out_file_path, .{});
defer out_file.close(io);
if (zig_tree.errors.len != 0) {
std.log.warn("generated file contains syntax errors! (cannot format file)", .{});
try out_file.writeStreamingAll(io, source);
return 1;
} else {
var buf: [1024]u8 = undefined;
var out = out_file.writer(io, &buf);
const w = &out.interface;
try zig_tree.render(gpa, w, .{});
try w.flush();
return 0;
}
}
const FormatToSnakeCase = struct {
text: []const u8,
pub fn format(ctx: FormatToSnakeCase, writer: *std.Io.Writer) std.Io.Writer.Error!void {
for (ctx.text, 0..) |c, i| {
if (std.ascii.isUpper(c)) {
const isNextUpper = i + 1 < ctx.text.len and std.ascii.isUpper(ctx.text[i + 1]);
if (i != 0 and !isNextUpper) try writer.writeByte('_');
try writer.writeByte(std.ascii.toLower(c));
} else {
try writer.writeByte(c);
}
}
}
};
fn fmtToSnakeCase(text: []const u8) std.fmt.Alt(FormatToSnakeCase, FormatToSnakeCase.format) {
return .{ .data = .{ .text = text } };
}
const FormatDocs = struct {
text: []const u8,
comment_kind: CommentKind,
const CommentKind = enum {
normal,
doc,
top_level,
};
pub fn format(ctx: FormatDocs, writer: *std.Io.Writer) std.Io.Writer.Error!void {
const prefix = switch (ctx.comment_kind) {
.normal => "// ",
.doc => "/// ",
.top_level => "//! ",
};
var iterator = std.mem.splitScalar(u8, ctx.text, '\n');
while (iterator.next()) |line| try writer.print("{s}{s}\n", .{ prefix, line });
}
};
fn fmtDocs(text: []const u8, comment_kind: FormatDocs.CommentKind) std.fmt.Alt(FormatDocs, FormatDocs.format) {
return .{ .data = .{ .text = text, .comment_kind = comment_kind } };
}
fn guessFieldName(meta_model: *const MetaModel, writer: *std.Io.Writer, typ: MetaModel.Type, i: usize) std.Io.Writer.Error!void {
switch (typ) {
.base => |base| switch (base.name) {
.URI => try writer.writeAll("uri"),
.DocumentUri => try writer.writeAll("document_uri"),
.integer => try writer.writeAll("integer"),
.uinteger => try writer.writeAll("uinteger"),
.decimal => try writer.writeAll("decimal"),
.RegExp => try writer.writeAll("regexp"),
.string => try writer.writeAll("string"),
.boolean => try writer.writeAll("bool"),
.null => try writer.writeAll("@\"null\""),
},
.reference => |ref| try writer.print("{f}", .{fmtToSnakeCase(ref.name)}),
.array => |arr| {
try guessFieldName(meta_model, writer, arr.element.*, 0);
try writer.writeByte('s');
},
.map => try writer.print("map_{d}", .{i}),
.@"and" => try writer.print("and_{d}", .{i}),
.@"or" => try writer.print("or_{d}", .{i}),
.tuple => try writer.print("tuple_{d}", .{i}),
.literal,
.stringLiteral,
.integerLiteral,
.booleanLiteral,
=> try writer.print("literal_{d}", .{i}),
}
}
fn isNull(ty: MetaModel.Type) bool {
return ty == .base and ty.base.name == .null;
}
fn unwrapOptional(ty: MetaModel.Type) ?MetaModel.Type {
if (ty != .@"or") return null;
const or_ty = ty.@"or";
if (or_ty.items.len == 2 and isNull(or_ty.items[1])) return or_ty.items[0];
return null;
}
fn isEnum(ty: MetaModel.Type) bool {
if (ty != .@"or") return false;
const or_ty = ty.@"or";
for (or_ty.items) |t| {
if (t != .stringLiteral) return false;
}
return true;
}
fn isNullable(ty: MetaModel.Type) bool {
if (ty != .@"or") return false;
const or_ty = ty.@"or";
return (or_ty.items.len == 2 and isNull(or_ty.items[1])) or isNull(or_ty.items[or_ty.items.len - 1]);
}
fn unwrapOptionalUnion(ty: MetaModel.Type) ?MetaModel.Type {
if (ty != .@"or") return null;
const or_ty = ty.@"or";
if (unwrapOptional(ty) != null) return null;
if (isEnum(ty)) return null;
if (!isNull(or_ty.items[or_ty.items.len - 1])) return null;
return .{ .@"or" = .{ .items = or_ty.items[0 .. or_ty.items.len - 1] } };
}
fn createOptional(arena: std.mem.Allocator, child_type: MetaModel.Type) error{OutOfMemory}!MetaModel.Type {
return .{ .@"or" = .{
.items = try arena.dupe(MetaModel.Type, &.{
child_type,
.{ .base = .{ .name = .null } },
}),
} };
}
/// Some LSP types will be rendered to a distinct Zig type (anonymous struct/container type).
fn findInnerDistinctType(ty: *MetaModel.Type, meta_model: *const MetaModel) ?*MetaModel.Type {
return switch (ty.*) {
.base => null,
.reference => null,
.array => |arr| findInnerDistinctType(arr.element, meta_model),
.map => |map| findInnerDistinctType(map.value, meta_model),
.@"and" => ty,
.@"or" => |or_ty| {
if (unwrapOptional(ty.*)) |_| {
return findInnerDistinctType(&or_ty.items[0], meta_model);
} else {
return ty;
}
},
.tuple => |tup| {
for (tup.items) |*sub_type| {
if (findInnerDistinctType(sub_type, meta_model)) |reason| return reason;
}
return null;
},
.literal => ty,
.stringLiteral => null,
.integerLiteral => @panic("unsupported"),
.booleanLiteral => @panic("unsupported"),
};
}
const Symbol = union(enum) {
namespace,
decl: struct {
type: MetaModel.Type,
original_name: ?[]const u8,
documentation: ?[]const u8,
},
structure: MetaModel.Structure,
enumeration: MetaModel.Enumeration,
};
const SymbolTree = struct {
root: std.StringArrayHashMapUnmanaged(Node) = .empty,
const Node = struct {
symbol: Symbol,
children: std.StringArrayHashMapUnmanaged(Node) = .empty,
fn deinit(
node: *Node,
gpa: std.mem.Allocator,
) void {
for (node.children.values()) |*child_node| child_node.deinit(gpa);
node.children.deinit(gpa);
node.* = undefined;
}
};
fn deinit(
tree: *SymbolTree,
gpa: std.mem.Allocator,
) void {
for (tree.root.values()) |*node| node.deinit(gpa);
tree.root.deinit(gpa);
tree.* = undefined;
}
fn insert(
tree: *SymbolTree,
gpa: std.mem.Allocator,
name: []const u8,
symbol: Symbol,
) error{OutOfMemory}!void {
var name_it = std.mem.splitScalar(u8, name, '.');
var current_node: ?*Node = null;
while (name_it.next()) |name_component| {
const children = if (current_node) |node| &node.children else &tree.root;
const gop = try children.getOrPutValue(gpa, name_component, .{ .symbol = .namespace });
current_node = gop.value_ptr;
}
const node = current_node.?;
if (node.symbol != .namespace) std.debug.panic("symbol collision: {s}", .{name[0 .. name_it.index orelse name.len]});
node.symbol = symbol;
}
/// Useful for debugging
fn dump(tree: *const SymbolTree) error{WriteFailed}!void {
const Landfill = struct {
indent_stack: std.bit_set.IntegerBitSet(64) = undefined,
indent_stack_size: u8 = 0,
fn dumpNode(
l: *@This(),
children: std.StringArrayHashMapUnmanaged(Node),
writer: *std.Io.Writer,
) error{WriteFailed}!void {
for (children.keys(), children.values(), 0..) |name, child_node, i| {
const is_last = i + 1 == children.count();
for (0..l.indent_stack_size) |indent_index| {
try writer.writeAll(if (l.indent_stack.isSet(indent_index)) " " else "| ");
}
try writer.print("{s}── {s}\n", .{ @as([]const u8, if (is_last) "└" else "├"), name });
l.indent_stack.setValue(l.indent_stack_size, is_last);
l.indent_stack_size += 1;
defer l.indent_stack_size -= 1;
try l.dumpNode(child_node.children, writer);
}
}
};
var buffer: [4096]u8 = undefined;
const writer, _ = std.debug.lockStderrWriter(&buffer);
defer std.debug.unlockStderrWriter();
var landfill: Landfill = .{};
try landfill.dumpNode(tree.root, writer);
}
};
const Renderer = struct {
scope_stack: std.ArrayList(Scope),
meta_model: *const MetaModel,
w: *std.Io.Writer,
const Scope = struct {
name: ?[]const u8,
symbols: std.StringArrayHashMapUnmanaged(SymbolTree.Node),
};
fn renderNode(r: *Renderer, node: *const SymbolTree.Node, name: []const u8) error{WriteFailed}!void {
r.scope_stack.appendAssumeCapacity(.{ .name = name, .symbols = node.children });
defer r.scope_stack.items.len -= 1;
switch (node.symbol) {
.namespace => {
try r.w.print("pub const {f} = struct {{\n", .{std.zig.fmtId(name)});
for (node.children.keys(), node.children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeAll("};\n\n");
},
.decl => |payload| {
if (payload.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
if (payload.original_name) |original_name| {
if (!std.mem.eql(u8, name, original_name)) {
if (payload.documentation != null) try r.w.writeAll("///\n");
try r.w.print("/// LSP Specification name: `{s}`\n", .{original_name});
}
}
try r.w.print("pub const {f} = ", .{std.zig.fmtId(name)});
try r.renderType(payload.type, node.children);
try r.w.writeAll(";\n\n");
},
.structure => |*structure| {
if (structure.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
if (!std.mem.eql(u8, name, structure.name)) {
if (structure.documentation != null) try r.w.writeAll("///\n");
try r.w.print("/// LSP Specification name: `{s}`\n", .{structure.name});
}
try r.w.print("pub const {f} = struct {{\n", .{std.zig.fmtId(name)});
try r.renderProperties(structure, null);
if (node.children.count() != 0) try r.w.writeAll("\n\n");
for (node.children.keys(), node.children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeAll("};\n\n");
},
.enumeration => |enumeration| {
const container_kind = switch (enumeration.type.name) {
.string => "union(enum)",
.integer => "enum(i32)",
.uinteger => "enum(u32)",
};
if (enumeration.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
if (!std.mem.eql(u8, name, enumeration.name)) {
if (enumeration.documentation != null) try r.w.writeAll("///\n");
try r.w.print("/// LSP Specification name: `{s}`\n", .{enumeration.name});
}
try r.w.print("pub const {f} = {s} {{\n", .{ std.zig.fmtId(name), container_kind });
var contains_empty_enum = false;
for (enumeration.values) |entry| {
if (entry.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
switch (entry.value) {
.string => |value| {
var value_name = if (value.len == 0) "empty" else value;
if (value.len == 0) contains_empty_enum = true;
// WORKAROUND: the enumeration value `pascal` appears twice in LanguageKind
if (std.mem.eql(u8, entry.name, "Delphi")) value_name = "delphi";
try r.w.print("{f},\n", .{std.zig.fmtIdP(value_name)});
},
.number => |value| try r.w.print("{f} = {d},\n", .{ std.zig.fmtIdP(entry.name), value }),
}
}
const supportsCustomValues = enumeration.supportsCustomValues orelse false;
const field_name, const docs = if (supportsCustomValues) .{ "custom_value", "Custom Value" } else .{ "unknown_value", "Unknown Value" };
switch (enumeration.type.name) {
.string => try r.w.print("{s}: []const u8,", .{field_name}),
.integer, .uinteger => try r.w.print("/// {s}\n_,", .{docs}),
}
if (node.children.count() != 0) try r.w.writeAll("\n\n");
for (node.children.keys(), node.children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeAll("\n\n");
switch (enumeration.type.name) {
.string => {
try r.w.print(
\\pub const eql = parser.EnumCustomStringValues(@This(), {0}).eql;
\\pub const jsonParse = parser.EnumCustomStringValues(@This(), {0}).jsonParse;
\\pub const jsonParseFromValue = parser.EnumCustomStringValues(@This(), {0}).jsonParseFromValue;
\\pub const jsonStringify = parser.EnumCustomStringValues(@This(), {0}).jsonStringify;
\\
, .{contains_empty_enum});
},
.integer, .uinteger => {
try r.w.writeAll(
\\pub const jsonStringify = parser.EnumStringifyAsInt(@This()).jsonStringify;
\\
);
},
}
try r.w.writeAll("};\n\n");
},
}
}
fn renderReference(r: *Renderer, reference_name: []const u8) error{WriteFailed}!void {
switch (symbol_map.get(reference_name).?) {
.remove => try r.w.writeAll(reference_name), // keep the old name
.rename, .replace_with => |namespaced_name| try r.renderNamespacedName(namespaced_name),
}
}
fn renderNamespacedName(r: *Renderer, namespaced_name: []const u8) error{WriteFailed}!void {
if (r.scope_stack.items.len <= 1) {
try r.w.writeAll(namespaced_name);
return;
}
var namespace_it: SymbolNamespaceIterator = .init(namespaced_name, .reverse);
next: while (true) {
const symbol_name = namespace_it.next() orelse break;
var reference_count: usize = 0;
for (r.scope_stack.items) |scope| {
const is_referencing = scope.symbols.contains(symbol_name);
if (is_referencing and !r.isRenderingNamespace(namespaced_name[0..namespace_it.index])) continue :next;
reference_count += @intFromBool(is_referencing);
if (reference_count > 1) continue :next;
}
switch (reference_count) {
0 => continue,
1 => {},
else => unreachable,
}
const trimmed_name = namespaced_name[if (namespace_it.index == 0) 0 else namespace_it.index + 1..];
try r.w.writeAll(trimmed_name);
return;
}
try r.w.writeAll("types.");
try r.w.writeAll(namespaced_name);
}
fn isRenderingName(r: *Renderer, name: []const u8) bool {
var it: SymbolNamespaceIterator = .init(name, .forward);
var i: usize = 1;
while (it.next()) |a| : (i += 1) {
if (i >= r.scope_stack.items.len) return false;
const b = r.scope_stack.items[i].name.?;
if (!std.mem.eql(u8, a, b)) return false;
}
if (i != r.scope_stack.items.len) return false;
return true;
}
fn isRenderingNamespace(r: *Renderer, namespace_name: []const u8) bool {
var it: SymbolNamespaceIterator = .init(namespace_name, .forward);
var i: usize = 1;
while (it.next()) |a| : (i += 1) {
if (i >= r.scope_stack.items.len) return false;
const b = r.scope_stack.items[i].name.?;
if (!std.mem.eql(u8, a, b)) return false;
}
return true;
}
fn renderType(
r: *Renderer,
ty: MetaModel.Type,
children: std.StringArrayHashMapUnmanaged(SymbolTree.Node),
) error{WriteFailed}!void {
switch (ty) {
.@"and", .@"or" => {},
else => if (children.count() != 0) {
std.debug.print("scope:\n", .{});
for (r.scope_stack.items, 0..) |scope, i| {
std.debug.print("{d}: {?s}\n", .{ i, scope.name });
}
std.debug.print("children:\n", .{});
for (children.keys(), 0..) |child_name, i| {
std.debug.print("{d}: {s}\n", .{ i, child_name });
}
std.debug.panic("renderType on a '{t}' type with children is unsupported", .{ty});
},
}
switch (ty) {
.base => |base| switch (base.name) {
.URI => try r.w.writeAll("URI"),
.DocumentUri => try r.w.writeAll("DocumentUri"),
.integer => try r.w.writeAll("i32"),
.uinteger => try r.w.writeAll("u32"),
.decimal => try r.w.writeAll("f32"),
.RegExp => try r.w.writeAll("RegExp"),
.string => try r.w.writeAll("[]const u8"),
.boolean => try r.w.writeAll("bool"),
.null => try r.w.writeAll("?void"),
},
.reference => |ref| try r.renderReference(ref.name),
.array => |arr| {
try r.w.writeAll("[]const ");
try r.renderType(arr.element.*, children);
},
.map => |map| {
try r.w.writeAll("parser.Map(");
switch (map.key) {
.base => |base| try switch (base.name) {
.Uri => r.w.writeAll("Uri"),
.DocumentUri => r.w.writeAll("DocumentUri"),
.integer => r.w.writeAll("i32"),
.string => r.w.writeAll("[]const u8"),
},
.reference => |ref| try r.renderType(.{ .reference = ref }, children),
}
try r.w.writeAll(", ");
try r.renderType(map.value.*, children);
try r.w.writeByte(')');
},
.@"and" => |andt| {
try r.w.writeAll("struct {\n");
for (andt.items) |item| {
if (item != .reference) @panic("Unimplemented and subject encountered!");
try r.w.print("// And {s}\n", .{item.reference.name});
try r.renderProperties(lookupStructure(r.meta_model, item.reference.name), null);
try r.w.writeAll("\n\n");
}
for (children.keys(), children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeAll("}");
},
.@"or" => |ort| {
if (unwrapOptional(ty)) |child_type| {
if (children.count() != 0) @panic("unsupported");
try r.w.writeByte('?');
try r.renderType(child_type, children);
} else if (isEnum(ty)) {
try r.w.writeAll("enum {");
for (ort.items) |sub_type| {
try r.w.print("{s},\n", .{sub_type.stringLiteral.value});
}
if (children.count() != 0) try r.w.writeAll("\n\n");
for (children.keys(), children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeByte('}');
} else {
try r.w.writeAll("union(enum) {\n");
for (ort.items, 0..) |sub_type, i| {
std.debug.assert(!isNull(sub_type));
try guessFieldName(r.meta_model, r.w, sub_type, i);
try r.w.writeAll(": ");
try r.renderType(sub_type, .empty);
try r.w.writeAll(",\n");
}
if (children.count() != 0) try r.w.writeAll("\n\n");
for (children.keys(), children.values()) |child_name, *child_node| {
try r.renderNode(child_node, child_name);
}
try r.w.writeAll("\n\n");
try r.w.writeAll(
\\pub const jsonParse = parser.UnionParser(@This()).jsonParse;
\\pub const jsonParseFromValue = parser.UnionParser(@This()).jsonParseFromValue;
\\pub const jsonStringify = parser.UnionParser(@This()).jsonStringify;
\\}
);
}
},
.tuple => |tup| {
try r.w.writeAll("struct {");
for (tup.items, 0..) |field_ty, i| {
if (i != 0) try r.w.writeByte(',');
try r.w.writeByte(' ');
try r.renderType(field_ty, children);
}
try r.w.writeAll(" }");
},
.literal => |lit| {
try r.w.writeAll("struct {");
if (lit.value.properties.len != 0) {
for (lit.value.properties) |property| {
try r.w.writeByte('\n');
try r.renderProperty(property);
}
try r.w.writeByte('\n');
}
try r.w.writeByte('}');
},
.stringLiteral => |lit| try r.w.print("[]const u8 = \"{f}\"", .{std.zig.fmtString(lit.value)}),
.integerLiteral => @panic("unsupported"),
.booleanLiteral => @panic("unsupported"),
}
}
fn renderProperty(r: *Renderer, property: MetaModel.Property) error{WriteFailed}!void {
std.debug.assert(property.optional == null);
if (property.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .doc)});
try r.w.print("{f}", .{std.zig.fmtIdPU(property.name)});
try r.w.writeAll(": ");
if (unwrapOptional(property.type)) |child_ty| {
// WORKAROUND: recursive SelectionRange
if (child_ty == .reference and std.mem.eql(u8, child_ty.reference.name, "SelectionRange")) {
try r.w.writeAll("?*const ");
try r.renderType(child_ty, .empty);
} else {
try r.renderType(property.type, .empty);
}
try r.w.writeAll(" = null");
} else {
try r.renderType(property.type, .empty);
}
try r.w.writeByte(',');
}
fn renderProperties(
r: *Renderer,
structure: *const MetaModel.Structure,
maybe_extender: ?*const MetaModel.Structure,
) error{WriteFailed}!void {
const properties: []MetaModel.Property = structure.properties;
const extends: []MetaModel.Type = structure.extends orelse &.{};
const mixins: []MetaModel.Type = structure.mixins orelse &.{};
var has_properties = false;
skip: for (properties) |property| {
std.debug.assert(property.optional == null);
if (maybe_extender) |ext| {
for (ext.properties) |ext_property| {
if (std.mem.eql(u8, property.name, ext_property.name)) {
// std.log.info("Skipping implemented field emission: {s}", .{property.name});
continue :skip;
}
}
}
try r.renderProperty(property);
try r.w.writeByte('\n');
has_properties = true;
}
if (has_properties and (extends.len != 0 or mixins.len != 0)) try r.w.writeByte('\n');
for (extends) |ext| {
if (ext != .reference) @panic("Expected reference for extends!");
try r.w.print("// Extends `{s}`\n", .{ext.reference.name});
try r.renderProperties(lookupStructure(r.meta_model, ext.reference.name), structure);
try r.w.writeByte('\n');
}
for (mixins) |ext| {
if (ext != .reference) @panic("Expected reference for mixin!");
try r.w.print("// Uses mixin `{s}`\n", .{ext.reference.name});
try r.renderProperties(lookupStructure(r.meta_model, ext.reference.name), structure);
try r.w.writeByte('\n');
}
}
const FormatType = struct {
r: *Renderer,
ty: MetaModel.Type,
children: std.StringArrayHashMapUnmanaged(SymbolTree.Node) = .empty,
pub fn format(
ctx: FormatType,
writer: *std.Io.Writer,
) std.Io.Writer.Error!void {
std.debug.assert(ctx.r.w == writer);
try ctx.r.renderType(ctx.ty, ctx.children);
}
};
fn renderRequest(r: *Renderer, request: MetaModel.Request) error{WriteFailed}!void {
if (request.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .normal)});
try r.w.print(
\\.{{
\\ "{f}",
\\ RequestMetadata{{
\\ .method = {f},
\\ .documentation = {?f},
\\ .direction = .{f},
\\ .Params = {?f},
\\ .Result = {f},
\\ .PartialResult = {?f},
\\ .ErrorData = {?f},
\\ .registration = .{{ .method = {?f}, .Options = {?f} }},
\\ }},
\\}},
\\
, .{
std.zig.fmtString(request.method),
std.json.fmt(request.method, .{}),
if (request.documentation) |documentation| std.json.fmt(documentation, .{}) else null,
fmtToSnakeCase(@tagName(request.messageDirection)),
// Multiparams not used here, so we dont have to implement them :)
if (request.params) |params| FormatType{ .r = r, .ty = params.one } else null,
FormatType{ .r = r, .ty = request.result },
if (request.partialResult) |ty| FormatType{ .r = r, .ty = ty } else null,
if (request.errorData) |ty| FormatType{ .r = r, .ty = ty } else null,
if (request.registrationMethod) |method| std.json.fmt(method, .{}) else null,
if (request.registrationOptions) |ty| FormatType{ .r = r, .ty = ty } else null,
});
}
fn renderNotification(r: *Renderer, notification: MetaModel.Notification) error{WriteFailed}!void {
if (notification.documentation) |docs| try r.w.print("{f}", .{fmtDocs(docs, .normal)});
try r.w.print(
\\.{{
\\ "{f}",
\\ NotificationMetadata{{
\\ .method = {f},
\\ .documentation = {?f},
\\ .direction = .{f},
\\ .Params = {?f},
\\ .registration = .{{ .method = {?f}, .Options = {?f} }},
\\ }},
\\}},
\\
, .{
std.zig.fmtString(notification.method),
std.json.fmt(notification.method, .{}),
if (notification.documentation) |documentation| std.json.fmt(documentation, .{}) else null,
fmtToSnakeCase(@tagName(notification.messageDirection)),
// Multiparams not used here, so we dont have to implement them :)
if (notification.params) |params| FormatType{ .r = r, .ty = params.one } else null,
if (notification.registrationMethod) |method| std.json.fmt(method, .{}) else null,
if (notification.registrationOptions) |ty| FormatType{ .r = r, .ty = ty } else null,
});
}
};
const SymbolNamespaceIterator = struct {
name: []const u8,
index: usize,
direction: Direction,
const Direction = enum {
forward,
reverse,
};
fn init(name: []const u8, direction: Direction) SymbolNamespaceIterator {
return .{
.name = name,
.index = switch (direction) {
.forward => 0,
.reverse => name.len,
},
.direction = direction,
};
}
fn next(it: *SymbolNamespaceIterator) ?[]const u8 {
switch (it.direction) {
.forward => {
if (it.index == it.name.len) return null;
const new_index = std.mem.indexOfScalarPos(u8, it.name, it.index, '.') orelse {
defer it.index = it.name.len;
return it.name[it.index..];
};
defer it.index = new_index + 1;
return it.name[it.index..new_index];
},
.reverse => {
if (it.index == 0) return null;
const name = it.name[0..it.index];
const new_index = std.mem.lastIndexOfScalar(u8, name, '.') orelse {
it.index = 0;
return name;
};
it.index = new_index;
return name[it.index + 1 ..];
},
}
}
};
test SymbolNamespaceIterator {
var it: SymbolNamespaceIterator = .init("foo.bar.baz", .forward);
try std.testing.expectEqualStrings("foo", it.next().?);
try std.testing.expectEqualStrings("bar", it.next().?);
try std.testing.expectEqualStrings("baz", it.next().?);
try std.testing.expect(it.next() == null);
it = .init("foo", .forward);
try std.testing.expectEqualStrings("foo", it.next().?);
try std.testing.expect(it.next() == null);
it = .init("foo.bar.baz", .reverse);
try std.testing.expectEqualStrings("baz", it.next().?);
try std.testing.expectEqualStrings("bar", it.next().?);
try std.testing.expectEqualStrings("foo", it.next().?);
try std.testing.expect(it.next() == null);
it = .init("foo", .reverse);
try std.testing.expectEqualStrings("foo", it.next().?);
try std.testing.expect(it.next() == null);
}
fn constructSymbolTree(
gpa: std.mem.Allocator,
arena: std.mem.Allocator,
meta_model: *const MetaModel,
) error{OutOfMemory}!SymbolTree {
var symbols: std.StringArrayHashMapUnmanaged(Symbol) = .empty;
defer symbols.deinit(gpa);
try symbols.ensureTotalCapacity(
gpa,
meta_model.structures.len + meta_model.enumerations.len + meta_model.typeAliases.len + config.symbolize.len,
);
for (meta_model.structures) |structure| {
symbols.putAssumeCapacityNoClobber(structure.name, .{ .structure = structure });
}
for (meta_model.enumerations) |enumeration| {
symbols.putAssumeCapacityNoClobber(enumeration.name, .{ .enumeration = enumeration });
}
for (meta_model.typeAliases) |type_alias| {
symbols.putAssumeCapacityNoClobber(type_alias.name, .{ .decl = .{
.type = type_alias.type,
.original_name = type_alias.name,
.documentation = type_alias.documentation,
} });
}
for (config.symbolize) |property_name| {
const expect_optional = std.mem.endsWith(u8, property_name, ".?");
const trimmed_property_name = property_name[0 .. property_name.len - @as(usize, if (expect_optional) 2 else 0)];
const property = lookupProperty(trimmed_property_name, meta_model);
std.debug.assert(property.optional == null);
if (expect_optional) property.type = unwrapOptional(property.type).?;
symbols.putAssumeCapacityNoClobber(property_name, .{ .decl = .{
.type = property.type,
.original_name = null,
.documentation = property.documentation,
} });
const reference_type: MetaModel.Type = .{ .reference = .{ .name = property_name } };
property.type = if (expect_optional) try createOptional(arena, reference_type) else reference_type;
}
// Add symbols for the Result types i.e. return types of various requests.
for (meta_model.requests) |*request| {
const distinct_result_type = findInnerDistinctType(&request.result, meta_model) orelse continue;
const result_name = try std.fmt.allocPrint(arena, "{s}.Result", .{request.method});
try symbols.putNoClobber(gpa, result_name, .{ .decl = .{
.type = distinct_result_type.*,
.original_name = null,
.documentation = null,
} });
defer distinct_result_type.* = .{ .reference = .{ .name = result_name } };
const distinct_partial_result_type = findInnerDistinctType(if (request.partialResult) |*ty| ty else continue, meta_model) orelse continue;
if (distinct_partial_result_type.eql(distinct_result_type.*)) {
distinct_partial_result_type.* = .{ .reference = .{ .name = result_name } };
} else {
const partial_result_name = try std.fmt.allocPrint(arena, "{s}.PartialResult", .{request.method});
try symbols.putNoClobber(gpa, partial_result_name, .{ .decl = .{
.type = distinct_partial_result_type.*,
.original_name = null,
.documentation = null,
} });
distinct_partial_result_type.* = .{ .reference = .{ .name = partial_result_name } };
}
}
var symbol_tree: SymbolTree = .{};
errdefer symbol_tree.deinit(gpa);
for (config.symbols) |item| {
const name, const action = item;
switch (action) {
.remove => {
if (!symbols.swapRemove(name)) {
std.debug.panic("invalid symbol '{s}' in @import(\"config.zig\").symbols", .{name});
}
},
.rename => |new_name| {
const kv = symbols.fetchSwapRemove(name) orelse
std.debug.panic("invalid symbol '{s}' in @import(\"config.zig\").symbols", .{name});
try symbol_tree.insert(gpa, new_name, kv.value);
},
.replace_with => {
if (!symbols.swapRemove(name)) {
std.debug.panic("invalid symbol '{s}' in @import(\"config.zig\").symbols", .{name});
}
},
}
}
if (symbols.count() > 0) {
for (symbols.keys()) |missing| {
std.log.warn("The LSP type '{s}' is missing in @import(\"config.zig\").symbols", .{missing});
}
}
return symbol_tree;
}
fn lookupStructure(meta_model: *const MetaModel, name: []const u8) *const MetaModel.Structure {
for (meta_model.structures) |*s| {
if (std.mem.eql(u8, s.name, name)) return s;
}
std.debug.panic("could not resolve reference to '{s}'", .{name});
}
fn lookupProperty(name: []const u8, meta_model: *const MetaModel) *MetaModel.Property {
var name_it: SymbolNamespaceIterator = .init(name, .forward);
const structure_name = name_it.next().?;
const first_property_name = name_it.next().?;
std.debug.assert(name_it.next() == null);
const structure = lookupStructure(meta_model, structure_name);
for (structure.properties) |*property| {
if (std.mem.eql(u8, property.name, first_property_name)) return property;
}
for (structure.extends orelse &.{}) |ty| {
const s = lookupStructure(meta_model, ty.reference.name);
for (s.properties) |*property| {
if (std.mem.eql(u8, property.name, first_property_name)) return property;
}
}
std.debug.panic("could not find field '{s}' in {s}", .{ first_property_name, structure.name });
}
/// - normalize representation of an optional union type
/// - normalize representation of optional properties
fn normalizeMetaModel(arena: std.mem.Allocator, meta_model: *MetaModel) error{OutOfMemory}!void {
const ctx = struct {
fn onType(ally: std.mem.Allocator, ty: *MetaModel.Type) error{OutOfMemory}!void {
// normalize the representation of an optional union type
if (unwrapOptionalUnion(ty.*)) |inner| {
ty.* = try createOptional(ally, inner);
return;
}
// TODO consider flattening nested or types (e.g `Definition.Result`)
switch (ty.*) {
.array => |*array| try onType(ally, array.element),
.map => |map| try onType(ally, map.value),
.@"and" => |and_ty| for (and_ty.items) |*inner_ty| try onType(ally, inner_ty),
.@"or" => |or_ty| for (or_ty.items) |*inner_ty| try onType(ally, inner_ty),
.tuple => |tuple| for (tuple.items) |*inner_ty| try onType(ally, inner_ty),
.literal => |literal| for (literal.value.properties) |*property| try onProperty(ally, property),
.base, .reference, .stringLiteral, .integerLiteral, .booleanLiteral => {},
}
}
fn onProperty(ally: std.mem.Allocator, property: *MetaModel.Property) error{OutOfMemory}!void {
try onType(ally, &property.type);
if (property.optional orelse false) {
if (unwrapOptional(property.type) == null) {
property.type = try createOptional(ally, property.type);
}
property.optional = null;
}
}
};
const onType = ctx.onType;
const onProperty = ctx.onProperty;
for (meta_model.requests) |*request| {
if (request.params) |*params| switch (params.*) {
.one => |*ty| try onType(arena, ty),
.multiple => |types| for (types) |*ty| try onType(arena, ty),
};
try onType(arena, &request.result);
if (request.partialResult) |*ty| try onType(arena, ty);
if (request.errorData) |*ty| try onType(arena, ty);
if (request.registrationOptions) |*ty| try onType(arena, ty);
}
for (meta_model.notifications) |*notification| {
if (notification.params) |*params| switch (params.*) {
.one => |*ty| try onType(arena, ty),
.multiple => |types| for (types) |*ty| try onType(arena, ty),
};
if (notification.registrationOptions) |*ty| try onType(arena, ty);
}
for (meta_model.structures) |*structure| {
if (structure.extends) |extends| for (extends) |*ty| try onType(arena, ty);
if (structure.mixins) |mixins| for (mixins) |*ty| try onType(arena, ty);
for (structure.properties) |*property| try onProperty(arena, property);
}
// for (meta_model.enumerations) |*enumeration| {}
for (meta_model.typeAliases) |*type_alias| {
try onType(arena, &type_alias.type);
}
}
fn renderMetaModel(gpa: std.mem.Allocator, meta_model: *MetaModel) error{ OutOfMemory, WriteFailed }![:0]u8 {
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
try normalizeMetaModel(arena.allocator(), meta_model);
var symbol_tree = try constructSymbolTree(gpa, arena.allocator(), meta_model);
defer symbol_tree.deinit(gpa);
var aw: std.Io.Writer.Allocating = .init(gpa);
defer aw.deinit();
var scope_stack: [8]Renderer.Scope = undefined;