Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libmustache/token.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

2017-10-11 18:48:27 +05:00
#include "token.hpp"
#include "utils.hpp"
using namespace mstch;
2017-10-17 19:29:56 +05:00
token::type token::token_info(char c)
{
switch (c)
{
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;
2017-10-11 18:48:27 +05:00
}
}
2017-10-17 19:29:56 +05:00
token::token(const std::string &str, std::size_t left, std::size_t right) : m_raw(str), m_eol(false), m_ws_only(false)
2017-10-11 18:48:27 +05:00
{
2017-10-17 19:29:56 +05:00
if (left != 0 && right != 0)
{
if (str[left] == '=' && str[str.size() - right - 1] == '=')
{
2017-10-11 18:48:27 +05:00
m_type = type::delimiter_change;
2017-10-17 19:29:56 +05:00
}
else if (str[left] == '{' && str[str.size() - right - 1] == '}')
{
2017-10-11 18:48:27 +05:00
m_type = type::unescaped_variable;
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
2017-10-17 19:29:56 +05:00
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
}
else
{
2017-10-11 18:48:27 +05: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};
2017-10-17 19:29:56 +05:00
m_delims = {{str.begin(), str.begin() + left}, {str.end() - right, str.end()}};
2017-10-11 18:48:27 +05:00
}
2017-10-17 19:29:56 +05:00
}
else
{
2017-10-11 18:48:27 +05:00
m_type = type::text;
m_eol = (str.size() > 0 && str[str.size() - 1] == '\n');
m_ws_only = (str.find_first_not_of(" \r\n\t") == std::string::npos);
}
}