#ifndef COMDEL_PARSER_H #define COMDEL_PARSER_H #include "source_error.h" #include "presult.h" #include "token.h" #include "ast_nodes.h" #include #include #include /// Records the current span and can later be called on /// an ast node to apply the entire span to that node /// /// Spanner's call operator MOVES from the node so it should /// be always used last. class Spanner { // REFERENCE to the parser's prevSpan. After parsing a node this will // "point" to the span of the last token contained in the node. const Span &prevSpan; public: const Span lo; // the low end of the span, beginning of the node Spanner(Span lo, Span &prevSpan) : lo(lo), prevSpan(prevSpan) {} template::value>> typename std::remove_reference_t operator()(T &&astNode) const { astNode.span = lo.to(prevSpan); return std::forward(astNode); } }; class ComdelParser { private: std::vector tokens; std::set expectedTokens; unsigned int position; std::vector errors; Span &getPreviousSpan(); /** Skips current token */ void bump(); /** Checks if next token is of given type and if it is consumes it */ bool consume(TokenType tokenType); /** Checks if next token is of given type */ bool check(TokenType tokenType); /** Skips until next keyword on same level * Used to continue parsing on error * */ void skipUntilNextKeyword(); /** Retuns current token */ Token ¤t(); /** Throws error of unexpected types */ [[nodiscard]] PError unexpected(); template PResult> parseList(std::optional openDelim, TokenType closeDelim, std::optional separator, bool allowTrailing, const std::function()> &parse_f); Spanner getSpanner(); /** Base types */ PResult parseString(); PResult parseColor(); PResult parseIdentifier(); PResult parseNumber(); PResult parseNumberPair(); PResult parseProperty(std::optional valueType); PResult parseStringProperty(); PResult parseValue(); /** Library types */ PResult parseConnectionWire(); PResult parseComponent(); PResult> parseComponentType(); PResult parseAddress(); PResult parsePin(); PResult parseDisplay(); PResult parseAttribute(); PResult parsePopup(); PResult parseRule(); PResult parseBus(); PResult parseWire(); PResult parseConnection(); PResult parseConnectionComponent(); PResult parseDisplayItem(); PResult parseIfStatement(); PResult> parseBusType(); /** Schema types */ PResult parseInstance(); PResult parseInstanceAttribute(); PResult parseConnectionInstance(); PResult parseConnectionComponentInstance(); public: explicit ComdelParser(std::vector tokens); std::optional parseSchema(); std::optional parseLibrary(); const std::vector &getErrors(); }; #endif // COMDEL_PARSER_H