Menu

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

Download this file

125 lines (99 with data), 3.5 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
#ifndef CHILON_PARSER_UNTIL_HPP
#define CHILON_PARSER_UNTIL_HPP
#include <chilon/parser/parse.hpp>
#include <chilon/parser/choice.hpp>
#include <chilon/parser/stored.hpp>
#include <chilon/parser/skipped.hpp>
#include <chilon/meta/flatten.hpp>
#include <chilon/meta/switch_template.hpp>
#include <chilon/meta/list.hpp>
#include <vector>
namespace chilon { namespace parser {
template <class Limit, class Match, bool Skip_whitespace>
struct until_base {
template <class Stream>
inline static bool skip(Stream& stream) {
while (! Limit::skip(stream) && ! stream.empty()) {
if (! Match::skip(stream)) return false;
if (Skip_whitespace) stream.skip_whitespace();
}
return true;
}
template <class Stream>
inline static bool match(Stream& stream) {
auto beginBackup = stream.begin();
while (! Limit::skip(stream) && ! stream.empty()) {
if (! Match::skip(stream)) {
stream.begin() = beginBackup;
return false;
}
if (Skip_whitespace) stream.skip_whitespace();
}
stream.begin() = beginBackup;
return true;
}
};
/**
* Ordered choice (Match .^*? Limit) storing range.
*/
template <class Limit, class Match>
struct until_range : until_base<Limit, Match, false> {};
template <class Limit, class Match>
struct until_list : until_base<Limit, Match, true> {};
template <class Limit, class Match>
struct parse< until_range<Limit, Match> > {
typedef range store_type;
template <class Stream, class Output>
inline static bool skip(Stream& stream, Output& output) {
output.begin() = stream.begin();
while (! Limit::skip(stream) && ! stream.empty()) {
if (! Match::skip(stream)) {
// TODO: don't always reset this
stream.begin() = output.begin();
return false;
}
else {
// is this optimal?
output.end() = stream.begin();
}
}
return true;
}
};
template <class Limit, class Match>
struct parse< until_list<Limit, Match> > {
typedef typename stored<Match>::type vector_element_type;
typedef std::vector<vector_element_type> store_type;
typedef typename meta::flatten_type<
meta::list, choice, Limit>::type limit_list;
template <class Stream, class Output>
inline static bool skip(Stream& stream, Output& output) {
while (! Limit::skip(stream) && ! stream.empty()) {
// TODO: reset instead of recreating in loop?
vector_element_type current_element;
if (! parse<Match>::skip(stream, current_element)) {
return false;
}
else {
output.push_back(current_element);
stream.template skip_whitespace_without<limit_list>();
}
}
return true;
}
};
namespace detail {
template <class Limit, class Match, class W>
struct make_until : parse_forward<until_list<Limit, Match>> {};
template <class Limit, class Match>
struct make_until<Limit, Match, char>
: parse_forward<until_range<Limit, Match>> {};
}
/**
* Forward to either until_range or until_list depending on types.
*/
template <class Limit, class Match>
struct until :
detail::make_until<Limit, Match, typename skipped_store<Match>::type> {};
} }
#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.