hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
diagnosticEngine.cpp
Go to the documentation of this file.
1//
2// Created for hoshi-lang LSP readiness refactoring.
3//
4
5#include "diagnosticEngine.h"
6#include <fstream>
7#include <share/def.hpp>
8#include <stdexcept>
9
10namespace yoi {
11
16
21
23 // Map legacy exception_categories labels to diagnostic severities.
24 // These match the defaults in share/def.cpp's exception_categories map.
27 m_categorySeverityOverrides["ELYSIA_RUNTIME_NOT_FOUND"] = DiagnosticSeverity::Warning;
28 m_categorySeverityOverrides["NULLABLE_VALUE_SUPPLY_TO_RAW"] = DiagnosticSeverity::Warning; // effectively suppressed by caller
29 m_categorySeverityOverrides["MODULE_NOT_MODIFIED"] = DiagnosticSeverity::Warning; // effectively suppressed by caller
30}
31
33 {
34 std::lock_guard<std::mutex> lock(m_mutex);
35 m_diagnostics.push_back(diag);
36 }
37
38 // In Immediate mode, Error severity aborts compilation immediately.
39 // This preserves the existing "stop on first error" parser behavior.
41 // Format the error message the same way the old panic() did for CLI output.
42 auto message = diag.message;
43 if (!diag.sourceFile.empty()) {
44 message += " near " + yoi::wstring2string(diag.sourceFile)
45 + ":" + std::to_string(diag.line)
46 + ":" + std::to_string(diag.column);
47 auto hint = getLineHint(diag.sourceFile, diag.line, diag.column);
48 if (!hint.empty()) {
49 message += "\n" + yoi::wstring2string(hint);
50 }
51 }
52 throw std::runtime_error(message);
53 }
54}
55
57 const std::string &msg,
58 DiagnosticSeverity severity,
59 DiagnosticCategory category) {
60 // Convert from 0-based (lexer/parser internal) to 1-based (display/LSP).
61 Diagnostic diag(m_currentFilePath, line + 1, col + 1, msg, severity, category);
62 report(diag);
63}
64
66 std::lock_guard<std::mutex> lock(m_mutex);
67 for (const auto &d : m_diagnostics) {
68 if (d.severity == DiagnosticSeverity::Error)
69 return true;
70 }
71 return false;
72}
73
75 std::lock_guard<std::mutex> lock(m_mutex);
76 for (const auto &d : m_diagnostics) {
77 if (d.severity == DiagnosticSeverity::Warning)
78 return true;
79 }
80 return false;
81}
82
84 std::lock_guard<std::mutex> lock(m_mutex);
85 size_t count = 0;
86 for (const auto &d : m_diagnostics) {
87 if (d.severity == DiagnosticSeverity::Error)
88 ++count;
89 }
90 return count;
91}
92
94 std::lock_guard<std::mutex> lock(m_mutex);
95 size_t count = 0;
96 for (const auto &d : m_diagnostics) {
97 if (d.severity == DiagnosticSeverity::Warning)
98 ++count;
99 }
100 return count;
101}
102
103const std::vector<Diagnostic> &DiagnosticEngine::getDiagnostics() const {
104 // This returns a const reference. Callers must not hold it across
105 // concurrent report() calls, or they must do their own locking.
106 return m_diagnostics;
107}
108
110 std::lock_guard<std::mutex> lock(m_mutex);
111 m_diagnostics.clear();
112}
113
115 m_mode = mode;
116}
117
121
125
129
130void DiagnosticEngine::setCategorySeverity(const std::string &label, DiagnosticSeverity severity) {
131 m_categorySeverityOverrides[label] = severity;
132}
133
135 auto it = m_categorySeverityOverrides.find(label);
136 if (it != m_categorySeverityOverrides.end()) {
137 return it->second;
138 }
139 // Default fallback: for unknown categories, Warning severity.
141}
142
144 // This mirrors the original get_line_hint_for_error() in share/def.cpp.
145 std::fstream fileStream(yoi::wstring2string(file), std::ios::in);
146 if (!fileStream.is_open()) {
147 return L"";
148 }
149 std::string lineStr;
150 for (yoi::indexT i = 0; i < line; i++) {
151 if (!std::getline(fileStream, lineStr)) {
152 return L"";
153 }
154 }
155 yoi::wstr result = yoi::string2wstring(lineStr) + L"\n";
156 // col is 1-based; place the caret at the right column
157 if (col > 0) {
158 result += std::wstring(col - 1, L' ') + L"^";
159 }
160 return result;
161}
162
163} // namespace yoi
const std::vector< Diagnostic > & getDiagnostics() const
yoi::wstr getLineHint(const yoi::wstr &file, yoi::indexT line, yoi::indexT col) const
const yoi::wstr & getCurrentFilePath() const
void setCurrentFilePath(const yoi::wstr &path)
Set the file path for diagnostics reported via the convenience overload.
void setCategorySeverity(const std::string &label, DiagnosticSeverity severity)
void report(const Diagnostic &diag)
DiagnosticSeverity getCategorySeverity(const std::string &label) const
Get the effective severity for a legacy category label.
std::map< std::string, DiagnosticSeverity > m_categorySeverityOverrides
std::vector< Diagnostic > m_diagnostics
DiagnosticCategory
Definition diagnostic.h:21
std::string wstring2string(const std::wstring &v)
Definition def.cpp:230
DiagnosticSeverity
Definition diagnostic.h:14
std::wstring string2wstring(const std::string &v)
Definition def.cpp:224
std::wstring wstr
Definition def.hpp:51
uint64_t indexT
Definition def.hpp:54
std::string message
Definition diagnostic.h:36
DiagnosticSeverity severity
Definition diagnostic.h:37
yoi::indexT column
Definition diagnostic.h:35
yoi::indexT line
Definition diagnostic.h:34
yoi::wstr sourceFile
Definition diagnostic.h:33