This commit is contained in:
Daniel Sipka 2015-04-13 02:15:51 +02:00
parent 61a700479d
commit 244477f0a0
20 changed files with 140 additions and 231 deletions

View file

@ -6,46 +6,41 @@
using namespace mstch;
std::tuple<int,int,token::type> token::token_info(const std::string& inside) {
switch (inside.at(0)) {
case '>': return std::make_tuple(1, 0, type::partial);
case '^': return std::make_tuple(1, 0, type::inverted_section_open);
case '/': return std::make_tuple(1, 0, type::section_close);
case '&': return std::make_tuple(1, 0, type::unescaped_variable);
case '#': return std::make_tuple(1, 0, type::section_open);
case '{':
if (inside.at(inside.size() - 1) == '}')
return std::make_tuple(1, 1, type::unescaped_variable);
else
return std::make_tuple(0, 0, type::variable);
case '!': return std::make_tuple(1, 0, type::comment);
default: return std::make_tuple(0, 0, type::variable);
}
}
token::token(const std::string& raw_token): raw_val(raw_token) {
std::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
if(std::regex_match(raw_token, token_match)) {
std::string inside = raw_token.substr(2, raw_token.size() - 4);
std::string inside{raw_token.substr(2, raw_token.size() - 4)};
boost::trim(inside);
if (inside.size() > 0 && inside.at(0) == '#') {
type_val = token_type::section_open;
content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '>') {
type_val = token_type::partial;
content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '^') {
type_val = token_type::inverted_section_open;
content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '/') {
type_val = token_type::section_close;
content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '&') {
type_val = token_type::unescaped_variable;
content_val = inside.substr(1);
} else if (inside.size() > 0 && inside.at(0) == '{' &&
inside.at(inside.size() - 1) == '}')
{
type_val = token_type::unescaped_variable;
content_val = inside.substr(1, inside.size() - 2);
} else if (inside.size() > 0 && inside.at(0) == '!') {
type_val = token_type::comment;
content_val = inside.substr(1);
} else {
type_val = token_type::variable;
content_val = inside;
if (inside.size() > 0) {
int lpad, rpad;
std::tie(lpad, rpad, type_val) = token_info(inside);
content_val = inside.substr(lpad, inside.size() - lpad - rpad);
boost::trim(content_val);
}
boost::trim(content_val);
} else {
type_val = token_type::text;
type_val = type::text;
content_val = raw_token;
}
}
token_type token::type() const {
token::type token::token_type() const {
return type_val;
}