]> Creatis software - cpPlugins.git/blob - lib/mstch/token.cpp
Moved to version 1.0
[cpPlugins.git] / lib / mstch / token.cpp
1 #include "token.hpp"
2 #include "utils.hpp"
3
4 using namespace mstch;
5
6 token::type token::token_info(char c) {
7   switch (c) {
8     case '>': return type::partial;
9     case '^': return type::inverted_section_open;
10     case '/': return type::section_close;
11     case '&': return type::unescaped_variable;
12     case '#': return type::section_open;
13     case '!': return type::comment;
14     default: return type::variable;
15   }
16 }
17
18 token::token(const std::string& str, std::size_t left, std::size_t right):
19     m_raw(str), m_eol(false), m_ws_only(false)
20 {
21   if (left != 0 && right != 0) {
22     if (str[left] == '=' && str[str.size() - right - 1] == '=') {
23       m_type = type::delimiter_change;
24     } else if (str[left] == '{' && str[str.size() - right - 1] == '}') {
25       m_type = type::unescaped_variable;
26       m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
27           first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
28     } else {
29       auto c = first_not_ws(str.begin() + left, str.end() - right);
30       m_type = token_info(*c);
31       if (m_type != type::variable)
32         c = first_not_ws(c + 1, str.end() - right);
33       m_name = {c, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
34       m_delims = {{str.begin(), str.begin() + left},
35           {str.end() - right, str.end()}};
36     }
37   } else {
38     m_type = type::text;
39     m_eol = (str.size() > 0 && str[str.size() - 1] == '\n');
40     m_ws_only = (str.find_first_not_of(" \r\n\t") == std::string::npos);
41   }
42 }