@@ -191,6 +191,31 @@ let pp_direction f (direction : J.for_direction) =
191
191
let return_sp f =
192
192
P. string f L. return ; P. space f
193
193
194
+ let bool f b =
195
+ P. string f (if b then L. true_ else L. false_)
196
+
197
+ let comma_sp f =
198
+ comma f ; P. space f
199
+ let comma_nl f =
200
+ comma f ; P. newline f
201
+
202
+ let debugger_nl f =
203
+ P. newline f ;
204
+ P. string f L. debugger;
205
+ semi f ;
206
+ P. newline f
207
+
208
+ let break_nl f =
209
+ P. string f L. break;
210
+ P. space f ;
211
+ semi f;
212
+ P. newline f
213
+
214
+ let continue f s =
215
+ P. string f L. continue;
216
+ P. space f ;
217
+ P. string f s;
218
+ semi f
194
219
let rec formal_parameter_list cxt (f : P.t ) (is_method : bool ) (l : Ident.t list ) (env : Js_fun_env.t ) =
195
220
let offset = if is_method then 1 else 0 in
196
221
let rec aux i cxt l =
@@ -199,8 +224,7 @@ let rec formal_parameter_list cxt (f : P.t) (is_method : bool) (l : Ident.t list
199
224
| [id] -> ipp_ident cxt f id (Js_fun_env. get_unused env i)
200
225
| id :: r ->
201
226
let cxt = ipp_ident cxt f id (Js_fun_env. get_unused env i) in
202
- comma f;
203
- P. space f;
227
+ comma_sp f;
204
228
aux (i + 1 ) cxt r in
205
229
match l with
206
230
| [] -> cxt
@@ -332,10 +356,8 @@ and pp_function is_method
332
356
P. space f ;
333
357
ignore @@ P. brace_vgroup f 1 (fun _ ->
334
358
let cxt =
335
- if not (Js_fun_env. get_unused env 0 ) then
336
- pp_var_assign_this cxt f this
337
- else
338
- cxt in
359
+ if Js_fun_env. get_unused env 0 then cxt
360
+ else pp_var_assign_this cxt f this in
339
361
function_body cxt f b
340
362
);
341
363
else
@@ -387,29 +409,24 @@ and pp_function is_method
387
409
P. string f L. function_;
388
410
pp_paren_params inner_cxt f lexical;
389
411
P. brace_vgroup f 0 (fun _ ->
390
- begin
391
- return_sp f;
392
- P. string f L. function_;
393
- P. space f ;
394
- (match name with
395
- | No_name -> ()
396
- | Name_non_top x | Name_top x -> ignore (Ext_pp_scope. ident inner_cxt f x));
397
- param_body ()
398
- end );
412
+ return_sp f;
413
+ P. string f L. function_;
414
+ P. space f ;
415
+ (match name with
416
+ | No_name -> ()
417
+ | Name_non_top x | Name_top x -> ignore (Ext_pp_scope. ident inner_cxt f x));
418
+ param_body () );
399
419
pp_paren_params inner_cxt f lexical;
400
420
P. string f L. rparen;
401
- begin match name with
402
- | No_name -> () (* expression *)
403
- | _ -> semi f (* has binding, a statement *)
404
- end
405
- in
406
- (match name with
407
- | Name_top name | Name_non_top name when Ident_set. mem name lexical ->
408
- (* TODO: when calculating lexical we should not include itself *)
409
- let lexical = (Ident_set. remove name lexical) in
410
- handle lexical
411
- | _ -> handle lexical)
412
- in
421
+ match name with
422
+ | No_name -> () (* expression *)
423
+ | _ -> semi f (* has binding, a statement *) in
424
+ handle
425
+ (match name with
426
+ | Name_top name | Name_non_top name when Ident_set. mem name lexical ->
427
+ (* TODO: when calculating lexical we should not include itself *)
428
+ Ident_set. remove name lexical
429
+ | _ -> lexical) in
413
430
enclose lexical return;
414
431
outer_cxt
415
432
@@ -482,12 +499,11 @@ and expression_desc cxt (level:int) f x : cxt =
482
499
| Var v ->
483
500
vident cxt f v
484
501
| Bool b ->
485
- ( if b then P. string f L. true_ else P. string f L. false_ ) ; cxt
502
+ bool f b ; cxt
486
503
| Seq (e1 , e2 ) ->
487
504
P. cond_paren_group f (level > 0 ) 1 (fun () ->
488
505
let cxt = expression 0 cxt f e1 in
489
- comma f;
490
- P. space f ;
506
+ comma_sp f;
491
507
expression 0 cxt f e2 )
492
508
| Fun (method_ , l , b , env ) -> (* TODO: dump for comments *)
493
509
pp_function method_ cxt f false l b env
@@ -533,8 +549,7 @@ and expression_desc cxt (level:int) f x : cxt =
533
549
P. string f L. apply;
534
550
P. paren_group f 1 (fun _ ->
535
551
P. string f L. null;
536
- comma f ;
537
- P. space f ;
552
+ comma_sp f ;
538
553
expression 1 cxt f el
539
554
)
540
555
)
@@ -585,15 +600,14 @@ and expression_desc cxt (level:int) f x : cxt =
585
600
P. paren_group f 1 (fun _ ->
586
601
comma_strings f params
587
602
);
588
- P. brace f (fun _ ->
589
- P. string f s);
603
+ P. brace f (fun _ -> P. string f s);
590
604
cxt
591
605
| Raw_js_code (s ,info ) ->
592
606
(match info with
593
607
| Exp ->
594
- P. string f " ( " ;
608
+ P. string f L. lparen ;
595
609
P. string f s ;
596
- P. string f " ) " ;
610
+ P. string f L. rparen ;
597
611
cxt
598
612
| Stmt ->
599
613
P. newline f ;
@@ -611,15 +625,13 @@ and expression_desc cxt (level:int) f x : cxt =
611
625
-> Int32. to_string v (* check , js convention with ocaml lexical convention *)
612
626
| Uint i
613
627
-> Format. asprintf " %lu" i
614
- | Nint i -> Nativeint. to_string i
615
- in
628
+ | Nint i -> Nativeint. to_string i in
616
629
let need_paren =
617
630
if s.[0 ] = '-'
618
631
then level > 13 (* Negative numbers may need to be parenthesized. *)
619
632
else level = 15 (* Parenthesize as well when followed by a dot. *)
620
633
&& s.[0 ] <> 'I' (* Infinity *)
621
- && s.[0 ] <> 'N' (* NaN *)
622
- in
634
+ && s.[0 ] <> 'N' (* NaN *) in
623
635
let action = fun _ -> P. string f s in
624
636
(
625
637
if need_paren
@@ -944,7 +956,7 @@ and expression_desc cxt (level:int) f x : cxt =
944
956
[level 1] is correct, however
945
957
to make nice indentation , force nested conditional to be parenthesized
946
958
*)
947
- let cxt = ( P. group f 1 @@ fun _ -> expression 3 cxt f e1) in
959
+ let cxt = P. group f 1 ( fun _ -> expression 3 cxt f e1) in
948
960
(* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *)
949
961
P. space f;
950
962
P. string f L. colon;
@@ -974,43 +986,25 @@ and expression_desc cxt (level:int) f x : cxt =
974
986
P. paren_group f 1 action
975
987
else action ()
976
988
977
- and property_name cxt f (s : J.property_name ) : unit =
978
- Js_dump_property. property_key f s
979
-
980
-
981
- and property_name_and_value_list cxt f l : cxt =
982
- match l with
983
- | [] -> cxt
984
- | [(pn, e)] ->
985
- property_name cxt f pn ;
989
+ and property_name_and_value_list cxt f l =
990
+ iter_lst cxt f l (fun cxt f (pn ,e ) ->
991
+ Js_dump_property. property_key f pn ;
986
992
P. string f L. colon;
987
993
P. space f;
988
994
expression 1 cxt f e
989
- | (pn , e ) :: r ->
990
- property_name cxt f pn ;
991
- P. string f L. colon;
992
- P. space f;
993
- let cxt = expression 1 cxt f e in
994
- comma f;
995
- P. newline f;
996
- property_name_and_value_list cxt f r
997
-
995
+ ) comma_nl
998
996
and array_element_list cxt f el : cxt =
999
- iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e )
1000
- (fun f -> comma f; P. newline f)
997
+ iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e ) comma_nl
1001
998
1002
999
and arguments cxt f l : cxt =
1003
- iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e)
1004
- (fun f -> comma f; P. space f)
1000
+ iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e) comma_sp
1005
1001
1006
-
1007
1002
and variable_declaration top cxt f
1008
1003
(variable : J.variable_declaration ) : cxt =
1009
1004
(* TODO: print [const/var] for different backends *)
1010
1005
match variable with
1011
1006
| {ident = i ; value = None ; ident_info ; _} ->
1012
- if ident_info.used_stats = Dead_pure
1013
- then cxt
1007
+ if ident_info.used_stats = Dead_pure then cxt
1014
1008
else pp_var_declare cxt f i
1015
1009
| { ident = name ; value = Some e ; ident_info = {used_stats; _} } ->
1016
1010
match used_stats with
@@ -1072,8 +1066,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1072
1066
let cxt =
1073
1067
(
1074
1068
if exp_need_paren e
1075
- then ( P. paren_group f 1 )
1076
- else ( P. group f 0 )
1069
+ then P. paren_group f 1
1070
+ else P. group f 0
1077
1071
) (fun _ -> expression 0 cxt f e ) in
1078
1072
semi f;
1079
1073
cxt
@@ -1088,7 +1082,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1088
1082
| If (e , s1 , s2 ) -> (* TODO: always brace those statements *)
1089
1083
P. string f L. if_;
1090
1084
P. space f;
1091
- let cxt = P. paren_group f 1 @@ fun _ -> expression 0 cxt f e in
1085
+ let cxt = P. paren_group f 1 ( fun _ -> expression 0 cxt f e) in
1092
1086
P. space f;
1093
1087
let cxt = block cxt f s1 in
1094
1088
(match s2 with
@@ -1119,14 +1113,14 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1119
1113
match e.expression_desc with
1120
1114
| Number (Int {i = 1l } ) ->
1121
1115
P. string f L. while_;
1122
- P. string f " ( " ;
1116
+ P. string f L. lparen ;
1123
1117
P. string f L. true_;
1124
- P. string f " ) " ;
1118
+ P. string f L. rparen ;
1125
1119
P. space f ;
1126
1120
cxt
1127
1121
| _ ->
1128
1122
P. string f L. while_;
1129
- let cxt = P. paren_group f 1 @@ fun _ -> expression 0 cxt f e in
1123
+ let cxt = P. paren_group f 1 ( fun _ -> expression 0 cxt f e) in
1130
1124
P. space f ;
1131
1125
cxt
1132
1126
in
@@ -1201,37 +1195,19 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1201
1195
let lexical = Ident_set. elements lexical in
1202
1196
P. vgroup f 0
1203
1197
(fun _ ->
1204
-
1205
- P. string f " (function " ;
1198
+ P. string f L. lparen;
1199
+ P. string f L. function_ ;
1206
1200
pp_paren_params inner_cxt f lexical;
1207
1201
let cxt = P. brace_vgroup f 0 (fun _ -> action inner_cxt) in
1208
1202
pp_paren_params inner_cxt f lexical;
1209
- P. string f " ) " ;
1203
+ P. string f L. rparen ;
1210
1204
semi f;
1211
1205
cxt
1212
1206
)
1213
- | Continue s ->
1214
- P. string f L. continue;
1215
- P. space f ;
1216
- P. string f s;
1217
- semi f;
1218
- (* P.newline f; *)
1219
- (* #2642 *)
1220
- cxt
1221
- | Debugger
1222
- ->
1223
- P. newline f ;
1224
- P. string f L. debugger;
1225
- semi f ;
1226
- P. newline f;
1227
- cxt
1228
- | Break
1229
- ->
1230
- P. string f L. break;
1231
- P. space f ;
1232
- semi f;
1233
- P. newline f;
1234
- cxt
1207
+ | Continue s -> continue f s ; cxt
1208
+ (* P.newline f; #2642 *)
1209
+ | Debugger -> debugger_nl f ; cxt
1210
+ | Break -> break_nl f; cxt
1235
1211
1236
1212
| Return {return_value = e } ->
1237
1213
begin match e with
@@ -1269,27 +1245,24 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1269
1245
| String_switch (e , cc , def ) ->
1270
1246
P. string f L. switch;
1271
1247
P. space f;
1272
- let cxt = P. paren_group f 1 @@ fun _ -> expression 0 cxt f e
1273
- in
1248
+ let cxt = P. paren_group f 1 @@ fun _ -> expression 0 cxt f e in
1274
1249
P. space f;
1275
- P. brace_vgroup f 1 @@ fun _ ->
1276
- let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string. pp_string f i ) cc in
1277
- (match def with
1278
- | None -> cxt
1279
- | Some def ->
1280
- P. group f 1 @@ fun _ ->
1281
- P. string f L. default;
1282
- P. string f L. colon;
1283
- P. newline f;
1284
- statement_list false cxt f def )
1285
-
1250
+ P. brace_vgroup f 1 (fun _ ->
1251
+ let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string. pp_string f i ) cc in
1252
+ match def with
1253
+ | None -> cxt
1254
+ | Some def ->
1255
+ P. group f 1 (fun _ ->
1256
+ P. string f L. default;
1257
+ P. string f L. colon;
1258
+ P. newline f;
1259
+ statement_list false cxt f def ))
1286
1260
| Throw e ->
1287
1261
P. string f L. throw;
1288
1262
P. space f ;
1289
- P. group f throw_indent @@ fun _ ->
1290
-
1291
- let cxt = expression 0 cxt f e in
1292
- semi f ; cxt
1263
+ P. group f throw_indent (fun _ ->
1264
+ let cxt = expression 0 cxt f e in
1265
+ semi f ; cxt)
1293
1266
1294
1267
(* There must be a space between the return and its
1295
1268
argument. A line return would not work *)
@@ -1307,16 +1280,15 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1307
1280
P. string f " catch (" ;
1308
1281
let cxt = Ext_pp_scope. ident cxt f i in
1309
1282
P. string f " )" ;
1310
- block cxt f b
1311
- in
1312
- begin match fin with
1313
- | None -> cxt
1314
- | Some b ->
1315
- P. group f 1 @@ fun _ ->
1316
- P. string f L. finally;
1317
- P. space f;
1318
- block cxt f b
1319
- end
1283
+ block cxt f b in
1284
+ match fin with
1285
+ | None -> cxt
1286
+ | Some b ->
1287
+ P. group f 1 (fun _ ->
1288
+ P. string f L. finally;
1289
+ P. space f;
1290
+ block cxt f b)
1291
+
1320
1292
and function_body cxt f b =
1321
1293
match b with
1322
1294
| [] -> cxt
0 commit comments