schema_editor/comdel/parser/comdelparser.h

96 lines
2.7 KiB
C++

#ifndef COMDELPARSER_H
#define COMDELPARSER_H
#include "sourceerror.h"
#include "presult.h"
#include "token.h"
#include <optional>
#include <set>
#include <vector>
#include <domain/library/library.h>
/// 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 <typename T, typename = std::enable_if<std::is_base_of<AstNode, T>::value>>
typename std::remove_reference_t<T> operator()(T&& astNode) const {
//astNode.span = lo.to(prevSpan);
return std::move(astNode);
}
};
class ComdelParser
{
private:
std::vector<Token> tokens;
std::set<TokenType> expectedTokens;
unsigned int position;
std::vector<SourceError> errors;
Span &getPreviousSpan();
void bump();
bool consume(TokenType tokenType);
bool check(TokenType tokenType);
Token &current();
[[nodiscard]] PError unexpected();
template<typename T>
PResult<std::vector<T>> parseList(std::optional<TokenType> openDelim,
TokenType closeDelim,
std::optional<TokenType> separator,
bool allowTrailing,
const std::function<PResult<T> ()> &parse_f);
Spanner getSpanner();
void parseBlock();
PResult<StringNode> parseString();
PResult<IdentifierNode> parseIdentifier();
PResult<NumberNode> parseNumber();
PResult<CountNode> parseCount();
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
PResult<EnumerationNode> parseEnumeration();
PResult<Component> parseComponent();
PResult<AddressSpace> parseAddress();
PResult<Pin> parsePin();
PResult<Display> parseDisplay();
PResult<PinConnection> parsePinConnection();
PResult<Attribute> parseAttribute();
PResult<Popup> parsePopup();
PResult<Rule> parseRule();
PResult<Bus> parseBus();
PResult<Wire> parseWire();
PResult<Connection> parseConnection();
PResult<DisplayItem> parseDisplayItem();
PResult<IfStmt> parseIfStatement();
PResult<Value> parseValue();
public:
ComdelParser(std::vector<Token> tokens);
std::optional<Library> parse();
const std::vector<SourceError>& getErrors();
};
#endif // COMDELPARSER_H