1
2
3
4
5
6 #include <sngcm/ast/AstReader.hpp>
7 #include <sngcm/ast/Identifier.hpp>
8 #include <sngcm/ast/Statement.hpp>
9 #include <sngcm/ast/Concept.hpp>
10 #include <sngcm/ast/Statement.hpp>
11 #include <sngcm/ast/Class.hpp>
12 #include <boost/uuid/nil_generator.hpp>
13
14 namespace sngcm { namespace ast {
15
16 AstReader::AstReader(const std::string& fileName_) :
17 binaryReader(fileName_), rootModuleId(boost::uuids::nil_uuid()), moduleNameTable(nullptr), moduleIdMap(nullptr)
18 {
19 }
20
21 Node* AstReader::ReadNode()
22 {
23 NodeType nodeType = static_cast<NodeType>(binaryReader.ReadByte());
24 Span span = ReadSpan();
25 boost::uuids::uuid moduleId;
26 GetBinaryReader().ReadUuid(moduleId);
27 Node* node = NodeFactory::Instance().CreateNode(nodeType, span, moduleId);
28 node->Read(*this);
29 return node;
30 }
31
32 AttributeNode* AstReader::ReadAttributeNode()
33 {
34 Node* node = ReadNode();
35 if (node->GetNodeType() == NodeType::attributeNode)
36 {
37 return static_cast<AttributeNode*>(node);
38 }
39 else
40 {
41 throw std::runtime_error("attribute node expected");
42 }
43 }
44
45 AttributesNode* AstReader::ReadAttributesNode()
46 {
47 Node* node = ReadNode();
48 if (node->GetNodeType() == NodeType::attributesNode)
49 {
50 return static_cast<AttributesNode*>(node);
51 }
52 else
53 {
54 throw std::runtime_error("attributes node expected");
55 }
56 }
57
58 IdentifierNode* AstReader::ReadIdentifierNode()
59 {
60 Node* node = ReadNode();
61 if (node->GetNodeType() == NodeType::identifierNode)
62 {
63 return static_cast<IdentifierNode*>(node);
64 }
65 else
66 {
67 throw std::runtime_error("identifier node expected");
68 }
69 }
70
71 LabelNode* AstReader::ReadLabelNode()
72 {
73 Node* node = ReadNode();
74 if (node->GetNodeType() == NodeType::labelNode)
75 {
76 return static_cast<LabelNode*>(node);
77 }
78 else
79 {
80 throw std::runtime_error("label node expected");
81 }
82 }
83
84 StatementNode* AstReader::ReadStatementNode()
85 {
86 Node* node = ReadNode();
87 if (node->IsStatementNode())
88 {
89 return static_cast<StatementNode*>(node);
90 }
91 else
92 {
93 throw std::runtime_error("statement node expected");
94 }
95 }
96
97 DefaultStatementNode* AstReader::ReadDefaultStatementNode()
98 {
99 Node* node = ReadNode();
100 if (node->GetNodeType() == NodeType::defaultStatementNode)
101 {
102 return static_cast<DefaultStatementNode*>(node);
103 }
104 else
105 {
106 throw std::runtime_error("default statement node expected");
107 }
108 }
109
110 CompoundStatementNode* AstReader::ReadCompoundStatementNode()
111 {
112 Node* node = ReadNode();
113 if (node->GetNodeType() == NodeType::compoundStatementNode)
114 {
115 return static_cast<CompoundStatementNode*>(node);
116 }
117 else
118 {
119 throw std::runtime_error("compound statement node expected");
120 }
121 }
122
123 ConstraintNode* AstReader::ReadConstraintNode()
124 {
125 Node* node = ReadNode();
126 if (node->NodeIsConstraintNode())
127 {
128 return static_cast<ConstraintNode*>(node);
129 }
130 else
131 {
132 throw std::runtime_error("constraint node expected");
133 }
134 }
135
136 WhereConstraintNode* AstReader::ReadWhereConstraintNode()
137 {
138 Node* node = ReadNode();
139 if (node->GetNodeType() == NodeType::whereConstraintNode)
140 {
141 return static_cast<WhereConstraintNode*>(node);
142 }
143 else
144 {
145 throw std::runtime_error("where constraint node expected");
146 }
147 }
148
149 ConceptIdNode* AstReader::ReadConceptIdNode()
150 {
151 Node* node = ReadNode();
152 if (node->GetNodeType() == NodeType::conceptIdNode)
153 {
154 return static_cast<ConceptIdNode*>(node);
155 }
156 else
157 {
158 throw std::runtime_error("concept id node expected");
159 }
160 }
161
162 ConceptNode* AstReader::ReadConceptNode()
163 {
164 Node* node = ReadNode();
165 if (node->IsConceptNode())
166 {
167 return static_cast<ConceptNode*>(node);
168 }
169 else
170 {
171 throw std::runtime_error("concept node expected");
172 }
173 }
174
175 TemplateIdNode* AstReader::ReadTemplateIdNode()
176 {
177 Node* node = ReadNode();
178 if (node->GetNodeType() == NodeType::templateIdNode)
179 {
180 return static_cast<TemplateIdNode*>(node);
181 }
182 else
183 {
184 throw std::runtime_error("template id node expected");
185 }
186 }
187
188 ConditionalCompilationExpressionNode* AstReader::ReadConditionalCompilationExpressionNode()
189 {
190 Node* node = ReadNode();
191 if (node->IsConditionalCompilationExpressionNode())
192 {
193 return static_cast<ConditionalCompilationExpressionNode*>(node);
194 }
195 else
196 {
197 throw std::runtime_error("conditional compilation expression node expected");
198 }
199 }
200
201 ConditionalCompilationPartNode* AstReader::ReadConditionalCompilationPartNode()
202 {
203 Node* node = ReadNode();
204 if (node->GetNodeType() == NodeType::conditionalCompilationPartNode)
205 {
206 return static_cast<ConditionalCompilationPartNode*>(node);
207 }
208 else
209 {
210 throw std::runtime_error("conditional compilation part node expected");
211 }
212 }
213
214 Specifiers AstReader::ReadSpecifiers()
215 {
216 return static_cast<Specifiers>(binaryReader.ReadUInt());
217 }
218
219 Span AstReader::ReadSpan()
220 {
221 bool valid = binaryReader.ReadBool();
222 if (!valid)
223 {
224 return Span();
225 }
226 else
227 {
228 uint32_t fileIndex = binaryReader.ReadULEB128UInt();
229 uint32_t line = binaryReader.ReadULEB128UInt();
230 uint32_t start = binaryReader.ReadULEB128UInt();
231 uint32_t end = binaryReader.ReadULEB128UInt();
232 return Span(static_cast<int32_t>(fileIndex), static_cast<int32_t>(line), static_cast<int32_t>(start), static_cast<int32_t>(end));
233 }
234 }
235
236 void AstReader::SetModuleMaps(const boost::uuids::uuid& rootModuleId_, std::std::unordered_map<int16_t, std::string>*moduleNameTable_, std::std::unordered_map<std::string, int16_t>*moduleIdMap_)
237 {
238 rootModuleId = rootModuleId_;
239 moduleNameTable = moduleNameTable_;
240 moduleIdMap = moduleIdMap_;
241 }
242
243 } }