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