-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsourcechange.cpp
More file actions
219 lines (180 loc) · 6.74 KB
/
sourcechange.cpp
File metadata and controls
219 lines (180 loc) · 6.74 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "MiniLua/sourcechange.hpp"
#include <sstream>
namespace lua {
namespace rt {
SourceChangeVisitor::~SourceChangeVisitor() {}
SourceChange::~SourceChange() {}
string SourceChangeOr::to_string() const {
stringstream ss;
ss << "(";
if (alternatives.size() != 0) {
ss << alternatives[0]->to_string();
}
for (unsigned i = 1; i < alternatives.size(); ++i)
ss << " | " << alternatives[i]->to_string();
ss << ")";
return ss.str();
}
string SourceChangeAnd::to_string() const {
stringstream ss;
ss << "(";
if (changes.size() != 0) {
ss << changes[0]->to_string();
}
for (unsigned i = 1; i < changes.size(); ++i)
ss << " & " << changes[i]->to_string();
ss << ")";
return ss.str();
}
void ApplySCVisitor::visit(const SourceChangeOr& sc_or) {
if (!sc_or.alternatives.empty())
sc_or.alternatives[0]->accept(*this);
}
void ApplySCVisitor::visit(const SourceChangeAnd& sc_and) {
for (const auto& c : sc_and.changes) {
c->accept(*this);
}
}
void ApplySCVisitor::visit(const SourceAssignment& sc_ass) {
changes.push_back(sc_ass);
}
vector<LuaToken> ApplySCVisitor::apply_changes(const vector<LuaToken>& tokens) {
auto new_tokens = tokens;
// sort the collected changes according to their position
std::sort(changes.begin(), changes.end(), [](const auto& a, const auto& b){
return a.token.pos > b.token.pos;
});
// apply the changes from back to front
for (const auto& sc : changes) {
for (auto& t : new_tokens) {
if (t.pos == sc.token.pos) {
t.match = sc.replacement;
t.length = static_cast<long>(sc.replacement.length());
}
}
}
// delete list of changes
changes.clear();
return new_tokens;
}
optional<string> default_source_change_label(const val& v) {
if (!v.source)
return nullopt;
auto possible_changes = v.forceValue(v);
if (!possible_changes)
return nullopt;
ApplySCVisitor visitor;
(*possible_changes)->accept(visitor);
for (const auto& c : visitor.changes) {
if (c.hint != "" && c.hint != "?")
return c.to_string();
}
return visitor.changes.front().to_string();
}
vector<string> source_change_labels(const val& v) {
if (!v.source)
return vector<string>();
auto possible_changes = v.forceValue(v);
if (!possible_changes)
return vector<string>();
vector<string> result;
struct SCLabelVisior : ApplySCVisitor {
virtual void visit(const SourceChangeOr& sc_or) override {
for (const auto& c : sc_or.alternatives) {
c->accept(*this);
}
}
virtual void visit(const SourceChangeAnd& sc_and) override {
if (!sc_and.changes.empty())
sc_and.changes.back()->accept(*this);
}
} visitor;
(*possible_changes)->accept(visitor);
for (const auto& c : visitor.changes) {
result.push_back(c.to_string());
}
return result;
}
/*
* removes the first alternative from a source change recursively. It does a
* depth first search for a source assignment and removes it, removing empty
* or/and nodes in the process.
*
* Precondition: the source changes must result from a call to x.forceValue(x)
* as it compares the match and replacement in the SourceAssignment to discard
* any SourceAssignments that do not influence the current source change.
* (probably not needed?)
*
* It returns true if a matching assignment was found, false otherwise.
*/
static bool remove_alternative(shared_ptr<SourceChange>& changes) {
if (auto node = dynamic_pointer_cast<SourceAssignment>(changes)) {
if (node->token.match == node->replacement) {
// the node is a possibility to change the source -> return true
return true;
}
} else if (auto node = dynamic_pointer_cast<SourceChangeAnd>(changes)) {
for (unsigned i = 0; i < node->changes.size(); ++i) {
auto& c = node->changes[i];
if (remove_alternative(c)) {
if (auto child_node = dynamic_pointer_cast<SourceChangeOr>(c); child_node && child_node->alternatives.empty()) {
node->changes.erase(begin(node->changes) + i);
} else if (auto child_node = dynamic_pointer_cast<SourceChangeAnd>(c); child_node && child_node->changes.empty()) {
node->changes.erase(begin(node->changes) + i);
} else if (auto child_node = dynamic_pointer_cast<SourceAssignment>(c); child_node) {
node->changes.erase(begin(node->changes) + i);
}
return true;
}
}
return false;
} else if (auto node = dynamic_pointer_cast<SourceChangeOr>(changes)) {
for (unsigned i = 0; i < node->alternatives.size(); ++i) {
auto& c = node->alternatives[i];
if (remove_alternative(c)) {
if (auto child_node = dynamic_pointer_cast<SourceChangeOr>(c); child_node && child_node->alternatives.empty()) {
node->alternatives.erase(begin(node->alternatives) + i);
} else if (auto child_node = dynamic_pointer_cast<SourceChangeAnd>(c); child_node && child_node->changes.empty()) {
node->alternatives.erase(begin(node->alternatives) + i);
} else if (auto child_node = dynamic_pointer_cast<SourceAssignment>(c); child_node) {
node->alternatives.erase(begin(node->alternatives) + i);
}
return true;
}
}
return false;
}
return false;
}
optional<shared_ptr<SourceChange>> get_sc_for_hint(const val& v, const string& hint) {
if (!v.source)
return nullopt;
auto possible_changes = v.forceValue(v);
if (!possible_changes)
return nullopt;
auto source_changes = make_shared<SourceChangeAnd>();
for(;;) {
ApplySCVisitor visitor;
(*possible_changes)->accept(visitor);
for (const auto& c : visitor.changes) {
std::cout << c.hint << std::endl;
if (c.to_string() == hint) {
// we don't have to do more!
return source_changes;
}
}
if (visitor.changes.empty())
break;
// remove current alternative and add it as a source change with $ to source_changes
remove_alternative(*possible_changes);
for (const auto& c : visitor.changes) {
if (c.token.match == c.replacement) {
source_changes->changes.push_back(SourceAssignment::create(c.token, "$" + c.replacement));
}
}
}
// We could not change the source to modify hint :-(
return nullopt;
}
}
}