patterns: Fixed eval_depth default and preprocessor errors
This commit is contained in:
parent
2326e090af
commit
f34b946ea9
@ -48,7 +48,7 @@ namespace hex::lang {
|
|||||||
std::vector<std::vector<PatternData*>*> m_currMembers;
|
std::vector<std::vector<PatternData*>*> m_currMembers;
|
||||||
LogConsole m_console;
|
LogConsole m_console;
|
||||||
|
|
||||||
u32 m_recursionLimit = 16;
|
u32 m_recursionLimit;
|
||||||
u32 m_currRecursionDepth;
|
u32 m_currRecursionDepth;
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,9 +39,9 @@ namespace hex::lang {
|
|||||||
Validator *m_validator;
|
Validator *m_validator;
|
||||||
Evaluator *m_evaluator;
|
Evaluator *m_evaluator;
|
||||||
|
|
||||||
prv::Provider *m_provider;
|
prv::Provider *m_provider = nullptr;
|
||||||
std::endian m_defaultEndian;
|
std::endian m_defaultEndian = std::endian::native;
|
||||||
u32 m_recursionLimit;
|
u32 m_recursionLimit = 32;
|
||||||
|
|
||||||
std::optional<std::pair<u32, std::string>> m_currError;
|
std::optional<std::pair<u32, std::string>> m_currError;
|
||||||
};
|
};
|
||||||
|
@ -32,8 +32,8 @@ namespace hex::lang {
|
|||||||
|
|
||||||
std::unordered_map<std::string, std::function<bool(std::string)>> m_pragmaHandlers;
|
std::unordered_map<std::string, std::function<bool(std::string)>> m_pragmaHandlers;
|
||||||
|
|
||||||
std::set<std::pair<std::string, std::string>> m_defines;
|
std::set<std::tuple<std::string, std::string, u32>> m_defines;
|
||||||
std::set<std::pair<std::string, std::string>> m_pragmas;
|
std::set<std::tuple<std::string, std::string, u32>> m_pragmas;
|
||||||
|
|
||||||
std::pair<u32, std::string> m_error;
|
std::pair<u32, std::string> m_error;
|
||||||
};
|
};
|
||||||
|
@ -439,11 +439,17 @@ namespace hex::lang {
|
|||||||
this->m_currMembers.push_back(&memberPatterns);
|
this->m_currMembers.push_back(&memberPatterns);
|
||||||
SCOPE_EXIT( this->m_currMembers.pop_back(); );
|
SCOPE_EXIT( this->m_currMembers.pop_back(); );
|
||||||
|
|
||||||
|
this->m_currRecursionDepth++;
|
||||||
|
if (this->m_currRecursionDepth > this->m_recursionLimit)
|
||||||
|
this->getConsole().abortEvaluation(hex::format("evaluation depth exceeds maximum of {0}. Use #pragma eval_depth <depth> to increase the maximum", this->m_recursionLimit));
|
||||||
|
|
||||||
auto startOffset = this->m_currOffset;
|
auto startOffset = this->m_currOffset;
|
||||||
for (auto &member : node->getMembers()) {
|
for (auto &member : node->getMembers()) {
|
||||||
this->evaluateMember(member, memberPatterns, true);
|
this->evaluateMember(member, memberPatterns, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->m_currRecursionDepth--;
|
||||||
|
|
||||||
return this->evaluateAttributes(node, new PatternDataStruct(startOffset, this->m_currOffset - startOffset, memberPatterns));
|
return this->evaluateAttributes(node, new PatternDataStruct(startOffset, this->m_currOffset - startOffset, memberPatterns));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -455,10 +461,16 @@ namespace hex::lang {
|
|||||||
|
|
||||||
auto startOffset = this->m_currOffset;
|
auto startOffset = this->m_currOffset;
|
||||||
|
|
||||||
|
this->m_currRecursionDepth++;
|
||||||
|
if (this->m_currRecursionDepth > this->m_recursionLimit)
|
||||||
|
this->getConsole().abortEvaluation(hex::format("evaluation depth exceeds maximum of {0}. Use #pragma eval_depth <depth> to increase the maximum", this->m_recursionLimit));
|
||||||
|
|
||||||
for (auto &member : node->getMembers()) {
|
for (auto &member : node->getMembers()) {
|
||||||
this->evaluateMember(member, memberPatterns, false);
|
this->evaluateMember(member, memberPatterns, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->m_currRecursionDepth--;
|
||||||
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
for (const auto &pattern : memberPatterns)
|
for (const auto &pattern : memberPatterns)
|
||||||
size = std::max(size, pattern->getSize());
|
size = std::max(size, pattern->getSize());
|
||||||
@ -538,10 +550,6 @@ namespace hex::lang {
|
|||||||
|
|
||||||
PatternData *pattern;
|
PatternData *pattern;
|
||||||
|
|
||||||
this->m_currRecursionDepth++;
|
|
||||||
if (this->m_currRecursionDepth > this->m_recursionLimit)
|
|
||||||
this->getConsole().abortEvaluation(hex::format("evaluation depth exceeds maximum of {0}. Use #pragma eval_depth <depth> to increase the maximum", this->m_recursionLimit));
|
|
||||||
|
|
||||||
if (auto builtinTypeNode = dynamic_cast<ASTNodeBuiltinType*>(type); builtinTypeNode != nullptr)
|
if (auto builtinTypeNode = dynamic_cast<ASTNodeBuiltinType*>(type); builtinTypeNode != nullptr)
|
||||||
return this->evaluateBuiltinType(builtinTypeNode);
|
return this->evaluateBuiltinType(builtinTypeNode);
|
||||||
else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(type); typeDeclNode != nullptr)
|
else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(type); typeDeclNode != nullptr)
|
||||||
@ -557,8 +565,6 @@ namespace hex::lang {
|
|||||||
else
|
else
|
||||||
this->getConsole().abortEvaluation("type could not be evaluated");
|
this->getConsole().abortEvaluation("type could not be evaluated");
|
||||||
|
|
||||||
this->m_currRecursionDepth--;
|
|
||||||
|
|
||||||
if (!node->getName().empty())
|
if (!node->getName().empty())
|
||||||
pattern->setTypeName(node->getName().data());
|
pattern->setTypeName(node->getName().data());
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ namespace hex::lang {
|
|||||||
if (replaceValue.empty())
|
if (replaceValue.empty())
|
||||||
throwPreprocessorError("no value given in #define directive", lineNumber);
|
throwPreprocessorError("no value given in #define directive", lineNumber);
|
||||||
|
|
||||||
this->m_defines.emplace(defineName, replaceValue);
|
this->m_defines.emplace(defineName, replaceValue, lineNumber);
|
||||||
} else if (code.substr(offset, 6) == "pragma") {
|
} else if (code.substr(offset, 6) == "pragma") {
|
||||||
offset += 6;
|
offset += 6;
|
||||||
|
|
||||||
@ -151,7 +151,7 @@ namespace hex::lang {
|
|||||||
if (pragmaValue.empty())
|
if (pragmaValue.empty())
|
||||||
throwPreprocessorError("missing value in #pragma directive", lineNumber);
|
throwPreprocessorError("missing value in #pragma directive", lineNumber);
|
||||||
|
|
||||||
this->m_pragmas.emplace(pragmaKey, pragmaValue);
|
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) == "//") {
|
} else if (code.substr(offset, 2) == "//") {
|
||||||
@ -181,13 +181,13 @@ namespace hex::lang {
|
|||||||
|
|
||||||
if (initialRun) {
|
if (initialRun) {
|
||||||
// Apply defines
|
// Apply defines
|
||||||
std::vector<std::pair<std::string, std::string>> sortedDefines;
|
std::vector<std::tuple<std::string, std::string, u32>> sortedDefines;
|
||||||
std::copy(this->m_defines.begin(), this->m_defines.end(), std::back_inserter(sortedDefines));
|
std::copy(this->m_defines.begin(), this->m_defines.end(), std::back_inserter(sortedDefines));
|
||||||
std::sort(sortedDefines.begin(), sortedDefines.end(), [](const auto &left, const auto &right) {
|
std::sort(sortedDefines.begin(), sortedDefines.end(), [](const auto &left, const auto &right) {
|
||||||
return left.first.size() > right.first.size();
|
return std::get<0>(left).size() > std::get<0>(right).size();
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const auto &[define, value] : sortedDefines) {
|
for (const auto &[define, value, defineLine] : sortedDefines) {
|
||||||
s32 index = 0;
|
s32 index = 0;
|
||||||
while((index = output.find(define, index)) != std::string::npos) {
|
while((index = output.find(define, index)) != std::string::npos) {
|
||||||
output.replace(index, define.length(), value);
|
output.replace(index, define.length(), value);
|
||||||
@ -196,12 +196,12 @@ namespace hex::lang {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle pragmas
|
// Handle pragmas
|
||||||
for (const auto &[type, value] : this->m_pragmas) {
|
for (const auto &[type, value, pragmaLine] : this->m_pragmas) {
|
||||||
if (this->m_pragmaHandlers.contains(type)) {
|
if (this->m_pragmaHandlers.contains(type)) {
|
||||||
if (!this->m_pragmaHandlers[type](value))
|
if (!this->m_pragmaHandlers[type](value))
|
||||||
throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), lineNumber);
|
throwPreprocessorError(hex::format("invalid value provided to '{0}' #pragma directive", type.c_str()), pragmaLine);
|
||||||
} else
|
} else
|
||||||
throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), lineNumber);
|
throwPreprocessorError(hex::format("no #pragma handler registered for type {0}", type.c_str()), pragmaLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (PreprocessorError &e) {
|
} catch (PreprocessorError &e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user