2022-04-08 18:17:58 +00:00
|
|
|
#ifndef COMDEL_PARSER_H
|
|
|
|
#define COMDEL_PARSER_H
|
2022-03-29 19:31:45 +00:00
|
|
|
|
|
|
|
#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 ¤t();
|
|
|
|
|
|
|
|
[[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();
|
|
|
|
|
|
|
|
|
|
|
|
PResult<StringNode> parseString();
|
|
|
|
PResult<IdentifierNode> parseIdentifier();
|
|
|
|
PResult<NumberNode> parseNumber();
|
|
|
|
PResult<CountNode> parseCount();
|
|
|
|
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
|
|
|
|
PResult<EnumerationNode> parseEnumeration();
|
|
|
|
|
2022-04-08 20:46:47 +00:00
|
|
|
PResult<ValueNode> parseConnectionWire();
|
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-04-08 18:17:58 +00:00
|
|
|
PResult<IfStatementNode> parseIfStatement();
|
2022-03-31 21:20:41 +00:00
|
|
|
PResult<ValueNode> parseValue();
|
|
|
|
|
2022-04-08 20:46:47 +00:00
|
|
|
|
2022-03-31 21:20:41 +00:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2022-04-08 18:17:58 +00:00
|
|
|
#endif // COMDEL_PARSER_H
|