schema_editor/comdel/parser/comdelparser.h

102 lines
3.0 KiB
C
Raw Normal View History

2022-03-29 19:31:45 +00:00
#ifndef COMDELPARSER_H
#define COMDELPARSER_H
#include "sourceerror.h"
#include "presult.h"
#include "token.h"
2022-03-29 21:49:32 +00:00
#include "astnode.h"
2022-03-29 19:31:45 +00:00
#include <optional>
#include <set>
#include <vector>
/// 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 {
2022-03-31 21:20:41 +00:00
astNode.span = lo.to(prevSpan);
2022-03-29 19:31:45 +00:00
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();
2022-03-29 21:49:32 +00:00
PResult<ComponentNode> parseComponent();
2022-03-31 21:20:41 +00:00
PResult<AddressSpaceNode> parseAddress();
2022-03-29 21:49:32 +00:00
PResult<PinNode> parsePin();
PResult<DisplayNode> parseDisplay();
PResult<PinConnectionNode> parsePinConnection();
PResult<AttributeNode> parseAttribute();
2022-03-31 21:20:41 +00:00
PResult<PopupNode> parsePopup();
PResult<RuleNode> parseRule();
2022-03-29 21:49:32 +00:00
PResult<BusNode> parseBus();
PResult<WireNode> parseWire();
PResult<ConnectionNode> parseConnection();
PResult<DisplayItemNode> parseDisplayItem();
2022-03-31 21:20:41 +00:00
PResult<IfStatementnode> parseIfStatement();
PResult<ValueNode> parseValue();
PResult<WireInstanceNode> parseWireInstance();
PResult<CountNode> parsePosition();
PResult<InstanceNode> parseInstance();
PResult<InstanceAttributeNode> parseInstanceAttribute();
PResult<ConnectionInstanceNode> parseConnectionInstance();
2022-03-29 19:31:45 +00:00
public:
ComdelParser(std::vector<Token> tokens);
2022-03-31 21:20:41 +00:00
std::optional<SchemaNode> parseSchema();
2022-03-29 21:49:32 +00:00
std::optional<LibraryNode> parse();
2022-03-29 19:31:45 +00:00
const std::vector<SourceError>& getErrors();
};
#endif // COMDELPARSER_H