forked from facebookarchive/libfbjs
/
walker.hpp
190 lines (174 loc) · 6.67 KB
/
walker.hpp
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
#pragma once
#include <memory>
#include <utility>
#include <boost/ptr_container/ptr_vector.hpp>
#include "node.hpp"
#define NODE_WALKER_VISIT_IMPL(TYPE, FALLBACK) \
virtual void visit(TYPE& node) { \
visit(static_cast<FALLBACK&>(node)); \
}
#define NODE_WALKER_ACCEPT_IMPL(TYPE, FALLBACK) \
void TYPE::accept(NodeWalker& walker) { \
walker.visit(*this); \
}
namespace fbjs {
class NodeWalker {
private:
NodeWalker* _parent;
Node* _node;
bool _remove;
bool _skip_delete;
protected:
typedef boost::ptr_vector<NodeWalker> ptr_vector;
public:
typedef std::auto_ptr<NodeWalker> ptr;
NodeWalker() : _parent(NULL), _node(NULL), _remove(false),
_skip_delete(false) {};
virtual ~NodeWalker() {};
virtual NodeWalker* clone() const = 0;
virtual Node* walk(Node* root) {
replaceAndVisit(root);
return _node;
}
NodeWalker* parent() const {
return _parent;
}
Node* node() const {
return _node;
}
protected:
template<class T>
static T& cast(NodeWalker& node) {
T* tmp = dynamic_cast<T*>(&node);
if (tmp) {
return *tmp;
} else {
throw std::runtime_error("invalid NodeWalker");
}
}
template<class T>
static std::auto_ptr<T> cast(NodeWalker::ptr node) {
std::auto_ptr<T> casted(&cast<T>(*node));
node.release();
return casted;
}
void remove(bool skip_delete = false) {
_remove = true;
_skip_delete = skip_delete;
}
void replace(Node* new_node, bool skip_delete = false) {
if (new_node && _node) {
new_node->setLineno(_node->lineno());
}
_node = new_node;
_remove = false;
_skip_delete = skip_delete;
}
void replaceAndVisit(Node* new_node) {
replace(new_node);
if (new_node == NULL) {
visit();
} else {
new_node->accept(*this);
}
if (new_node != _node && new_node) {
delete new_node;
}
}
std::auto_ptr<ptr_vector> visitChildren() {
ptr_vector ret;
node_list_t::iterator ii = _node->childNodes().begin();
while (ii != _node->childNodes().end()) {
ret.push_back(visitChild(ii++).release());
}
return ret.release();
}
ptr visitChild(node_list_t::iterator ii) {
ptr walker(clone());
walker->_parent = this;
walker->_node = *ii;
if (*ii == NULL) {
visit();
} else {
(*ii)->accept(*walker);
}
if (_remove) {
if (!_skip_delete) {
delete _node->removeChild(ii);
}
} else if (*ii != walker->_node) {
Node* old_node = _node->replaceChild(walker->_node, ii);
if (!_skip_delete && old_node) {
delete old_node;
}
}
return walker;
}
public:
virtual void visit() {}
virtual void visit(Node& _node) {
visitChildren();
}
NODE_WALKER_VISIT_IMPL(NodeProgram, Node);
NODE_WALKER_VISIT_IMPL(NodeStatementList, Node);
NODE_WALKER_VISIT_IMPL(NodeExpression, Node);
NODE_WALKER_VISIT_IMPL(NodeNumericLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeStringLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeRegexLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeBooleanLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeNullLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeThis, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeEmptyExpression, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeOperator, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeConditionalExpression, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeParenthetical, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeAssignment, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeUnary, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodePostfix, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeFunctionCall, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeFunctionConstructor, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeObjectLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeArrayLiteral, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeStaticMemberExpression, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeDynamicMemberExpression, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeStatement, Node);
NODE_WALKER_VISIT_IMPL(NodeStatementWithExpression, NodeStatement);
NODE_WALKER_VISIT_IMPL(NodeVarDeclaration, NodeStatement);
NODE_WALKER_VISIT_IMPL(NodeTypehint, Node);
NODE_WALKER_VISIT_IMPL(NodeFunctionDeclaration, Node);
NODE_WALKER_VISIT_IMPL(NodeFunctionExpression, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeArgList, Node);
NODE_WALKER_VISIT_IMPL(NodeIf, Node);
NODE_WALKER_VISIT_IMPL(NodeWith, Node);
NODE_WALKER_VISIT_IMPL(NodeTry, Node);
NODE_WALKER_VISIT_IMPL(NodeLabel, Node);
NODE_WALKER_VISIT_IMPL(NodeCaseClause, Node);
NODE_WALKER_VISIT_IMPL(NodeSwitch, Node);
NODE_WALKER_VISIT_IMPL(NodeDefaultClause, NodeCaseClause);
NODE_WALKER_VISIT_IMPL(NodeObjectLiteralProperty, Node);
NODE_WALKER_VISIT_IMPL(NodeObjectLiteralAccessor, Node);
NODE_WALKER_VISIT_IMPL(NodeForLoop, Node);
NODE_WALKER_VISIT_IMPL(NodeForIn, Node);
NODE_WALKER_VISIT_IMPL(NodeForEachIn, Node);
NODE_WALKER_VISIT_IMPL(NodeWhile, Node);
NODE_WALKER_VISIT_IMPL(NodeDoWhile, NodeStatement);
NODE_WALKER_VISIT_IMPL(NodeXMLDefaultNamespace, NodeStatement);
NODE_WALKER_VISIT_IMPL(NodeXMLName, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLElement, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeXMLComment, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLPI, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLContentList, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLTextData, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLEmbeddedExpression, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLAttributeList, Node);
NODE_WALKER_VISIT_IMPL(NodeXMLAttribute, Node);
NODE_WALKER_VISIT_IMPL(NodeWildcardIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeStaticAttributeIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeDynamicAttributeIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeStaticQualifiedIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeDynamicQualifiedIdentifier, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeFilteringPredicate, NodeExpression);
NODE_WALKER_VISIT_IMPL(NodeDescendantExpression, NodeExpression);
};
}