Created
March 20, 2020 12:29
-
-
Save Bloodb0ne/e5f1750b42496ae379fa8ccbbcce0bc9 to your computer and use it in GitHub Desktop.
Parser Combinators Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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