1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162(* Bridge between YAMLx and the ATD jsonlike AST.
See Atd_yamlx.mli for documentation. *)openAtd_jsonlike(* ===== Location conversion ===== *)(* YAMLx.pos uses 1-based line numbers and 0-based columns.
Atd_jsonlike.Pos.t uses 0-based row and 0-based column. *)letconvert_pos(p:YAMLx.pos):Pos.t={row=p.line-1;(* 1-based → 0-based *)column=p.column;(* already 0-based *)}letconvert_loc?file(l:YAMLx.loc):Loc.t={start=convert_posl.start_pos;end_=convert_posl.end_pos;file;}(* ===== Key conversion ===== *)(* YAML map keys must be strings for JSON object compatibility.
Non-string keys could be converted to strings in multiple ways and
the choice would be arbitrary, so we require users to pre-process
their data if they need non-string keys. *)letkey_to_string?file(key:YAMLx.value):string=letopenYAMLxinmatchkeywith|String(_,s)->s|Nullloc|Bool(loc,_)|Int(loc,_)|Float(loc,_)|Seq(loc,_)|Map(loc,_)->letloc_str=YAMLx.default_format_loc?filelocininvalid_arg(loc_str^"map key must be a string; \
pre-process the YAML document to convert non-string keys if needed")letrecof_yamlx_value_exn?file(v:YAMLx.value):AST.t=letopenYAMLxinletlocl=convert_loc?filelinmatchvwith|Nulll->Null(locl)|Bool(l,b)->Bool(locl,b)|Int(l,i)->Number(locl,Number.of_int64i)|Float(l,f)->Number(locl,Number.of_floatf)|String(l,s)->String(locl,s)|Seq(l,items)->Array(locl,List.map(of_yamlx_value_exn?file)items)|Map(l,pairs)->(* Each pair is (pair_loc, key_value, value_value).
pair_loc is the source range of the key (used as the key location
in the jsonlike Object). *)letconvert_pair(key_loc,key,value)=(convert_loc?filekey_loc,key_to_string?filekey,of_yamlx_value_exn?filevalue)inObject(locl,List.mapconvert_pairpairs)letof_yamlx_value?filev=matchof_yamlx_value_exn?filevwith|result->Okresult|exceptionInvalid_argumentmsg->Error("invalid argument: "^msg)