refactor: nlohmann/json entegrasyonu — string birleştirme yerine nesne API

- src/vendor/nlohmann/json.hpp eklendi (single-header, MIT)
- SourceLocation  → toJsonObj() + toJson() (nlohmann)
- Type            → toJsonObj() + toJson() (nlohmann, özyinelemeli)
- Diagnostic      → toJsonObj() + toJson() (nlohmann)
- DiagnosticEngine→ toJsonObj() + toJson() (nlohmann)
- symbols.hpp     → tamamen nlohmann ile yeniden yazıldı

Tüm toJson() imzaları std::string döndürmeye devam eder;
AST JsonObject builder'ı dokunulmadı (ayrı katman).
This commit is contained in:
saqut 2026-06-18 16:21:58 +03:00
parent 79d8618f03
commit 1af9ea8a2b
8 changed files with 26204 additions and 134 deletions

Binary file not shown.

View File

@ -1,20 +1,31 @@
# ninja log v7 # ninja log v7
1 2432 1781560000479022647 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1 0 5993 1781788863286900733 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1
1 1218 1781560000479623743 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6 1 5798 1781788863287900734 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6
2433 3980 1781560002911027568 CMakeFiles/saqut.dir/src/tokenizer/tokenizer.cpp.o a01677f8bb4f4dbc 8108 10913 1781788871394910212 CMakeFiles/saqut.dir/src/tokenizer/tokenizer.cpp.o a01677f8bb4f4dbc
1 1408 1781786522326465778 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32 5798 10663 1781788869086425023 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32
1 2454 1781786563274205322 CMakeFiles/saqut.dir/src/parser/nodes/program.cpp.o ac5bbcd74d87561a 5 8301 1781788863292536727 CMakeFiles/saqut.dir/src/parser/nodes/program.cpp.o ac5bbcd74d87561a
1 2570 1781786563274005192 CMakeFiles/saqut.dir/src/parser/nodes/identifier.cpp.o eb96bb4b1eb4ad80 3 8830 1781788863290580789 CMakeFiles/saqut.dir/src/parser/nodes/identifier.cpp.o eb96bb4b1eb4ad80
1 2619 1781786563273914998 CMakeFiles/saqut.dir/src/parser/nodes/expressions.cpp.o 4057e3d63c63a1ab 2 8108 1781788863290412878 CMakeFiles/saqut.dir/src/parser/nodes/expressions.cpp.o 4057e3d63c63a1ab
1 2552 1781786563274095617 CMakeFiles/saqut.dir/src/parser/nodes/literal.cpp.o 78f2c4da7c9b2281 3 8768 1781788863290742048 CMakeFiles/saqut.dir/src/parser/nodes/literal.cpp.o 78f2c4da7c9b2281
1 2664 1781786563274315492 CMakeFiles/saqut.dir/src/parser/nodes/statements.cpp.o b5c20724bbf3648c 8 8333 1781788863295900743 CMakeFiles/saqut.dir/src/parser/nodes/statements.cpp.o b5c20724bbf3648c
1 2656 1781786563273513379 CMakeFiles/saqut.dir/src/parser/nodes/binary_expr.cpp.o d2e2bb2f8a63c6d2 1 9121 1781788863288931744 CMakeFiles/saqut.dir/src/parser/nodes/binary_expr.cpp.o d2e2bb2f8a63c6d2
1 2517 1781786563273824541 CMakeFiles/saqut.dir/src/parser/nodes/declarations.cpp.o b6c56f04a257f685 1 8732 1781788863289045597 CMakeFiles/saqut.dir/src/parser/nodes/declarations.cpp.o b6c56f04a257f685
1 2736 1781786295699197219 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23 1 10559 1781788863288812886 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23
2664 2825 1781786565936516467 saqut 8525928b86934b0a 11577 11832 1781788874863914253 saqut 8525928b86934b0a
0 22 1781786586571540360 build.ninja 1876a59d627a585 0 22 1781788881496630820 build.ninja 1876a59d627a585
0 22 1781786586571540360 /home/saqut/Masaüstü/saqutcompiler/build/cmake_install.cmake 1876a59d627a585 0 22 1781788881495921955 /home/saqut/Masaüstü/saqutcompiler/build/cmake_install.cmake 1876a59d627a585
1 2086 1781786305407208932 CMakeFiles/saqut.dir/src/symbol/symbol_collector.cpp.o 3348f498f369213d 5993 11577 1781788869279907744 CMakeFiles/saqut.dir/src/symbol/symbol_collector.cpp.o 3348f498f369213d
10 1265 1781788264862998611 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32 1 3694 1781788886610186419 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6
10 2216 1781788264862423222 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23 1 6106 1781788886608927872 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1
2216 2383 1781788267068424572 saqut 8525928b86934b0a 3 8929 1781788886610927874 CMakeFiles/saqut.dir/src/parser/nodes/identifier.cpp.o eb96bb4b1eb4ad80
3 9947 1781788886611876031 CMakeFiles/saqut.dir/src/parser/nodes/program.cpp.o ac5bbcd74d87561a
3 10066 1781788886611770636 CMakeFiles/saqut.dir/src/parser/nodes/literal.cpp.o 78f2c4da7c9b2281
1 10950 1781788886610366872 CMakeFiles/saqut.dir/src/parser/nodes/binary_expr.cpp.o d2e2bb2f8a63c6d2
2 11212 1781788886610587227 CMakeFiles/saqut.dir/src/parser/nodes/expressions.cpp.o 4057e3d63c63a1ab
1 11244 1781788886610473997 CMakeFiles/saqut.dir/src/parser/nodes/declarations.cpp.o b6c56f04a257f685
3694 11807 1781788890301932134 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32
7 12109 1781788886614927879 CMakeFiles/saqut.dir/src/parser/nodes/statements.cpp.o b5c20724bbf3648c
1 12570 1781788886610272711 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23
8929 12696 1781788895536938159 CMakeFiles/saqut.dir/src/tokenizer/tokenizer.cpp.o a01677f8bb4f4dbc
6106 13677 1781788892713934912 CMakeFiles/saqut.dir/src/symbol/symbol_collector.cpp.o 3348f498f369213d
13677 13929 1781788900284943608 saqut 8525928b86934b0a

View File

@ -6,70 +6,13 @@
#define SAQUT_CLI_SYMBOLS #define SAQUT_CLI_SYMBOLS
#include <iostream> #include <iostream>
#include <sstream>
#include "cli/args.hpp" #include "cli/args.hpp"
#include "tokenizer/tokenizer.hpp" #include "tokenizer/tokenizer.hpp"
#include "parser/parser.hpp" #include "parser/parser.hpp"
#include "symbol/symbol_table.hpp" #include "symbol/symbol_table.hpp"
#include "symbol/symbol_collector.hpp" #include "symbol/symbol_collector.hpp"
#include "diagnostic/diagnostic_engine.hpp" #include "diagnostic/diagnostic_engine.hpp"
#include "tools.hpp" #include "vendor/nlohmann/json.hpp"
// ─────────────────────────────────────────────────────────────────────────────
// symbolsToJson — sembol tablosunu JSON olarak serileştir
// ─────────────────────────────────────────────────────────────────────────────
inline std::string symbolsToJson(const std::string& filePath,
const SymbolTable& table,
const DiagnosticEngine& diag) {
auto symbols = table.allSymbols();
std::ostringstream ss;
ss << "{\n";
ss << " \"file\": \"" << jsonEscape(filePath) << "\",\n";
ss << " \"symbols\": [";
bool firstSym = true;
for (Symbol* s : symbols) {
if (s->isBuiltin) continue;
if (!firstSym) ss << ",";
firstSym = false;
ss << "\n {\n";
ss << " \"name\": \"" << jsonEscape(s->name) << "\",\n";
ss << " \"kind\": \"" << symbolKindName(s->kind) << "\",\n";
ss << " \"type\": \"" << jsonEscape(s->type.toString()) << "\",\n";
ss << " \"typeDetail\": " << s->type.toJson() << ",\n";
ss << " \"definition\": " << s->definitionLoc.toJson() << ",\n";
ss << " \"isBuiltin\": " << (s->isBuiltin ? "true" : "false") << ",\n";
// referanslar
ss << " \"references\": [";
bool firstRef = true;
for (const SourceLocation& ref : s->references) {
if (!firstRef) ss << ", ";
firstRef = false;
ss << ref.toJson();
}
ss << "]\n";
ss << " }";
}
if (!firstSym) ss << "\n ";
ss << "],\n";
// tanılar (diagnostic engine'den hazır JSON al, iç kısmını sar)
ss << " \"diagnostics\": " << diag.toJson() << "\n";
ss << "}\n";
return ss.str();
}
// ─────────────────────────────────────────────────────────────────────────────
// cmdSymbols — giriş noktası
// ─────────────────────────────────────────────────────────────────────────────
inline int cmdSymbols(const CliArgs& args) { inline int cmdSymbols(const CliArgs& args) {
std::string filePath = inputFilePath(args); std::string filePath = inputFilePath(args);
@ -82,21 +25,41 @@ inline int cmdSymbols(const CliArgs& args) {
Parser parser; Parser parser;
ASTNode* ast = parser.parse(tokens); ASTNode* ast = parser.parse(tokens);
if (!ast) {
// AST null olursa boş ama geçerli bir JSON çıktısı üret
DiagnosticEngine diag;
diag.report("E000", SourceLocation{}, "AST üretilemedi");
SymbolTable empty;
std::cout << symbolsToJson(filePath, empty, diag);
for (auto* t : tokens) delete t;
return 1;
}
SymbolTable table; SymbolTable table;
DiagnosticEngine diag; DiagnosticEngine diag;
SymbolCollector(table, diag).collect(ast);
std::cout << symbolsToJson(filePath, table, diag); if (ast) {
SymbolCollector(table, diag).collect(ast);
} else {
diag.report("E000", SourceLocation{}, "AST üretilemedi");
}
// ── JSON çıktı ──────────────────────────────────────────────────────────
nlohmann::json out;
out["file"] = filePath;
nlohmann::json symArray = nlohmann::json::array();
for (Symbol* s : table.allSymbols()) {
if (s->isBuiltin) continue;
nlohmann::json refs = nlohmann::json::array();
for (const SourceLocation& r : s->references)
refs.push_back(r.toJsonObj());
symArray.push_back({
{"name", s->name},
{"kind", symbolKindName(s->kind)},
{"type", s->type.toString()},
{"typeDetail", s->type.toJsonObj()},
{"definition", s->definitionLoc.toJsonObj()},
{"references", refs},
{"isBuiltin", s->isBuiltin}
});
}
out["symbols"] = symArray;
out["diagnostics"] = diag.toJsonObj();
std::cout << out.dump(2) << "\n";
delete ast; delete ast;
for (auto* t : tokens) delete t; for (auto* t : tokens) delete t;

View File

@ -22,6 +22,7 @@
#define SAQUT_CORE_LOCATION #define SAQUT_CORE_LOCATION
#include <string> #include <string>
#include "vendor/nlohmann/json.hpp"
// ============================================================================ // ============================================================================
// SourceLocation — Kaynak Koddaki Bir Nokta // SourceLocation — Kaynak Koddaki Bir Nokta
@ -66,15 +67,17 @@ struct SourceLocation {
} }
// JSON formatı: {"file":"...","line":5,"column":10,"offset":134} // JSON formatı: {"file":"...","line":5,"column":10,"offset":134}
std::string toJson() const { nlohmann::json toJsonObj() const {
if (!isValid()) return "null"; if (!isValid()) return nullptr;
return "{" return {
"\"file\":\"" + filePath + "\"," {"file", filePath},
"\"line\":" + std::to_string(line) + "," {"line", line},
"\"column\":" + std::to_string(column) + "," {"column", column},
"\"offset\":" + std::to_string(offset) + {"offset", offset}
"}"; };
} }
std::string toJson() const { return toJsonObj().dump(); }
}; };
#endif // SAQUT_CORE_LOCATION #endif // SAQUT_CORE_LOCATION

View File

@ -34,6 +34,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "vendor/nlohmann/json.hpp"
// ============================================================================ // ============================================================================
// Enum'lar // Enum'lar
@ -220,31 +221,37 @@ struct Type {
// ------------------------------------------------------------------ // // ------------------------------------------------------------------ //
// toJson — Makine-okur (cam ilkesi: her tip dışarıdan sorgulanabilir) // toJson — Makine-okur (cam ilkesi: her tip dışarıdan sorgulanabilir)
// ------------------------------------------------------------------ // // ------------------------------------------------------------------ //
std::string toJson() const { nlohmann::json toJsonObj() const {
nlohmann::json j;
switch (kind) { switch (kind) {
case TypeKind::Primitive: case TypeKind::Primitive:
return std::string("{\"kind\":\"primitive\",\"name\":\"") + primName(prim) + "\"}"; j["kind"] = "primitive";
j["name"] = primName(prim);
break;
case TypeKind::Array: case TypeKind::Array:
return std::string("{\"kind\":\"array\",\"element\":") + j["kind"] = "array";
(elementType ? elementType->toJson() : "null") + "}"; j["element"] = elementType ? elementType->toJsonObj() : nullptr;
break;
case TypeKind::Struct: case TypeKind::Struct:
return "{\"kind\":\"struct\",\"name\":\"" + structName + "\"}"; j["kind"] = "struct";
j["name"] = structName;
break;
case TypeKind::Function: { case TypeKind::Function: {
std::string s = "{\"kind\":\"function\",\"returns\":"; j["kind"] = "function";
s += returnType ? returnType->toJson() : "null"; j["returns"] = returnType ? returnType->toJsonObj() : nullptr;
s += ",\"params\":["; nlohmann::json params = nlohmann::json::array();
for (size_t i = 0; i < paramTypes.size(); ++i) { for (const auto& p : paramTypes) params.push_back(p.toJsonObj());
if (i) s += ","; j["params"] = params;
s += paramTypes[i].toJson(); break;
}
s += "]}";
return s;
} }
case TypeKind::Error: case TypeKind::Error:
return "{\"kind\":\"error\"}"; j["kind"] = "error";
break;
} }
return "null"; return j;
} }
std::string toJson() const { return toJsonObj().dump(); }
}; };
#endif // SAQUT_CORE_TYPE #endif // SAQUT_CORE_TYPE

View File

@ -85,18 +85,17 @@ struct Diagnostic {
std::string message; // bağlama özel açıklama std::string message; // bağlama özel açıklama
std::string hint; // opsiyonel "şunu dene" önerisi std::string hint; // opsiyonel "şunu dene" önerisi
std::string toJson() const { nlohmann::json toJsonObj() const {
std::string s = "{"; nlohmann::json j;
s += "\"level\":\""; s += diagLevelName(level); s += "\","; j["level"] = diagLevelName(level);
s += "\"code\":\""; s += jsonEscape(code); s += "\","; j["code"] = code;
s += "\"location\":"; s += loc.toJson(); s += ","; j["location"] = loc.toJsonObj();
s += "\"message\":\""; s += jsonEscape(message); s += "\""; j["message"] = message;
if (!hint.empty()) { if (!hint.empty()) j["hint"] = hint;
s += ",\"hint\":\""; s += jsonEscape(hint); s += "\""; return j;
}
s += "}";
return s;
} }
std::string toJson() const { return toJsonObj().dump(); }
}; };
// ============================================================================ // ============================================================================

View File

@ -25,6 +25,7 @@
#include <vector> #include <vector>
#include <ostream> #include <ostream>
#include "diagnostic/diagnostic.hpp" #include "diagnostic/diagnostic.hpp"
#include "vendor/nlohmann/json.hpp"
// ============================================================================ // ============================================================================
// DiagnosticEngine // DiagnosticEngine
@ -88,18 +89,19 @@ public:
} }
// --- Makine-okur çıktı --- // --- Makine-okur çıktı ---
std::string toJson() const { nlohmann::json toJsonObj() const {
std::string s = "{\"diagnostics\":["; nlohmann::json items = nlohmann::json::array();
for (size_t i = 0; i < diagnostics_.size(); ++i) { for (const auto& d : diagnostics_)
if (i) s += ","; items.push_back(d.toJsonObj());
s += diagnostics_[i].toJson(); return {
} {"diagnostics", items},
s += "],\"errorCount\":" + std::to_string(errorCount()); {"errorCount", errorCount()},
s += ",\"warningCount\":" + std::to_string(warningCount()); {"warningCount", warningCount()}
s += "}"; };
return s;
} }
std::string toJson() const { return toJsonObj().dump(); }
private: private:
std::vector<Diagnostic> diagnostics_; std::vector<Diagnostic> diagnostics_;

26085
src/vendor/nlohmann/json.hpp vendored Normal file

File diff suppressed because it is too large Load Diff