Created
July 2, 2012 03:06
-
-
Save takahisa/3030757 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static Parser<TToken, IEnumerable<TResult>> Sequence<TToken, TResult>( | |
IEnumerable<Parser<TToken, TResult>> parsers) | |
{ | |
if (parsers == null) | |
throw new ArgumentNullException("parsers"); | |
return stream => | |
{ | |
try | |
{ | |
var current = stream; | |
var results = parsers.Select(p => | |
{ | |
Reply<TToken, TResult> reply; | |
TResult result; ErrorMessage message; | |
switch ((reply = p(current)).TryGetValue(out result, out message)) | |
{ | |
case ReplyStatus.Success: | |
current = reply.Stream; | |
return result; | |
default: | |
throw new YieldBreakException(); | |
} | |
}); | |
return Reply.Success<TToken, IEnumerable<TResult>>(current, results); | |
} | |
catch (YieldBreakException) | |
{ | |
return Reply.Failure<TToken, IEnumerable<TResult>>(stream); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment