schema_editor/comdel/parser/comdel_parser.h

137 lines
3.4 KiB
C
Raw Normal View History

2022-04-08 18:17:58 +00:00
#ifndef COMDEL_PARSER_H
#define COMDEL_PARSER_H
2022-03-29 19:31:45 +00:00
2022-05-27 06:18:17 +00:00
#include "source_error.h"
2022-03-29 19:31:45 +00:00
#include "presult.h"
#include "token.h"
2022-05-27 06:18:17 +00:00
#include "ast_nodes.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 {
// REFERENCE to the parser's prevSpan. After parsing a node this will
// "point" to the span of the last token contained in the node.
2022-05-27 06:18:17 +00:00
const Span &prevSpan;
2022-03-29 19:31:45 +00:00
public:
const Span lo; // the low end of the span, beginning of the node
2022-05-27 06:18:17 +00:00
Spanner(Span lo, Span &prevSpan) : lo(lo), prevSpan(prevSpan) {}
2022-05-27 06:18:17 +00:00
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-05-27 06:18:17 +00:00
return std::forward<T>(astNode);
2022-03-29 19:31:45 +00:00
}
};
2022-05-27 06:18:17 +00:00
class ComdelParser {
2022-03-29 19:31:45 +00:00
private:
std::vector<Token> tokens;
std::set<TokenType> expectedTokens;
unsigned int position;
std::vector<SourceError> errors;
Span &getPreviousSpan();
void bump();
bool consume(TokenType tokenType);
2022-05-27 06:18:17 +00:00
2022-03-29 19:31:45 +00:00
bool check(TokenType tokenType);
void skipUntilNextKeyword();
2022-03-29 19:31:45 +00:00
Token &current();
[[nodiscard]] PError unexpected();
template<typename T>
PResult<std::vector<T>> parseList(std::optional<TokenType> openDelim,
2022-05-27 06:18:17 +00:00
TokenType closeDelim,
std::optional<TokenType> separator,
bool allowTrailing,
const std::function<PResult<T>()> &parse_f);
2022-03-29 19:31:45 +00:00
Spanner getSpanner();
2022-05-27 06:18:17 +00:00
// used to parse library and schema
2022-03-29 19:31:45 +00:00
PResult<StringNode> parseString();
2022-05-27 06:18:17 +00:00
PResult<ColorNode> parseColor();
2022-03-29 19:31:45 +00:00
PResult<IdentifierNode> parseIdentifier();
2022-05-27 06:18:17 +00:00
2022-03-29 19:31:45 +00:00
PResult<NumberNode> parseNumber();
2022-05-27 06:18:17 +00:00
2022-03-29 19:31:45 +00:00
PResult<CountNode> parseCount();
2022-05-27 06:18:17 +00:00
2022-03-29 19:31:45 +00:00
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
2022-05-27 06:18:17 +00:00
2022-03-29 19:31:45 +00:00
PResult<EnumerationNode> parseEnumeration();
PResult<ValueNode> parseConnectionWire();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<ComponentNode> parseComponent();
2022-05-27 06:18:17 +00:00
2022-03-31 21:20:41 +00:00
PResult<AddressSpaceNode> parseAddress();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<PinNode> parsePin();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<DisplayNode> parseDisplay();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<PinConnectionNode> parsePinConnection();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<AttributeNode> parseAttribute();
2022-05-27 06:18:17 +00:00
2022-03-31 21:20:41 +00:00
PResult<PopupNode> parsePopup();
2022-05-27 06:18:17 +00:00
2022-03-31 21:20:41 +00:00
PResult<RuleNode> parseRule();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<BusNode> parseBus();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<WireNode> parseWire();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<ConnectionNode> parseConnection();
2022-05-27 06:18:17 +00:00
2022-03-29 21:49:32 +00:00
PResult<DisplayItemNode> parseDisplayItem();
2022-05-27 06:18:17 +00:00
2022-04-08 18:17:58 +00:00
PResult<IfStatementNode> parseIfStatement();
2022-03-31 21:20:41 +00:00
2022-05-27 06:18:17 +00:00
PResult<ValueNode> parseValue();
2022-05-27 06:18:17 +00:00
// used to parse schema
2022-03-31 21:20:41 +00:00
PResult<CountNode> parsePosition();
2022-05-27 06:18:17 +00:00
2022-03-31 21:20:41 +00:00
PResult<InstanceNode> parseInstance();
2022-05-27 06:18:17 +00:00
2022-03-31 21:20:41 +00:00
PResult<InstanceAttributeNode> parseInstanceAttribute();
2022-03-29 19:31:45 +00:00
2022-05-27 06:18:17 +00:00
PResult<ConnectionInstanceNode> parseConnectionInstance();
2022-04-24 20:21:45 +00:00
PResult<EnumNode<ComponentNode::ComponentType>> parseComponentType();
PResult<EnumNode<BusNode::BusType>> parseBusType();
PResult<EnumNode<PinConnectionNode::ConnectionType>> parseConnectionType();
2022-05-27 06:18:17 +00:00
public:
explicit ComdelParser(std::vector<Token> tokens);
std::optional<SchemaNode> parseSchema();
std::optional<LibraryNode> parseLibrary();
const std::vector<SourceError> &getErrors();
2022-03-29 19:31:45 +00:00
};
2022-04-08 18:17:58 +00:00
#endif // COMDEL_PARSER_H