1 #include <boost/algorithm/string.hpp>
2 #include <boost/lexical_cast.hpp>
3 #include <boost/tokenizer.hpp>
15 const std::string &_expr) {
17 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
18 boost::char_separator<char> sep1(
","), sep2(
" \t",
":");
19 tokenizer tok1(_expr, sep1);
20 std::vector<std::string> split_expr(tok1.begin(), tok1.end());
21 std::vector<long> ind_vec_begin(split_expr.size());
22 std::vector<long> ind_vec_end(split_expr.size());
23 for (
Index i = 0; i < split_expr.size(); i++) {
24 tokenizer tok2(split_expr[i], sep2);
25 std::vector<std::string> ind_expr(tok2.begin(), tok2.end());
26 if (ind_expr.size() == 1) {
27 if (ind_expr[0][0] ==
':')
28 ind_vec_begin[i] = -1;
30 ind_vec_begin[i] = boost::lexical_cast<long>(ind_expr[0]);
31 ind_vec_end[i] = ind_vec_begin[i];
32 }
else if (ind_expr.size() == 3) {
33 ind_vec_begin[i] = boost::lexical_cast<long>(ind_expr[0]);
34 ind_vec_end[i] = boost::lexical_cast<long>(ind_expr[2]);
36 throw std::runtime_error(
38 "In index_expression_to_bounds(), invalid index expression \"") +
43 return std::make_pair(std::move(ind_vec_begin), std::move(ind_vec_end));
48 std::string::const_iterator end_it) {
49 for (++it; it != end_it; ++it) {
53 else if ((*it) ==
'\'')
64 std::vector<std::string> &tag_names,
65 std::vector<std::string> &sub_exprs) {
66 std::string::const_iterator it(input_expr.cbegin()),
67 it_end(input_expr.cend()), t_it1, t_it2;
68 while (it != it_end) {
69 while (it != it_end && (isspace(*it) || boost::is_any_of(
",#")(*it))) ++it;
70 if (it == it_end)
break;
77 if (t_it2 == t_it1 || (*t_it2) !=
'\'') {
78 throw std::runtime_error(
79 "Mismatched quotation marks in expression:\n \"" + input_expr +
84 while (it != it_end && !(isspace(*it) || (*it) ==
',') && (*it) !=
'(' &&
87 if (*it ==
'\\') ++it;
92 throw std::runtime_error(
93 "Mismatched parentheses in expression:\n \"" + input_expr +
98 tag_names.push_back(std::string(t_it1, it));
99 sub_exprs.push_back(std::string());
102 if (it == it_end || (*it) !=
'(')
continue;
105 while (it != it_end && isspace(*(++it)))
109 throw std::runtime_error(
"Stray comma in expression:\n \"" +
110 input_expr +
"\"\n");
116 Index paren_depth(0);
117 while (it != it_end && !((*it) ==
')' && paren_depth == 0)) {
121 else if ((*it) ==
'(')
123 else if ((*it) ==
')')
130 throw std::runtime_error(
"Mismatched parentheses in expression:\n \"" +
131 input_expr +
"\"\n");
134 while (isspace(*(--t_it2)))
138 throw std::runtime_error(
"Stray comma in expression:\n \"" +
139 input_expr +
"\"\n");
141 sub_exprs.back() = std::string(t_it1, ++t_it2);
std::pair< std::vector< long >, std::vector< long > > index_expression_to_bounds(const std::string &_expr)
std::string::const_iterator end_of_literal(std::string::const_iterator it, std::string::const_iterator end_it)
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
INDEX_TYPE Index
For long integer indexing: