From 46a09217fe29edd83af77fed2ff96cacbd96c72d Mon Sep 17 00:00:00 2001 From: Username404 Date: Thu, 26 May 2022 13:32:41 +0200 Subject: [PATCH] Parser.hpp: Prevent the reuse of identifiers Signed-off-by: Username404 --- src/headers/parsing/Parser.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index 89591a7..c69e239 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -88,8 +88,6 @@ namespace Parser { i += parametersDistance; } if (nextAre({LCOMP, LCOMP, LBRACE})) { - if (parseTree.template findReferenceByName(current.toktext).has_value()) - parsingError(current, " is already defined", true); Function function(current.toktext); if (parametersDistance > 2) { function.parameters = parse(filter_comma_list(lexed.begin() + ((i + 2) - parametersDistance), lexed.begin() + i)); @@ -140,6 +138,22 @@ namespace Parser { } default: parsingError(current, " \u27F5 Unexpected character", true); } + try { + const auto& last = parseTree.cend() - 1; + const type_info& lastId = last->get()->getId(); + const auto& last_identifier = dynamic_cast(**last); + if (lastId != typeid(Reference) && + lastId != typeid(Call) && + lastId != typeid(Define) and + any_of(parseTree.cbegin(), last, [&last_identifier](const component_ptr& pointer){ + try { + return dynamic_cast(*pointer).name == last_identifier.name; + } catch (const bad_cast&) { + return false; + } + })) + { parsingError(current, " has already been defined previously", true); } + } catch (const bad_cast&) {} } return parseTree; }