-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExpr.cpp
More file actions
136 lines (105 loc) · 3.19 KB
/
StringExpr.cpp
File metadata and controls
136 lines (105 loc) · 3.19 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
#include "StringExpr.hpp"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/function.hpp>
#include <boost/variant.hpp>
#include <boost/version.hpp>
#define foreach BOOST_FOREACH
#if BOOST_VERSION >= 103800
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_confix.hpp>
#include <boost/spirit/include/classic_escape_char.hpp>
#include <boost/spirit/include/classic_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
using namespace boost::spirit::classic;
#else
#include <boost/spirit/core.hpp>
#include <boost/spirit/utility/confix.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/spirit/iterator/multi_pass.hpp>
#include <boost/spirit/iterator/position_iterator.hpp>
using namespace boost::spirit;
#endif
#include <stdexcept>
#include <sstream>
using namespace std;
namespace oneandone {
namespace nms {
struct variable
{
std::string name;
variable(string n) : name(n) {}
};
typedef boost::variant<std::string, variable> node;
struct StringExprPrivate
{
std::vector<node> parsed;
void add_string(const char *first, const char *last) {
parsed.push_back(string(first, last));
}
void add_variable(const char *first, const char *last) {
parsed.push_back(variable(string(first, last)));
}
};
StringExpr::StringExpr(const string& expr) :
string_parse_(new StringExprPrivate),
string_expr_(expr)
{
using boost::bind;
boost::function<void(const char *, const char *)> add_string, add_variable;
add_string = bind(&StringExprPrivate::add_string, string_parse_.get(), _1, _2);
add_variable = bind(&StringExprPrivate::add_variable, string_parse_.get(), _1, _2);
bool success = parse(expr.c_str(), *(
confix_p("${", (+anychar_p) [add_variable], "}")
| ((+(anychar_p - "${")) [add_string])
)).full;
if (!success) {
throw runtime_error("Parsing " + expr + " as a string_expr failed");
}
}
StringExpr::~StringExpr() {}
struct eval_data : public boost::static_visitor<void>
{
std::stringstream result;
const map<string, string> &vars;
eval_data(const map<string, string> &v) : vars(v) {}
void operator()(const std::string& node)
{
result << node;
}
void operator()(const variable& node)
{
map<string, string>::const_iterator it_var = vars.find(node.name);
if (it_var != vars.end())
result << it_var->second;
}
};
const std::string StringExpr::Eval(const std::map<string, string>& vars) const
{
eval_data ret(vars);
foreach (const node &n, string_parse_->parsed) {
boost::apply_visitor(ret, n);
}
return ret.result.str();
}
struct vars_data : public boost::static_visitor<void>
{
std::set<std::string> vars;
void operator()(const std::string& node)
{
}
void operator()(const variable& node)
{
vars.insert(node.name);
}
};
const std::set<std::string> StringExpr::GetVars() const
{
vars_data vars_collector;
foreach (const node &n, string_parse_->parsed) {
boost::apply_visitor(vars_collector, n);
}
return vars_collector.vars;
}
}
}