LILAC
Language to language Interop LAyer Compiler
Loading...
Searching...
No Matches
DiagnosticGenerator.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using Microsoft.CodeAnalysis;
6using Microsoft.CodeAnalysis.Text;
7
9
10[Generator]
11public class DiagnosticGenerator : ISourceGenerator
12{
13 public void Initialize(GeneratorInitializationContext context)
14 {
15 }
16
17 public void Execute(GeneratorExecutionContext context)
18 {
19 foreach (var additionalFile in context.AdditionalFiles)
20 {
21 if (Path.GetFileName(additionalFile.Path) != "Diagnostics.csv" ||
22 additionalFile.GetText() is not { } text)
23 continue;
24
25 Console.WriteLine(additionalFile.Path);
26
27 var builder = new StringBuilder();
28
29 builder.AppendLine(
30 """
31 // <auto-generated />
32
33 using Microsoft.CodeAnalysis;
34
35 namespace Lilac.Generator
36 {
37 internal static class Diagnostics
38 {
39 """);
40
41 foreach (var line in text.Lines.Skip(1))
42 {
43 var props = line.ToString().Split(',');
44
45 var severity = props[2]
46 .ToLowerInvariant()
47 .ToCharArray();
48 severity[0] = char.ToUpperInvariant(severity[0]);
49
50 builder.AppendLine(
51 $$"""
52 public static DiagnosticDescriptor {{props[3].Replace(' ', '_')}} { get; } = new(
53 "{{props[0]}}",
54 "{{props[3]}}",
55 "{{props[4]}}",
56 "{{props[1]}}",
57 DiagnosticSeverity.{{new string(severity)}},
58 true);
59 """);
60 }
61
62 builder.AppendLine(
63 """
64 }
65 }
66 """);
67
68 context.AddSource("Diagnostics.g.cs", SourceText.From(builder.ToString(), Encoding.UTF8));
69 }
70 }
71}
void Execute(GeneratorExecutionContext context)
void Initialize(GeneratorInitializationContext context)