ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Diagnostics.hpp
Go to the documentation of this file.
1#pragma once
2
18
19#include <cstddef>
20#include <cstdint>
21#include <optional>
22#include <ostream>
23#include <string>
24#include <string_view>
25#include <vector>
26
27namespace ZenitUI
28{
29
33
41 {
42 std::string file;
43 std::uint32_t line = 0;
44 std::uint32_t column = 0;
45 std::uint32_t length = 0;
46
47 [[nodiscard]] bool valid() const noexcept { return line != 0 && column != 0; }
48 };
49
52 {
53 Info,
54 Warning,
55 Error
56 };
57
69 enum class DiagnosticCode
70 {
71 UnterminatedString,
72 UnbalancedBrace,
73 UnexpectedToken,
74
75 UnknownProperty,
76 InvalidValue,
77 UnknownPseudo,
78 UnknownPart,
79 UnresolvedVariable,
80 UnusedVariable,
81
82 UnknownSelectorTag,
83 SelectorMatchesNothing,
84 DuplicateRule,
85
86 UnknownTag,
87 UnknownAttribute,
88 };
89
90 [[nodiscard]] const char *toString(DiagnosticSeverity s) noexcept;
91 [[nodiscard]] const char *toString(DiagnosticCode c) noexcept;
92
95 {
96 DiagnosticCode code{};
97 DiagnosticSeverity severity{DiagnosticSeverity::Error};
98 SourceSpan span{};
99 std::string message;
100 std::string hint;
101 std::optional<SourceSpan> related;
102 };
103
112 [[nodiscard]] ZStyleDiagnostic
114 DiagnosticSeverity severity,
115 SourceSpan span,
116 std::string message,
117 std::string hint = {});
118
127 {
128 public:
129 virtual ~DiagnosticSink() = default;
130 virtual void report(ZStyleDiagnostic d) = 0;
131 [[nodiscard]] virtual bool hasErrors() const = 0;
132 [[nodiscard]] virtual std::vector<ZStyleDiagnostic> sorted() const = 0;
133 };
134
137 {
138 public:
139 void report(ZStyleDiagnostic) override {}
140 [[nodiscard]] bool hasErrors() const override { return false; }
141 [[nodiscard]] std::vector<ZStyleDiagnostic> sorted() const override { return {}; }
142 };
143
151 {
152 public:
153 void report(ZStyleDiagnostic d) override
154 {
155 diagnostics_.push_back(std::move(d));
156 }
157 [[nodiscard]] bool hasErrors() const override
158 {
159 for (const auto &d : diagnostics_)
160 if (d.severity == DiagnosticSeverity::Error)
161 return true;
162 return false;
163 }
164 [[nodiscard]] std::vector<ZStyleDiagnostic> sorted() const override;
165 [[nodiscard]] const std::vector<ZStyleDiagnostic> &diagnostics() const noexcept
166 {
167 return diagnostics_;
168 }
169 void clear() { diagnostics_.clear(); }
170
171 private:
172 std::vector<ZStyleDiagnostic> diagnostics_;
173 };
174
181 [[nodiscard]] std::size_t editDistance(std::string_view a, std::string_view b);
182
191 [[nodiscard]] std::vector<std::string>
192 suggestSimilar(std::string_view unknown,
193 const std::vector<std::string_view> &candidates,
194 std::size_t maxDistance = 2,
195 std::size_t maxSuggestions = 3);
196
204 void formatDiagnostic(const ZStyleDiagnostic &d, std::ostream &os);
205
211 [[nodiscard]] const std::vector<std::string_view> &getKnownStyleProperties();
213} // namespace ZenitUI
Sink that accumulates diagnostics in insertion order.
Definition Diagnostics.hpp:151
Abstract destination for diagnostics.
Definition Diagnostics.hpp:127
Sink that discards every diagnostic.
Definition Diagnostics.hpp:137
std::vector< std::string > suggestSimilar(std::string_view unknown, const std::vector< std::string_view > &candidates, std::size_t maxDistance=2, std::size_t maxSuggestions=3)
Suggest candidates within maxDistance edits of unknown.
DiagnosticSeverity
Diagnostic severity level.
Definition Diagnostics.hpp:52
std::size_t editDistance(std::string_view a, std::string_view b)
Levenshtein edit distance between a and b.
ZStyleDiagnostic makeDiagnostic(DiagnosticCode code, DiagnosticSeverity severity, SourceSpan span, std::string message, std::string hint={})
Build a diagnostic record.
const std::vector< std::string_view > & getKnownStyleProperties()
Return the list of style property names known to the parser.
DiagnosticCode
Machine-readable diagnostic identifier.
Definition Diagnostics.hpp:70
void formatDiagnostic(const ZStyleDiagnostic &d, std::ostream &os)
Write a diagnostic to os in file:line:col: severity: message [code] format.
Source location for a diagnostic.
Definition Diagnostics.hpp:41
A fully-formed diagnostic record.
Definition Diagnostics.hpp:95