schema_editor/comdel/parser/astnode.h

401 lines
8.4 KiB
C
Raw Normal View History

2022-04-08 18:17:58 +00:00
#ifndef AST_NODE_H
#define AST_NODE_H
2022-03-29 19:31:45 +00:00
#include "token.h"
2022-05-17 22:14:33 +00:00
#include "sourceerror.h"
2022-03-29 21:49:32 +00:00
#include <optional>
#include <vector>
2022-03-29 19:31:45 +00:00
/**
2022-04-08 18:17:58 +00:00
* AST base class, all AST node classes extend this class. Class contains basic
* information about nodes location in file.
2022-03-29 19:31:45 +00:00
*/
class AstNode {
public:
Span span;
AstNode() = default;
2022-04-24 20:21:45 +00:00
virtual ~AstNode();
2022-03-29 19:31:45 +00:00
2022-04-24 20:21:45 +00:00
AstNode(AstNode&&) = default;
2022-03-29 19:31:45 +00:00
AstNode& operator=(AstNode&&) = default;
AstNode(const AstNode&) = default;
AstNode& operator=(const AstNode&) = default;
2022-04-24 20:21:45 +00:00
};
template <typename T>
struct EnumNode: public AstNode
{
EnumNode() = default;
explicit EnumNode(T value): value(value) {}
2022-03-29 19:31:45 +00:00
2022-04-24 20:21:45 +00:00
T value;
2022-03-29 19:31:45 +00:00
};
2022-03-29 21:49:32 +00:00
struct StringNode: public AstNode
{
2022-03-29 19:31:45 +00:00
std::string value;
2022-04-24 20:21:45 +00:00
std::string asString();
2022-03-29 19:31:45 +00:00
};
2022-03-29 21:49:32 +00:00
struct IdentifierNode: public AstNode
{
2022-03-29 19:31:45 +00:00
std::string value;
};
2022-03-29 21:49:32 +00:00
struct NumberNode: public AstNode {
2022-03-29 19:31:45 +00:00
long long int value;
2022-04-24 20:21:45 +00:00
explicit NumberNode(const std::string& expression);
2022-03-29 19:31:45 +00:00
NumberNode(): value(0) {}
};
2022-03-31 21:20:41 +00:00
2022-03-29 21:49:32 +00:00
struct CountNode: public AstNode
{
2022-03-29 19:31:45 +00:00
NumberNode first;
NumberNode second;
CountNode(NumberNode first, NumberNode second): first(first), second(second) {}
2022-04-24 20:21:45 +00:00
CountNode() = default;
2022-03-29 19:31:45 +00:00
};
2022-03-29 21:49:32 +00:00
2022-03-31 21:20:41 +00:00
struct AddressSpaceNode: public AstNode
2022-03-29 21:49:32 +00:00
{
IdentifierNode name;
NumberNode start;
NumberNode end;
};
2022-04-08 17:13:53 +00:00
class ValueNode: public AstNode
2022-03-29 21:49:32 +00:00
{
public:
enum ValueType {
INT,
STRING,
BOOL,
2022-03-31 21:20:41 +00:00
WIRE,
IDENTIFIER,
2022-05-15 21:55:03 +00:00
MEMORY,
NIL,
2022-03-29 21:49:32 +00:00
};
private:
2022-04-24 20:21:45 +00:00
EnumNode<ValueType> type;
2022-03-29 21:49:32 +00:00
std::optional<long long> intValue;
std::optional<std::string> stringValue;
std::optional<bool> boolValue;
std::optional<std::string> identifierValue;
public:
2022-05-15 21:55:03 +00:00
ValueNode() = default;
2022-03-31 21:20:41 +00:00
2022-03-29 21:49:32 +00:00
ValueType getType() {
2022-04-24 20:21:45 +00:00
return type.value;
2022-03-31 21:20:41 +00:00
}
2022-04-24 20:21:45 +00:00
long long asInt();
std::string asString();
std::string asIdentifier();
bool asBool();
bool is(ValueType valueType);
static ValueNode ofBool(bool _value);
static ValueNode ofInt(long long _value);
static ValueNode ofString(std::string _value);
static ValueNode ofIdentifier(std::string _value);
2022-05-15 21:55:03 +00:00
static ValueNode ofMemory(std::optional<std::string> _value);
2022-04-24 20:21:45 +00:00
static ValueNode ofNull();
2022-05-15 21:55:03 +00:00
static ValueNode ofWire(std::optional<std::string> _value);
2022-03-29 21:49:32 +00:00
};
2022-03-31 21:20:41 +00:00
struct ConditionNode
2022-03-29 21:49:32 +00:00
{
bool negated;
IdentifierNode functionName;
2022-03-31 21:20:41 +00:00
std::vector<ValueNode> params;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
class ActionNode: public AstNode {
2022-03-29 21:49:32 +00:00
public:
enum ActionType {
ERROR,
WARNING
};
2022-04-24 20:21:45 +00:00
EnumNode<ActionType> type;
2022-03-29 21:49:32 +00:00
StringNode message;
};
2022-04-09 17:44:02 +00:00
struct IfStatementNode: public AstNode
2022-03-29 21:49:32 +00:00
{
2022-03-31 21:20:41 +00:00
ConditionNode condition;
ActionNode action;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct RuleNode: public AstNode
2022-03-29 21:49:32 +00:00
{
2022-04-08 18:17:58 +00:00
std::vector<IfStatementNode> statements;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct EnumerationNode: public AstNode
2022-03-29 21:49:32 +00:00
{
StringNode key;
2022-03-31 21:20:41 +00:00
ValueNode value;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct PopupNode: public AstNode
2022-03-29 21:49:32 +00:00
{
enum PopupType {
AUTOMATIC,
ON_DEMAND
};
2022-04-24 20:21:45 +00:00
std::optional<EnumNode<PopupType>> type;
2022-03-31 21:20:41 +00:00
std::optional<StringNode> title;
std::optional<StringNode> text;
2022-03-29 21:49:32 +00:00
bool enumerated;
std::vector<EnumerationNode> enumeration;
2022-03-31 21:20:41 +00:00
std::vector<RuleNode> rules;
2022-03-29 21:49:32 +00:00
};
struct PropertyNode: public AstNode
{
IdentifierNode key;
2022-03-31 21:20:41 +00:00
ValueNode value;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct DisplayItemNode: public AstNode
2022-03-29 21:49:32 +00:00
{
IdentifierNode type;
std::vector<PropertyNode> values;
2022-05-17 22:14:33 +00:00
long long int asInt(std::vector<SourceError>* errors, const std::string& property, long long int _default = 0) {
for(auto& prop: values) {
if(prop.key.value == property) {
if(prop.value.is(ValueNode::INT)) {
return prop.value.asInt();
} else {
if(errors != nullptr) {
errors->emplace_back(prop.value.span, "expected number");
}
}
}
}
return _default;
}
std::string asIdentifier(std::vector<SourceError>* errors, const std::string& property, std::string _default = "") {
for(auto& prop: values) {
if(prop.key.value == property) {
if(prop.value.is(ValueNode::IDENTIFIER)) {
return prop.value.asIdentifier();
} else {
if(errors != nullptr) {
errors->emplace_back(prop.value.span, "expected identifier");
}
}
}
}
return _default;
}
std::string asString(std::vector<SourceError>* errors, const std::string& property, std::string _default = "") {
for(auto& prop: values) {
if(prop.key.value == property) {
if(prop.value.is(ValueNode::STRING)) {
return prop.value.asString();
} else {
if(errors != nullptr) {
errors->emplace_back(prop.value.span, "expected string");
}
}
}
}
return _default;
}
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct DisplayNode: public AstNode
2022-03-29 21:49:32 +00:00
{
std::vector<DisplayItemNode> items;
};
2022-04-09 17:44:02 +00:00
struct PinConnectionNode: public AstNode
2022-03-29 21:49:32 +00:00
{
enum ConnectionType {
REQUIRED,
OPTIONAL
2022-03-29 21:49:32 +00:00
};
StringNode message;
2022-04-24 20:21:45 +00:00
EnumNode<ConnectionType> type;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct PinNode: public AstNode
2022-03-29 21:49:32 +00:00
{
enum PinType {
IN_OUT,
IN,
OUT
};
IdentifierNode name;
2022-04-24 20:21:45 +00:00
EnumNode<PinType> type;
2022-03-31 21:20:41 +00:00
std::optional<StringNode> tooltip;
std::optional<PinConnectionNode> connection;
std::optional<DisplayNode> display;
2022-04-09 12:10:40 +00:00
std::optional<std::vector<ValueNode>> wires;
2022-03-29 21:49:32 +00:00
};
struct WireNode: public AstNode
{
enum WireType {
WIRE,
WIRED_AND,
WIRED_OR,
R_WIRE
};
2022-04-24 20:21:45 +00:00
EnumNode<WireType> type;
2022-03-29 21:49:32 +00:00
IdentifierNode name;
NumberNode size;
bool hidden = false;
2022-04-05 21:48:07 +00:00
2022-04-24 20:21:45 +00:00
bool hasTerminateWith;
ValueNode terminateWith;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct AttributeNode: public AstNode
2022-03-29 21:49:32 +00:00
{
2022-03-31 21:20:41 +00:00
ValueNode::ValueType type;
2022-03-29 21:49:32 +00:00
IdentifierNode name;
2022-03-31 21:20:41 +00:00
std::optional<ValueNode> defaultValue;
std::optional<PopupNode> popup;
2022-03-29 21:49:32 +00:00
};
2022-04-09 17:44:02 +00:00
struct ConnectionComponentNode: public AstNode
2022-03-29 21:49:32 +00:00
{
IdentifierNode component;
IdentifierNode pin;
2022-04-09 17:44:02 +00:00
};
struct ConnectionNode: public AstNode
{
ConnectionComponentNode first;
std::optional<ConnectionComponentNode> second;
2022-03-29 21:49:32 +00:00
IdentifierNode bus;
std::vector<AttributeNode> attributes;
2022-04-09 17:44:02 +00:00
std::vector<ValueNode> firstWires;
std::optional<std::vector<ValueNode>> secondWires;
};
2022-03-29 21:49:32 +00:00
2022-04-09 17:44:02 +00:00
struct ComponentNode: public AstNode
2022-03-29 21:49:32 +00:00
{
enum ComponentType {
OTHER,
PROCESSOR,
MEMORY
};
IdentifierNode name;
2022-03-31 21:20:41 +00:00
std::optional<StringNode> tooltip;
std::optional<StringNode> source;
2022-04-24 20:21:45 +00:00
EnumNode<ComponentType> type;
2022-03-31 21:20:41 +00:00
std::vector<RuleNode> rules;
std::optional<StringNode> instanceName;
std::optional<CountNode> count;
std::optional<DisplayNode> display;
std::vector<PinNode> pins;
2022-03-29 21:49:32 +00:00
std::vector<AttributeNode> attributes;
};
2022-04-09 17:44:02 +00:00
struct BusNode: public AstNode
2022-03-29 21:49:32 +00:00
{
enum BusType {
AUTOMATIC,
REGULAR,
SINGLE_AUTOMATIC
2022-03-29 21:49:32 +00:00
};
2022-04-24 20:21:45 +00:00
EnumNode<BusType> type;
2022-03-29 21:49:32 +00:00
IdentifierNode name;
2022-03-31 21:20:41 +00:00
std::optional<StringNode> tooltip;
std::optional<CountNode> count;
std::optional<DisplayNode> display;
2022-03-29 21:49:32 +00:00
std::vector<WireNode> wires;
};
2022-04-09 17:44:02 +00:00
struct LibraryNode: public AstNode
2022-03-29 21:49:32 +00:00
{
2022-03-31 21:20:41 +00:00
std::optional<StringNode> name;
std::optional<StringNode> libraryInfo;
std::optional<StringNode> header;
std::optional<StringNode> componentDirectory;
2022-05-15 23:02:34 +00:00
std::optional<StringNode> componentHeader;
2022-03-29 21:49:32 +00:00
2022-03-31 21:20:41 +00:00
std::vector<AddressSpaceNode> addressSpaces;
2022-03-29 21:49:32 +00:00
std::vector<ComponentNode> components;
std::vector<BusNode> buses;
std::vector<ConnectionNode> connections;
std::vector<PropertyNode> messages;
};
2022-03-31 21:20:41 +00:00
// SCHEMA models
2022-04-09 17:44:02 +00:00
struct InstanceAttributeNode: public AstNode
2022-03-31 21:20:41 +00:00
{
IdentifierNode name;
ValueNode value;
};
2022-04-09 17:44:02 +00:00
struct InstanceNode: public AstNode
2022-03-31 21:20:41 +00:00
{
IdentifierNode name;
IdentifierNode component;
std::optional<CountNode> position;
std::vector<InstanceAttributeNode> attributes;
std::optional<NumberNode> size;
};
2022-04-24 20:21:45 +00:00
struct ConnectionComponentInstance: public AstNode
{
2022-03-31 21:20:41 +00:00
IdentifierNode instance;
IdentifierNode pin;
2022-04-09 17:44:02 +00:00
};
struct ConnectionInstanceNode: public AstNode
{
ConnectionComponentInstance first;
std::optional<ConnectionComponentInstance> second;
2022-03-31 21:20:41 +00:00
IdentifierNode bus;
std::vector<InstanceAttributeNode> attributes;
};
2022-04-09 17:44:02 +00:00
struct SchemaNode: public AstNode
2022-03-31 21:20:41 +00:00
{
std::optional<StringNode> source;
2022-03-29 21:49:32 +00:00
2022-03-31 21:20:41 +00:00
std::vector<InstanceNode> instances;
std::vector<ConnectionInstanceNode> connections;
std::optional<LibraryNode> library;
};
2022-03-29 21:49:32 +00:00
2022-04-08 18:17:58 +00:00
#endif // AST_NODE_H