forked from mmbednarek/minecpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
289 lines (261 loc) · 9.75 KB
/
Main.cpp
File metadata and controls
289 lines (261 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "generator/NbtGenerator.h"
#include "generator/NetworkGenerator.h"
#include "IGenerator.h"
#include "Lexer.h"
#include "Parser.h"
#include "SymbolTable.h"
#include <boost/program_options.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
namespace opts = boost::program_options;
using Path = std::filesystem::path;
using minecpp::tool::schema_compiler::Document;
using minecpp::tool::schema_compiler::GeneratorError;
using minecpp::tool::schema_compiler::Symbol;
using minecpp::tool::schema_compiler::TypeClass;
using minecpp::tool::schema_compiler::generator::NbtGenerator;
using minecpp::tool::schema_compiler::generator::NbtGeneratorFactory;
using minecpp::tool::schema_compiler::generator::NetworkGeneratorFactory;
void register_core_types(minecpp::tool::schema_compiler::SymbolTable &table);
void list_schema_files(const Path &path, std::vector<Path> &out);
void ensure_directory(const std::string &path);
constexpr auto g_version = "0.1.0";
int main(int argc, char **argv)
{
opts::options_description desc("schema_compiler");
// clang-format off
desc.add_options()
("help", "print help information")
("version,v", "print version")
("input-path,i", opts::value<std::string>(), "specify schema input path")
("include-path,I", opts::value<std::string>(), "specify header include path")
("source-output,s", opts::value<std::string>(), "specify source output path")
("header-output,h", opts::value<std::string>(), "specify header output path");
// clang-format on
opts::variables_map values;
opts::store(opts::command_line_parser(argc, argv).options(desc).run(), values);
if (auto help = values["help"]; !help.empty()) {
std::cerr << desc;
return EXIT_SUCCESS;
}
if (auto version = values["version"]; !version.empty()) {
std::cerr << g_version << '\n';
return EXIT_SUCCESS;
}
if (not values.contains("input-path")) {
std::cerr << "schema_compiler: missing input path" << '\n';
std::cerr << desc;
return EXIT_FAILURE;
}
if (not values.contains("header-output")) {
std::cerr << "schema_compiler: missing header output path" << '\n';
std::cerr << desc;
return EXIT_FAILURE;
}
if (not values.contains("source-output")) {
std::cerr << "schema_compiler: missing source output path" << '\n';
std::cerr << desc;
return EXIT_FAILURE;
}
if (not values.contains("include-path")) {
std::cerr << "schema_compiler: missing include path" << '\n';
std::cerr << desc;
return EXIT_FAILURE;
}
const std::filesystem::path api{values["input-path"].as<std::string>()};
std::vector<Path> files;
list_schema_files(api, files);
minecpp::tool::schema_compiler::SymbolTable table;
register_core_types(table);
std::vector<std::pair<std::string, Document>> documents;
for (const auto &file : files) {
std::ifstream stream(file);
if (not stream.is_open())
return 1;
/**
* Internal parser uses std::basic_istream<CharT,Traits>::readsome to read from the stream
* which is highly implementation specific and was returning empty on LLVM18 in OSX.
*
* Therefore we create a stringstream to read from the file into memory, where readsome()
* function will then return non-empty, as the contents are in memory.
*
* https://en.cppreference.com/w/cpp/io/basic_istream/readsome
*/
std::stringstream in_memory_stream;
in_memory_stream << stream.rdbuf();
in_memory_stream.seekg(0);
auto tokens = minecpp::tool::schema_compiler::lex_input(in_memory_stream);
minecpp::tool::schema_compiler::Parser parser(tokens);
try {
auto document = parser.parse_document();
table.read_document(file.string(), document);
documents.emplace_back(file.string(), std::move(document));
} catch (minecpp::lexer::ParserError &parser_err) {
std::cerr << "schema_compiler: failed to parse document " << file.string() << ": \n\t";
std::cerr << parser_err.what() << '\n';
return EXIT_FAILURE;
} catch (std::runtime_error &runtime_error) {
std::cerr << "schema_compiler: failure analysing document " << file.string() << ": \n\t";
std::cerr << runtime_error.what() << '\n';
return EXIT_FAILURE;
}
}
for (const auto &[filename, document] : documents) {
table.read_document_for_aliases(filename, document);
}
std::map<std::string, std::unique_ptr<minecpp::tool::schema_compiler::IGeneratorFactory>> generators;
generators.emplace("nbt", std::make_unique<NbtGeneratorFactory>());
generators.emplace("net", std::make_unique<NetworkGeneratorFactory>());
for (const auto &[filename, document] : documents) {
minecpp::tool::schema_compiler::PathInfo path_info{
.root_origin_dir{fmt::format("{}/", values["input-path"].as<std::string>())},
.root_source_target_dir{values["source-output"].as<std::string>()},
.root_header_target_dir{values["header-output"].as<std::string>()},
.origin_document_path{filename},
.root_include_path{values["include-path"].as<std::string>()},
};
try {
auto generator_name{document.generator()};
if (not generators.contains(generator_name)) {
std::cerr << "schema_generator: error generating code for " << filename
<< ", no such generator \"" << generator_name << "\"\n";
return 1;
}
auto generator = generators[generator_name]->create_generator(document, table, path_info);
{
std::cerr << "schema_compiler: writing header to " << generator->target_header_path() << '\n';
ensure_directory(generator->target_header_path());
std::ofstream header_output{generator->target_header_path()};
assert(header_output.is_open());
generator->generate_header(header_output);
}
{
std::cerr << "schema_compiler: writing source to " << generator->target_source_path() << '\n';
ensure_directory(generator->target_source_path());
std::ofstream source_output{generator->target_source_path()};
assert(source_output.is_open());
generator->generate_source(source_output);
}
} catch (GeneratorError &err) {
std::cerr << "schema_compiler: failed to compile source " << filename << ":\n\t" << err.what()
<< '\n';
return 1;
}
}
return 0;
}
void register_core_types(minecpp::tool::schema_compiler::SymbolTable &table)
{
table.register_symbol(Symbol{
.type_class = TypeClass::Int8,
.name{"int8"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Int16,
.name{"int16"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Int32,
.name{"int32"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Int64,
.name{"int64"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UInt8,
.name{"uint8"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UInt16,
.name{"uint16"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UInt32,
.name{"uint32"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UInt64,
.name{"uint64"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Float32,
.name{"float32"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Float64,
.name{"float64"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::String,
.name{"string"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Map,
.name{"map"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::List,
.name{"list"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Optional,
.name{"optional"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Variant,
.name{"variant"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Varint,
.name{"varint"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Varlong,
.name{"varlong"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UnsignedVarint,
.name{"uvarint"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::UnsignedVarlong,
.name{"uvarlong"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Bool,
.name{"bool"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Uuid,
.name{"uuid"},
});
table.register_symbol(Symbol{
.type_class = TypeClass::Extern,
.name{"extern"},
});
table.register_symbol(Symbol{.type_class = TypeClass::NbtCompoundContent,
.name{"CompoundContent"},
.package{"minecpp.nbt"}});
}
void list_schema_files(const Path &path, std::vector<Path> &out)
{
for (const auto &file : std::filesystem::directory_iterator{path}) {
if (file.is_directory()) {
list_schema_files(file.path(), out);
continue;
}
if (file.path().string().ends_with(".schema")) {
out.push_back(file.path());
}
}
}
void ensure_directory(const std::string &path)
{
auto res = path.find_last_of('/');
if (res == std::string::npos)
return;
std::filesystem::create_directories(std::filesystem::path{path.substr(0, res)});
}