Parser.hpp: Add a "filter_comma_list" function to fix commas in parameters or arguments lists

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-04-22 01:04:03 +02:00
parent 0d12f7eceb
commit 21ab59fc6d
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 14 additions and 11 deletions

View File

@ -19,6 +19,18 @@ namespace Parser {
const bool& quoteTokenText = false
) { error(token, text, token.line, quoteTokenText); }
void filter_comma_list(vector<tok>& 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<tok> filter_comma_list(vector<tok>&& 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<tok> 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&>(*parseTree.at(parseTree.size() - 1));