mstch/src/token.cpp

50 lines
1.6 KiB
C++
Raw Normal View History

2015-04-12 15:35:13 +02:00
#include "token.hpp"
2015-04-12 15:25:16 +02:00
#include <boost/algorithm/string/trim.hpp>
2015-04-13 10:18:58 +02:00
#include <boost/regex.hpp>
2015-04-09 20:41:27 +02:00
using namespace mstch;
2015-04-13 02:15:51 +02:00
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);
2015-04-13 02:34:27 +02:00
case '!': return std::make_tuple(1, 0, type::comment);
2015-04-13 02:15:51 +02:00
case '{':
if (inside.at(inside.size() - 1) == '}')
return std::make_tuple(1, 1, type::unescaped_variable);
default: return std::make_tuple(0, 0, type::variable);
}
}
2015-04-09 20:41:27 +02:00
token::token(const std::string& raw_token): raw_val(raw_token) {
2015-04-13 10:18:58 +02:00
boost::regex token_match("\\{{2}[^\\}]*\\}{2}|\\{{3}[^\\}]*\\}{3}");
if(boost::regex_match(raw_token, token_match)) {
2015-04-13 02:15:51 +02:00
std::string inside{raw_token.substr(2, raw_token.size() - 4)};
2015-04-12 15:25:16 +02:00
boost::trim(inside);
2015-04-13 02:15:51 +02:00
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);
2015-04-09 20:41:27 +02:00
}
} else {
2015-04-13 02:15:51 +02:00
type_val = type::text;
2015-04-09 20:41:27 +02:00
content_val = raw_token;
}
}
2015-04-13 02:15:51 +02:00
token::type token::token_type() const {
2015-04-09 20:41:27 +02:00
return type_val;
}
std::string token::content() const {
return content_val;
}
std::string token::raw() const {
return raw_val;
}