Menu

[94f52a]: / parser / char.hpp  Maximize  Restore  History

Download this file

149 lines (123 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef CHILON_PARSER_CHAR_HPP
#define CHILON_PARSER_CHAR_HPP
#include <chilon/parser/detail/char.hpp>
#include <chilon/parser/skip.hpp>
namespace chilon { namespace parser {
template <class T>
struct char_base {
template <class Stream>
inline static bool match(Stream const& stream) {
return T::match_char(stream.front());
}
template <class Stream>
inline static bool skip(Stream& stream) {
if (match(stream)) {
stream.advance();
return true;
}
else return false;
}
};
/**
* @brief Matches multiple characters consecutively.
* @tparam c A parameter pack of the characters to match.
*/
template <char... c>
struct char_;
/**
* @brief Matches a single character.
* @tparam c The character to match.
*/
template <char c>
struct char_<c> : char_base<char_<c>> {
template <class Char>
inline static bool match_char(Char const ch) {
return ch == c;
}
};
template <char... T>
struct char_from;
template <>
struct char_from<> {
template <class Char>
inline static bool match_char(Char const ch) {
return false;
}
};
template <char H, char... T>
struct char_from<H, T...> : char_base<char_from<H, T...>> {
template <class Char>
inline static bool match_char(Char const ch) {
return ch == H || char_from<T...>::match_char(ch);
}
};
template <char... T>
struct parse<char_from<T...>> : detail::parse_char<char_from<T...>> {};
template <char... T>
struct not_char_from;
template <>
struct not_char_from<> {
template <class Char>
inline static bool match_char(Char const ch) {
return true;
}
};
template <char H, char... T>
struct not_char_from<H, T...> : char_base<not_char_from<H, T...>> {
template <class Char>
inline static bool match_char(Char const ch) {
return ch != H && not_char_from<T...>::match_char(ch);
}
};
template <char... T>
struct parse<not_char_from<T...>> : detail::parse_char<not_char_from<T...>> {};
/**
* @brief Matches multiple characters in a row.
* @tparam cs Parameter pack of the characters to match.
*/
template <char... cs>
struct char_ {
template <int idx, char... tail>
struct match_tail;
template <int idx, char head>
struct match_tail<idx, head> {
template <class Stream>
inline static bool match(Stream const& stream) {
return head == stream[idx];
}
};
template <int idx, char head, char... tail>
struct match_tail<idx, head, tail...> {
template <class Stream>
inline static bool match(Stream const& stream) {
return head == stream[idx] && match_tail<idx + 1, tail...>::match(stream);
}
};
template <class Stream>
inline static bool match(Stream const& stream) {
return stream.size() > sizeof...(cs) && match_tail<0, cs...>::match(stream);
}
template <class Stream>
inline static bool skip(Stream& stream) {
if (match(stream)) {
stream.advance(sizeof...(cs));
return true;
}
else return false;
}
};
template <char C>
struct parse<char_<C>> : skip_<char_<C>, detail::parse_char<char_<C>>> {};
template <char... C>
struct parse<char_<C...>> : skip_<char_<C...>, detail::parse_range<char_<C...>>> {};
// TODO: reimplement this in terms of not_char_from
struct non_whitespace : char_base<non_whitespace> {
template <class Char>
inline static bool match_char(Char const ch) {
return ch != ' ' && ch != '\n' && ch != '\t';
}
};
template <>
struct parse<non_whitespace> : detail::parse_char<non_whitespace> {};
} }
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.