// Parser combinators adapted from perl-friends-2017/parsing-fsharp.org
type Success = true;
type Failure = false;
//infix 1 <&>;
/*
Match a target char in a string
On success, return the match and the rest of the input.
On failure, simply return the entire input.
*/
pChar target subject =
if head subject == target
then (Success, target, tail subject)
else (Failure, subject)
;
/*
Match a sequence of parsers
parser1 and parser2 are functions (ex: parser1 = pChar "a"; parser2 = pChar "b")
Subject is a string.
*/
pAnd parser1 parser2 subject =
// Try the first parser. If it succeeds, try the other
case (parser1 subject) of
(Failure, f ) = (Failure, f);
(Success, match1, rest) = case (parser2 rest) of
(Success, match2, rest2) = (Success, match1 + match2, rest2);
(Failure, s) = (Failure, s);
end;
end;
pA = pChar "a";
pB = pChar "b";
//<&> p1 p2 = pAnd pA pB;
//And now the big test...
pAnd pA pB "abcd";
rule never reduced: Success,match1,rest = case parser2 rest of Failure,s = Failure,s; Success,match2,rest2 = Success,match1+match2,rest2
Got it. Failure and Success defined as symbols.
Yes I will have to re-read the section about types. It's one thing that trips me up, a bit different than what I'm used to.
Maybe I can post my progress as I go, for others to see. and be free to point out where things could be more idiomatic or style improved.