12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788(*
* Copyright yutopp 2017 - .
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*)(**)moduleCombinator=structletrecrepeatpctx=matchpctxwith|Ok((result,rest)asc)->repeatpc|Error_->Okctx(* or *)let(/)papbctx=matchpactxwith|Ok((result,rest)asc)->Okc|Error_->beginmatchpbctxwith|Ok((result,rest)asc)->Okc|Error_->Error()end(* and *)let(>>)papbctx=matchpactxwith|Ok((result,rest)asc)->pbc|Error_aserr->err(* bind *)let(>>=)papbctx=matchpactxwith|Ok((result,rest)asc)->pbresultc|Error_aserr->err(* return *)letreturna(_,rest)=Ok(a,rest)(**)letactpactionctx=matchpctxwith|Ok(result,rest)->let(prev,_)=ctxinletnew_result=actionresultprevinOk(new_result,rest)|Error_aserr->err(**)letlistpnbuf=letrecfold(xs,sub_buf)c=matchc=nwith|true->Ok(xs,sub_buf)|_->beginmatchp(xs,sub_buf)with|Ok(v,rest)->fold(v::xs,rest)(c+1)|Error_aserr->errendinmatchfold([],buf)0with|Ok(rev_list,buf)->Ok(List.revrev_list,buf)|Error_aserr->err(**)letmapfres=matchreswith|Ok(v,rest)->Ok(fv,rest)|Error_aserr->errend