Repository navigation
Expand file tree
/
Copy pathhack_format.ml
More file actions
1842 lines (1734 loc) · 55.9 KB
/
Copy pathhack_format.ml
File metadata and controls
1842 lines (1734 loc) · 55.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
module SourceText = Full_fidelity_source_text
module SyntaxKind = Full_fidelity_syntax_kind
module Syntax = Full_fidelity_editable_syntax
module TriviaKind = Full_fidelity_trivia_kind
module Trivia = Full_fidelity_editable_trivia
open Syntax
open Core
(* TODO: move this to a config file *)
let __INDENT_WIDTH = 2
type open_span = {
open_span_start: int;
open_span_cost: Cost.t;
}
type rule_state =
| NoRule
| RuleKind of Rule.kind
| LazyRuleID of int
let open_span start cost = {
open_span_start = start;
open_span_cost = cost;
}
let builder = object (this)
val open_spans = Stack.create ();
val mutable rules = [];
val mutable lazy_rules = ISet.empty;
val mutable chunks = [];
val mutable next_split_rule = NoRule;
val mutable next_lazy_rules = ISet.empty;
val mutable pending_spans = [];
val mutable space_if_not_split = false;
val mutable pending_comma = None;
val mutable pending_whitespace = "";
val mutable chunk_groups = [];
val mutable rule_alloc = Rule_allocator.make ();
val mutable nesting_alloc = Nesting_allocator.make ();
val mutable span_alloc = Span_allocator.make ();
val mutable block_indent = 0;
val mutable in_range = Chunk_group.No;
val mutable start_char = 0;
val mutable end_char = max_int;
val mutable seen_chars = 0;
(* TODO: Make builder into an instantiable class instead of
* having this reset method
*)
method reset start_c end_c =
Stack.clear open_spans;
rules <- [];
lazy_rules <- ISet.empty;
chunks <- [];
next_split_rule <- NoRule;
next_lazy_rules <- ISet.empty;
pending_spans <- [];
space_if_not_split <- false;
pending_comma <- None;
pending_whitespace <- "";
chunk_groups <- [];
rule_alloc <- Rule_allocator.make ();
nesting_alloc <- Nesting_allocator.make ();
span_alloc <- Span_allocator.make ();
block_indent <- 0;
in_range <- Chunk_group.No;
start_char <- start_c;
end_char <- end_c;
seen_chars <- 0;
()
method add_string ?(is_trivia=false) s =
chunks <- (match chunks with
| hd :: tl when hd.Chunk.is_appendable ->
let text = hd.Chunk.text ^ pending_whitespace ^ s in
{hd with Chunk.text = text} :: tl
| _ -> begin
let nesting = nesting_alloc.Nesting_allocator.current_nesting in
let handle_started_next_split_rule () =
let cs = Chunk.make s (List.hd rules) nesting :: chunks in
this#end_rule ();
next_split_rule <- NoRule;
cs
in
match next_split_rule with
| NoRule -> Chunk.make s (List.hd rules) nesting :: chunks
| LazyRuleID rule_id ->
this#start_lazy_rule rule_id;
handle_started_next_split_rule ()
| RuleKind rule_kind ->
this#start_rule_kind ~rule_kind ();
handle_started_next_split_rule ()
end
);
if not is_trivia then begin
lazy_rules <- ISet.union next_lazy_rules lazy_rules;
next_lazy_rules <- ISet.empty;
let rev_pending_spans_stack = List.rev pending_spans in
pending_spans <- [];
List.iter rev_pending_spans_stack ~f:(fun cost ->
Stack.push (open_span (List.length chunks - 1) cost) open_spans
);
end;
pending_whitespace <- ""
method set_pending_comma () =
chunks <- (match chunks with
| hd :: tl when not hd.Chunk.is_appendable ->
{hd with Chunk.comma_rule = Some (List.hd_exn rules)} :: tl;
| _ -> pending_comma <- Some (List.hd_exn rules); chunks;
);
method set_pending_whitespace s =
pending_whitespace <- s
method split space =
chunks <- (match chunks with
| hd :: tl when hd.Chunk.is_appendable ->
let rule_id = hd.Chunk.rule in
let rule = match rules with
| [] when rule_id = Rule.null_rule_id ->
this#create_rule (Rule.Simple Cost.Base)
| hd :: _ when rule_id = Rule.null_rule_id -> hd
| _ -> rule_id
in
let chunk = Chunk.finalize hd rule rule_alloc
space_if_not_split pending_comma in
space_if_not_split <- space;
pending_comma <- None;
chunk :: tl
| _ -> chunks
)
method private create_rule rule_kind =
let ra, rule = Rule_allocator.make_rule rule_alloc rule_kind in
rule_alloc <- ra;
rule.Rule.id
method create_lazy_rule ?(rule_kind=Rule.Simple Cost.Base) () =
let id = this#create_rule rule_kind in
next_lazy_rules <- ISet.add id next_lazy_rules;
id
(* TODO: after unit tests, make this idempotency a property of method split *)
method simple_space_split_if_unsplit () =
match chunks with
| hd :: _ when hd.Chunk.is_appendable -> this#simple_space_split ()
| _ -> ()
method simple_split_if_unsplit () =
match chunks with
| hd :: _ when hd.Chunk.is_appendable -> this#simple_split ()
| _ -> ()
(* TODO: is this the right whitespace strategy? *)
method add_always_empty_chunk () =
let nesting = nesting_alloc.Nesting_allocator.current_nesting in
let create_empty_chunk () =
let rule = this#create_rule Rule.Always in
let chunk = Chunk.make "" (Some rule) nesting in
Chunk.finalize chunk rule rule_alloc false None
in
(chunks <- match chunks with
| [] -> create_empty_chunk () :: chunks
| hd :: _ when not hd.Chunk.is_appendable ->
create_empty_chunk () :: chunks
(* TODO: this is probably wrong, figure out what this means *)
| _ -> chunks)
method simple_space_split ?(cost=Cost.Base) () =
this#split true;
this#set_next_split_rule (RuleKind (Rule.Simple cost));
()
method simple_split ?(cost=Cost.Base) () =
this#split false;
this#set_next_split_rule (RuleKind (Rule.Simple cost));
()
method hard_split () =
this#split false;
this#set_next_split_rule (RuleKind Rule.Always);
()
method set_next_split_rule rule_type =
next_split_rule <- (match next_split_rule with
| RuleKind Rule.Always -> next_split_rule
| _ -> rule_type
)
method nest ?(amount=__INDENT_WIDTH) ?(skip_parent=false) () =
nesting_alloc <- Nesting_allocator.nest nesting_alloc amount skip_parent
method unnest () =
nesting_alloc <- Nesting_allocator.unnest nesting_alloc
method private start_rule_id rule_id =
rule_alloc <- Rule_allocator.mark_dependencies
rule_alloc lazy_rules rules rule_id;
rules <- rule_id :: rules
method start_rule_kind ?(rule_kind=Rule.Simple Cost.Base) () =
(* Override next_split_rule unless it's an Always rule *)
next_split_rule <- (match next_split_rule with
| RuleKind kind when kind <> Rule.Always -> NoRule
| _ -> next_split_rule
);
let rule = this#create_rule rule_kind in
this#start_rule_id rule
method start_lazy_rule lazy_rule_id =
if ISet.mem lazy_rule_id next_lazy_rules then begin
next_lazy_rules <- ISet.remove lazy_rule_id next_lazy_rules;
this#start_rule_id lazy_rule_id
end else if ISet.mem lazy_rule_id lazy_rules then begin
lazy_rules <- ISet.remove lazy_rule_id lazy_rules;
this#start_rule_id lazy_rule_id
end else
raise (Failure "Called start_lazy_rule with a rule id is not a lazy rule")
method end_rule () =
rules <- match rules with
| hd :: tl -> tl
| [] -> [] (*TODO: error *)
method has_rule_kind kind =
List.exists rules ~f:(fun id ->
(Rule_allocator.get_rule_kind rule_alloc id) = kind
)
method start_span ?(cost=Cost.Base) () =
pending_spans <- cost :: pending_spans;
method end_span () =
pending_spans <- match pending_spans with
| hd :: tl -> tl
| [] ->
let os = Stack.pop open_spans in
let sa, span = Span_allocator.make_span span_alloc os.open_span_cost in
span_alloc <- sa;
let r_chunks = List.rev chunks in
chunks <- List.rev_mapi r_chunks ~f:(fun n x ->
if n <= os.open_span_start then
x
else
(* TODO: handle required hard splits *)
{x with Chunk.spans = span :: x.Chunk.spans}
);
[]
(*
TODO: find a better way to represt the rules empty case
for end chunks and block nesting
*)
method start_block_nest () =
if List.is_empty rules
then block_indent <- block_indent + 2
else this#nest ()
method end_block_nest () =
if List.is_empty rules
then block_indent <- block_indent - 2
else this#unnest ()
method end_chunks () =
this#hard_split ();
if List.is_empty rules && not @@ List.is_empty chunks
then this#push_chunk_group ()
method push_chunk_group () =
let print_range = Chunk_group.(match in_range with
| StartAt n when n = List.length chunks -> No
| EndAt n when n = List.length chunks -> All
| _ -> in_range
) in
chunk_groups <- Chunk_group.({
chunks = (List.rev chunks);
rule_map = rule_alloc.Rule_allocator.rule_map;
rule_dependency_map = rule_alloc.Rule_allocator.dependency_map;
block_indentation = block_indent;
print_range;
}) :: chunk_groups;
in_range <- Chunk_group.(match in_range with
| StartAt _ -> All
| All -> All
| Range _ -> No
| EndAt _ -> No
| No -> No
);
chunks <- [];
rule_alloc <- Rule_allocator.make ();
nesting_alloc <- Nesting_allocator.make ();
span_alloc <- Span_allocator.make ()
method _end () =
(*TODO: warn if not empty? *)
if not (List.is_empty chunks) then this#end_chunks ();
List.rev_filter
chunk_groups ~f:(fun cg -> Chunk_group.(cg.print_range <> No))
(* TODO: move the partial formatting logic to it's own module somehow *)
method advance n =
seen_chars <- seen_chars + n;
(* TODO: make this idempotent *)
method check_range () =
in_range <-
if seen_chars <> start_char
then in_range
else begin match chunks with
| [] -> Chunk_group.All
| hd :: _ ->
next_split_rule <- RuleKind Rule.Always;
this#simple_split_if_unsplit ();
Chunk_group.StartAt (List.length chunks)
end;
in_range <-
if seen_chars <> end_char
then in_range
else begin
if List.is_empty chunks
then Chunk_group.No
else begin
let end_at = List.length chunks in
match in_range with
| Chunk_group.StartAt start_at ->
Chunk_group.Range (start_at, end_at)
| Chunk_group.Range _ -> in_range
| _ -> Chunk_group.EndAt end_at
end;
end
method closing_brace_token x =
(* TODO: change this to split and recompose *)
let is_not_end_of_line t = Trivia.kind t <> TriviaKind.EndOfLine in
let rev_leading_trivia = List.rev @@ EditableToken.leading x in
let (rev_after_last_newline, rev_rest_including_last_newline) =
List.split_while rev_leading_trivia ~f:is_not_end_of_line in
this#handle_trivia ~is_leading:true @@
this#break_out_delimited @@ List.rev rev_rest_including_last_newline;
this#end_chunks();
this#end_block_nest ();
this#handle_trivia ~is_leading:true @@
this#break_out_delimited @@ List.rev rev_after_last_newline;
this#handle_token x;
this#handle_trivia ~is_leading:false @@
this#break_out_delimited (EditableToken.trailing x);
()
(* TODO: Handle (aka remove) excess whitespace inside of an ast node *)
method handle_trivia ~is_leading trivia_list =
if not (List.is_empty trivia_list) then begin
let handle_newlines ~is_trivia newlines =
match newlines with
| 0 when is_trivia -> this#simple_space_split_if_unsplit ()
| 0 -> ()
| 1 -> this#hard_split ()
| _ ->
this#hard_split ();
this#add_always_empty_chunk ()
in
this#check_range ();
let newlines, only_whitespace = List.fold trivia_list ~init:(0, true)
~f:(fun (newlines, only_whitespace) t ->
this#advance (Trivia.width t);
(match Trivia.kind t with
| TriviaKind.WhiteSpace -> newlines, only_whitespace
| TriviaKind.EndOfLine ->
if only_whitespace && is_leading then
this#add_always_empty_chunk ();
this#check_range ();
newlines + 1, false
| TriviaKind.Unsafe
| TriviaKind.UnsafeExpression
| TriviaKind.FallThrough
| TriviaKind.FixMe
| TriviaKind.IgnoreError
| TriviaKind.SingleLineComment
| TriviaKind.DelimitedComment ->
handle_newlines ~is_trivia:true newlines;
this#add_string ~is_trivia:true @@ Trivia.text t;
0, false
)
) in
if is_leading
then handle_newlines ~is_trivia:(not only_whitespace) newlines;
end;
method handle_token t =
this#check_range ();
this#add_string (EditableToken.text t);
this#advance (EditableToken.width t);
method token x =
this#handle_trivia
~is_leading:true (this#break_out_delimited @@ EditableToken.leading x);
this#handle_token x;
this#handle_trivia
~is_leading:false (this#break_out_delimited @@ EditableToken.trailing x);
()
method token_trivia_only x =
this#handle_trivia
~is_leading:true (this#break_out_delimited @@ EditableToken.leading x);
this#check_range ();
this#advance (EditableToken.width x);
this#handle_trivia
~is_leading:false (this#break_out_delimited @@ EditableToken.trailing x);
()
method break_out_delimited trivia =
let new_line_regex = Str.regexp "\n" in
List.concat_map trivia ~f:(fun triv ->
match Trivia.kind triv with
| TriviaKind.UnsafeExpression
| TriviaKind.FixMe
| TriviaKind.IgnoreError
| TriviaKind.DelimitedComment ->
let delimited_lines =
Str.split new_line_regex @@ Trivia.text triv in
let map_tail str =
let prefix_space_count str =
let len = String.length str in
let rec aux i =
if i = len || str.[i] <> ' '
then 0
else 1 + (aux (i + 1))
in
aux 0
in
let start_index = min block_indent (prefix_space_count str) in
let len = String.length str - start_index in
let dc = Trivia.make_delimited_comment @@
String.sub str start_index len in
let spacer dc = if 0 = start_index then dc else
Trivia.make_whitespace (String.make start_index ' ') :: dc
in
Trivia.make_eol "\n" :: spacer [dc]
in
Trivia.make_delimited_comment (List.hd_exn delimited_lines) ::
List.concat_map (List.tl_exn delimited_lines) ~f:map_tail
| _ ->
[triv]
)
end
let split ?space:(space=false) () =
builder#split space
let token = builder#token
let add_space () =
builder#add_string " "
let pending_space () =
builder#set_pending_whitespace " "
let rec transform node =
let t = transform in
let span = Some Cost.Base in
let nest = true in
let space = true in
let () = match syntax node with
| Missing -> ()
| Token x ->
token x;
()
| SyntaxList _ ->
raise (Failure (Printf.sprintf
"Error: SyntaxList should never be handled directly;
offending text is '%s'." (text node)));
| ScriptHeader x ->
let (lt, q, lang_kw) = get_script_header_children x in
t lt;
t q;
t lang_kw;
builder#end_chunks ();
| EndOfFile x ->
let token = get_end_of_file_children x in
t token;
| Script x ->
let (header, declarations) = get_script_children x in
t header;
handle_possible_list declarations;
| ScriptFooter x -> t @@ get_script_footer_children x
| SimpleTypeSpecifier x -> t @@ get_simple_type_specifier_children x
| LiteralExpression x ->
(* Double quoted string literals can create a list *)
handle_possible_list @@ get_literal_expression_children x
| VariableExpression x -> t @@ get_variable_expression_children x
| QualifiedNameExpression x -> t @@ get_qualified_name_expression_children x
| PipeVariableExpression x -> t @@ get_pipe_variable_expression_children x
| EnumDeclaration x ->
let (attr, kw, name, colon_kw, base, enum_type, left_b, enumerators,
right_b) = get_enum_declaration_children x in
t attr;
(* TODO: create a "when_present" abstraction to replace this pattern *)
if not (is_missing attr) then builder#end_chunks ();
t kw;
pending_space ();
t name;
t colon_kw;
builder#simple_space_split ();
tl_with ~nest ~f:(fun () ->
pending_space ();
t base;
pending_space ();
t enum_type;
pending_space ();
t left_b;
) ();
builder#end_chunks ();
builder#start_block_nest ();
handle_possible_list enumerators;
transform_and_unnest_closing_brace right_b;
builder#end_chunks ();
| Enumerator x ->
let (name, eq_kw, value, semi) = get_enumerator_children x in
t name;
pending_space ();
t eq_kw;
builder#simple_space_split ();
t_with ~nest value;
t semi;
builder#end_chunks ();
| AliasDeclaration x ->
(* TODO: revisit this for long names *)
let (attr, kw, name, generic, type_constraint, eq_kw, alias_type, semi) =
get_alias_declaration_children x in
t attr;
if not (is_missing attr) then builder#end_chunks ();
t kw;
pending_space ();
t name;
t generic;
pending_space ();
t type_constraint;
pending_space ();
t eq_kw;
builder#simple_space_split ();
t_with ~nest alias_type;
t semi;
builder#end_chunks ();
| PropertyDeclaration x ->
let (modifiers, prop_type, declarators, semi) =
get_property_declaration_children x in
handle_possible_list ~after_each:(fun _ -> add_space ()) modifiers;
t prop_type;
tl_with ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
handle_possible_list ~before_each:(split ~space) declarators;
) ();
t semi;
builder#end_chunks ();
()
| PropertyDeclarator x ->
let (name, prop_initializer) = get_property_declarator_children x in
t name;
t prop_initializer;
()
| NamespaceDeclaration _
| NamespaceBody _
| NamespaceUseDeclaration _
| NamespaceGroupUseDeclaration _
| NamespaceUseClause _ ->
let error = Printf.sprintf "%s not supported - exiting \n"
(SyntaxKind.to_string (kind node)) in
raise (Failure error);
| FunctionDeclaration x ->
let (attr, header, body) = get_function_declaration_children x in
t attr;
if not (is_missing attr) then builder#end_chunks ();
t header;
handle_possible_compound_statement body;
builder#end_chunks ();
()
| FunctionDeclarationHeader x ->
transform_function_declaration_header ~span_started:false x;
()
| WhereClause x ->
let (where, constraints) = get_where_clause_children x in
t where;
add_space ();
t constraints;
()
| WhereConstraint x ->
let (left, op, right) = get_where_constraint_children x in
t left;
add_space ();
t op;
add_space ();
t right;
()
| MethodishDeclaration x ->
let (attr, modifiers, func_decl, body, semi) =
get_methodish_declaration_children x
in
t attr;
if not (is_missing attr) then builder#end_chunks ();
builder#start_span ();
handle_possible_list ~after_each:(fun _ -> add_space ()) modifiers;
(match syntax func_decl with
| FunctionDeclarationHeader x ->
transform_function_declaration_header ~span_started:true x
| _ ->
raise (Failure
"invalid parse tree provided, expecting a function declaration header"
)
);
if not (is_missing body) then handle_possible_compound_statement body;
t semi;
builder#end_chunks ();
()
| ClassishDeclaration x ->
let (attr, modifiers, kw, name, type_params, extends_kw, extends,
impl_kw, impls, body) = get_classish_declaration_children x
in
t attr;
if not (is_missing attr) then builder#end_chunks ();
tl_with ~span ~f:(fun () ->
handle_possible_list ~after_each:(fun _ -> add_space ()) modifiers;
t kw;
split ~space ();
tl_with ~nest ~f:(fun () ->
t name;
t type_params;
) ();
) ();
let after_each_ancestor is_last = if is_last then () else split ~space () in
if not (is_missing extends_kw) then begin
split ~space ();
tl_with ~span ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
t extends_kw;
split ~space ();
tl_with ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
handle_possible_list ~after_each:after_each_ancestor extends
) ();
) ();
()
end;
if not (is_missing impl_kw) then begin
split ~space ();
tl_with ~span ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
t impl_kw;
split ~space ();
tl_with ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
handle_possible_list ~after_each:after_each_ancestor impls
) ();
) ();
()
end;
t body;
()
| ClassishBody x ->
let (left_b, body, right_b) = get_classish_body_children x in
add_space ();
t left_b;
builder#end_chunks ();
builder#start_block_nest ();
handle_possible_list body;
transform_and_unnest_closing_brace right_b;
builder#end_chunks ();
()
| TraitUse x ->
let (kw, elements, semi) = get_trait_use_children x in
t kw;
tl_with ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
handle_possible_list ~before_each:(split ~space) elements;
) ();
t semi;
builder#end_chunks();
()
| RequireClause x ->
let (kw, kind, name, semi) = get_require_clause_children x in
t kw;
add_space ();
t kind;
split ~space ();
t name;
t semi;
builder#end_chunks ();
()
| ConstDeclaration x ->
let (abstr, kw, const_type, declarators, semi) =
get_const_declaration_children x in
t abstr;
if not (is_missing abstr) then add_space ();
t kw;
if not (is_missing const_type) then add_space ();
t const_type;
tl_with ~nest ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
handle_possible_list ~before_each:(split ~space) declarators;
) ();
t semi;
builder#end_chunks ();
()
| ConstantDeclarator x ->
let (name, const_initializer) = get_constant_declarator_children x in
t name;
t const_initializer;
()
| TypeConstDeclaration x ->
let (abs, kw, type_kw, name, type_constraint, eq, type_spec, semi) =
get_type_const_declaration_children x in
t abs;
pending_space ();
t kw;
pending_space ();
t type_kw;
pending_space ();
t name;
pending_space ();
t type_constraint;
pending_space ();
t eq;
builder#simple_space_split ();
t_with ~nest type_spec;
t semi;
builder#end_chunks ();
()
| DecoratedExpression x ->
let (decorator, expr) = get_decorated_expression_children x in
t decorator;
t expr;
()
| ParameterDeclaration x ->
let (attr, visibility, param_type, name, default) =
get_parameter_declaration_children x
in
t attr;
t visibility;
pending_space ();
t param_type;
if is_missing visibility && is_missing param_type
then t name
else begin
builder#simple_space_split ();
t_with ~nest name
end;
t default;
| VariadicParameter x ->
let ellipsis = get_variadic_parameter_children x in
t ellipsis;
| AttributeSpecification x ->
let (left_da, attrs, right_da) = get_attribute_specification_children x in
transform_argish left_da attrs right_da;
()
| Attribute x ->
let (name, left_p, values, right_p) = get_attribute_children x in
t name;
transform_argish left_p values right_p;
()
| InclusionExpression x ->
let (kw, expr) = get_inclusion_expression_children x in
t kw;
builder#simple_space_split ();
t expr;
| InclusionDirective x ->
let (expr, semi) = get_inclusion_directive_children x in
t expr;
t semi;
builder#end_chunks ()
| CompoundStatement x ->
handle_possible_compound_statement node;
| ExpressionStatement x ->
t x.expression_statement_expression;
t x.expression_statement_semicolon;
builder#end_chunks()
| UnsetStatement x ->
let (kw, left_p, args, right_p, semi) = get_unset_statement_children x in
t kw;
transform_argish left_p args right_p;
t semi;
| WhileStatement x ->
t x.while_keyword;
add_space ();
t x.while_left_paren;
split ();
tl_with ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
t_with ~nest x.while_condition;
split ();
t x.while_right_paren;
) ();
handle_possible_compound_statement x.while_body;
builder#end_chunks ();
()
| IfStatement x ->
let (kw, left_p, condition, right_p, if_body, elseif_clauses, else_clause) =
get_if_statement_children x in
t kw;
add_space ();
transform_condition left_p condition right_p;
handle_possible_compound_statement if_body;
handle_possible_list elseif_clauses;
t else_clause;
builder#end_chunks ();
()
| ElseifClause x ->
let (kw, left_p, condition, right_p, body) = get_elseif_clause_children x in
t kw;
pending_space ();
transform_condition left_p condition right_p;
handle_possible_compound_statement x.elseif_statement;
()
| ElseClause x ->
t x.else_keyword;
let _ = match syntax x.else_statement with
| IfStatement _ ->
pending_space ();
t x.else_statement;
pending_space ();
()
| _ -> handle_possible_compound_statement x.else_statement in
()
| TryStatement x ->
(* TODO: revisit *)
let (kw, body, catch_clauses, finally_clause) =
get_try_statement_children x in
t kw;
handle_possible_compound_statement body;
handle_possible_list catch_clauses;
t finally_clause;
builder#end_chunks ();
| CatchClause x ->
let (kw, left_p, ex_type, var, right_p, body) =
get_catch_clause_children x in
t kw;
pending_space ();
t left_p;
split ();
tl_with ~nest ~f:(fun () ->
t ex_type;
pending_space ();
t var;
split ();
) ();
t right_p;
handle_possible_compound_statement body;
();
| FinallyClause x ->
let (kw, body) = get_finally_clause_children x in
t kw;
pending_space ();
handle_possible_compound_statement body;
| DoStatement x ->
let (do_kw, body, while_kw, left_p, cond, right_p, semi) =
get_do_statement_children x in
t do_kw;
pending_space ();
handle_possible_compound_statement body;
t while_kw;
pending_space ();
transform_condition left_p cond right_p;
t semi;
builder#end_chunks ();
| ForStatement x ->
let (kw, left_p, init, semi1, control, semi2, after_iter, right_p, body) =
get_for_statement_children x in
t kw;
add_space ();
t left_p;
tl_with ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
split ();
tl_with ~nest ~f:(fun () ->
handle_possible_list init;
t semi1;
split ~space ();
handle_possible_list control;
t semi2;
split ~space ();
handle_possible_list after_iter;
) ();
split ();
t right_p;
) ();
handle_possible_compound_statement body;
builder#end_chunks ();
()
| ForeachStatement x ->
let (kw, left_p, collection, await_kw, as_kw, key, arrow, value, right_p,
body) = get_foreach_statement_children x in
t kw;
pending_space ();
t left_p;
t collection;
pending_space ();
t await_kw;
pending_space ();
t as_kw;
pending_space ();
t key;
pending_space ();
t arrow;
split ~space ();
t value;
split ();
t right_p;
handle_possible_compound_statement body;
builder#end_chunks ();
()
| SwitchStatement x ->
let (kw, left_p, expr, right_p, left_b, sections, right_b) =
get_switch_statement_children x in
t kw;
add_space ();
t left_p;
split ();
tl_with ~rule:(RuleKind Rule.Argument) ~f:(fun () ->
tl_with ~nest ~f:(fun () -> t expr) ();
t right_p;
) ();
let () = handle_switch_body left_b sections right_b in
builder#end_chunks ();
()
| SwitchSection x ->
raise (Failure "SwitchSection should be handled by handle_switch_body")
| CaseLabel x ->
raise (Failure "CaseLabel should be handled by handle_switch_body")
| DefaultLabel x ->
raise (Failure "DefaultLabel should be handled by handle_switch_body")
| SwitchFallthrough x ->
raise (Failure "SwitchFallthrough should be handled by handle_switch_body")
| ReturnStatement x ->
let (kw, expr, semi) = get_return_statement_children x in
transform_keyword_expression_statement kw expr semi;
()
| ThrowStatement x ->
let (kw, expr, semi) = get_throw_statement_children x in
transform_keyword_expression_statement kw expr semi;
()
| BreakStatement x ->
let (kw, expr, semi) = get_break_statement_children x in
transform_keyword_expression_statement kw expr semi;
()
| ContinueStatement x ->
let (kw, level, semi) = get_continue_statement_children x in
transform_keyword_expression_statement kw level semi;
| FunctionStaticStatement x ->
let (static_kw, declarators, semi) =
get_function_static_statement_children x in
transform_keyword_expr_list_statement static_kw declarators semi;
| StaticDeclarator x ->
let (name, static_initializer) = get_static_declarator_children x in
t name;
t static_initializer;
| EchoStatement x ->
let (kw, expr_list, semi) = get_echo_statement_children x in
transform_keyword_expr_list_statement kw expr_list semi;
| GlobalStatement x ->
let (kw, var_list, semi) = get_global_statement_children x in
transform_keyword_expr_list_statement kw var_list semi;
| SimpleInitializer x ->
let (eq_kw, value) = get_simple_initializer_children x in
pending_space ();
t eq_kw;
builder#simple_space_split ~cost:Cost.Assignment ();
t_with ~nest value;
| AnonymousFunction x ->
let (async_kw, fun_kw, lp, params, rp, colon, ret_type, use, body) =
get_anonymous_function_children x in
t async_kw;
if not (is_missing async_kw) then add_space ();
t fun_kw;
transform_argish_with_return_type ~in_span:false lp params rp colon
ret_type;
t use;
handle_possible_compound_statement body;
builder#end_chunks ();
()
| AnonymousFunctionUseClause x ->
(* TODO: Revisit *)