Skip to content

Commit 3f68035

Browse files
spec/ reaches the backend through build.mcpp, parsed at startup; no generated file in the repository
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent a263370 commit 3f68035

8 files changed

Lines changed: 202 additions & 938 deletions

File tree

‎build.mcpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// mcpp-ide's build program: the backend's view of spec/, generated where the build writes.
2+
//
3+
// spec/ is the one source of every editor-facing declaration (settings, presets, runtime strings,
4+
// mcpp's manifest keys and build-program directives). The backend reads it as JSON at startup, so
5+
// this program only carries the files into a module, `mcppide.generated.spec`, as string literals:
6+
// nothing is generated into the repository and nothing parses JSON here. The editors' declarations
7+
// (package.json's contributes, plugin.xml's actions, the message bundles) are written by
8+
// `mcpp-ide-gen`, a program of this package, when an adapter is built.
9+
import std;
10+
import mcpp;
11+
12+
namespace {
13+
14+
std::string env_or_empty(const char* name) {
15+
const char* v = std::getenv(name);
16+
return v ? std::string(v) : std::string{};
17+
}
18+
19+
std::string slurp(const std::filesystem::path& p) {
20+
std::ifstream in(p, std::ios::binary);
21+
return std::string(std::istreambuf_iterator<char>(in), {});
22+
}
23+
24+
// A literal of any length: pieces of at most 8000 bytes, each a raw string, which the compiler
25+
// joins (one compiler bounds a single literal at 16 KiB). A piece never splits a UTF-8 sequence.
26+
std::string literal(std::string_view text) {
27+
std::string out;
28+
std::size_t at = 0;
29+
if (text.empty()) return "R\"mcppide()mcppide\"";
30+
while (at < text.size()) {
31+
std::size_t n = std::min<std::size_t>(8000, text.size() - at);
32+
while (at + n < text.size() && (static_cast<unsigned char>(text[at + n]) & 0xC0) == 0x80) --n;
33+
out += "R\"mcppide(";
34+
out += text.substr(at, n);
35+
out += ")mcppide\"\n ";
36+
at += n;
37+
}
38+
return out;
39+
}
40+
41+
} // namespace
42+
43+
int main() {
44+
const std::filesystem::path root = env_or_empty("MCPP_MANIFEST_DIR");
45+
const std::filesystem::path out = env_or_empty("MCPP_OUT_DIR");
46+
const std::filesystem::path spec = root / "spec";
47+
const std::array<std::pair<const char*, const char*>, 5> files{{
48+
{"kSettingsJson", "settings.json"},
49+
{"kPresetsJson", "presets.json"},
50+
{"kRuntimeZhCnJson", "i18n/runtime.zh-cn.json"},
51+
{"kTomlSchemaJson", "toml-schema.json"},
52+
{"kBuildScriptApiJson", "buildscript-api.json"},
53+
}};
54+
std::string module =
55+
"// Generated by mcpp-ide's build.mcpp from spec/ (not part of the repository).\n"
56+
"export module mcppide.generated.spec;\n\nimport std;\n\nexport namespace mcppide::generated {\n\n";
57+
for (const auto& [name, file] : files) {
58+
const auto path = spec / file;
59+
mcpp::rerun_if_changed(path.generic_string().c_str());
60+
module += std::format("// spec/{}\ninline constexpr std::string_view {} =\n {};\n\n", file, name, literal(slurp(path)));
61+
}
62+
module += "} // namespace mcppide::generated\n";
63+
std::filesystem::create_directories(out);
64+
const auto target = out / "spec.cppm";
65+
if (slurp(target) != module) std::ofstream(target, std::ios::binary) << module;
66+
mcpp::generated(target.generic_string().c_str());
67+
return 0;
68+
}

‎mcpp.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ modules = true
2222
import_std = true
2323

2424
[build]
25-
sources = ["src/*.cppm", "src/generated/*.cppm"]
25+
sources = ["src/*.cppm"]
2626
macos_deployment_target = "12.0"
2727

2828
[targets.mcpp-ide]

‎src/generated/spec.cppm‎

Lines changed: 0 additions & 726 deletions
This file was deleted.

‎src/i18n.cppm‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export module mcppide.i18n;
66

77
import std;
88
import nlohmann.json;
9-
import mcppide.generated.spec;
9+
import mcppide.spec;
1010

1111
export namespace mcppide::i18n {
1212

@@ -28,21 +28,11 @@ std::string normalize_locale(std::string_view locale) {
2828
return "en";
2929
}
3030

31-
namespace {
32-
const std::map<std::string_view, std::string_view>& zh() {
33-
static const auto table = [] {
34-
std::map<std::string_view, std::string_view> m;
35-
for (const auto& t : generated::kZhCn) m.emplace(t.en, t.zh);
36-
return m;
37-
}();
38-
return table;
39-
}
40-
} // namespace
4131

4232
std::string translate(std::string_view key, std::string_view locale, const std::vector<std::string>& args) {
4333
std::string text(key);
4434
if (normalize_locale(locale) == "zh-cn")
45-
if (auto it = zh().find(key); it != zh().end()) text = std::string(it->second);
35+
if (auto it = spec::runtime_zh_cn().find(key); it != spec::runtime_zh_cn().end()) text = it->second;
4636
for (std::size_t i = 0; i < args.size(); ++i) {
4737
const auto marker = std::format("{{{}}}", i);
4838
for (auto pos = text.find(marker); pos != std::string::npos; pos = text.find(marker, pos + args[i].size()))
@@ -54,7 +44,7 @@ std::string translate(std::string_view key, std::string_view locale, const std::
5444
nlohmann::json bundle(std::string_view locale) {
5545
nlohmann::json out = nlohmann::json::object();
5646
if (normalize_locale(locale) == "zh-cn")
57-
for (const auto& [en, z] : zh()) out[std::string(en)] = z;
47+
for (const auto& [en, z] : spec::runtime_zh_cn()) out[en] = z;
5848
return out;
5949
}
6050

‎src/manifest.cppm‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export module mcppide.manifest;
1212

1313
import std;
1414
import nlohmann.json;
15-
import mcppide.generated.spec;
15+
import mcppide.spec;
1616

1717
export namespace mcppide::manifest {
1818

@@ -49,12 +49,10 @@ namespace mcppide::manifest {
4949
namespace {
5050

5151
const Json& schema() {
52-
static const Json s = Json::parse(generated::kTomlSchemaJson);
53-
return s;
52+
return spec::toml_schema();
5453
}
5554
const Json& api() {
56-
static const Json a = Json::parse(generated::kBuildScriptApiJson);
57-
return a;
55+
return spec::buildscript_api();
5856
}
5957

6058
std::vector<std::string_view> split_lines(std::string_view text) {

‎src/settings.cppm‎

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export module mcppide.settings;
77

88
import std;
99
import nlohmann.json;
10-
import mcppide.generated.spec;
10+
import mcppide.spec;
1111

1212
export namespace mcppide::settings {
1313

@@ -19,9 +19,9 @@ struct Checked {
1919
std::string expectation; // what the setting takes
2020
};
2121

22-
const generated::Setting* find(std::string_view key);
23-
Json default_of(const generated::Setting& s);
24-
Checked check(const generated::Setting& s, const Json& given);
22+
const spec::Setting* find(std::string_view key);
23+
Json default_of(const spec::Setting& s);
24+
Checked check(const spec::Setting& s, const Json& given);
2525
// Every setting: the values to use, the rejections, and the renames an
2626
// earlier key asks for (`aliases`).
2727
Json resolve(const Json& given);
@@ -33,32 +33,32 @@ std::optional<Json> preset(std::string_view id);
3333

3434
namespace mcppide::settings {
3535

36-
const generated::Setting* find(std::string_view key) {
37-
for (const auto& s : generated::kSettings) if (s.key == key) return &s;
36+
const spec::Setting* find(std::string_view key) {
37+
for (const auto& s : spec::settings()) if (s.key == key) return &s;
3838
return nullptr;
3939
}
4040

41-
Json default_of(const generated::Setting& s) {
42-
auto v = Json::parse(s.defaultJson, nullptr, false);
43-
return v.is_discarded() ? Json(nullptr) : v;
44-
}
41+
Json default_of(const spec::Setting& s) { return s.defaultValue; }
4542

4643
namespace {
47-
std::string expectation_of(const generated::Setting& s) {
48-
if (!s.enumJson.empty()) {
49-
auto e = Json::parse(s.enumJson, nullptr, false);
44+
std::string number_text(double d) {
45+
auto t = std::format("{}", d);
46+
return t;
47+
}
48+
49+
std::string expectation_of(const spec::Setting& s) {
50+
if (s.allowed.is_array()) {
5051
std::string out;
51-
for (const auto& v : e) { if (!out.empty()) out += " | "; out += v.get<std::string>(); }
52+
for (const auto& v : s.allowed) { if (!out.empty()) out += " | "; out += v.is_string() ? v.get<std::string>() : v.dump(); }
5253
return out;
5354
}
54-
if (s.type == "number" && (!s.minimum.empty() || !s.maximum.empty()))
55-
return std::format("{} … {}", s.minimum.empty() ? "-∞" : std::string(s.minimum),
56-
s.maximum.empty() ? "∞" : std::string(s.maximum));
57-
return s.type == "array" ? "array of strings" : std::string(s.type);
55+
if (s.type == "number" && (s.minimum || s.maximum))
56+
return std::format("{} … {}", s.minimum ? number_text(*s.minimum) : "-∞", s.maximum ? number_text(*s.maximum) : "∞");
57+
return s.type == "array" ? "array of strings" : s.type;
5858
}
5959
} // namespace
6060

61-
Checked check(const generated::Setting& s, const Json& v) {
61+
Checked check(const spec::Setting& s, const Json& v) {
6262
Checked out{default_of(s), std::nullopt, expectation_of(s)};
6363
auto reject = [&](std::string why) { out.reason = std::move(why); return out; };
6464
if (v.is_null()) return out;
@@ -67,14 +67,11 @@ Checked check(const generated::Setting& s, const Json& v) {
6767
} else if (s.type == "number") {
6868
if (!v.is_number()) return reject("type");
6969
const double d = v.get<double>();
70-
if (!s.minimum.empty() && d < std::stod(std::string(s.minimum))) return reject("minimum");
71-
if (!s.maximum.empty() && d > std::stod(std::string(s.maximum))) return reject("maximum");
70+
if (s.minimum && d < *s.minimum) return reject("minimum");
71+
if (s.maximum && d > *s.maximum) return reject("maximum");
7272
} else if (s.type == "string") {
7373
if (!v.is_string()) return reject("type");
74-
if (!s.enumJson.empty()) {
75-
auto e = Json::parse(s.enumJson, nullptr, false);
76-
if (std::ranges::find(e, v) == e.end()) return reject("enum");
77-
}
74+
if (s.allowed.is_array() && std::ranges::find(s.allowed, v) == s.allowed.end()) return reject("enum");
7875
} else if (s.type == "array") {
7976
if (!v.is_array()) return reject("type");
8077
for (const auto& item : v) if (!item.is_string()) return reject("items");
@@ -85,44 +82,42 @@ Checked check(const generated::Setting& s, const Json& v) {
8582

8683
Json resolve(const Json& given) {
8784
Json values = Json::object(), rejected = Json::array(), renames = Json::array(), deprecated = Json::array();
88-
for (const auto& s : generated::kSettings) {
89-
Json v = given.is_object() && given.contains(std::string(s.key)) ? given[std::string(s.key)] : Json(nullptr);
85+
for (const auto& s : spec::settings()) {
86+
Json v = given.is_object() && given.contains(s.key) ? given[s.key] : Json(nullptr);
9087
if (v.is_null()) {
9188
// An earlier spelling of this setting still in the user's file.
92-
auto aliases = Json::parse(s.aliasesJson, nullptr, false);
93-
if (!aliases.is_discarded())
94-
for (const auto& a : aliases)
95-
if (a.is_string() && given.is_object() && given.contains(a.get<std::string>())) {
96-
v = given[a.get<std::string>()];
97-
renames.push_back(Json{{"from", a}, {"to", s.key}, {"value", v}});
98-
break;
99-
}
89+
for (const auto& a : s.aliases)
90+
if (given.is_object() && given.contains(a)) {
91+
v = given[a];
92+
renames.push_back(Json{{"from", a}, {"to", s.key}, {"value", v}});
93+
break;
94+
}
10095
}
10196
auto c = check(s, v);
10297
if (c.reason) rejected.push_back(Json{{"key", s.key}, {"reason", *c.reason}, {"expectation", c.expectation},
10398
{"given", v}});
10499
if (s.deprecated && !v.is_null()) deprecated.push_back(s.key);
105-
values[std::string(s.key)] = c.value;
100+
values[s.key] = c.value;
106101
}
107102
return Json{{"values", values}, {"rejected", rejected}, {"renames", renames}, {"deprecated", deprecated}};
108103
}
109104

110105
Json registry() {
111106
Json entries = Json::array();
112-
for (const auto& s : generated::kSettings) {
107+
for (const auto& s : spec::settings()) {
113108
Json e{{"key", s.key}, {"type", s.type}, {"default", default_of(s)}, {"group", s.group},
114109
{"scope", s.scope}, {"applies", s.applies}, {"tier", s.tier}, {"deprecated", s.deprecated},
115110
{"title", s.title}, {"description", s.description}, {"expectation", expectation_of(s)}};
116-
if (!s.enumJson.empty()) e["enum"] = Json::parse(s.enumJson);
117-
if (!s.minimum.empty()) e["minimum"] = std::stod(std::string(s.minimum));
118-
if (!s.maximum.empty()) e["maximum"] = std::stod(std::string(s.maximum));
111+
if (s.allowed.is_array()) e["enum"] = s.allowed;
112+
if (s.minimum) e["minimum"] = *s.minimum;
113+
if (s.maximum) e["maximum"] = *s.maximum;
119114
entries.push_back(std::move(e));
120115
}
121-
return Json{{"settings", entries}, {"presets", Json::parse(generated::kPresetsJson)["presets"]}};
116+
return Json{{"settings", entries}, {"presets", spec::presets()}};
122117
}
123118

124119
std::optional<Json> preset(std::string_view id) {
125-
for (const auto& p : Json::parse(generated::kPresetsJson)["presets"])
120+
for (const auto& p : spec::presets())
126121
if (p.value("id", "") == id) return p;
127122
return std::nullopt;
128123
}

‎src/spec.cppm‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// spec/ as the backend reads it: the settings registry, the presets, the runtime strings, and
2+
// mcpp's manifest keys and build-program directives, parsed once from the JSON that build.mcpp
3+
// carried into `mcppide.generated.spec`.
4+
export module mcppide.spec;
5+
6+
import std;
7+
import nlohmann.json;
8+
import mcppide.generated.spec;
9+
10+
export namespace mcppide::spec {
11+
12+
using Json = nlohmann::json;
13+
14+
struct Setting {
15+
std::string key;
16+
std::string type; // boolean | string | number | array
17+
Json defaultValue;
18+
Json allowed; // the allowed values, an array, or null
19+
std::optional<double> minimum, maximum;
20+
std::string group, scope, applies, tier;
21+
bool deprecated = false;
22+
std::vector<std::string> aliases; // earlier keys of this setting
23+
std::string title, description;
24+
};
25+
26+
const std::vector<Setting>& settings();
27+
const Json& presets(); // spec/presets.json's "presets"
28+
const std::map<std::string, std::string, std::less<>>& runtime_zh_cn();
29+
const Json& toml_schema();
30+
const Json& buildscript_api();
31+
32+
} // namespace mcppide::spec
33+
34+
namespace mcppide::spec {
35+
36+
const std::vector<Setting>& settings() {
37+
static const std::vector<Setting> all = [] {
38+
std::vector<Setting> out;
39+
const auto doc = Json::parse(generated::kSettingsJson);
40+
for (const auto& s : doc.at("settings")) {
41+
Setting v;
42+
v.key = s.value("key", "");
43+
v.type = s.value("type", "");
44+
v.defaultValue = s.contains("default") ? s["default"] : Json(nullptr);
45+
v.allowed = s.contains("enum") ? s["enum"] : Json(nullptr);
46+
if (s.contains("minimum") && s["minimum"].is_number()) v.minimum = s["minimum"].get<double>();
47+
if (s.contains("maximum") && s["maximum"].is_number()) v.maximum = s["maximum"].get<double>();
48+
v.group = s.value("group", "");
49+
v.scope = s.value("scope", "");
50+
v.applies = s.value("applies", "");
51+
v.tier = s.value("tier", "");
52+
v.deprecated = s.value("deprecated", false);
53+
if (s.contains("aliases") && s["aliases"].is_array())
54+
for (const auto& a : s["aliases"]) if (a.is_string()) v.aliases.push_back(a.get<std::string>());
55+
v.title = s.value("title", "");
56+
v.description = s.value("description", "");
57+
out.push_back(std::move(v));
58+
}
59+
return out;
60+
}();
61+
return all;
62+
}
63+
64+
const Json& presets() {
65+
static const Json p = Json::parse(generated::kPresetsJson).value("presets", Json::array());
66+
return p;
67+
}
68+
69+
const std::map<std::string, std::string, std::less<>>& runtime_zh_cn() {
70+
static const auto m = [] {
71+
std::map<std::string, std::string, std::less<>> out;
72+
const auto doc = Json::parse(generated::kRuntimeZhCnJson);
73+
for (auto it = doc.begin(); it != doc.end(); ++it)
74+
if (it->is_string()) out.emplace(it.key(), it->get<std::string>());
75+
return out;
76+
}();
77+
return m;
78+
}
79+
80+
const Json& toml_schema() {
81+
static const Json s = Json::parse(generated::kTomlSchemaJson);
82+
return s;
83+
}
84+
85+
const Json& buildscript_api() {
86+
static const Json a = Json::parse(generated::kBuildScriptApiJson);
87+
return a;
88+
}
89+
90+
} // namespace mcppide::spec

0 commit comments

Comments
 (0)