From 6d33f05021d6709470a6df59cda2f3185101c689 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Fri, 26 Nov 2021 16:53:14 +0100 Subject: [PATCH] Add a parsingError() function to Parser.hpp --- src/headers/parsing/Parser.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index f93551d..5c331ce 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -8,6 +8,13 @@ namespace Parser { typedef Yerbacon::Exception ParsingException; + void error(const tok& token, const string& text, unsigned long line, const bool& quoteTokenText = false) { + throw ParsingException(quoteTokenText ? "\"" + token.toktext + "\"" + text : text, line); + } + inline void parsingError( + const tok& token, const string& text, + const bool& quoteTokenText = false + ) { error(token, text, token.line, quoteTokenText); } ParseTree parseVector(const vector& lexed) { ParseTree parseTree; using namespace StandardComponents; @@ -32,13 +39,19 @@ namespace Parser { if (current.toktext == "class" || current.toktext == "structure") { if (next.toktype == IDENTIFIER) { parseTree << Class(next.toktext); ++i; - } else throw ParsingException((not (next.toktext.empty() or next.toktype == tok::EOF_)) ? '"' + next.toktext + "\" is not a valid class identifier" : "A class identifier is required", next.line); + } else { + const bool isNotBlank = (not (next.toktext.empty() or next.toktype == tok::EOF_)); + parsingError(next, isNotBlank ? " is not a valid class identifier" + : "A class identifier is required", isNotBlank); + } } else { bool isFinalDefine = nextAre({TAG, DEFINE}); if (isFinalDefine || next.toktype == DEFINE) { const optional previousDefinition = parseTree.findReferenceByName(current.toktext); - if (previousDefinition.has_value() && previousDefinition.value().get().final) { - throw ParsingException(current.toktext + " cannot be redefined as it is final", current.line); + if (previousDefinition.has_value()) { + if (previousDefinition.value().get().final || isFinalDefine) { + parsingError(current, previousDefinition->get().final ? " cannot be redefined as it is final" : " cannot be made final after it has been declared", true); + } } parseTree << Define(isFinalDefine, current.toktext); i += 1 + isFinalDefine;