#ifndef COMDEL_PARSER_H #define COMDEL_PARSER_H #include "sourceerror.h" #include "presult.h" #include "token.h" #include "astnode.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 { const Span lo; // the low end of the span, beginning of the node // 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: 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::move(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); 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(); PResult parseString(); PResult parseIdentifier(); PResult parseNumber(); PResult parseCount(); PResult parseProperty(std::optional valueType); PResult parseEnumeration(); 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(); PResult parseWireInstance(); PResult parsePosition(); PResult parseInstance(); PResult parseInstanceAttribute(); PResult parseConnectionInstance(); public: ComdelParser(std::vector tokens); std::optional parseSchema(); std::optional parse(); const std::vector& getErrors(); }; #endif // COMDEL_PARSER_H