-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathDotvvmCompilationDiagnostic.cs
188 lines (173 loc) · 8.07 KB
/
DotvvmCompilationDiagnostic.cs
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
using System.Collections.Generic;
using System.Collections.Immutable;
using DotVVM.Framework.Binding.Properties;
using DotVVM.Framework.Compilation.Parser;
using DotVVM.Framework.Hosting;
using System.Linq;
using DotVVM.Framework.Compilation.Parser.Dothtml.Parser;
using System;
using DotVVM.Framework.Compilation.ControlTree.Resolved;
using DotVVM.Framework.Binding;
using Newtonsoft.Json;
using DotVVM.Framework.Binding.Expressions;
namespace DotVVM.Framework.Compilation
{
/// <summary> Represents a dothtml compilation error or a warning, along with its location. </summary>
public record DotvvmCompilationDiagnostic: IEquatable<DotvvmCompilationDiagnostic>
{
public DotvvmCompilationDiagnostic(
string message,
DiagnosticSeverity severity,
DotvvmCompilationSourceLocation? location,
IEnumerable<DotvvmCompilationDiagnostic>? notes = null,
Exception? innerException = null)
{
Message = message;
Severity = severity;
Location = location ?? DotvvmCompilationSourceLocation.Unknown;
Notes = notes?.ToImmutableArray() ?? ImmutableArray<DotvvmCompilationDiagnostic>.Empty;
InnerException = innerException;
}
public string Message { get; init; }
public Exception? InnerException { get; init; }
public DiagnosticSeverity Severity { get; init; }
public DotvvmCompilationSourceLocation Location { get; init; }
public ImmutableArray<DotvvmCompilationDiagnostic> Notes { get; init; }
/// <summary> Errors with lower number are preferred when selecting the primary fault to the user. When equal, errors are sorted based on the location. 0 is default for semantic errors, 100 for parser errors and 200 for tokenizer errors. </summary>
public int Priority { get; init; }
public bool IsError => Severity == DiagnosticSeverity.Error;
public bool IsWarning => Severity == DiagnosticSeverity.Warning;
public override string ToString() =>
$"{Severity}: {Message}\n at {Location?.ToString() ?? "unknown location"}";
}
public sealed record DotvvmCompilationSourceLocation
{
public string? FileName { get; init; }
[JsonIgnore]
public MarkupFile? MarkupFile { get; init; }
[JsonIgnore]
public ImmutableArray<TokenBase> Tokens { get; init; }
public int? LineNumber { get; init; }
public int? ColumnNumber { get; init; }
public int LineErrorLength { get; init; }
[JsonIgnore]
public DothtmlNode? RelatedSyntaxNode { get; init; }
[JsonIgnore]
public ResolvedTreeNode? RelatedResolvedNode { get; init; }
public DotvvmProperty? RelatedProperty { get; init; }
public IBinding? RelatedBinding { get; init; }
public Type? RelatedControlType => this.RelatedResolvedNode?.GetAncestors(true).OfType<ResolvedControl>().FirstOrDefault()?.Metadata.Type;
public DotvvmCompilationSourceLocation(
string? fileName,
MarkupFile? markupFile,
IEnumerable<TokenBase>? tokens,
int? lineNumber = null,
int? columnNumber = null,
int? lineErrorLength = null)
{
this.Tokens = tokens?.ToImmutableArray() ?? ImmutableArray<TokenBase>.Empty;
if (this.Tokens.Length > 0)
{
lineNumber ??= this.Tokens[0].LineNumber;
columnNumber ??= this.Tokens[0].ColumnNumber;
lineErrorLength ??= this.Tokens.Where(t => t.LineNumber == lineNumber).Select(t => (int?)(t.ColumnNumber + t.Length)).LastOrDefault() - columnNumber;
}
this.MarkupFile = markupFile;
this.FileName = fileName ?? markupFile?.FileName;
this.LineNumber = lineNumber;
this.ColumnNumber = columnNumber;
this.LineErrorLength = lineErrorLength ?? 0;
}
public DotvvmCompilationSourceLocation(
IEnumerable<TokenBase> tokens): this(fileName: null, null, tokens) { }
public DotvvmCompilationSourceLocation(
DothtmlNode syntaxNode, IEnumerable<TokenBase>? tokens = null)
: this(fileName: null, null, tokens ?? syntaxNode?.Tokens)
{
RelatedSyntaxNode = syntaxNode;
}
public DotvvmCompilationSourceLocation(
ResolvedTreeNode resolvedNode, DothtmlNode? syntaxNode = null, IEnumerable<TokenBase>? tokens = null)
: this(
syntaxNode ?? resolvedNode.GetAncestors(true).FirstOrDefault(n => n.DothtmlNode is {})?.DothtmlNode!,
tokens
)
{
RelatedResolvedNode = resolvedNode;
if (resolvedNode.GetAncestors().OfType<ResolvedPropertySetter>().FirstOrDefault() is {} property)
RelatedProperty = property.Property;
}
public static readonly DotvvmCompilationSourceLocation Unknown = new(fileName: null, null, null);
public bool IsUnknown => FileName is null && MarkupFile is null && Tokens.IsEmpty && LineNumber is null && ColumnNumber is null;
/// <summary> Text of the affected tokens. Consecutive tokens are concatenated - usually, this returns a single element array. </summary>
public string[] AffectedSpans
{
get
{
if (Tokens.IsEmpty)
return Array.Empty<string>();
var spans = new List<string> { Tokens[0].Text };
for (int i = 1; i < Tokens.Length; i++)
{
if (Tokens[i].StartPosition == Tokens[i - 1].EndPosition)
spans[spans.Count - 1] += Tokens[i].Text;
else
spans.Add(Tokens[i].Text);
}
return spans.ToArray();
}
}
/// <summary> Ranges of the affected tokens (in UTF-16 codepoint positions). Consecutive rangess are merged - usually, this returns a single element array. </summary>
public (int start, int end)[] AffectedRanges
{
get
{
if (Tokens.IsEmpty)
return Array.Empty<(int, int)>();
var ranges = new (int start, int end)[Tokens.Length];
ranges[0] = (Tokens[0].StartPosition, Tokens[0].EndPosition);
int ri = 0;
for (int i = 1; i < Tokens.Length; i++)
{
if (Tokens[i].StartPosition == Tokens[i - 1].EndPosition)
ranges[i].end = Tokens[i].EndPosition;
else
{
ri += 1;
ranges[ri] = (Tokens[i].StartPosition, Tokens[i].EndPosition);
}
}
return ranges.AsSpan(0, ri + 1).ToArray();
}
}
public int? EndLineNumber => Tokens.LastOrDefault()?.LineNumber ?? LineNumber;
public int? EndColumnNumber => (Tokens.LastOrDefault()?.ColumnNumber + Tokens.LastOrDefault()?.Length) ?? ColumnNumber;
public override string ToString()
{
if (IsUnknown)
return "Unknown location";
else if (FileName is {} && LineNumber is {})
{
// MSBuild-style file location
return $"{FileName}({LineNumber}{(ColumnNumber is {} ? "," + ColumnNumber : "")})";
}
else
{
// only position, plus add the affected spans
var location =
LineNumber is {} && ColumnNumber is {} ? $"{LineNumber},{ColumnNumber}: " :
LineNumber is {} ? $"{LineNumber}: " :
"";
return $"{location}{string.Join("; ", AffectedSpans)}";
}
}
public DotvvmLocationInfo ToRuntimeLocation() =>
new DotvvmLocationInfo(
this.FileName,
this.AffectedRanges,
this.LineNumber,
this.RelatedControlType,
this.RelatedProperty
);
}
}