schema_editor/comdel/parser/ast_nodes.h

554 lines
17 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"
#include "color.h"
2022-05-27 06:18:17 +00:00
#include "source_error.h"
2022-03-29 21:49:32 +00:00
#include <optional>
2022-05-27 06:18:17 +00:00
#include <utility>
2022-03-29 21:49:32 +00:00
#include <vector>
2022-03-29 19:31:45 +00:00
/*****************************************************************************
* BASE TYPES *
*****************************************************************************/
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:
/** Contains information about where in source file given node is located */
2022-03-29 19:31:45 +00:00
Span span;
AstNode() = default;
2022-05-27 06:18:17 +00:00
AstNode(AstNode &&) = default;
AstNode &operator=(AstNode &&) = default;
AstNode(const AstNode &) = default;
AstNode &operator=(const AstNode &) = default;
2022-04-24 20:21:45 +00:00
};
/**
* AST base enum class,
* Used to represent AST enums
* */
2022-05-27 06:18:17 +00:00
template<typename T>
struct EnumNode : public AstNode {
2022-04-24 20:21:45 +00:00
EnumNode() = default;
2022-05-27 06:18:17 +00:00
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
};
/** Represents string
* value contains quote-marks ("" or '' depending on string type)
* */
2022-05-27 06:18:17 +00:00
struct StringNode : public AstNode {
/** String including quote-marks*/
2022-03-29 19:31:45 +00:00
std::string value;
2022-05-27 06:18:17 +00:00
/** Returns string without quote-marks */
2022-04-24 20:21:45 +00:00
std::string asString();
2022-03-29 19:31:45 +00:00
};
/** Represents identifiers */
2022-05-27 06:18:17 +00:00
struct IdentifierNode : public AstNode {
2022-03-29 19:31:45 +00:00
std::string value;
};
/** Represents all numbers used
* All numbers must fit into long long int
* */
2022-05-27 06:18:17 +00:00
struct NumberNode : public AstNode {
long long int value = 0;
2022-05-27 06:18:17 +00:00
explicit NumberNode(const std::string &expression);
NumberNode() = default;
2022-03-29 19:31:45 +00:00
};
/** Represents color
* color comes in two formats #RRGGBB or #RRGGBBAA (AA representing opacity)
* */
struct ColorNode : public AstNode {
Color color;
explicit ColorNode(const std::string &expression);
ColorNode() = default;
};
/** Represents ordered number pair */
struct NumberPairNode : public AstNode {
2022-03-29 19:31:45 +00:00
NumberNode first;
NumberNode second;
NumberPairNode(NumberNode first, NumberNode second) : first(first), second(second) {}
2022-05-27 06:18:17 +00:00
NumberPairNode() = default;
2022-03-29 21:49:32 +00:00
};
/** Represents generic value
* Because attributes can contain different value types,
* this allows us to use one generic type for all attributes
* */
2022-05-27 06:18:17 +00:00
class ValueNode : public AstNode {
2022-03-29 21:49:32 +00:00
public:
enum ValueType {
/** Stores same content as NumberNode */
2022-03-29 21:49:32 +00:00
INT,
/** Stores same content as StringNode */
2022-03-29 21:49:32 +00:00
STRING,
/** Stores true or false */
2022-03-29 21:49:32 +00:00
BOOL,
/** Stores wire nam or null */
2022-03-31 21:20:41 +00:00
WIRE,
/** Default type assigned when node value is of type IdentifierNode and more correct type is assigned later */
IDENTIFIER,
/** Stores memory name or null */
2022-05-15 21:55:03 +00:00
MEMORY,
/** Stores null */
NIL,
/** Store same content as ColorNode */
COLOR,
2022-03-29 21:49:32 +00:00
};
private:
/** Type determines what is stored inside ValueNode */
EnumNode<ValueType> type = EnumNode(NIL);
/** All possible values for ValueNode are stored inside optionals */
std::optional<long long> intValue = std::nullopt;
std::optional<std::string> stringValue = std::nullopt;
std::optional<bool> boolValue = std::nullopt;
std::optional<std::string> identifierValue = std::nullopt;
std::optional<Color> colorValue = std::nullopt;
2022-03-29 21:49:32 +00:00
public:
2022-05-15 21:55:03 +00:00
ValueNode() = default;
2022-03-31 21:20:41 +00:00
ValueType getType() const;
2022-05-27 06:18:17 +00:00
/** Checks ValueNode is of requested type and returns it or returns default value for given type*/
long long int asInt();
2022-04-24 20:21:45 +00:00
std::string asString();
std::string asIdentifier();
Color asColor();
2022-04-24 20:21:45 +00:00
bool asBool();
/** Returns true if ValueNode is of given valueType */
2022-04-24 20:21:45 +00:00
bool is(ValueType valueType);
/** Static methods used to generate ValueNodes of requested type */
2022-04-24 20:21:45 +00:00
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();
static ValueNode ofColor(Color color);
2022-05-15 21:55:03 +00:00
static ValueNode ofWire(std::optional<std::string> _value);
};
/** Represents an identifier-value pair*/
struct PropertyNode : public AstNode {
IdentifierNode key;
ValueNode value;
PropertyNode() = default;
PropertyNode(IdentifierNode key, ValueNode value): key(key), value(value) {}
};
/** Represents a string-value pair */
struct StringPropertyNode : public AstNode {
StringNode key;
ValueNode value;
StringPropertyNode() = default;
StringPropertyNode(StringNode key, ValueNode value): key(key), value(value) {}
};
/*****************************************************************************
* RULE TYPES *
*****************************************************************************/
struct IfStatementNode;
/** Represents validation rule
* Rules are made from a list of
* if - else if statements
* */
struct RuleNode : public AstNode {
std::vector<IfStatementNode> statements;
2022-03-29 21:49:32 +00:00
};
/** Represents condition inside if statement
* Every condition is made from optional negation operator !
* Function called and list of function parameters
* */
2022-05-27 06:18:17 +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
};
/** Represents action executed inside if statement
* */
struct ActionNode : public AstNode {
/** There are two types of action determined by action type */
2022-03-29 21:49:32 +00:00
enum ActionType {
/** Error actions represent invalid state and cause validation to fail */
2022-03-29 21:49:32 +00:00
ERROR,
/** Warning actions represent states that can cause issue when simulating
* model but models created with it are still valid
* */
2022-03-29 21:49:32 +00:00
WARNING
};
2022-04-24 20:21:45 +00:00
EnumNode<ActionType> type;
/** Message used if condition is fulfilled */
2022-03-29 21:49:32 +00:00
StringNode message;
};
/** If statements represents one validation check inside rule */
2022-05-27 06:18:17 +00:00
struct IfStatementNode : public AstNode {
2022-03-31 21:20:41 +00:00
ConditionNode condition;
ActionNode action;
2022-03-29 21:49:32 +00:00
};
/*****************************************************************************
* ATTRIBUTE TYPES *
*****************************************************************************/
2022-03-29 21:49:32 +00:00
/** Represents popup dialog used to modified attribute inside which it is defined
* */
2022-05-27 06:18:17 +00:00
struct PopupNode : public AstNode {
/** Determines type of popup*/
2022-03-29 21:49:32 +00:00
enum PopupType {
/** Automatic popup is opened when component or connection containing it is defined */
2022-03-29 21:49:32 +00:00
AUTOMATIC,
/** On demand popups are opened on user request usually from context menus*/
2022-03-29 21:49:32 +00:00
ON_DEMAND
};
2022-04-24 20:21:45 +00:00
std::optional<EnumNode<PopupType>> type;
/** Title of popup */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> title;
/** Text of popup */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> text;
2022-03-29 21:49:32 +00:00
/** If popup contains an enumeration*/
2022-03-29 21:49:32 +00:00
bool enumerated;
std::vector<StringPropertyNode> enumeration;
2022-03-29 21:49:32 +00:00
/** Validation rules for given popup */
2022-03-31 21:20:41 +00:00
std::vector<RuleNode> rules;
2022-03-29 21:49:32 +00:00
};
/** Represents component or connection attribute
* Attributes are values that can programmatically be changed
* if popup is defined
* */
struct AttributeNode : public AstNode {
/** Type of attribute */
ValueNode::ValueType type;
/** Name of attribute */
IdentifierNode name;
2022-06-14 19:27:40 +00:00
/** Display name */
std::optional<StringNode> displayName;
/** Default type of attribute */
std::optional<ValueNode> defaultValue;
/** Popup used to change attribute value */
std::optional<PopupNode> popup;
};
/*****************************************************************************
* DISPLAY TYPES *
*****************************************************************************/
struct DisplayItemNode;
/** Represents how a component or bus is rendered
* Display is made from an list of display items
* */
struct DisplayNode : public AstNode {
std::vector<DisplayItemNode> items;
2022-03-29 21:49:32 +00:00
};
/** DisplayItem represents one rendered item
* in source code items are defined similar to json objects
* eg.
* @code rect {
* x: 100;
* y: 100;
* w: 100;
* h: 100;
* fillColor: #123456
* }
* */
2022-05-27 06:18:17 +00:00
struct DisplayItemNode : public AstNode {
/** Contains type of display item */
2022-03-29 21:49:32 +00:00
IdentifierNode type;
/** Contains all property nodes */
2022-03-29 21:49:32 +00:00
std::vector<PropertyNode> values;
2022-05-17 22:14:33 +00:00
/** Returns value of requested property
* If requested property doesn't exists default value is returned (eg. asInt is called but PropertyNode contains string)
* If value of requested property is different than expected nullopt is retured
* */
std::optional<long long int> asInt(const std::string &property, long long int _default = 0);
std::optional<Color> asColor(const std::string &property, Color _default = Color(0, 0, 0));
std::optional<std::string> asString(const std::string &property, std::string _default = "");
2022-03-29 21:49:32 +00:00
private:
std::optional<PropertyNode> getProperty(const std::string &property);
2022-03-29 21:49:32 +00:00
};
/*****************************************************************************
* LIBRARY TYPES *
*****************************************************************************/
2022-03-29 21:49:32 +00:00
/** Represents AddressSpaces
* Address spaces are defined with their name and address range
* */
struct AddressSpaceNode : public AstNode {
IdentifierNode name;
/** Range represent which addresses are available for components that use memory space [first, second> */
NumberPairNode range;
2022-03-29 21:49:32 +00:00
};
/** Pins are used to create component-component or component-bus connections */
2022-05-27 06:18:17 +00:00
struct PinNode : public AstNode {
/** Determines pin type, pin types currently only affect how pins are rendered */
2022-03-29 21:49:32 +00:00
enum PinType {
IN_OUT,
IN,
OUT
};
/** Name of pin */
2022-03-29 21:49:32 +00:00
IdentifierNode name;
/** Type of pin */
2022-04-24 20:21:45 +00:00
EnumNode<PinType> type;
/** Tooltip content displayed on hover over pin */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> tooltip;
/** If present this means pin must be connected to another component or bus to create connection
* Connection contains error message shown
* */
std::optional<StringNode> connection;
/** Determines how the pin is displayed */
2022-03-31 21:20:41 +00:00
std::optional<DisplayNode> display;
/** If pin connection is optional it requires list of wires used to populate comdel model */
2022-04-09 12:10:40 +00:00
std::optional<std::vector<ValueNode>> wires;
2022-03-29 21:49:32 +00:00
};
/**
* Represents COMDEL component
* */
struct ComponentNode : public AstNode {
/** Determines type of component */
enum ComponentType {
/** Generic component */
OTHER,
/** Represents processor, all processors have implicit attribute _memory if type memory
* used when generating COMDEL model to connect memories and processors
*/
PROCESSOR,
/** Represents memory, all components of type memory can be selected in _memory attribute of processor */
MEMORY
};
/** Component name */
IdentifierNode name;
2022-06-14 19:27:40 +00:00
/** Component name */
std::optional<StringNode> displayName;
/** Tooltip displayed on hover */
std::optional<StringNode> tooltip;
/** Contains path to COMDEL source containing current component */
std::optional<StringNode> source;
/** Type of component */
EnumNode<ComponentType> type;
/** List of component level rules */
std::vector<RuleNode> rules;
/** Default used to name instances */
std::optional<IdentifierNode> instanceName;
/** Count determines number of instances allowed in a schema */
std::optional<NumberPairNode> count;
/** Display determines how component is rendered */
std::optional<DisplayNode> display;
/** List of all pins */
std::vector<PinNode> pins;
/** List of all attributes */
std::vector<AttributeNode> attributes;
};
/** WireNode represents COMDEL wire
* */
2022-05-27 06:18:17 +00:00
struct WireNode : public AstNode {
/** Determines type of wires */
2022-03-29 21:49:32 +00:00
enum WireType {
/** Generic wire */
2022-03-29 21:49:32 +00:00
WIRE,
/** wired_and can have multiple sources, that are ANDed together */
2022-03-29 21:49:32 +00:00
WIRED_AND,
/** wired_and can have multiple sources, that are ORed together */
2022-03-29 21:49:32 +00:00
WIRED_OR,
/** r_wire can remain unconnected */
2022-03-29 21:49:32 +00:00
R_WIRE
};
2022-04-24 20:21:45 +00:00
EnumNode<WireType> type;
/** Name of wire */
2022-03-29 21:49:32 +00:00
IdentifierNode name;
/** Number of bits inside of a wire */
2022-03-29 21:49:32 +00:00
NumberNode size;
/** If wire is visible or hidden, this determines how wires are generated in COMDEL */
bool hidden = false;
2022-04-05 21:48:07 +00:00
/** If wire isn't connected to anything it is replaced with terminate with terminateWith value */
2022-04-24 20:21:45 +00:00
bool hasTerminateWith;
ValueNode terminateWith;
2022-03-29 21:49:32 +00:00
};
/** Buses dont exist in COMDEL but they are useful
* as they allow us to connect multiple COMDEL wires together
* */
struct BusNode : public AstNode {
enum BusType {
/** This busses connect two components */
AUTOMATIC,
/**
* This busses allow us to connect multiple component together using one bus
* */
REGULAR,
/** This busses connect two components,
* they differ from automatic as they allow us to connect same pins multiple times */
SINGLE_AUTOMATIC
};
EnumNode<BusType> type;
2022-06-14 19:27:40 +00:00
/** Bus name */
2022-03-29 21:49:32 +00:00
IdentifierNode name;
2022-06-14 19:27:40 +00:00
/** Display name */
std::optional<StringNode> displayName;
/** Default used to name instances */
std::optional<IdentifierNode> instanceName;
/** Tooltip displayed on hover */
std::optional<StringNode> tooltip;
/** Count determines number of instances allowed in a schema */
std::optional<NumberPairNode> count;
/** Display determines how component is rendered */
std::optional<DisplayNode> display;
/** List of all COMDEL wires contained in bus */
std::vector<WireNode> wires;
2022-03-29 21:49:32 +00:00
};
/** Represents ComponentConnection key in Connection node
* (eg. componentName.pinName)
* */
2022-05-27 06:18:17 +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
};
/** Represents Connection node
* Connection can be between component and bus in which second is null,
* or between two components
* */
2022-05-27 06:18:17 +00:00
struct ConnectionNode : public AstNode {
2022-04-09 17:44:02 +00:00
ConnectionComponentNode first;
std::optional<ConnectionComponentNode> second;
2022-03-29 21:49:32 +00:00
IdentifierNode bus;
std::vector<AttributeNode> attributes;
/** If connection is of type component-component it contains two pairs of wires */
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
/** LibraryNode represent library instance */
2022-05-27 06:18:17 +00:00
struct LibraryNode : public AstNode {
/** Name of library */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> name;
/** Library info contains generic information about library */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> libraryInfo;
/** Contains text that is added to top of COMDEL file */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> header;
/** Contains path to component directory */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> componentDirectory;
/** Contains text that is added to top of System component in COMDEL file */
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;
/** Contains properties used to translate dialog and error messages */
2022-03-29 21:49:32 +00:00
std::vector<PropertyNode> messages;
};
/*****************************************************************************
* LIBRARY TYPES *
*****************************************************************************/
/** Represents instance of attribute in component or connection instance */
2022-05-27 06:18:17 +00:00
struct InstanceAttributeNode : public AstNode {
2022-03-31 21:20:41 +00:00
IdentifierNode name;
ValueNode value;
};
/** Represents instance of a component or a bus */
2022-05-27 06:18:17 +00:00
struct InstanceNode : public AstNode {
/** Contains instance name */
2022-03-31 21:20:41 +00:00
IdentifierNode name;
/** Contains component name */
2022-03-31 21:20:41 +00:00
IdentifierNode component;
/** Contains position of component instance */
std::optional<NumberPairNode> position;
2022-03-31 21:20:41 +00:00
std::vector<InstanceAttributeNode> attributes;
/** Contains size of bus instances */
2022-03-31 21:20:41 +00:00
std::optional<NumberNode> size;
};
/** Represents ComponentConnection of a selected instance */
struct ConnectionComponentInstanceNode : public AstNode {
/** Name of component instance */
2022-03-31 21:20:41 +00:00
IdentifierNode instance;
/** Name of pin */
2022-03-31 21:20:41 +00:00
IdentifierNode pin;
2022-04-09 17:44:02 +00:00
};
/** Represents Connection instance */
2022-05-27 06:18:17 +00:00
struct ConnectionInstanceNode : public AstNode {
ConnectionComponentInstanceNode first;
std::optional<ConnectionComponentInstanceNode> second;
2022-04-09 17:44:02 +00:00
2022-03-31 21:20:41 +00:00
IdentifierNode bus;
std::vector<InstanceAttributeNode> attributes;
};
/** Represent schema instance */
2022-05-27 06:18:17 +00:00
struct SchemaNode : public AstNode {
/** Contains path to library source */
2022-03-31 21:20:41 +00:00
std::optional<StringNode> source;
2022-03-29 21:49:32 +00:00
/** Contains list of instances */
2022-03-31 21:20:41 +00:00
std::vector<InstanceNode> instances;
/** Contains list of connection */
2022-03-31 21:20:41 +00:00
std::vector<ConnectionInstanceNode> connections;
/** Contains library */
2022-03-31 21:20:41 +00:00
std::optional<LibraryNode> library;
};
2022-03-29 21:49:32 +00:00
2022-04-08 18:17:58 +00:00
#endif // AST_NODE_H