NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
diagnostic.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <neograph/api.h>
9#include <neograph/json.h>
10
11#include <cstddef>
12#include <cstdint>
13#include <optional>
14#include <stdexcept>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace neograph::program {
20
21enum class CompilePhase {
22 Source,
23 Schema,
24 Normalize,
25 Resolve,
26 TypeCheck,
27 CoreParse,
28 CoreValidate,
29 Seal,
30};
31
32enum class DiagnosticSeverity { Error, Warning, Note };
33
34struct SourceSpan {
35 std::uint64_t byte_begin = 0;
36 std::uint64_t byte_end = 0;
37 std::uint32_t line_begin = 1;
38 std::uint32_t column_begin = 1;
39 std::uint32_t line_end = 1;
40 std::uint32_t column_end = 1;
41
42 bool operator==(const SourceSpan&) const = default;
43};
44
45struct SourceCoordinate {
46 std::string source_id;
47 std::string json_pointer;
48 std::optional<SourceSpan> span;
49
50 bool operator==(const SourceCoordinate&) const = default;
51};
52
55 Machine,
56 Maybe,
58};
59
68 std::string op;
69 std::string path;
70 std::string from;
71 json value;
72};
73
74struct DiagnosticSuggestion {
75 std::string message;
76 std::vector<SuggestionEdit> edits;
77 SuggestionApplicability applicability = SuggestionApplicability::Maybe;
78};
79
82 std::string label;
83 SourceCoordinate coordinate;
84};
85
93 std::string doc;
94 std::vector<LabelledCoordinate> related;
95 std::vector<DiagnosticSuggestion> suggestions;
97 std::vector<std::string> candidates;
98};
99
100struct Diagnostic {
101 CompilePhase phase = CompilePhase::Source;
102 std::string code;
103 DiagnosticSeverity severity = DiagnosticSeverity::Error;
104 SourceCoordinate primary;
105 std::string message;
106 json witness;
107 std::vector<SourceCoordinate> related;
109 std::optional<DiagnosticHelp> help;
110};
111
113inline constexpr std::string_view DIAGNOSTICS_TRUNCATED_CODE = "P_DIAGNOSTICS_TRUNCATED";
115inline constexpr std::size_t MAX_RETURNED_DIAGNOSTICS = 20;
116
117NEOGRAPH_PROGRAM_API std::string_view to_string(CompilePhase phase) noexcept;
118NEOGRAPH_PROGRAM_API std::string_view to_string(DiagnosticSeverity severity) noexcept;
119NEOGRAPH_PROGRAM_API std::string_view to_string(SuggestionApplicability applicability) noexcept;
120NEOGRAPH_PROGRAM_API CompilePhase compile_phase_from_string(std::string_view value);
121NEOGRAPH_PROGRAM_API DiagnosticSeverity diagnostic_severity_from_string(std::string_view value);
122NEOGRAPH_PROGRAM_API SuggestionApplicability
123suggestion_applicability_from_string(std::string_view value);
124
129NEOGRAPH_PROGRAM_API std::string diagnostic_doc_link(std::string_view code);
130
135NEOGRAPH_PROGRAM_API std::size_t diagnostic_name_distance(std::string_view left,
136 std::string_view right,
137 std::size_t limit = 2);
138
140NEOGRAPH_PROGRAM_API std::vector<std::string> diagnostic_name_candidates(
141 std::string_view unknown, const std::vector<std::string>& declared);
142
144NEOGRAPH_PROGRAM_API Diagnostic sealed_diagnostic(Diagnostic diagnostic);
145
146NEOGRAPH_PROGRAM_API void to_json(json& value, const SourceSpan& span);
147NEOGRAPH_PROGRAM_API void from_json(const json& value, SourceSpan& span);
148NEOGRAPH_PROGRAM_API void to_json(json& value, const SourceCoordinate& coordinate);
149NEOGRAPH_PROGRAM_API void from_json(const json& value, SourceCoordinate& coordinate);
150NEOGRAPH_PROGRAM_API void to_json(json& value, const DiagnosticHelp& help);
151NEOGRAPH_PROGRAM_API void from_json(const json& value, DiagnosticHelp& help);
153NEOGRAPH_PROGRAM_API void to_json(json& value, const Diagnostic& diagnostic);
154NEOGRAPH_PROGRAM_API void from_json(const json& value, Diagnostic& diagnostic);
155
159class NEOGRAPH_PROGRAM_API ProgramDiagnosticError : public std::runtime_error,
161public:
162 explicit ProgramDiagnosticError(Diagnostic diagnostic);
163 const Diagnostic& diagnostic() const noexcept;
164
165private:
166 Diagnostic diagnostic_;
167};
168
169} // namespace neograph::program
NEOGRAPH_API export/import macro for shared-library builds.
Marks an exception type as a configuration or definition verdict.
A Program diagnostic raised at runtime.
Definition diagnostic.h:160
Marker for exceptions that are configuration or definition verdicts (#364).
constexpr std::size_t MAX_RETURNED_DIAGNOSTICS
At most this many diagnostics are returned, plus the truncation note.
Definition diagnostic.h:115
NEOGRAPH_PROGRAM_API Diagnostic sealed_diagnostic(Diagnostic diagnostic)
The copy that is sealed into a bundle: the same diagnostic without help.
NEOGRAPH_PROGRAM_API std::vector< std::string > diagnostic_name_candidates(std::string_view unknown, const std::vector< std::string > &declared)
Names within distance 2 of unknown, nearest first, then by byte order.
NEOGRAPH_PROGRAM_API std::string diagnostic_doc_link(std::string_view code)
The documentation link for a diagnostic code: its own entry in docs/diagnostics.md,...
constexpr std::string_view DIAGNOSTICS_TRUNCATED_CODE
Code of the note that replaces returned diagnostics past the cap.
Definition diagnostic.h:113
SuggestionApplicability
How safely a tool may apply a suggestion without review.
Definition diagnostic.h:54
@ Machine
The edits are the fix; apply them as given.
@ Placeholder
The edits contain a value the author must fill in.
@ Maybe
A likely fix; review it first. The default.
NEOGRAPH_PROGRAM_API std::size_t diagnostic_name_distance(std::string_view left, std::string_view right, std::size_t limit=2)
Damerau-Levenshtein (optimal string alignment) distance, counted in bytes.
Thin C++ RAII wrapper around yyjson with nlohmann-compatible API.
Self-repair help for one diagnostic.
Definition diagnostic.h:91
std::string doc
Repository-relative documentation link, e.g. "docs/diagnostics.md#p_core_e15".
Definition diagnostic.h:93
std::vector< std::string > candidates
Declared names close to an unknown one (Damerau distance <= 2), nearest first.
Definition diagnostic.h:97
A secondary location with a one-line label ("also referenced here").
Definition diagnostic.h:81
One edit of a suggestion, as an RFC 6902 JSON Patch operation on the Program document the compiler pa...
Definition diagnostic.h:67