1#include <Poco/JSON/Array.h>
2#include <Poco/JSON/Parser.h>
12using Array = Poco::JSON::Array;
19void diagnostic(Snapshot& snapshot,
const std::string&
code,
const std::string&
id,
const std::string&
message) {
26std::string valueType(
const Poco::Dynamic::Var&
value) {
27 if (
value.isEmpty())
return "null";
28 if (
value.type() ==
typeid(Object::Ptr))
return "object";
29 if (
value.type() ==
typeid(Array::Ptr))
return "array";
30 if (
value.isString())
return "string";
31 if (
value.isBoolean())
return "boolean";
32 if (
value.isInteger())
return "integer";
33 return value.isNumeric() ?
"number" :
"unknown";
35std::string escapedPointer(
const std::string&
name) {
40Object::Ptr
source(
const Match& match) {
42 out->set(
"file", match.path);
43 out->set(
"pointer", match.pointer);
44 if (match.line)
out->set(
"line", match.line);
45 out->set(
"inputHash", match.hash);
48Object::Ptr
node(
const std::string&
id,
const std::string&
kind,
const std::string&
label) {
53 out->set(
"status",
"resolved");
56void addEdge(Snapshot& snapshot,
const std::string&
from,
const std::string&
to,
const std::string&
relation,
66void flatten(Snapshot& snapshot, std::vector<Match>&
objects,
const Poco::Dynamic::Var&
value,
const Match& location,
67 const std::string&
parent = {}) {
68 if (
snapshot.
nodes.size() >= 16384)
throw std::runtime_error(
"Project structure budget exceeded");
69 const auto id =
"json:" + location.path +
"#" + location.pointer;
70 auto item =
node(
id,
"json_" + valueType(
value), location.pointer.empty() ? location.path : location.
pointer);
71 item->set(
"source",
source(location));
72 item->set(
"evidence",
"parsed-json");
74 if (!
parent.empty()) addEdge(snapshot,
parent,
id,
"contains",
"parsed-json");
75 if (
value.type() ==
typeid(Object::Ptr)) {
76 auto object =
value.extract<Object::Ptr>();
77 auto match = location;
80 std::vector<std::string> names;
81 object->getNames(names);
83 for (
const auto&
name : names) {
88 auto child = location;
89 child.pointer +=
"/" + escapedPointer(
name);
92 item->set(
"fields",
fields);
93 }
else if (
value.type() ==
typeid(Array::Ptr)) {
95 item->set(
"count",
values->size());
97 auto child = location;
101 }
else if (
value.isString()) {
102 const auto text =
value.convert<std::string>();
103 item->set(
"value",
text.substr(0, 512));
104 item->set(
"valueTruncated",
text.size() > 512);
106 item->set(
"value",
value);
108std::vector<Match> resolve(Object::Ptr declaration,
const std::vector<Match>&
objects,
109 const std::map<std::string, std::vector<Match>>&
functions) {
110 std::vector<Match> matches;
111 if (
static_cast<int>(declaration->has(
"symbol")) +
static_cast<int>(declaration->has(
"jsonId")) != 1)
112 throw std::runtime_error(
"Source declaration requires exactly one symbol or jsonId selector");
113 if (declaration->has(
"symbol")) {
117 if (declaration->has(
"contains")) {
119 std::erase_if(matches, [&](
const Match& match) {
return match.body.find(anchor) == std::string::npos; });
121 }
else if (declaration->has(
"jsonId")) {
123 for (
const auto& match :
objects) {
124 if (!match.value->has(
"id") || !match.value->get(
"id").isString() ||
125 match.value->getValue<std::string>(
"id") !=
id)
128 if (declaration->has(
"fields")) {
129 auto fields = declaration->getArray(
"fields");
130 if (!
fields)
throw std::runtime_error(
"fields must be an array");
133 if (!
field.
isString())
throw std::runtime_error(
"field name must be a string");
134 if (!match.value->has(
field.convert<std::string>()))
valid =
false;
137 if (
valid) matches.push_back(match);
140 throw std::runtime_error(
"Semantic node requires api, symbol or jsonId");
148 out.projectDir = std::move(projectDir);
149 const auto manifestFile =
out.files.find(
"game.semantic.json");
150 Object::Ptr manifest;
151 if (manifestFile ==
out.files.end()) {
153 manifest->set(
"schema",
"eve.project-semantic");
154 manifest->set(
"schemaVersion", 2);
155 manifest->set(
"projectId", defaultProjectId);
157 if (!manifestFile->second->error.empty() || manifestFile->second->json.type() !=
typeid(Object::Ptr))
158 throw std::runtime_error(
"Missing or invalid game.semantic.json");
159 manifest = manifestFile->second->json.extract<Object::Ptr>();
161 if (
requiredString(manifest,
"schema") !=
"eve.project-semantic" || !manifest->has(
"schemaVersion") ||
162 !manifest->get(
"schemaVersion").isInteger() ||
163 (manifest->getValue<
int>(
"schemaVersion") != 1 && manifest->getValue<
int>(
"schemaVersion") != 2))
164 throw std::runtime_error(
"Unsupported project semantic schema");
168 std::map<std::string, std::vector<Match>>
functions;
172 item->set(
"inputHash",
file->hash);
173 item->set(
"parser", std::filesystem::path(
path).extension() ==
".dnut" ?
"opaque-dsl" :
"source-metadata");
174 out.nodes.emplace(
"file:" +
path, item);
175 if (!
file->error.empty()) {
179 if (!
file->json.isEmpty())
180 flatten(
out,
objects,
file->json, Match{path,
"", file->hash, 0, {}, {}},
"file:" +
path);
186 symbol->set(
"source",
source(match));
187 symbol->set(
"erasedType",
"dynamic");
188 symbol->set(
"evidence",
"source-metadata");
189 out.nodes.emplace(
id, symbol);
190 addEdge(
out,
"file:" +
path,
id,
"declares",
"source-metadata");
197 addEdge(
out,
"file:" +
path,
"file:" +
target,
"loads",
"literal-dofile-reference");
200 if (manifest->getValue<
int>(
"schemaVersion") == 2) {
201 if (manifest->has(
"nodes") || manifest->has(
"links"))
202 throw std::runtime_error(
"Compiler-derived projects do not accept manual nodes or links");
203 out.automatic =
true;
208 auto declarations = manifest->getArray(
"nodes"), links = manifest->getArray(
"links");
209 if (!declarations || !links || declarations->size() > 256 || links->size() > 1024)
210 throw std::runtime_error(
"Invalid semantic nodes/links budget");
212 auto declaration = declarations->getObject(
static_cast<unsigned>(
index));
214 if (
static_cast<int>(declaration->has(
"api")) +
static_cast<int>(declaration->has(
"symbol")) +
215 static_cast<int>(declaration->has(
"jsonId")) !=
217 throw std::runtime_error(
"Semantic node requires exactly one api, symbol or jsonId selector");
218 if (
out.nodes.contains(
id))
throw std::runtime_error(
"Duplicate semantic node ID: " +
id);
220 item->set(
"evidence",
"project-declaration");
221 if (declaration->has(
"api")) {
226 Poco::JSON::Parser parser;
227 item->set(
"contract", parser.parse(
contract->toJson()));
228 item->set(
"evidence",
"engine-build-contract");
230 item->set(
"status",
"unavailable");
233 if (matches.size() == 1) {
234 item->set(
"source",
source(matches.front()));
235 if (matches.front().value) item->set(
"definition", matches.front().value);
236 if (!matches.front().body.empty()) {
237 item->set(
"snippet", matches.front().body.substr(0, 2048));
238 item->set(
"snippetTruncated", matches.front().body.size() > 2048);
241 item->set(
"status", matches.empty() ?
"missing" :
"ambiguous");
243 if (item->getValue<std::string>(
"status") !=
"resolved")
244 diagnostic(
out,
"unresolved_semantic_node",
id, item->getValue<std::string>(
"status"));
245 out.nodes.emplace(
id, item);
248 auto link = links->getObject(
static_cast<unsigned>(
index));
252 if (!
out.nodes.contains(
from) || !
out.nodes.contains(
to) || matches.size() != 1 ||
253 out.nodes.at(
from)->getValue<std::string>(
"status") !=
"resolved" ||
254 out.nodes.at(
to)->getValue<std::string>(
"status") !=
"resolved") {
AuthorityStoreHandleRef reference
std::vector< std::string > fields
EnvironmentMetricContract contract
const ColourScope * parent
std::map< std::string, std::vector< Function > > functions
std::set< std::string > values
std::vector< EquipmentStat > stats
MigrationFunction function
Value get(const char *key) const
Returns the value.
bool isString() const
True when string.
const BindingContract * find(std::string_view key) const noexcept
Finds a binding contract.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::string pathText(const std::filesystem::path &path)
UTF-8 source path, including Windows project names.
void automaticGraph(Snapshot &snapshot, std::string &version)
Project typed compiler syntax through generated engine contracts; no project declarations required.
Poco::JSON::Object Object
std::map< std::string, std::shared_ptr< const File > > Files
Snapshot project(Files files, Object::Ptr stats, std::string defaultProjectId, std::string projectDir)
Build a candidate evidence graph without touching the published snapshot.
std::string requiredString(Object::Ptr object, const std::string &field)
Strict source string fields; invalid manifest shapes throw a diagnostic exception.
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
const BindingContractRegistry & generatedBindingContracts()
Process-wide generated Binding Contracts for this linked engine build.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
std::map< std::string, Object::Ptr > nodes
std::vector< Object::Ptr > diagnostics
std::vector< Object::Ptr > edges