mstch/src/token.cpp

59 lines
2 KiB
C++
Raw Normal View History

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