Add a new variadic lambda in Parser.hpp to check the types of tokens

This commit is contained in:
Username404 2021-08-23 14:58:46 +02:00
parent 079d99e08a
commit 5d4be1039d
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
2 changed files with 26 additions and 7 deletions

View File

@ -4,6 +4,8 @@
#include <string>
#include "ParseComponents.hpp"
#include "../Yerbacon.hpp"
#include <cstdarg>
#include <concepts>
namespace Parser {
class ParsingException: public Yerbacon::Exception {
@ -14,7 +16,20 @@ namespace Parser {
using namespace StandardComponents;
using enum tok::type;
if (lexed.size() > 1) {
for (unsigned int i = 0; i < lexed.size() - 1; ++i) {
unsigned int i = 0;
const auto nextAre = [&i, &lexed](convertible_to<unsigned int> auto nextValues,...) {
va_list argumentsList;
va_start(argumentsList, nextValues);
for (decltype(nextValues) j = 1; j <= nextValues; ++j) {
if (!cmp_greater(lexed.size() - i, nextValues) || lexed[i + j].toktype != va_arg(argumentsList, decltype(nextValues))) {
va_end(argumentsList);
return false;
}
}
va_end(argumentsList);
return true;
};
for (;i < lexed.size() - 1; ++i) {
const auto& current = lexed[i], next = lexed[i + 1];
switch (current.toktype) {
@ -25,7 +40,8 @@ namespace Parser {
parseTree << Class(next.toktext); ++i;
} else throw ParsingException('"' + next.toktext + "\" is not a valid class identifier");
} else {
const bool isFinalDefine = next.toktype == TAG && (lexed.size() - i) > 2 && lexed[i + 2].toktype == DEFINE;
if ((lexed.size() - i) > 2) {
bool isFinalDefine = nextAre(2, TAG, DEFINE);
if (isFinalDefine || next.toktype == DEFINE) {
parseTree << Define(isFinalDefine, current.toktext);
i += isFinalDefine ? 2 : 1;
@ -34,6 +50,7 @@ namespace Parser {
}
}
}
}
default: break;
}
}

View File

@ -14,6 +14,7 @@ class Target {
protected:
std::stringstream output;
INCLUDECOMPONENT(StandardComponents::Define);
INCLUDECOMPONENT(StandardComponents::types::String)
INCLUDECOMPONENT(StandardComponents::Reference);
INCLUDECOMPONENT(StandardComponents::Class);
public:
@ -25,6 +26,7 @@ public:
for (const unique_ptr<ParseComponent>& component: tree) {
const type_info& id = (*component).getId();
with(StandardComponents::Define);
with(StandardComponents::types::String);
with(StandardComponents::Reference);
with(StandardComponents::Class);
}