12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061openPpxlibopenAst_helperletrecexpression_to_patternexpression=letloc=expression.pexp_locinmatchexpression.pexp_descwith|Pexp_ident{txt=Lident"__";_}->Pat.any~loc()|Pexp_ident{txt=Lidentid;_}->Pat.var~loc{txt=id;loc}|Pexp_tupleexpressions->Pat.tuple(List.mapexpression_to_patternexpressions)(* FIXME: record (including _) *)|_->Location.raise_errorf~loc"unsupported pattern in do notation"letexpander~bind~loc=letrecexpander=function|[%expr[%e?{pexp_desc=Pexp_setinstvar(x,e);_}];[%e?next]]->(* x <- e; next *)[%expr[%ebind][%ee](fun[%pPat.varx]->[%eexpandernext])]|[%expr[%e?x]<--[%e?e];[%e?next]]->letx=expression_to_patternxin[%expr[%ebind][%ee](fun[%px]->[%eexpandernext])]|[%expr[%e?e];[%e?next]]->[%expr[%ebind][%ee](fun()->[%eexpandernext])]|[%exprlet[%p?x]=[%e?e]in[%e?next]]->[%exprlet[%px]=[%ee]in[%eexpandernext]]|expression->expressioninexpanderletextract_bind_from_attributes~locattributes=letbind_from_payload~loc=function|PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_identbind;_},_);_}]->bind|_->Location.raise_errorf~loc"the attribute `bind` expects a function identifier"inletmonad_from_payload~loc=function|PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_construct(monad,None);_},_);_}]->monad|_->Location.raise_errorf~loc"the attribute `monad` expects a module identifier"inletrecextract_bind_from_attributes=function|[]->{txt=Lident"bind";loc}|{attr_name={txt="bind";_};attr_payload;attr_loc}::_->bind_from_payload~loc:attr_locattr_payload|{attr_name={txt="monad";_};attr_payload;attr_loc}::_->let{txt;loc}=monad_from_payload~loc:attr_locattr_payloadin{txt=Ldot(txt,"bind");loc}|_::rest->extract_bind_from_attributesrestinExp.ident(extract_bind_from_attributesattributes)letexpander~ctxtexpression_=letloc=Expansion_context.Extension.extension_point_locctxtinletbind=extract_bind_from_attributes~locexpression.pexp_attributesinexpander~bind~locexpressionlet()=letextension=Extension.V3.declare"do"Extension.Context.expressionAst_pattern.(pstr((pstr_eval____)^::nil))expanderinletrule=Ppxlib.Context_free.Rule.extensionextensioninDriver.register_transformation~rules:[rule]"do"