-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeatre_file.cpp
More file actions
106 lines (94 loc) · 3.42 KB
/
theatre_file.cpp
File metadata and controls
106 lines (94 loc) · 3.42 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
#include "./theatre_file.hpp"
#include "things/thing_data.hpp"
#include "things/thing_factory.hpp"
using namespace TheatreFile;
bool TheatreFile::gDebugPrintLexerLogs{false},
TheatreFile::gDebugPrintParserLogs{false},
TheatreFile::gDebugDontPrintWhitespaceInLexerLogs{false},
TheatreFile::gDebugDontPrintCommentsInLexerLogs{false};
static void sDebugPrintLexerLogs(Farg<TokenArray>);
void TheatreData::sort_by_priority()
{
std::sort(begin(),
end(),
[](Farg<ThingData> lhs, Farg<ThingData> rhs)
{ return ThingFactory::GetPriority(lhs.type) > ThingFactory::GetPriority(rhs.type); });
}
std::string TheatreData::get_log() const
{
std::string _out{std::format("TheatreData [{}, {}]\n", name, index)};
for(FAUTO _data : data)
{ _out += std::format("{}\n", _data.get_log()); }
return _out;
}
std::string TheatreData::get_parsable_string() const
{
std::string _out{std::format("@{}#{}\n\n", name, index)};
for(FAUTO [type, super] : type_declarations)
{ _out += std::format("declare {} inherits {}\n", type, super); }
for(FAUTO _data : data)
{ _out += std::format("{}\n", _data.get_parsable_string()); }
return _out;
}
Error TheatreFile::Load(Sarg inFilePath, Shared<TheatreData> outData)
{
FileData theatre_file{};
if(not theatre_file.LoadFile(inFilePath))
{ return ERR_FILE_LOAD; }
return Load(theatre_file, outData);
}
Error TheatreFile::Load(Farg<FileData> inFileData, Shared<TheatreData> outData)
{
TokenArray tokens{};
Error lexer_code{Lex(inFileData, tokens)};
outData->file_path = FileSystem::GetAbsolute(inFileData.filepath());
if(gDebugPrintLexerLogs)
{ sDebugPrintLexerLogs(tokens); }
if(!print_error_enum(lexer_code))
{ return ERR_THEATRE_LEXER; }
Error parser_code{Parse(tokens, outData)};
if(gDebugPrintParserLogs)
{ print_debug("Parser Output\n{}\n", outData->get_log()); }
if(!print_error_enum(parser_code))
{ return ERR_THEATRE_PARSER; }
return OK;
}
void sDebugPrintLexerLogs(Farg<TokenArray> inTokens)
{
enum class Comment : int { SINGLE, MULTI, NO_COMMENT };
Comment comment{Comment::NO_COMMENT};
print_debug("Lexer Output");
for(FAUTO token : inTokens)
{
if(token.category == TheatreFile::TokenName::Whitespace)
{
if(token.token[0] == '\n' and comment == Comment::SINGLE)
{ comment = Comment::NO_COMMENT; }
if(gDebugDontPrintWhitespaceInLexerLogs)
{ continue; }
}
if(token.category == TheatreFile::TokenName::SinglelineComment)
{
if(comment == Comment::NO_COMMENT)
{ comment = Comment::SINGLE; }
if(gDebugDontPrintCommentsInLexerLogs)
{ continue; }
}
if(token.category == TheatreFile::TokenName::MultilineComment)
{
if(comment == Comment::NO_COMMENT)
{ comment = Comment::MULTI; }
else if(comment == Comment::MULTI)
{ comment = Comment::NO_COMMENT; }
if(gDebugDontPrintCommentsInLexerLogs)
{ continue; }
}
if(gDebugDontPrintCommentsInLexerLogs and comment != Comment::NO_COMMENT)
{ continue; }
debug_print("\tToken [{}, '{}']",
EnumRegistry::GetEnumName(token.category),
(token.token[0] == '\n')
? "\\n"
: token.token);
}
}