Skip to content

Instantly share code, notes, and snippets.

@Bloodb0ne
Created March 20, 2020 12:29
Show Gist options
  • Save Bloodb0ne/e5f1750b42496ae379fa8ccbbcce0bc9 to your computer and use it in GitHub Desktop.
Save Bloodb0ne/e5f1750b42496ae379fa8ccbbcce0bc9 to your computer and use it in GitHub Desktop.
Parser Combinators Example
template<typename Parser>
struct Repeat {
using is_parser_type = std::true_type;
using return_type = typename std::vector<typename Parser::return_type>;
using temp_result_type = typename Parser::return_type;
Parser const p;
Repeat(Parser const& p_) :p(p_) {};
template<typename Iterator>
bool operator()(Iterator& it, Iterator end, return_type* result) const {
Iterator backtrack = it;
size_t cnt = 0;
temp_result_type tmpres;
while (p(it, end, &tmpres)) {
result->push_back(tmpres);
backtrack = it;
++cnt;
}
if (cnt == 0) return false;
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment