schema_editor/comdel/parser/comdel_parser.h

137 lines
3.4 KiB
C++

#ifndef COMDEL_PARSER_H
#define COMDEL_PARSER_H
#include "source_error.h"
#include "presult.h"
#include "token.h"
#include "ast_nodes.h"
#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 {
// 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<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::forward<T>(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);
void skipUntilNextKeyword();
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();
// used to parse library and schema
PResult<StringNode> parseString();
PResult<ColorNode> parseColor();
PResult<IdentifierNode> parseIdentifier();
PResult<NumberNode> parseNumber();
PResult<CountNode> parseCount();
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
PResult<EnumerationNode> parseEnumeration();
PResult<ValueNode> parseConnectionWire();
PResult<ComponentNode> parseComponent();
PResult<AddressSpaceNode> parseAddress();
PResult<PinNode> parsePin();
PResult<DisplayNode> parseDisplay();
PResult<PinConnectionNode> parsePinConnection();
PResult<AttributeNode> parseAttribute();
PResult<PopupNode> parsePopup();
PResult<RuleNode> parseRule();
PResult<BusNode> parseBus();
PResult<WireNode> parseWire();
PResult<ConnectionNode> parseConnection();
PResult<DisplayItemNode> parseDisplayItem();
PResult<IfStatementNode> parseIfStatement();
PResult<ValueNode> parseValue();
// used to parse schema
PResult<CountNode> parsePosition();
PResult<InstanceNode> parseInstance();
PResult<InstanceAttributeNode> parseInstanceAttribute();
PResult<ConnectionInstanceNode> parseConnectionInstance();
PResult<EnumNode<ComponentNode::ComponentType>> parseComponentType();
PResult<EnumNode<BusNode::BusType>> parseBusType();
PResult<EnumNode<PinConnectionNode::ConnectionType>> parseConnectionType();
public:
explicit ComdelParser(std::vector<Token> tokens);
std::optional<SchemaNode> parseSchema();
std::optional<LibraryNode> parseLibrary();
const std::vector<SourceError> &getErrors();
};
#endif // COMDEL_PARSER_H