-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast.cpp
More file actions
159 lines (133 loc) · 5.45 KB
/
Copy pathast.cpp
File metadata and controls
159 lines (133 loc) · 5.45 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
#include "parser/ast.h"
#include "ccs/domain.h"
#include "dag/node.h"
#include "parser/build_context.h"
#include "parser/loader.h"
namespace ccs { namespace ast {
void Import::addTo(BuildContext::P buildContext, BuildContext::P baseContext) const
{ ast.addTo(buildContext, baseContext); }
void PropDef::addTo(BuildContext::P buildContext, BuildContext::P) const
{ buildContext->addProperty(*this); }
void Constraint::addTo(BuildContext::P buildContext, BuildContext::P) const
{ buildContext->node().addConstraint(key_); }
bool PropDef::resolveImports(ImportResolver &, Loader &,
std::vector<std::string> &)
{ return true; }
bool Constraint::resolveImports(ImportResolver &, Loader &,
std::vector<std::string> &)
{ return true; }
bool Import::resolveImports(ImportResolver &importResolver, Loader &loader,
std::vector<std::string> &inProgress) {
bool result = false;
if (std::find(inProgress.begin(), inProgress.end(), location) !=
inProgress.end()) {
std::ostringstream msg;
msg << "Circular import detected involving '" << location << "'";
loader.tracer().onParseError(msg.str());
} else {
inProgress.push_back(location);
result = importResolver.resolve(location,
[&](std::istream &stream) {
return loader.parseCcsStream(stream, location,
importResolver, inProgress, ast);
});
inProgress.pop_back();
if (!result) {
std::ostringstream msg;
msg << "Failed to resolve '" << location
<< "'! (User-provided resolver returned false.)";
loader.tracer().onParseError(msg.str());
}
}
return result;
}
void Nested::addTo(BuildContext::P buildContext, BuildContext::P baseContext) const {
BuildContext::P bc = buildContext;
if (selector_)
bc = selector_->traverse(buildContext, baseContext);
for (auto it = rules_.begin(); it != rules_.end(); ++it)
(*it)->addTo(bc, baseContext);
}
bool Nested::resolveImports(ImportResolver &importResolver, Loader &loader,
std::vector<std::string> &inProgress) {
for (auto it = rules_.begin(); it != rules_.end(); ++it)
if (!(*it)->resolveImports(importResolver, loader, inProgress))
return false;
return true;
}
struct Wrap : public SelectorLeaf {
std::vector<SelectorBranch::P> branches;
SelectorLeaf::P right;
Wrap(SelectorBranch::P branch, SelectorLeaf::P leaf) : right(leaf)
{ branches.push_back(branch); }
// left == this, always. guess we could just use enable_shared_from_this,
// but that's just as ugly... spirit just doesn't like this way of doing
// things... it works fine, though, and i don't want to invest a lot of
// time in switching to some kind of value-types-in-a-variant design just
// to play nicer with spirit. it's also worth something to stay roughly
// in sync with the java version. anyway, maybe reconsider down the road.
virtual P descendant(P left, P right)
{ push(SelectorBranch::descendant(this->right), right); return left; }
virtual P conjunction(P left, P right)
{ push(SelectorBranch::conjunction(this->right), right); return left; }
virtual P disjunction(P left, P right)
{ push(SelectorBranch::disjunction(this->right), right); return left; }
void push(SelectorBranch::P newBranch, SelectorLeaf::P newRight) {
branches.push_back(newBranch);
right = newRight;
}
virtual Node &traverse(BuildContext::P context) {
BuildContext::P tmp = context;
for (auto it = branches.begin(); it != branches.end(); ++it)
tmp = (*it)->traverse(tmp, context);
return tmp->traverse(*right);
}
};
struct Step : public SelectorLeaf {
Key key_;
Step(const Key &key) : key_(key) {}
virtual P descendant(P left, P right)
{ return std::make_shared<Wrap>(SelectorBranch::descendant(left), right); }
virtual P conjunction(P left, P right)
{ return std::make_shared<Wrap>(SelectorBranch::conjunction(left), right); }
virtual P disjunction(P left, P right)
{ return std::make_shared<Wrap>(SelectorBranch::disjunction(left), right); }
virtual Node &traverse(BuildContext::P context)
{ return context->node().addChild(key_); }
};
SelectorLeaf::P SelectorLeaf::step(const Key &key) {
return std::make_shared<Step>(key);
}
class BranchImpl : public SelectorBranch {
typedef std::function<BuildContext::P (SelectorLeaf &, BuildContext::P,
BuildContext::P)> Traverse;
SelectorLeaf::P first_;
Traverse traverse_;
public:
BranchImpl(SelectorLeaf::P first, Traverse traverse) :
first_(first), traverse_(traverse) {}
virtual BuildContext::P traverse(BuildContext::P context,
BuildContext::P baseContext) {
return traverse_(*first_, context, baseContext);
}
};
SelectorBranch::P SelectorBranch::descendant(SelectorLeaf::P first) {
return std::make_shared<BranchImpl>(first, [](SelectorLeaf &first,
BuildContext::P context, BuildContext::P baseContext) {
(void)baseContext;
return context->descendant(context->traverse(first));
});
}
SelectorBranch::P SelectorBranch::conjunction(SelectorLeaf::P first) {
return std::make_shared<BranchImpl>(first, [](SelectorLeaf &first,
BuildContext::P context, BuildContext::P baseContext) {
return context->conjunction(context->traverse(first), baseContext);
});
}
SelectorBranch::P SelectorBranch::disjunction(SelectorLeaf::P first) {
return std::make_shared<BranchImpl>(first, [](SelectorLeaf &first,
BuildContext::P context, BuildContext::P baseContext) {
return context->disjunction(context->traverse(first), baseContext);
});
}
}}