1
2
3
4
5
6 #include <sngcpp/ast/Enumeration.hpp>
7 #include <sngcpp/ast/Visitor.hpp>
8 #include <sngcpp/ast/Writer.hpp>
9 #include <sngcpp/ast/Reader.hpp>
10
11 namespace sngcpp { namespace ast {
12
13 std::u32string ToString(EnumKey enumKey)
14 {
15 switch (enumKey)
16 {
17 case EnumKey::enumClass_: return U"enum class";
18 case EnumKey::enumStruct_: return U"enum struct";
19 case EnumKey::enum_: return U"enum";
20 }
21 return std::u32string();
22 }
23
24 EnumTypeNode::EnumTypeNode() : Node(NodeType::enumTypeNode), enumKey()
25 {
26 }
27
28 EnumTypeNode::EnumTypeNode(const Span& span_, EnumKey enumKey_, Node* enumName_, Node* enumBase_) : Node(NodeType::enumTypeNode, span_), enumKey(enumKey_), enumName(enumName_), enumBase(enumBase_)
29 {
30 }
31
32 void EnumTypeNode::Accept(Visitor& visitor)
33 {
34 visitor.Visit(*this);
35 }
36
37 void EnumTypeNode::Write(Writer& writer)
38 {
39 Node::Write(writer);
40 writer.Write(enumKey);
41 enumName->Write(writer);
42 writer.GetBinaryWriter().Write(enumBase != nullptr);
43 if (enumBase)
44 {
45 enumBase->Write(writer);
46 }
47 writer.GetBinaryWriter().Write(enumerators != nullptr);
48 if (enumerators)
49 {
50 enumerators->Write(writer);
51 }
52 writer.Write(openBraceSpan);
53 writer.Write(closeBraceSpan);
54 }
55
56 void EnumTypeNode::Read(Reader& reader)
57 {
58 Node::Read(reader);
59 enumKey = reader.ReadEnumKey();
60 enumName.reset(reader.ReadNode());
61 bool hasEnumBase = reader.GetBinaryReader().ReadBool();
62 if (hasEnumBase)
63 {
64 enumBase.reset(reader.ReadNode());
65 }
66 bool hasEnumerators = reader.GetBinaryReader().ReadBool();
67 if (hasEnumerators)
68 {
69 enumerators.reset(reader.ReadNode());
70 }
71 reader.Read(openBraceSpan);
72 reader.Read(closeBraceSpan);
73 }
74
75 void EnumTypeNode::AddEnumerator(const Span& span_, Node* enumerator_)
76 {
77 if (enumerators)
78 {
79 enumerators.reset(new EnumeratorSequenceNode(span_, enumerators.release(), enumerator_));
80 }
81 else
82 {
83 enumerators.reset(enumerator_);
84 }
85 }
86
87 EnumeratorNode::EnumeratorNode() : Node(NodeType::enumeratorNode)
88 {
89 }
90
91 EnumeratorNode::EnumeratorNode(const Span& span_, const std::u32string& enumerator_, Node* value_, const std::u32string& valueStr_) :
92 Node(NodeType::enumeratorNode, span_), enumerator(enumerator_), value(value_), valueStr(valueStr_)
93 {
94 }
95
96 void EnumeratorNode::Accept(Visitor& visitor)
97 {
98 visitor.Visit(*this);
99 }
100
101 void EnumeratorNode::Write(Writer& writer)
102 {
103 Node::Write(writer);
104 writer.GetBinaryWriter().Write(enumerator);
105 writer.GetBinaryWriter().Write(value != nullptr);
106 if (value)
107 {
108 value->Write(writer);
109 }
110 writer.GetBinaryWriter().Write(valueStr);
111 writer.Write(assignmentSignSpan);
112 }
113
114 void EnumeratorNode::Read(Reader& reader)
115 {
116 Node::Read(reader);
117 enumerator = reader.GetBinaryReader().ReadUtf32String();
118 bool hasValue = reader.GetBinaryReader().ReadBool();
119 if (hasValue)
120 {
121 value.reset(reader.ReadNode());
122 }
123 valueStr = reader.GetBinaryReader().ReadUtf32String();
124 reader.Read(assignmentSignSpan);
125 }
126
127 EnumeratorSequenceNode::EnumeratorSequenceNode() : BinaryNode(NodeType::enumeratorSequenceNode)
128 {
129 }
130
131 EnumeratorSequenceNode::EnumeratorSequenceNode(const Span& span_, Node* left_, Node* right_) : BinaryNode(NodeType::enumeratorSequenceNode, span_, left_, right_)
132 {
133 }
134
135 void EnumeratorSequenceNode::Accept(Visitor& visitor)
136 {
137 visitor.Visit(*this);
138 }
139
140 } }