IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IkvmToolDiagnosticEvent.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Text.Json;
3using System.Text.Json.Nodes;
4
6{
7
11 public readonly record struct IkvmToolDiagnosticEvent(int Id, IkvmToolDiagnosticEventLevel Level, string Message, object?[] Args, IkvmToolDiagnosticEventLocation Location = default)
12 {
13
20 public static IkvmToolDiagnosticEvent ReadJson(ref Utf8JsonReader reader)
21 {
22 IkvmToolDiagnosticEventLevel? level = null;
23 int? id = null;
24 string? message = null;
25 List<object?>? args = null;
26
27 string? path = null;
28 int startLine = 0;
29 int startColumn = 0;
30 int endLine = 0;
31 int endColumn = 0;
32
33 // advance to first actual JSON node
34 while (reader.TokenType == JsonTokenType.None && reader.Read())
35 continue;
36
37
38 // first JSON node should be an object
39 if (reader.TokenType != JsonTokenType.StartObject)
40 throw new JsonException("Could not locate JSON object.");
41
42 // read until end of object
43 while (reader.Read())
44 {
45 if (reader.TokenType == JsonTokenType.EndObject)
46 break;
47
48 if (reader.TokenType == JsonTokenType.PropertyName)
49 {
50 if (id == null && reader.ValueTextEquals("id"))
51 {
52 reader.Skip();
53 if (reader.TokenType != JsonTokenType.Number)
54 throw new JsonException();
55
56 id = reader.GetInt32();
57 continue;
58 }
59
60 if (level == null && reader.ValueTextEquals("level"))
61 {
62 reader.Skip();
63 if (reader.TokenType != JsonTokenType.String)
64 throw new JsonException();
65
66 level = ParseLevel(ref reader);
67 continue;
68 }
69
70 if (message == null && reader.ValueTextEquals("message"))
71 {
72 reader.Skip();
73 if (reader.TokenType != JsonTokenType.String)
74 throw new JsonException();
75
76 message = reader.GetString();
77 continue;
78 }
79
80 if (args == null && reader.ValueTextEquals("args"))
81 {
82 args ??= [];
83
84 if (reader.Read() == false || reader.TokenType != JsonTokenType.StartArray)
85 throw new JsonException("Expected array for 'args'.");
86
87 while (reader.Read())
88 {
89 if (reader.TokenType == JsonTokenType.EndArray)
90 break;
91
92 switch (reader.TokenType)
93 {
94 case JsonTokenType.String:
95 args.Add(reader.GetString());
96 break;
97 case JsonTokenType.Number:
98 args.Add(ParseNumber(ref reader));
99 break;
100 case JsonTokenType.True:
101 args.Add(true);
102 break;
103 case JsonTokenType.False:
104 args.Add(false);
105 break;
106 case JsonTokenType.Null:
107 args.Add(null);
108 break;
109 }
110 }
111
112 continue;
113 }
114
115 if (reader.ValueTextEquals("location"))
116 {
117 if (reader.Read() == false || reader.TokenType != JsonTokenType.StartObject)
118 throw new JsonException("Expected JSON object for 'location'.");
119
120 // read until end of object
121 while (reader.Read())
122 {
123 if (reader.TokenType == JsonTokenType.EndObject)
124 break;
125
126 if (reader.TokenType == JsonTokenType.PropertyName)
127 {
128 if (path == null && reader.ValueTextEquals("path"))
129 {
130 reader.Skip();
131 if (reader.TokenType != JsonTokenType.String)
132 throw new JsonException();
133
134 path = reader.GetString();
135 continue;
136 }
137
138 if (reader.ValueTextEquals("position"))
139 {
140 if (reader.Read() == false || reader.TokenType != JsonTokenType.StartArray)
141 throw new JsonException("Expected array for 'location/position' property.");
142
143 if (reader.Read() == false || reader.TokenType != JsonTokenType.Number || reader.TryGetInt32(out startLine) == false)
144 throw new JsonException("Could not read start line.");
145 if (reader.Read() == false || reader.TokenType != JsonTokenType.Number || reader.TryGetInt32(out startColumn) == false)
146 throw new JsonException("Could not read start column.");
147 if (reader.Read() == false || reader.TokenType != JsonTokenType.Number || reader.TryGetInt32(out endLine) == false)
148 throw new JsonException("Could not read end line.");
149 if (reader.Read() == false || reader.TokenType != JsonTokenType.Number || reader.TryGetInt32(out endColumn) == false)
150 throw new JsonException("Could not read end column.");
151
152 if (reader.Read() == false || reader.TokenType != JsonTokenType.EndArray)
153 throw new JsonException("Expected end of array.");
154
155 continue;
156 }
157 }
158 }
159 }
160 }
161 }
162
163 if (id == null)
164 throw new JsonException("Missing 'id' property.");
165 if (level == null)
166 throw new JsonException("Missing 'level' property.");
167 if (message == null)
168 throw new JsonException("Missing 'message' property.");
169
170 return new IkvmToolDiagnosticEvent(id.Value, level.Value, message, args?.ToArray() ?? [], new IkvmToolDiagnosticEventLocation(path, startLine, startColumn, endLine, endColumn));
171 }
172
179 internal static IkvmToolDiagnosticEventLevel ParseLevel(ref Utf8JsonReader reader)
180 {
181 if (reader.ValueTextEquals("trace"))
182 return IkvmToolDiagnosticEventLevel.Trace;
183 if (reader.ValueTextEquals("info"))
185 if (reader.ValueTextEquals("warning"))
186 return IkvmToolDiagnosticEventLevel.Warning;
187 if (reader.ValueTextEquals("error"))
188 return IkvmToolDiagnosticEventLevel.Error;
189 if (reader.ValueTextEquals("fatal"))
190 return IkvmToolDiagnosticEventLevel.Fatal;
191
192 throw new JsonException();
193 }
194
200 internal static object ParseNumber(ref Utf8JsonReader reader)
201 {
202 if (reader.TryGetInt16(out var _short))
203 return _short;
204 if (reader.TryGetInt32(out var _int))
205 return _int;
206 if (reader.TryGetInt64(out var _long))
207 return _int;
208 if (reader.TryGetSingle(out var _float))
209 return _float;
210 if (reader.TryGetDouble(out var _double))
211 return _double;
212
213 throw new JsonException();
214 }
215
219 public int Id { get; } = Id;
220
224 public IkvmToolDiagnosticEventLevel Level { get; } = Level;
225
229 public string Message { get; } = Message;
230
234 public object?[] Args { get; } = Args;
235
239 public IkvmToolDiagnosticEventLocation Location { get; } = Location;
240
241 }
242
243}
readonly record struct IkvmToolDiagnosticEvent(int Id, IkvmToolDiagnosticEventLevel Level, string Message, object?[] Args, IkvmToolDiagnosticEventLocation Location=default)
Describes an event emitted from a tool.
readonly record struct IkvmToolDiagnosticEventLocation(string? Path, int StartLine, int StartColumn, int EndLine, int EndColumn)
Represents the location of a diagnostic event.
IkvmToolDiagnosticEventLevel
Describes the level of diagnostic event a tool could emit.