1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>
#include <lexy/dsl/option.hpp>
#include <lexy/encoding.hpp>
#include <lexy/input_location.hpp>
#include <dryad/node.hpp>
#include <dryad/tree.hpp>
#include "ParseState.hpp"
#include <range/v3/view/drop.hpp>
using namespace ovdl::v2script::ast;
ListValue::ListValue(dryad::node_ctor ctor, StatementList statements)
: node_base(ctor) {
insert_child_list_after(nullptr, statements);
if (statements.empty()) {
_last_statement = nullptr;
} else {
_last_statement = statements.back();
}
}
ListValue::ListValue(dryad::node_ctor ctor, AssignStatementList statements) : node_base(ctor) {
insert_child_list_after(nullptr, statements);
if (statements.empty()) {
_last_statement = nullptr;
} else {
_last_statement = statements.back();
}
}
ListValue::ListValue(dryad::node_ctor ctor) : node_base(ctor) {
_last_statement = nullptr;
}
FileTree::FileTree(dryad::node_ctor ctor, StatementList statements) : node_base(ctor) {
insert_child_list_after(nullptr, statements);
if (statements.empty()) {
_last_node = nullptr;
} else {
_last_node = statements.back();
}
}
FileTree::FileTree(dryad::node_ctor ctor, AssignStatementList statements) : node_base(ctor) {
insert_child_list_after(nullptr, statements);
if (statements.empty()) {
_last_node = nullptr;
} else {
_last_node = statements.back();
}
}
FileTree::FileTree(dryad::node_ctor ctor) : node_base(ctor) {
_last_node = nullptr;
}
std::string FileAbstractSyntaxTree::make_list_visualizer() const {
const int INDENT_SIZE = 2;
std::string result;
unsigned int level = 0;
for (auto [event, node] : dryad::traverse(this->_tree)) {
if (event == dryad::traverse_event::exit) {
--level;
continue;
}
result.append(INDENT_SIZE * level, ' ');
result.append(fmt::format("- {}: ", get_kind_name(node->kind())));
dryad::visit_node(
node,
[&](const FlatValue* value) {
result.append(value->value(this->_symbol_interner));
},
[&](const ListValue* value) {
},
[&](const NullValue* value) {
},
[&](const EventStatement* statement) {
result.append(statement->is_province_event() ? "province_event" : "country_event");
},
[&](const AssignStatement* statement) {
},
[&](const FileTree* tree) {
});
result.append(1, '\n');
if (event == dryad::traverse_event::enter)
++level;
}
return result;
}
std::string FileAbstractSyntaxTree::make_native_visualizer() const {
constexpr int INDENT_SIZE = 2;
std::string result;
unsigned int level = 0;
dryad::visit_tree(
this->_tree,
[&](const IdentifierValue* value) {
result.append(value->value(this->_symbol_interner));
},
[&](const StringValue* value) {
result.append(1, '"').append(value->value(this->_symbol_interner)).append(1, '"');
},
[&](dryad::child_visitor<NodeKind> visitor, const ValueStatement* statement) {
visitor(statement->value());
},
[&](dryad::child_visitor<NodeKind> visitor, const AssignStatement* statement) {
visitor(statement->left());
if (statement->right()->kind() != NodeKind::NullValue) {
result.append(" = ");
visitor(statement->right());
}
},
[&](dryad::child_visitor<NodeKind> visitor, const ListValue* value) {
result.append(1, '{');
level++;
for (const auto& statement : value->statements()) {
result
.append(1, '\n')
.append(INDENT_SIZE * level, ' ');
visitor(statement);
}
level--;
result
.append(1, '\n')
.append(INDENT_SIZE * level, ' ')
.append(1, '}');
},
[&](dryad::child_visitor<NodeKind> visitor, const EventStatement* statement) {
result.append(statement->is_province_event() ? "province_event" : "country_event");
if (statement->right()->kind() != NodeKind::NullValue) {
result.append(" = ");
visitor(statement->right());
}
},
[&](dryad::child_visitor<NodeKind> visitor, const FileTree* value) {
auto statements = value->statements();
visitor(*statements.begin());
for (const auto& statement : statements | ranges::views::drop(1)) {
result
.append(1, '\n')
.append(INDENT_SIZE * level, ' ');
visitor(statement);
}
});
return result;
}
|