1
0
mirror of synced 2024-11-12 10:10:53 +01:00

patterns: Fixed comments behind pre-processor defines

This commit is contained in:
WerWolv 2022-04-07 13:15:07 +02:00
parent 942a4e9616
commit 74a5c974e6
2 changed files with 30 additions and 18 deletions

View File

@ -19,7 +19,7 @@ namespace hex::pl {
public: public:
Preprocessor() = default; Preprocessor() = default;
std::optional<std::string> preprocess(const std::string &code, bool initialRun = true); std::optional<std::string> preprocess(std::string code, bool initialRun = true);
void addPragmaHandler(const std::string &pragmaType, const std::function<bool(const std::string &)> &function); void addPragmaHandler(const std::string &pragmaType, const std::function<bool(const std::string &)> &function);
void removePragmaHandler(const std::string &pragmaType); void removePragmaHandler(const std::string &pragmaType);

View File

@ -8,7 +8,7 @@
namespace hex::pl { namespace hex::pl {
std::optional<std::string> Preprocessor::preprocess(const std::string &code, bool initialRun) { std::optional<std::string> Preprocessor::preprocess(std::string code, bool initialRun) {
u32 offset = 0; u32 offset = 0;
u32 lineNumber = 1; u32 lineNumber = 1;
bool isInString = false; bool isInString = false;
@ -22,6 +22,34 @@ namespace hex::pl {
output.reserve(code.length()); output.reserve(code.length());
try { try {
while (offset < code.length()) {
if (code.substr(offset, 2) == "//") {
while (code[offset] != '\n' && offset < code.length())
offset += 1;
} else if (code.substr(offset, 2) == "/*") {
while (code.substr(offset, 2) != "*/" && offset < code.length()) {
if (code[offset] == '\n') {
output += '\n';
lineNumber++;
}
offset += 1;
}
offset += 2;
if (offset >= code.length())
throwPreprocessorError("unterminated comment", lineNumber - 1);
} else {
output += code[offset];
offset++;
}
}
offset = 0;
code = output;
output.clear();
output.reserve(code.size());
bool startOfLine = true; bool startOfLine = true;
while (offset < code.length()) { while (offset < code.length()) {
if (offset > 0 && code[offset - 1] != '\\' && code[offset] == '\"') if (offset > 0 && code[offset - 1] != '\\' && code[offset] == '\"')
@ -179,22 +207,6 @@ namespace hex::pl {
this->m_pragmas.emplace(pragmaKey, pragmaValue, lineNumber); this->m_pragmas.emplace(pragmaKey, pragmaValue, lineNumber);
} else } else
throwPreprocessorError("unknown preprocessor directive", lineNumber); throwPreprocessorError("unknown preprocessor directive", lineNumber);
} else if (code.substr(offset, 2) == "//") {
while (code[offset] != '\n' && offset < code.length())
offset += 1;
} else if (code.substr(offset, 2) == "/*") {
while (code.substr(offset, 2) != "*/" && offset < code.length()) {
if (code[offset] == '\n') {
output += '\n';
lineNumber++;
}
offset += 1;
}
offset += 2;
if (offset >= code.length())
throwPreprocessorError("unterminated comment", lineNumber - 1);
} }
if (code[offset] == '\n') { if (code[offset] == '\n') {