1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071open!Baseopen!ImportopenPpxlibtypet={message_label:[`Literalofconstant|`String_exprofexpression]option;tags:(arg_label*expression)list;loc:location}letof_extension_payloadextension_payload~loc=letmessage_label,tags=matchExtension_payload.to_argsextension_payloadwith|hd::tl->(* The first argument may be a tag, nothing, or a label. *)(matchhdwith(* [%log log "" ...] has "" ignored *)|Nolabel,[%expr""]->None,tl(* [%log log (a : t1) (b : t2)] treats a and b both as tags *)|_,[%expr([%e?_]:[%t?_])](* [%log log ~label:...] treats the the first arg as a tag as well *)|(Labelled(_:string)|Optional(_:string)),(_:expression)(* [%message] has a special case for [%here]. We can interpret it as a tag. *)|Nolabel,[%expr[%here]]->None,hd::tl(* Unlabelled literals can be interpreted as a label. These should only be
strings in practice. *)|Nolabel,{pexp_desc=Pexp_constantc;_}->Some(`Literalc),tl(* Otherwise, [%log [%e e]] ought to be an unlabelled expression, in which case it
can be interpreted as a string label payload. *)|Nolabel,hd->Some(`String_exprhd),tl)|[]->None,[]in{message_label;tags;loc};;letconstant_to_string_exprconstant~loc=letopen(valAst_builder.makeloc)inmatchconstantwith|Pconst_string_asstring_constant->pexp_constantstring_constant|_->[%exprmatch[%ePpx_sexp_message_expander.sexp_of_labelled_exprs[Nolabel,pexp_constantconstant]~omit_nil:false~loc]with|Atomx->x|List_->assertfalse];;letrender_message_label~loc=function|None->[%exprNone]|Some(`Literalconst)->[%exprSome(String_literal[%econstant_to_string_exprconst~loc])]|Some(`String_exprexpr)->[%exprSome(String[%eexpr])];;letrender{message_label;tags;loc}~render_with_additional_parentheses=letmessage_label=render_message_labelmessage_label~locinlettags=List.maptags~f:Log_tag.parse_arg|>Log_tag.render_list~locinifrender_with_additional_parenthesesthen[%exprPpx_log_types.Message_sexp.create[%emessage_label]~tags:[%etags]~legacy_render_with_additional_parentheses:true]else[%exprPpx_log_types.Message_sexp.create[%emessage_label]~tags:[%etags]];;