-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule_builder.cpp
More file actions
78 lines (58 loc) · 2.12 KB
/
Copy pathrule_builder.cpp
File metadata and controls
78 lines (58 loc) · 2.12 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
#include "ccs/rule_builder.h"
#include "dag/dag_builder.h"
#include "parser/ast.h"
namespace ccs {
struct RuleBuilder::Impl : std::enable_shared_from_this<Impl> {
std::unique_ptr<ast::Nested> ast;
Impl() : ast(new ast::Nested()) {}
virtual ~Impl() {}
void set(const std::string &name, const std::string &value) {
auto def = std::make_unique<ast::PropDef>();
def->name_ = name;
def->value_.setString(StringVal(value));
ast->addRule(std::move(def));
}
void add(std::unique_ptr<ast::Nested> child)
{ ast->addRule(std::move(child)); }
std::shared_ptr<Impl> select(const std::string &name,
const std::vector<std::string> &values);
virtual std::shared_ptr<Impl> &pop() = 0;
};
struct RuleBuilder::Root : RuleBuilder::Impl {
DagBuilder &dag;
Root(DagBuilder &dag) : dag(dag) {}
~Root() { ast->addTo(dag.buildContext(), dag.buildContext()); }
std::shared_ptr<Impl> &pop() { throw std::runtime_error("unmatched pop()!"); }
};
struct RuleBuilder::Child : RuleBuilder::Impl {
std::shared_ptr<Impl> parent;
Child(const std::shared_ptr<Impl> &parent, const std::string &name,
const std::vector<std::string> &values) : parent(parent) {
Key key(name, values);
ast->selector_ = ast::SelectorBranch::conjunction(
ast::SelectorLeaf::step(key));
}
~Child() { parent->add(std::move(ast)); }
std::shared_ptr<Impl> &pop() { return parent; }
};
std::shared_ptr<RuleBuilder::Impl> RuleBuilder::Impl::select(
const std::string &name, const std::vector<std::string> &values) {
return std::shared_ptr<Impl>(new Child(shared_from_this(), name, values));
}
RuleBuilder::RuleBuilder(DagBuilder &dag) : impl(new Root(dag)) {}
RuleBuilder RuleBuilder::pop() {
return RuleBuilder(impl->pop());
}
RuleBuilder RuleBuilder::set(const std::string &name,
const std::string &value) {
impl->set(name, value);
return *this;
}
RuleBuilder RuleBuilder::select(const std::string &name) {
return select(name, std::vector<std::string>());
}
RuleBuilder RuleBuilder::select(const std::string &name,
const std::vector<std::string> &values) {
return RuleBuilder(impl->select(name, values));
}
}