@@ -12,18 +12,17 @@ let nat n = I32.to_string_u (I32.of_int_u n)
1212let nat32 = I32. to_string_u
1313
1414let add_hex_char buf c = Printf. bprintf buf " \\ %02x" (Char. code c)
15- let add_char buf c =
16- if c < '\x20' || c > = '\x7f' then
17- add_hex_char buf c
18- else begin
19- if c = '\"' || c = '\\' then Buffer. add_char buf '\\' ;
20- Buffer. add_char buf c
21- end
22- let add_unicode_char buf uc =
23- if uc < 0x20 || uc > = 0x7f then
24- Printf. bprintf buf " \\ u{%02x}" uc
25- else
26- add_char buf (Char. chr uc)
15+ let add_char buf = function
16+ | '\n' -> Buffer. add_string buf " \\ n"
17+ | '\t' -> Buffer. add_string buf " \\ t"
18+ | '\"' -> Buffer. add_string buf " \\\" "
19+ | '\\' -> Buffer. add_string buf " \\\\ "
20+ | c when '\x20' < = c && c < '\x7f' -> Buffer. add_char buf c
21+ | c -> add_hex_char buf c
22+ let add_unicode_char buf = function
23+ | (0x09 | 0x0a ) as uc -> add_char buf (Char. chr uc)
24+ | uc when 0x20 < = uc && uc < 0x7f -> add_char buf (Char. chr uc)
25+ | uc -> Printf. bprintf buf " \\ u{%02x}" uc
2726
2827let string_with iter add_char s =
2928 let buf = Buffer. create 256 in
@@ -49,6 +48,10 @@ let break_bytes s =
4948 let ss = Lib.String. breakup s 16 in
5049 list (atom bytes) ss
5150
51+ let break_string s =
52+ let ss, s' = Lib.List. split_last (Lib.String. split s '\n' ) in
53+ list (atom string ) (List. map (fun s -> s ^ " \n " ) ss @ [s'])
54+
5255
5356(* Types *)
5457
@@ -369,7 +372,10 @@ let module_with_var_opt x_opt m =
369372 )
370373
371374let binary_module_with_var_opt x_opt bs =
372- Node (" module" ^ var_opt x_opt, break_bytes bs)
375+ Node (" module" ^ var_opt x_opt ^ " binary" , break_bytes bs)
376+
377+ let quoted_module_with_var_opt x_opt s =
378+ Node (" module" ^ var_opt x_opt ^ " quote" , break_string s)
373379
374380let module_ = module_with_var_opt None
375381
@@ -384,19 +390,26 @@ let literal lit =
384390 | Values. F64 z -> Node (" f64.const " ^ F64. to_string z, [] )
385391
386392let definition mode x_opt def =
387- match mode, def.it with
388- | `Textual , _ | `Original , Textual _ ->
389- let m =
390- match def.it with
391- | Textual m -> m
392- | Encoded (_ , bs ) -> Decode. decode " " bs
393- in module_with_var_opt x_opt m
394- | `Binary , _ | `Original , Encoded _ ->
395- let bs =
396- match def.it with
397- | Textual m -> Encode. encode m
398- | Encoded (_ , bs ) -> bs
399- in binary_module_with_var_opt x_opt bs
393+ try
394+ match mode, def.it with
395+ | `Textual , _ | `Original , Textual _ ->
396+ let rec unquote def =
397+ match def.it with
398+ | Textual m -> m
399+ | Encoded (_ , bs ) -> Decode. decode " " bs
400+ | Quoted (_ , s ) -> unquote (Parse. string_to_module s)
401+ in module_with_var_opt x_opt (unquote def)
402+ | `Binary , _ | `Original , Encoded _ ->
403+ let rec unquote bs =
404+ match def.it with
405+ | Textual m -> Encode. encode m
406+ | Encoded (_ , bs ) -> bs
407+ | Quoted (_ , s ) -> unquote (Parse. string_to_module s)
408+ in binary_module_with_var_opt x_opt (unquote def)
409+ | `Original , Quoted (_ , s ) ->
410+ quoted_module_with_var_opt x_opt s
411+ with Parse. Syntax _ ->
412+ quoted_module_with_var_opt x_opt " <invalid module>"
400413
401414let access x_opt n =
402415 String. concat " " [var_opt x_opt; name n]
0 commit comments