44 lines
1.8 KiB
C++
44 lines
1.8 KiB
C++
#ifndef YERBACON_PARSER_HPP
|
|
#define YERBACON_PARSER_HPP
|
|
|
|
#include <string>
|
|
#include "ParseComponents.hpp"
|
|
#include "../Yerbacon.hpp"
|
|
|
|
namespace Parser {
|
|
class ParsingException: public Yerbacon::Exception {
|
|
using Yerbacon::Exception::Exception;
|
|
};
|
|
ParseTree parseVector(const vector<tok>& lexed) {
|
|
ParseTree parseTree;
|
|
using namespace StandardComponents;
|
|
if (lexed.size() > 1) {
|
|
for (unsigned int i = 0; i < lexed.size() - 1; ++i) {
|
|
const auto& current = lexed[i], next = lexed[i + 1];
|
|
|
|
switch (current.toktype) {
|
|
case tok::STRING: parseTree << types::String(current.toktext.data()); break;
|
|
case tok::IDENTIFIER: {
|
|
if (current.toktext == "class") {
|
|
if (next.toktype == tok::IDENTIFIER) {
|
|
parseTree << Class(next.toktext); ++i;
|
|
} else throw ParsingException('"' + next.toktext + "\" is not a valid class identifier");
|
|
} else {
|
|
const bool isFinalDefine = next.toktype == tok::TAG && (lexed.size() - i) > 2 && lexed[i + 2].toktype == tok::DEFINE;
|
|
if (isFinalDefine || next.toktype == tok::DEFINE) {
|
|
parseTree << Define(isFinalDefine, current.toktext);
|
|
i += isFinalDefine ? 2 : 1;
|
|
} else {
|
|
parseTree << Reference(current.toktext);
|
|
}
|
|
}
|
|
}
|
|
default: break;
|
|
}
|
|
}
|
|
} else throw ParsingException("At least 2 tokens must be provided");
|
|
return parseTree;
|
|
}
|
|
}
|
|
|
|
#endif //YERBACON_PARSER_HPP
|