Throw an exception when a final variable is being redefined, and use types instead of auto in the ParseTree::findReferenceByName() method and when calling it in Parser.hpp

This commit is contained in:
Username404 2021-11-25 21:38:49 +01:00
parent 47b5135402
commit a73c7a9608
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
2 changed files with 6 additions and 2 deletions

View File

@ -81,8 +81,8 @@ public:
}
IS(StandardComponents::NamedIdentifier)
optional<reference_wrapper<T>> findReferenceByName(const string& name) const {
auto identifiers = findById<T>();
for (const auto& identifier: identifiers) {
const vector<T*> identifiers = findById<T>();
for (T* identifier: identifiers) {
if (identifier->getId() == typeid(T) && identifier->name == name) {
return make_optional(ref(static_cast<T&>(*identifier)));
}

View File

@ -36,6 +36,10 @@ namespace Parser {
} else {
bool isFinalDefine = nextAre({TAG, DEFINE});
if (isFinalDefine || next.toktype == DEFINE) {
const optional previousDefinition = parseTree.findReferenceByName<Define>(current.toktext);
if (previousDefinition.has_value() && previousDefinition.value().get().final) {
throw ParsingException(current.toktext + " cannot be redefined as it is final", current.line);
}
parseTree << Define(isFinalDefine, current.toktext);
i += 1 + isFinalDefine;
} else {