mstch/src/token.cpp

41 lines
1.4 KiB
C++
Raw Normal View History

2015-04-12 15:35:13 +02:00
#include "token.hpp"
2015-04-15 22:32:27 +02:00
#include "utils.hpp"
2015-04-09 20:41:27 +02:00
using namespace mstch;
2015-04-15 16:13:23 +02:00
token::type token::token_info(char c) {
2015-04-23 12:54:08 +02:00
switch (c) {
2015-04-15 16:13:23 +02:00
case '>': return type::partial;
case '^': return type::inverted_section_open;
case '/': return type::section_close;
case '&': return type::unescaped_variable;
case '#': return type::section_open;
case '!': return type::comment;
default: return type::variable;
2015-04-23 12:54:08 +02:00
}
2015-04-13 02:15:51 +02:00
}
2015-04-23 11:06:44 +02:00
token::token(const std::string& str, std::size_t left, std::size_t right):
2015-04-23 12:54:08 +02:00
m_raw(str), m_eol(false), m_ws_only(false)
2015-04-13 16:35:12 +02:00
{
2015-04-23 12:54:08 +02:00
if (left != 0 && right != 0) {
if (str[left] == '=' && str[str.size() - right - 1] == '=') {
m_type = type::delimiter_change;
} else if (str[left] == '{' && str[str.size() - right - 1] == '}') {
2015-04-23 12:54:08 +02:00
m_type = type::unescaped_variable;
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
2015-04-09 20:41:27 +02:00
} else {
2015-04-23 12:54:08 +02:00
auto c = first_not_ws(str.begin() + left, str.end() - right);
m_type = token_info(*c);
if (m_type != type::variable)
c = first_not_ws(c + 1, str.end() - right);
m_name = {c, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
2015-04-09 20:41:27 +02:00
}
2015-04-23 12:54:08 +02:00
} else {
m_type = type::text;
m_eol = (str.size() > 0 && str[str.size() - 1] == '\n');
2015-05-04 10:38:42 +02:00
m_ws_only = (str.find_first_not_of(" \r\n\t") == std::string::npos);
2015-04-23 12:54:08 +02:00
}
2015-04-09 20:41:27 +02:00
}