From 21ab59fc6d9486193ed59ca61c160e5fe0d4b626 Mon Sep 17 00:00:00 2001 From: Username404 Date: Fri, 22 Apr 2022 01:04:03 +0200 Subject: [PATCH] Parser.hpp: Add a "filter_comma_list" function to fix commas in parameters or arguments lists Signed-off-by: Username404 --- src/headers/parsing/Parser.hpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index a52f3db..4711f64 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -19,6 +19,18 @@ namespace Parser { const bool& quoteTokenText = false ) { error(token, text, token.line, quoteTokenText); } + void filter_comma_list(vector& tokens) { + if (tokens.size() >= 2 && tokens[1].toktype != tok::RPAR) { + for (auto iterator = tokens.begin(); iterator < tokens.end() - 1; ++iterator) { + const auto nextIterator = iterator + 1; + if (nextIterator->toktype == tok::COMMA) { + tokens.erase(nextIterator); + } else throw ParsingException("Missing comma after \"" + iterator->toktext + '"'); + } + } + } + inline vector filter_comma_list(vector&& tokens) { filter_comma_list(tokens); return tokens; } + IS(ParseTree) inline T parse(const input_iterator auto&, const input_iterator auto&); @@ -74,7 +86,7 @@ namespace Parser { if (nextAre({LCOMP, LCOMP, LBRACE})) { Function function(current.toktext); if (parametersDistance > 2) { - function.parameters = parse(lexed.begin() + ((i + 2) - parametersDistance), lexed.begin() + i); + function.parameters = parse(filter_comma_list(vector(lexed.begin() + ((i + 2) - parametersDistance), lexed.begin() + i))); } parseTree << function; i += 2; @@ -106,16 +118,7 @@ namespace Parser { case LPAR: case LBRACE: case LBRACKET: { const auto closingCharacter = find_corresponding(lexed.begin() + i + 1, lexed.end(), current.toktype, tok::inverseLCharacter(current.toktype)); vector subTokens(lexed.begin() + i + 1, closingCharacter); - if (current.toktype == LPAR || current.toktype == LBRACKET) { - if (subTokens.size() >= 2 && subTokens[1].toktype != RPAR) { - for (auto iterator = subTokens.cbegin(); iterator < (subTokens.cend() - 1); ++iterator) { - const auto nextIterator = iterator + 1; - if (nextIterator->toktype == COMMA) { - subTokens.erase(nextIterator); - } else throw ParsingException("Missing comma after \"" + iterator->toktext + '"'); - } - } - } + if (current.toktype == LPAR || current.toktype == LBRACKET) filter_comma_list(subTokens); if (not parseTree.empty()) { try { auto& previous = dynamic_cast(*parseTree.at(parseTree.size() - 1));