1
2
3
4
5
6 #include <sngcm/ast/AstWriter.hpp>
7 #include <sngcm/ast/Node.hpp>
8
9 namespace sngcm { namespace ast {
10
11 AstWriter::AstWriter(const std::string& fileName_) : binaryWriter(fileName_), lexers(nullptr)
12 {
13 }
14
15 void AstWriter::Write(Node* node)
16 {
17 binaryWriter.Write(static_cast<uint8_t>(node->GetNodeType()));
18 Write(node->GetSpan());
19 node->Write(*this);
20 }
21
22 void AstWriter::Write(Specifiers specifiers)
23 {
24 binaryWriter.Write(static_cast<uint32_t>(specifiers));
25 }
26
27 void AstWriter::Write(const Span& span)
28 {
29 if (!span.Valid())
30 {
31 binaryWriter.Write(false);
32 }
33 else
34 {
35 Span s = span;
36 if (span.fileIndex >= 0 && span.fileIndex < lexers->size())
37 {
38 soulng::lexer::Lexer* lexer = (*lexers)[span.fileIndex];
39 lexer->ConvertExternal(s);
40 }
41 binaryWriter.Write(true);
42 binaryWriter.WriteULEB128UInt(static_cast<uint32_t>(s.fileIndex));
43 binaryWriter.WriteULEB128UInt(static_cast<uint32_t>(s.line));
44 binaryWriter.WriteULEB128UInt(static_cast<uint32_t>(s.start));
45 binaryWriter.WriteULEB128UInt(static_cast<uint32_t>(s.end));
46 }
47 }
48
49 void AstWriter::SetLexers(std::std::vector<soulng::lexer::Lexer*>*lexers_)
50 {
51 lexers = lexers_;
52 }
53
54 } }