#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(); void bump(); bool consume(TokenType tokenType); bool check(TokenType tokenType); void skipUntilNextKeyword(); Token ¤t(); [[nodiscard]] PError unexpected(); template PResult> parseList(std::optional openDelim, TokenType closeDelim, std::optional separator, bool allowTrailing, const std::function()> &parse_f); Spanner getSpanner(); // used to parse library and schema PResult parseString(); PResult parseColor(); PResult parseIdentifier(); PResult parseNumber(); PResult parseCount(); PResult parseProperty(std::optional valueType); PResult parseEnumeration(); PResult parseConnectionWire(); PResult parseComponent(); PResult parseAddress(); PResult parsePin(); PResult parseDisplay(); PResult parsePinConnection(); PResult parseAttribute(); PResult parsePopup(); PResult parseRule(); PResult parseBus(); PResult parseWire(); PResult parseConnection(); PResult parseDisplayItem(); PResult parseIfStatement(); PResult parseValue(); // used to parse schema PResult parsePosition(); PResult parseInstance(); PResult parseInstanceAttribute(); PResult parseConnectionInstance(); PResult> parseComponentType(); PResult> parseBusType(); PResult> parseConnectionType(); public: explicit ComdelParser(std::vector tokens); std::optional parseSchema(); std::optional parseLibrary(); const std::vector &getErrors(); }; #endif // COMDEL_PARSER_H