-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathBaseSyntaxNodeClassEmitter.cs
More file actions
93 lines (79 loc) · 3.06 KB
/
Copy pathBaseSyntaxNodeClassEmitter.cs
File metadata and controls
93 lines (79 loc) · 3.06 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
namespace Reqnroll.CodeAnalysis.Gherkin.SyntaxGenerator;
internal class BaseSyntaxNodeClassEmitter(SyntaxNodeClassInfo classInfo)
{
public string EmitSyntaxNodeClass()
{
var builder = new CSharpBuilder();
builder.AppendLine("// <auto-generated/>");
builder.AppendLine("#nullable enable");
builder.Append("namespace ").Append(classInfo.ClassNamespace).AppendLine(';');
builder.Append("public abstract partial class ").AppendLine(classInfo.ClassName);
builder.AppendBodyBlock(builder =>
{
AppendOrphanConstructorTo(builder);
builder.AppendLine();
AppendParentalConstructorTo(builder);
builder.AppendLine();
AppendInternalNodePropertyTo(builder);
builder.AppendLine();
//foreach (var property in classInfo.SlotProperties.Where(property => !property.IsInherited))
//{
// new SlotPropertyEmitter(property).EmitSlotPropertyTo(builder);
// builder.AppendLine();
//}
//AppendGetSlotAsSyntaxNodeMethodTo(builder);
//builder.AppendLine();
});
return builder.ToString();
}
private void AppendInternalNodePropertyTo(CSharpBuilder builder)
{
builder
.Append("internal new ")
.Append(InternalNodeClassEmitter.ClassName)
.Append(" InternalNode => (")
.Append(InternalNodeClassEmitter.ClassName)
.AppendLine(")base.InternalNode;");
}
private void AppendGetSlotAsSyntaxNodeMethodTo(CSharpBuilder builder)
{
builder.AppendLine("internal override SyntaxNode? GetSlotAsSyntaxNode(int index)");
builder.AppendBodyBlock(builder =>
{
builder.AppendLine("return index switch");
builder.AppendBlock("{", builder =>
{
foreach (var property in classInfo.SlotProperties
.Where(property => property.NodeType == SyntaxNodeType.SyntaxNode))
{
builder.Append(property.Index.ToString()).Append(" => ").Append(property.Name).AppendLine(",");
}
builder.AppendLine("_ => null");
}, "};");
});
}
private void AppendParentalConstructorTo(CSharpBuilder builder)
{
builder
.Append("internal ")
.Append(classInfo.ClassName)
.Append('(')
.Append(InternalNodeClassEmitter.ClassName)
.AppendLine(" node, SyntaxNode? parent, int position)");
builder.BeginBlock();
builder.AppendLine(": base(node, parent, position) {}");
builder.EndBlock();
}
private void AppendOrphanConstructorTo(CSharpBuilder builder)
{
builder
.Append("internal ")
.Append(classInfo.ClassName)
.Append('(')
.Append(InternalNodeClassEmitter.ClassName)
.AppendLine(" node)");
builder.BeginBlock();
builder.AppendLine(": base(node) {}");
builder.EndBlock();
}
}