1
2
3
4
5
6 #include <sngcpp/ast/Reader.hpp>
7
8 namespace sngcpp { namespace ast {
9
10 Reader::Reader(soulng::util::BinaryReader& binaryReader_) : binaryReader(binaryReader_)
11 {
12 }
13
14 void Reader::Read(soulng::lexer::Span& span)
15 {
16 span.fileIndex = binaryReader.ReadInt();
17 span.line = binaryReader.ReadInt();
18 span.start = binaryReader.ReadInt();
19 span.end = binaryReader.ReadInt();
20 }
21
22 SimpleTypeSpecifier Reader::ReadSimpleTypeSpecifier()
23 {
24 return SimpleTypeSpecifier(binaryReader.ReadByte());
25 }
26
27 NodeType Reader::ReadNodeType()
28 {
29 uint8_t x = binaryReader.ReadByte();
30 return static_cast<NodeType>(x);
31 }
32
33 Specifier Reader::ReadSpecifiers()
34 {
35 return Specifier(binaryReader.ReadUInt());
36 }
37
38 ClassKey Reader::ReadClassKey()
39 {
40 return ClassKey(binaryReader.ReadByte());
41 }
42
43 EnumKey Reader::ReadEnumKey()
44 {
45 return EnumKey(binaryReader.ReadByte());
46 }
47
48 Operator Reader::ReadOperator()
49 {
50 return Operator(binaryReader.ReadByte());
51 }
52
53 Suffix Reader::ReadSuffix()
54 {
55 return Suffix(binaryReader.ReadByte());
56 }
57
58 Base Reader::ReadBase()
59 {
60 return Base(binaryReader.ReadByte());
61 }
62
63 Node* Reader::ReadNode()
64 {
65 NodeType nodeType = ReadNodeType();
66 Node* node = NodeFactory::Instance().CreateNode(nodeType);
67 node->Read(*this);
68 return node;
69 }
70
71 CompoundStatementNode* Reader::ReadCompoundStatementNode()
72 {
73 Node* node = ReadNode();
74 if (node->GetNodeType() == NodeType::compoundStatementNode)
75 {
76 return static_cast<CompoundStatementNode*>(node);
77 }
78 else
79 {
80 throw std::runtime_error("compound statement node expected");
81 }
82 }
83
84 StringLiteralNode* Reader::ReadStringLiteralNode()
85 {
86 Node* node = ReadNode();
87 if (node->GetNodeType() == NodeType::stringLiteralNode)
88 {
89 return static_cast<StringLiteralNode*>(node);
90 }
91 else
92 {
93 throw std::runtime_error("string literal node expected");
94 }
95 }
96
97 NamespaceNode* Reader::ReadNamespaceNode()
98 {
99 Node* node = ReadNode();
100 if (node->GetNodeType() == NodeType::namespaceNode)
101 {
102 return static_cast<NamespaceNode*>(node);
103 }
104 else
105 {
106 throw std::runtime_error("namespace node expected");
107 }
108 }
109
110 IdentifierNode* Reader::ReadIdentifierNode()
111 {
112 Node* node = ReadNode();
113 if (node->GetNodeType() == NodeType::identifierNode)
114 {
115 return static_cast<IdentifierNode*>(node);
116 }
117 else
118 {
119 throw std::runtime_error("identifier node expected");
120 }
121 }
122
123 } }