Skip to content

Instantly share code, notes, and snippets.

@BraedonWooding
Created April 9, 2019 04:39
Show Gist options
  • Save BraedonWooding/712a833d2f843e6e8dd306ae6a2dae3d to your computer and use it in GitHub Desktop.
Save BraedonWooding/712a833d2f843e6e8dd306ae6a2dae3d to your computer and use it in GitHub Desktop.
template<typename Fn, typename ForEach>
std::optional<ParseError> ParseListConjugate(Fn fn, ForEach for_each,
Token pair_sep = Token::Colon,
Token next_pair = Token::Comma) {
int index = 0;
while (true) {
auto first = (this->*fn)();
if (!first) return first.error();
if (!ConsumeToken(pair_sep.type))
return ParseError(ParseError::Kind::MissingToken, "Requiring separator",
pair_sep);
auto second = (this->*fn)();
if (!second) return second.error();
for_each(index, std::move(*first), std::move(*second));
if (stream.PeekCur().type == next_pair.type) {
index++;
stream.PopCur();
} else {
break;
}
}
return std::nullopt;
}
template<typename Fn, typename ForEach>
std::optional<ParseError> ParseList(Fn fn, ForEach for_each,
Token continuer = Token::Comma) {
int index = 0;
while (true) {
auto res = (this->*fn)();
if (!res) return res.error();
for_each(index, std::move(*res));
if (stream.PeekCur().type == continuer.type) {
index++;
stream.PopCur();
} else {
break;
}
}
return std::nullopt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment