-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
417 lines (392 loc) · 16.3 KB
/
parser.cpp
File metadata and controls
417 lines (392 loc) · 16.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "../theatre_file.hpp"
#include "things/resource_database.hpp"
#include "things/thing_factory.hpp"
#include "console/console.hpp"
#include "thirdparty/frozen/map.h"
#include "thirdparty/frozen/string.h"
// These preprocessor definitions are used on unknown/invalid types to approximate their closest inherited type
#define ACTOR_VAR_NAMES "Position", "Origin", "Scale", "Size", "OuuughImSoBigAndRound", "RotationDegrees", "Rotation", "RotationRadians"
#define ACTOR3D_VAR_NAMES "EulerDegrees", "Euler", "EulerRadians", "Quaternion"
#define RESOURCE_VAR_NAMES "File", "Data", "Path", "Font", "Model", "Image", "Image0", "Image1", "DiffuseTexture", "SpecularTexture", "FullBright"
static constexpr frozen::map<frozen::string, frozen::string, 2>
sNamedNumbers{
{"ALL", "0b111111111111111111111111111111"},
{"NONE", "0"},
};
static constexpr const char* cKeywordDeclare{"declare"};
static constexpr char
cDelimiterTheatreName {'@'},
cDelimiterTheatreIndex {'#'},
cDelimiterNamedNumber {'#'},
cDelimiterStartSandwich {':'},
cDelimiterEnterDefinition {'{'},
cDelimiterExitDefinition {'}'},
cDelimiterEnterNumber {'['},
cDelimiterExitNumber {']'},
cDelimiterEnterEnum {'('},
cDelimiterEnterReference {'<'},
cDelimiterExitReference {'>'},
cDelimiterWeakString {'"'},
cDelimiterStrongString {'\''};
using namespace TheatreFile;
enum Comment { SINGLE_LINE, MULTI_LINE, NO_COMMENT }; // yes, `NO_COMMENT` is a pun
static std::map<std::string, PID> s_ThingNameToTypeID{};
static void s_SetupNamesAndTypes(Farg<TokenArray>, TheatreFile::TheatreData&);
static void s_ParseDeclaration(size_t&, Farg<TokenArray>, TheatreFile::TheatreData&);
static TheatreFile::ThingData s_ParseThing(size_t&, Farg<TokenArray>, TheatreFile::TheatreData&);
static bool s_CheckIfComment(Comment&, Farg<Token>);
Error TheatreFile::Parse(Farg<TokenArray> inTokens, Shared<TheatreData> outData)
{
ThingData thing_dat{};
ThingVariable thing_var{};
Comment in_comment{NO_COMMENT};
s_ThingNameToTypeID.clear();
s_SetupNamesAndTypes(inTokens, *outData);
if(Console::GetVariable("TheatreFile.Parser.print_declarations")->int_value)
{
print_debug("Parser Forward Declarations:");
for(FAUTO [name, type] : outData->type_declarations)
{ debug_print("\t[ {} == {} ]", name, type); }
}
if(Console::GetVariable("TheatreFile.Parser.print_name_type_map")->int_value)
{
print_debug("Parser Name to TypeID Map:");
for(FAUTO [name, type] : s_ThingNameToTypeID)
{ debug_print("\t[ {} == {} ]", name, type.name()); }
}
for(size_t i{0}; i < inTokens.size(); ++i)
{
Farg<Token> token{inTokens.at(i)};
if(s_CheckIfComment(in_comment, token))
{ continue; }
else if(token.category == TokenName::Separator
and i+1 < inTokens.size())
{
if(token.token[0] == cDelimiterTheatreName)
{ outData->name = inTokens.at(++i).token; }
else if(token.token[0] == cDelimiterTheatreIndex)
{
try { outData->index = std::stoi(inTokens.at(++i).token); }
catch(std::invalid_argument const& e) {}
}
}
else if(token.category == TokenName::Keyword and not token.token.compare(cKeywordDeclare))
{
while(++i < inTokens.size())
{
FAUTO _token{inTokens.at(i)};
if(_token.category == TokenName::Whitespace and _token.token[0] == '\n')
{ break; }
}
}
else if(token.category == TokenName::Identifier)
{ s_ParseThing(i, inTokens, *outData); }
}
return OK;
}
void s_SetupNamesAndTypes(Farg<TokenArray> inTokens, TheatreFile::TheatreData& outData)
{
std::string _type{};
Comment in_comment{NO_COMMENT};
bool _skip_definition{false}, _in_sandwich{false};
for(size_t i{0}; i < inTokens.size(); ++i)
{
Token _token{inTokens.at(i)};
if(s_CheckIfComment(in_comment, _token))
{ continue; }
else if(_token.token[0] == cDelimiterStartSandwich)
{ _in_sandwich = true; }
else if(_token.token[0] == cDelimiterEnterDefinition)
{ _skip_definition = true; }
else if(_token.token[0] == cDelimiterExitDefinition)
{ _skip_definition = false; }
else if(_skip_definition and not _in_sandwich)
{ continue; }
else if(_token.category == TokenName::Keyword and not _token.token.compare(cKeywordDeclare))
{ s_ParseDeclaration(i, inTokens, outData); }
else if(_token.category == TokenName::Identifier)
{
if(ThingFactory::IsThing(_token.token))
{ _type = _token.token; }
else if(auto found_it{outData.type_declarations.find(_token.token)};
found_it != outData.type_declarations.end())
{ _type = _token.token; }
else
{ continue; }
while(++i < inTokens.size())
{
_token = inTokens.at(i);
if(_token.category == TokenName::Identifier)
{
s_ThingNameToTypeID[_token.token] = _type;
_type.clear();
break;
}
}
_in_sandwich = false;
_skip_definition = true;
continue;
}
}
}
void s_ParseDeclaration(size_t& ioIndex, Farg<TokenArray> inTokens, TheatreFile::TheatreData& outData)
{
std::string new_type{}, base_type{};
while(ioIndex < inTokens.size() and (new_type.empty() or base_type.empty()))
{
FAUTO token{inTokens.at(++ioIndex)};
if(token.category == TokenName::Identifier)
{
if(new_type.empty())
{ new_type = token.token; }
else if(base_type.empty())
{ base_type = token.token; }
}
}
outData.type_declarations[new_type] = base_type;
print_error_enum(ThingFactory::AddThingDeclaration(new_type, base_type, true));
}
bool s_CheckIfComment(Comment& ioComment, Farg<Token> inToken)
{
switch(inToken.category)
{
case TokenName::SinglelineComment:
if(ioComment == NO_COMMENT)
{ ioComment = SINGLE_LINE; }
break;
case TokenName::MultilineComment:
if(ioComment == NO_COMMENT)
{ ioComment = MULTI_LINE; }
else if(ioComment == MULTI_LINE)
{ ioComment = NO_COMMENT; }
break;
case TokenName::Whitespace:
if(inToken.token[0] == '\n' and ioComment == SINGLE_LINE)
{ ioComment = NO_COMMENT; }
break;
default:
break;
}
return ioComment != NO_COMMENT;
}
TheatreFile::ThingData s_ParseThing(size_t& ioIndex,
Farg<TokenArray> inTokens,
TheatreFile::TheatreData& outData)
{
TheatreFile::ThingData thing_data{};
ThingVariable thing_var{};
Comment in_comment{NO_COMMENT};
bool in_literal{false},
in_string{false};
for(; ioIndex < inTokens.size(); ++ioIndex)
{
FAUTO token{inTokens.at(ioIndex)};
if(s_CheckIfComment(in_comment, token))
{ continue; }
else if((in_literal and token.token[0] != cDelimiterExitNumber)
or (in_string
and token.token[0] != cDelimiterStrongString
and token.token[0] != cDelimiterWeakString))
{ thing_var.value += token.token; continue; }
switch(token.category)
{
case TokenName::SinglelineComment:
[[fallthrough]];
case TokenName::MultilineComment:
[[fallthrough]];
case TokenName::None:
break;
case TokenName::Literal:
if(!in_literal)
{
thing_var.type = ThingVarType::String;
thing_var.value = token.token.substr(1, token.token.size() - 2);
}
else
{ thing_var.value += token.token; }
break;
case TokenName::Operator:
if(token.token[0] == cDelimiterStartSandwich)
{
Token next_token{};
bool exit_for{false};
#pragma message("TODO: make this less shit")
++ioIndex;
for(; ioIndex < inTokens.size() and !exit_for; ++ioIndex)
{
next_token = inTokens.at(ioIndex);
if(next_token.category != TokenName::Keyword and
next_token.category != TokenName::Identifier)
{ continue; }
thing_var.name = next_token.token;
for(size_t i{ioIndex+1}; i < inTokens.size() and !exit_for; ++i)
{
next_token = inTokens.at(i);
if(next_token.category != TokenName::Identifier)
{ continue; }
thing_var.value = next_token.token;
thing_var.thing_type = thing_var.name;
exit_for = true;
}
--ioIndex;
}
thing_data.children_variables.push_back(thing_var);
thing_var.clear();
s_ParseThing(ioIndex, inTokens, outData);
continue;
}
break;
case TokenName::Whitespace:
if(!thing_var.value.empty()
and !in_string
and !in_literal
and token.token[0] == '\n')
{
if(thing_var.type == ThingVarType::Child)
{ thing_data.children_variables.push_back(thing_var); }
else if(thing_var.type == ThingVarType::Parent)
{ thing_data.parent_variable = thing_var; }
else
{ thing_data.variables.push_back(thing_var); }
thing_var.clear();
}
break;
case TokenName::Separator:
switch(token.token[0])
{
case cDelimiterExitReference:
{
std::string _name{thing_var.value};
if(thing_var.type != ThingVarType::ID and thing_var.name.compare("Child"))
{ _name = thing_var.name; }
if(auto found_it{s_ThingNameToTypeID.find(_name)}; found_it != s_ThingNameToTypeID.end())
{ thing_var.thing_type = found_it->second; }
else
{ thing_var.thing_type = ResourceDatabase::TypeOf(_name); }
break;
}
case cDelimiterEnterReference:
if(thing_var.type == ThingVarType::None)
{ thing_var.type = ThingVarType::ID; }
break;
case cDelimiterEnterEnum:
thing_var.type = ThingVarType::Enum;
break;
case cDelimiterEnterNumber:
thing_var.type = ThingVarType::Number;
if(ioIndex + 2 < inTokens.size())
{
FAUTO _first_token{inTokens.at(ioIndex + 1)};
FAUTO _second_token{inTokens.at(ioIndex + 2)};
if(_first_token.category == TokenName::Separator
and _first_token.token[0] == cDelimiterNamedNumber)
{
++ioIndex;
if(auto found_it{sNamedNumbers.find(frozen::string{_second_token.token})};
found_it != sNamedNumbers.end())
{
++ioIndex;
thing_var.value = found_it->second.data();
}
break;
}
}
in_literal = true;
break;
case cDelimiterExitNumber:
in_literal = false;
if(!thing_var.value.empty())
{
auto _val{thing_var.value};
// https://stackoverflow.com/a/313990
std::transform(_val.begin(), _val.end(), _val.begin(),
[](unsigned char character){ return std::tolower(character); });
if(!_val.compare("false") || !_val.compare("true"))
{
thing_var.type = ThingVarType::Bool;
thing_var.value = _val;
}
}
break;
case cDelimiterWeakString:
case cDelimiterStrongString:
in_string = !in_string;
break;
case cDelimiterExitDefinition:
if(not thing_var.invalid())
{
if(thing_var.type == ThingVarType::Child)
{ thing_data.children_variables.push_back(thing_var); }
else if(thing_var.type == ThingVarType::Parent)
{ thing_data.parent_variable = thing_var; }
else
{ thing_data.variables.push_back(thing_var); }
}
// This has to be done here and not when initially assigning the type PID, because it uses the
// unknown Thing's variables to approximate its base type. It's not super accurate, but since this
// is an error, anyways, I only really focus on a few more "important" types.
if(not ThingFactory::IsThing(thing_data.type))
{
thing_data.type = (thing_data.children_variables.empty() and
thing_data.parent_variable.invalid())
? ThingType::Invalid
: ThingType::Thinker;
if(glm::vec2 actor2d_test{};
thing_data.get_variable(actor2d_test, ACTOR_VAR_NAMES) == OK)
{ thing_data.type = ThingType::Actor2D; }
else if(glm::vec4 actor3d_test{};
thing_data.get_variable(actor3d_test, ACTOR3D_VAR_NAMES, ACTOR_VAR_NAMES) == OK)
{ thing_data.type = ThingType::Actor3D; }
else if(glm::vec2 viewport_test{};
thing_data.get_variable(viewport_test, "ViewportSize", "ContentSize") == OK)
{ thing_data.type = ThingType::Viewport; }
else if(std::string resource_test{};
thing_data.get_variable(resource_test, RESOURCE_VAR_NAMES) == OK)
{ thing_data.type = ThingType::Resource; }
}
outData.push_back(thing_data);
return thing_data;
default:
break;
}
break;
case TokenName::Keyword:
if(!token.token.compare("Child"))
{ thing_var.type = ThingVarType::Child; thing_var.name = token.token; }
else if(!token.token.compare("Parent"))
{ thing_var.type = ThingVarType::Parent; thing_var.name = token.token; }
break;
case TokenName::Identifier:
if(thing_data.type.invalid())
{
if(ThingFactory::IsThing(token.token))
{ thing_data.type = token.token; }
else if(auto found_it{outData.type_declarations.find(token.token)};
found_it != outData.type_declarations.end())
{
thing_data.type = found_it->second;
print_warning("ThingType '{}' wasn't loaded, but a forward declaration was found. A '{}' will be made in its place",
found_it->first, found_it->second);
}
else
{ thing_data.type = token.token; }
}
else if(thing_data.name.empty())
{
thing_data.name = token.token;
s_ThingNameToTypeID[thing_data.name] = thing_data.type;
}
else if(thing_var.name.empty())
{ thing_var.name = token.token; }
else if(thing_var.value.empty())
{ thing_var.value = token.token; }
break;
default:
break;
}
}
return thing_data;
}
#undef ACTOR_VAR_NAMES
#undef ACTOR3D_VAR_NAMES
#undef RESOURCE_VAR_NAMES