#ifndef YERBACON_PARSER_HPP #define YERBACON_PARSER_HPP #include #include "ParseComponents.hpp" #include "../Yerbacon.hpp" #include #include #include #include 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); } IS(ParseTree) inline T parse(const input_iterator auto&, const input_iterator auto&); IS(ParseTree) T parse(const span&& lexed) { T parseTree; using namespace StandardComponents; using enum tok::type; unsigned int i = 0; const auto nextAre = [&i, &lexed] Y>(const initializer_list& nextValues) -> bool { unsigned int j = 1; for (const Y& nextValue: nextValues) { if (cmp_less(lexed.size() - i, nextValues.size()) || lexed[i + j].toktype != nextValue) { return false; } ++j; } return true; }; for (;i < lexed.size(); ++i) { const bool hasNext = (i + 1) < lexed.size(); const tok& current = lexed[i], next = hasNext ? lexed[i + 1] : tok(UNEXPECTED, current.line); switch (current.toktype) { case NUMBER: { long double v = stoul(current.toktext); if (i > 0 && lexed[i - 1].toktype == HYPHEN) v = -v; types::Integer::precision_type p = 0; if (nextAre({DOT, NUMBER})) { i += 2; const string& right = lexed[i].toktext; p = min(static_cast(right.size()), numeric_limits::digits10); v += copysign(stold(right.substr(0, p)) / powl(10, p), v); } parseTree << types::Integer(v, p); break; } case STRING: parseTree << types::String(current.toktext.data()); break; case IDENTIFIER: { if (current.toktext == "class" || current.toktext == "structure") { if (next.toktype == IDENTIFIER) { parseTree << Class(next.toktext); ++i; } else { parsingError(next, hasNext ? " is not a valid class identifier" : "A class identifier is required", hasNext); } } else { unsigned int parametersDistance = 0; if (next.toktype == LPAR) { const auto closing = find_if(lexed.begin() + i, lexed.end(), [](const tok& token){ return token.toktype == RPAR; }); parametersDistance = distance(lexed.begin() + i, closing); i += parametersDistance; } if (nextAre({LCOMP, LCOMP, LBRACE})) { Function function(current.toktext); if (parametersDistance > 2) { function.parameters = parse(lexed.begin() + ((i + 2) - parametersDistance), lexed.begin() + i); } parseTree << function; i += 2; break; } else i -= parametersDistance; bool isFinalDefine = nextAre({TAG, DEFINE}); if (isFinalDefine || next.toktype == DEFINE) { const optional previousDefinition = parseTree.template findReferenceByName(current.toktext); 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); } } const unsigned increment = 2 + isFinalDefine; const auto beginning = lexed.begin() + i + increment; const auto end = find_if(beginning, lexed.end(), [¤t](const tok& it){ // TODO Find another way of choosing the tokens to parse return it.toktype == SEMICOLON || it.line != current.line; }); parseTree << Define(isFinalDefine, current.toktext, parse(beginning, end)); i += 1 + isFinalDefine + distance(beginning, end); } else if (next.toktype == LPAR) { parseTree << Call(current.toktext); } else parseTree << Reference(current.toktext); } } break; 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 + '"'); } } } const component_ptr& previous = parseTree.at(parseTree.size() - 1); if (current.toktype == LPAR) { try { dynamic_cast(*previous).ParseTree::operator=(parse(subTokens)); } catch (const bad_cast&) { parsingError(current, "Unexpected parenthesis"); } } else if (current.toktype == LBRACE) { const type_info& previous_id = previous->getId(); if (previous_id == typeid(Function)) { dynamic_cast(*previous).ParseTree::operator=(parse(subTokens)); } else if (previous_id == typeid(Class)) { dynamic_cast(*previous).body = parse(subTokens); } } else parseTree << parse(subTokens); i = distance(lexed.begin(), closingCharacter); break; } case RPAR: case RBRACE: case RBRACKET: parsingError(current, " \u27F5 Unexpected character", true); default: break; } } return parseTree; } template T> inline T parse(const input_iterator auto& begin, const input_iterator auto& end) { return parse(span(begin, end)); } } #endif //YERBACON_PARSER_HPP