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).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
saqut 2026-06-18 16:21:58 +03:00
parent 79c51af501
commit 98b4bc6114
8 changed files with 26204 additions and 134 deletions

Binary file not shown.

View File

@ -1,20 +1,31 @@
# ninja log v7
1 2432 1781560000479022647 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1
1 1218 1781560000479623743 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6
2433 3980 1781560002911027568 CMakeFiles/saqut.dir/src/tokenizer/tokenizer.cpp.o a01677f8bb4f4dbc
1 1408 1781786522326465778 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32
1 2454 1781786563274205322 CMakeFiles/saqut.dir/src/parser/nodes/program.cpp.o ac5bbcd74d87561a
1 2570 1781786563274005192 CMakeFiles/saqut.dir/src/parser/nodes/identifier.cpp.o eb96bb4b1eb4ad80
1 2619 1781786563273914998 CMakeFiles/saqut.dir/src/parser/nodes/expressions.cpp.o 4057e3d63c63a1ab
1 2552 1781786563274095617 CMakeFiles/saqut.dir/src/parser/nodes/literal.cpp.o 78f2c4da7c9b2281
1 2664 1781786563274315492 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 2517 1781786563273824541 CMakeFiles/saqut.dir/src/parser/nodes/declarations.cpp.o b6c56f04a257f685
1 2736 1781786295699197219 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23
2664 2825 1781786565936516467 saqut 8525928b86934b0a
0 22 1781786586571540360 build.ninja 1876a59d627a585
0 22 1781786586571540360 /home/saqut/Masaüstü/saqutcompiler/build/cmake_install.cmake 1876a59d627a585
1 2086 1781786305407208932 CMakeFiles/saqut.dir/src/symbol/symbol_collector.cpp.o 3348f498f369213d
10 1265 1781788264862998611 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32
10 2216 1781788264862423222 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23
2216 2383 1781788267068424572 saqut 8525928b86934b0a
0 5993 1781788863286900733 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1
1 5798 1781788863287900734 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6
8108 10913 1781788871394910212 CMakeFiles/saqut.dir/src/tokenizer/tokenizer.cpp.o a01677f8bb4f4dbc
5798 10663 1781788869086425023 CMakeFiles/saqut.dir/src/parser/parser.cpp.o 2c65b7be26cead32
5 8301 1781788863292536727 CMakeFiles/saqut.dir/src/parser/nodes/program.cpp.o ac5bbcd74d87561a
3 8830 1781788863290580789 CMakeFiles/saqut.dir/src/parser/nodes/identifier.cpp.o eb96bb4b1eb4ad80
2 8108 1781788863290412878 CMakeFiles/saqut.dir/src/parser/nodes/expressions.cpp.o 4057e3d63c63a1ab
3 8768 1781788863290742048 CMakeFiles/saqut.dir/src/parser/nodes/literal.cpp.o 78f2c4da7c9b2281
8 8333 1781788863295900743 CMakeFiles/saqut.dir/src/parser/nodes/statements.cpp.o b5c20724bbf3648c
1 9121 1781788863288931744 CMakeFiles/saqut.dir/src/parser/nodes/binary_expr.cpp.o d2e2bb2f8a63c6d2
1 8732 1781788863289045597 CMakeFiles/saqut.dir/src/parser/nodes/declarations.cpp.o b6c56f04a257f685
1 10559 1781788863288812886 CMakeFiles/saqut.dir/src/main.cpp.o 110c26cb1d0c3a23
11577 11832 1781788874863914253 saqut 8525928b86934b0a
0 22 1781788881496630820 build.ninja 1876a59d627a585
0 22 1781788881495921955 /home/saqut/Masaüstü/saqutcompiler/build/cmake_install.cmake 1876a59d627a585
5993 11577 1781788869279907744 CMakeFiles/saqut.dir/src/symbol/symbol_collector.cpp.o 3348f498f369213d
1 3694 1781788886610186419 CMakeFiles/saqut.dir/src/lexer/lexer.cpp.o 90eeec811f2137e6
1 6106 1781788886608927872 CMakeFiles/saqut.dir/src/core/sourcefile.cpp.o da6f5fc90e87e6b1
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
#include <iostream>
#include <sstream>
#include "cli/args.hpp"
#include "tokenizer/tokenizer.hpp"
#include "parser/parser.hpp"
#include "symbol/symbol_table.hpp"
#include "symbol/symbol_collector.hpp"
#include "diagnostic/diagnostic_engine.hpp"
#include "tools.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ı
// ─────────────────────────────────────────────────────────────────────────────
#include "vendor/nlohmann/json.hpp"
inline int cmdSymbols(const CliArgs& args) {
std::string filePath = inputFilePath(args);
@ -82,21 +25,41 @@ inline int cmdSymbols(const CliArgs& args) {
Parser parser;
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;
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;
for (auto* t : tokens) delete t;

View File

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

View File

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

View File

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

View File

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

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

File diff suppressed because it is too large Load Diff