schema_editor/comdel/parser/comdel_parser.h

113 lines
3.7 KiB
C
Raw Permalink 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();
/** Skips current token */
2022-03-29 19:31:45 +00:00
void bump();
/** Checks if next token is of given type and if it is consumes it */
2022-03-29 19:31:45 +00:00
bool consume(TokenType tokenType);
/** Checks if next token is of given type */
2022-03-29 19:31:45 +00:00
bool check(TokenType tokenType);
/** Skips until next keyword on same level
* Used to continue parsing on error
* */
void skipUntilNextKeyword();
/** Retuns current token */
2022-03-29 19:31:45 +00:00
Token &current();
/** Throws error of unexpected types */
2022-03-29 19:31:45 +00:00
[[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();
/** Base types */
2022-03-29 19:31:45 +00:00
PResult<StringNode> parseString();
PResult<ColorNode> parseColor();
2022-03-29 19:31:45 +00:00
PResult<IdentifierNode> parseIdentifier();
PResult<NumberNode> parseNumber();
PResult<NumberPairNode> parseNumberPair();
2022-03-29 19:31:45 +00:00
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
PResult<StringPropertyNode> parseStringProperty();
PResult<ValueNode> parseValue();
2022-05-27 06:18:17 +00:00
/** Library types */
PResult<ValueNode> parseConnectionWire();
2022-03-29 21:49:32 +00:00
PResult<ComponentNode> parseComponent();
PResult<EnumNode<ComponentNode::ComponentType>> parseComponentType();
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<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<ConnectionComponentNode> parseConnectionComponent();
2022-03-29 21:49:32 +00:00
PResult<DisplayItemNode> parseDisplayItem();
2022-04-08 18:17:58 +00:00
PResult<IfStatementNode> parseIfStatement();
PResult<EnumNode<BusNode::BusType>> parseBusType();
2022-03-31 21:20:41 +00:00
/** Schema types */
2022-03-31 21:20:41 +00:00
PResult<InstanceNode> parseInstance();
PResult<InstanceAttributeNode> parseInstanceAttribute();
2022-05-27 06:18:17 +00:00
PResult<ConnectionInstanceNode> parseConnectionInstance();
PResult<ConnectionComponentInstanceNode> parseConnectionComponentInstance();
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