syntax_tree 2.0.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1213 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class Visitor
5
+ class PrettyPrintVisitor < Visitor
6
+ attr_reader :q
7
+
8
+ def initialize(q)
9
+ @q = q
10
+ end
11
+
12
+ def visit_aref(node)
13
+ node("aref") do
14
+ field("collection", node.collection)
15
+ field("index", node.index)
16
+ comments(node)
17
+ end
18
+ end
19
+
20
+ def visit_aref_field(node)
21
+ node("aref_field") do
22
+ field("collection", node.collection)
23
+ field("index", node.index)
24
+ comments(node)
25
+ end
26
+ end
27
+
28
+ def visit_alias(node)
29
+ node("alias") do
30
+ field("left", node.left)
31
+ field("right", node.right)
32
+ comments(node)
33
+ end
34
+ end
35
+
36
+ def visit_arg_block(node)
37
+ node("arg_block") do
38
+ field("value", node.value) if node.value
39
+ comments(node)
40
+ end
41
+ end
42
+
43
+ def visit_arg_paren(node)
44
+ node("arg_paren") do
45
+ field("arguments", node.arguments)
46
+ comments(node)
47
+ end
48
+ end
49
+
50
+ def visit_arg_star(node)
51
+ node("arg_star") do
52
+ field("value", node.value)
53
+ comments(node)
54
+ end
55
+ end
56
+
57
+ def visit_args(node)
58
+ node("args") do
59
+ list("parts", node.parts)
60
+ comments(node)
61
+ end
62
+ end
63
+
64
+ def visit_args_forward(node)
65
+ visit_token("args_forward", node)
66
+ end
67
+
68
+ def visit_array(node)
69
+ node("array") do
70
+ field("contents", node.contents)
71
+ comments(node)
72
+ end
73
+ end
74
+
75
+ def visit_aryptn(node)
76
+ node("aryptn") do
77
+ field("constant", node.constant) if node.constant
78
+ list("requireds", node.requireds) if node.requireds.any?
79
+ field("rest", node.rest) if node.rest
80
+ list("posts", node.posts) if node.posts.any?
81
+ comments(node)
82
+ end
83
+ end
84
+
85
+ def visit_assign(node)
86
+ node("assign") do
87
+ field("target", node.target)
88
+ field("value", node.value)
89
+ comments(node)
90
+ end
91
+ end
92
+
93
+ def visit_assoc(node)
94
+ node("assoc") do
95
+ field("key", node.key)
96
+ field("value", node.value) if node.value
97
+ comments(node)
98
+ end
99
+ end
100
+
101
+ def visit_assoc_splat(node)
102
+ node("assoc_splat") do
103
+ field("value", node.value)
104
+ comments(node)
105
+ end
106
+ end
107
+
108
+ def visit_backref(node)
109
+ visit_token("backref", node)
110
+ end
111
+
112
+ def visit_backtick(node)
113
+ visit_token("backtick", node)
114
+ end
115
+
116
+ def visit_bare_assoc_hash(node)
117
+ node("bare_assoc_hash") do
118
+ list("assocs", node.assocs)
119
+ comments(node)
120
+ end
121
+ end
122
+
123
+ def visit_BEGIN(node)
124
+ node("BEGIN") do
125
+ field("statements", node.statements)
126
+ comments(node)
127
+ end
128
+ end
129
+
130
+ def visit_begin(node)
131
+ node("begin") do
132
+ field("bodystmt", node.bodystmt)
133
+ comments(node)
134
+ end
135
+ end
136
+
137
+ def visit_binary(node)
138
+ node("binary") do
139
+ field("left", node.left)
140
+ text("operator", node.operator)
141
+ field("right", node.right)
142
+ comments(node)
143
+ end
144
+ end
145
+
146
+ def visit_blockarg(node)
147
+ node("blockarg") do
148
+ field("name", node.name) if node.name
149
+ comments(node)
150
+ end
151
+ end
152
+
153
+ def visit_block_var(node)
154
+ node("block_var") do
155
+ field("params", node.params)
156
+ list("locals", node.locals) if node.locals.any?
157
+ comments(node)
158
+ end
159
+ end
160
+
161
+ def visit_bodystmt(node)
162
+ node("bodystmt") do
163
+ field("statements", node.statements)
164
+ field("rescue_clause", node.rescue_clause) if node.rescue_clause
165
+ field("else_clause", node.else_clause) if node.else_clause
166
+ field("ensure_clause", node.ensure_clause) if node.ensure_clause
167
+ comments(node)
168
+ end
169
+ end
170
+
171
+ def visit_brace_block(node)
172
+ node("brace_block") do
173
+ field("block_var", node.block_var) if node.block_var
174
+ field("statements", node.statements)
175
+ comments(node)
176
+ end
177
+ end
178
+
179
+ def visit_break(node)
180
+ node("break") do
181
+ field("arguments", node.arguments)
182
+ comments(node)
183
+ end
184
+ end
185
+
186
+ def visit_call(node)
187
+ node("call") do
188
+ field("receiver", node.receiver)
189
+ field("operator", node.operator)
190
+ field("message", node.message)
191
+ field("arguments", node.arguments) if node.arguments
192
+ comments(node)
193
+ end
194
+ end
195
+
196
+ def visit_case(node)
197
+ node("case") do
198
+ field("keyword", node.keyword)
199
+ field("value", node.value) if node.value
200
+ field("consequent", node.consequent)
201
+ comments(node)
202
+ end
203
+ end
204
+
205
+ def visit_CHAR(node)
206
+ visit_token("CHAR", node)
207
+ end
208
+
209
+ def visit_class(node)
210
+ node("class") do
211
+ field("constant", node.constant)
212
+ field("superclass", node.superclass) if node.superclass
213
+ field("bodystmt", node.bodystmt)
214
+ comments(node)
215
+ end
216
+ end
217
+
218
+ def visit_comma(node)
219
+ node("comma") do
220
+ field("value", node)
221
+ end
222
+ end
223
+
224
+ def visit_command(node)
225
+ node("command") do
226
+ field("message", node.message)
227
+ field("arguments", node.arguments)
228
+ comments(node)
229
+ end
230
+ end
231
+
232
+ def visit_command_call(node)
233
+ node("command_call") do
234
+ field("receiver", node.receiver)
235
+ field("operator", node.operator)
236
+ field("message", node.message)
237
+ field("arguments", node.arguments) if node.arguments
238
+ comments(node)
239
+ end
240
+ end
241
+
242
+ def visit_comment(node)
243
+ node("comment") do
244
+ field("value", node.value)
245
+ end
246
+ end
247
+
248
+ def visit_const(node)
249
+ visit_token("const", node)
250
+ end
251
+
252
+ def visit_const_path_field(node)
253
+ node("const_path_field") do
254
+ field("parent", node.parent)
255
+ field("constant", node.constant)
256
+ comments(node)
257
+ end
258
+ end
259
+
260
+ def visit_const_path_ref(node)
261
+ node("const_path_ref") do
262
+ field("parent", node.parent)
263
+ field("constant", node.constant)
264
+ comments(node)
265
+ end
266
+ end
267
+
268
+ def visit_const_ref(node)
269
+ node("const_ref") do
270
+ field("constant", node.constant)
271
+ comments(node)
272
+ end
273
+ end
274
+
275
+ def visit_cvar(node)
276
+ visit_token("cvar", node)
277
+ end
278
+
279
+ def visit_def(node)
280
+ node("def") do
281
+ field("name", node.name)
282
+ field("params", node.params)
283
+ field("bodystmt", node.bodystmt)
284
+ comments(node)
285
+ end
286
+ end
287
+
288
+ def visit_def_endless(node)
289
+ node("def_endless") do
290
+ if node.target
291
+ field("target", node.target)
292
+ field("operator", node.operator)
293
+ end
294
+
295
+ field("name", node.name)
296
+ field("paren", node.paren) if node.paren
297
+ field("statement", node.statement)
298
+ comments(node)
299
+ end
300
+ end
301
+
302
+ def visit_defined(node)
303
+ node("defined") do
304
+ field("value", node.value)
305
+ comments(node)
306
+ end
307
+ end
308
+
309
+ def visit_defs(node)
310
+ node("defs") do
311
+ field("target", node.target)
312
+ field("operator", node.operator)
313
+ field("name", node.name)
314
+ field("params", node.params)
315
+ field("bodystmt", node.bodystmt)
316
+ comments(node)
317
+ end
318
+ end
319
+
320
+ def visit_do_block(node)
321
+ node("do_block") do
322
+ field("block_var", node.block_var) if node.block_var
323
+ field("bodystmt", node.bodystmt)
324
+ comments(node)
325
+ end
326
+ end
327
+
328
+ def visit_dot2(node)
329
+ node("dot2") do
330
+ field("left", node.left) if node.left
331
+ field("right", node.right) if node.right
332
+ comments(node)
333
+ end
334
+ end
335
+
336
+ def visit_dot3(node)
337
+ node("dot3") do
338
+ field("left", node.left) if node.left
339
+ field("right", node.right) if node.right
340
+ comments(node)
341
+ end
342
+ end
343
+
344
+ def visit_dyna_symbol(node)
345
+ node("dyna_symbol") do
346
+ list("parts", node.parts)
347
+ comments(node)
348
+ end
349
+ end
350
+
351
+ def visit_END(node)
352
+ node("END") do
353
+ field("statements", node.statements)
354
+ comments(node)
355
+ end
356
+ end
357
+
358
+ def visit_else(node)
359
+ node("else") do
360
+ field("statements", node.statements)
361
+ comments(node)
362
+ end
363
+ end
364
+
365
+ def visit_elsif(node)
366
+ node("elsif") do
367
+ field("predicate", node.predicate)
368
+ field("statements", node.statements)
369
+ field("consequent", node.consequent) if node.consequent
370
+ comments(node)
371
+ end
372
+ end
373
+
374
+ def visit_embdoc(node)
375
+ node("embdoc") do
376
+ field("value", node.value)
377
+ end
378
+ end
379
+
380
+ def visit_embexpr_beg(node)
381
+ node("embexpr_beg") do
382
+ field("value", node.value)
383
+ end
384
+ end
385
+
386
+ def visit_embexpr_end(node)
387
+ node("embexpr_end") do
388
+ field("value", node.value)
389
+ end
390
+ end
391
+
392
+ def visit_embvar(node)
393
+ node("embvar") do
394
+ field("value", node.value)
395
+ end
396
+ end
397
+
398
+ def visit_ensure(node)
399
+ node("ensure") do
400
+ field("statements", node.statements)
401
+ comments(node)
402
+ end
403
+ end
404
+
405
+ def visit_excessed_comma(node)
406
+ visit_token("excessed_comma", node)
407
+ end
408
+
409
+ def visit_fcall(node)
410
+ node("fcall") do
411
+ field("value", node.value)
412
+ field("arguments", node.arguments) if node.arguments
413
+ comments(node)
414
+ end
415
+ end
416
+
417
+ def visit_field(node)
418
+ node("field") do
419
+ field("parent", node.parent)
420
+ field("operator", node.operator)
421
+ field("name", node.name)
422
+ comments(node)
423
+ end
424
+ end
425
+
426
+ def visit_float(node)
427
+ visit_token("float", node)
428
+ end
429
+
430
+ def visit_fndptn(node)
431
+ node("fndptn") do
432
+ field("constant", node.constant) if node.constant
433
+ field("left", node.left)
434
+ list("values", node.values)
435
+ field("right", node.right)
436
+ comments(node)
437
+ end
438
+ end
439
+
440
+ def visit_for(node)
441
+ node("for") do
442
+ field("index", node.index)
443
+ field("collection", node.collection)
444
+ field("statements", node.statements)
445
+ comments(node)
446
+ end
447
+ end
448
+
449
+ def visit_gvar(node)
450
+ visit_token("gvar", node)
451
+ end
452
+
453
+ def visit_hash(node)
454
+ node("hash") do
455
+ list("assocs", node.assocs) if node.assocs.any?
456
+ comments(node)
457
+ end
458
+ end
459
+
460
+ def visit_heredoc(node)
461
+ node("heredoc") do
462
+ list("parts", node.parts)
463
+ comments(node)
464
+ end
465
+ end
466
+
467
+ def visit_heredoc_beg(node)
468
+ visit_token("heredoc_beg", node)
469
+ end
470
+
471
+ def visit_hshptn(node)
472
+ node("hshptn") do
473
+ field("constant", node.constant) if node.constant
474
+
475
+ if node.keywords.any?
476
+ q.breakable
477
+ q.group(2, "(", ")") do
478
+ q.seplist(node.keywords) do |(key, value)|
479
+ q.group(2, "(", ")") do
480
+ key.pretty_print(q)
481
+
482
+ if value
483
+ q.breakable
484
+ value.pretty_print(q)
485
+ end
486
+ end
487
+ end
488
+ end
489
+ end
490
+
491
+ field("keyword_rest", node.keyword_rest) if node.keyword_rest
492
+ comments(node)
493
+ end
494
+ end
495
+
496
+ def visit_ident(node)
497
+ visit_token("ident", node)
498
+ end
499
+
500
+ def visit_if(node)
501
+ node("if") do
502
+ field("predicate", node.predicate)
503
+ field("statements", node.statements)
504
+ field("consequent", node.consequent) if node.consequent
505
+ comments(node)
506
+ end
507
+ end
508
+
509
+ def visit_if_mod(node)
510
+ node("if_mod") do
511
+ field("statement", node.statement)
512
+ field("predicate", node.predicate)
513
+ comments(node)
514
+ end
515
+ end
516
+
517
+ def visit_if_op(node)
518
+ node("ifop") do
519
+ field("predicate", node.predicate)
520
+ field("truthy", node.truthy)
521
+ field("falsy", node.falsy)
522
+ comments(node)
523
+ end
524
+ end
525
+
526
+ def visit_imaginary(node)
527
+ visit_token("imaginary", node)
528
+ end
529
+
530
+ def visit_in(node)
531
+ node("in") do
532
+ field("pattern", node.pattern)
533
+ field("statements", node.statements)
534
+ field("consequent", node.consequent) if node.consequent
535
+ comments(node)
536
+ end
537
+ end
538
+
539
+ def visit_int(node)
540
+ visit_token("int", node)
541
+ end
542
+
543
+ def visit_ivar(node)
544
+ visit_token("ivar", node)
545
+ end
546
+
547
+ def visit_kw(node)
548
+ visit_token("kw", node)
549
+ end
550
+
551
+ def visit_kwrest_param(node)
552
+ node("kwrest_param") do
553
+ field("name", node.name)
554
+ comments(node)
555
+ end
556
+ end
557
+
558
+ def visit_label(node)
559
+ node("label") do
560
+ q.breakable
561
+ q.text(":")
562
+ q.text(node.value[0...-1])
563
+ comments(node)
564
+ end
565
+ end
566
+
567
+ def visit_label_end(node)
568
+ node("label_end") do
569
+ field("value", node.value)
570
+ end
571
+ end
572
+
573
+ def visit_lambda(node)
574
+ node("lambda") do
575
+ field("params", node.params)
576
+ field("statements", node.statements)
577
+ comments(node)
578
+ end
579
+ end
580
+
581
+ def visit_lbrace(node)
582
+ visit_token("lbrace", node)
583
+ end
584
+
585
+ def visit_lbracket(node)
586
+ visit_token("lbracket", node)
587
+ end
588
+
589
+ def visit_lparen(node)
590
+ visit_token("lparen", node)
591
+ end
592
+
593
+ def visit_massign(node)
594
+ node("massign") do
595
+ field("target", node.target)
596
+ field("value", node.value)
597
+ comments(node)
598
+ end
599
+ end
600
+
601
+ def visit_method_add_block(node)
602
+ node("method_add_block") do
603
+ field("call", node.call)
604
+ field("block", node.block)
605
+ comments(node)
606
+ end
607
+ end
608
+
609
+ def visit_mlhs(node)
610
+ node("mlhs") do
611
+ list("parts", node.parts)
612
+ comments(node)
613
+ end
614
+ end
615
+
616
+ def visit_mlhs_paren(node)
617
+ node("mlhs_paren") do
618
+ field("contents", node.contents)
619
+ comments(node)
620
+ end
621
+ end
622
+
623
+ def visit_module(node)
624
+ node("module") do
625
+ field("constant", node.constant)
626
+ field("bodystmt", node.bodystmt)
627
+ comments(node)
628
+ end
629
+ end
630
+
631
+ def visit_mrhs(node)
632
+ node("mrhs") do
633
+ list("parts", node.parts)
634
+ comments(node)
635
+ end
636
+ end
637
+
638
+ def visit_next(node)
639
+ node("next") do
640
+ field("arguments", node.arguments)
641
+ comments(node)
642
+ end
643
+ end
644
+
645
+ def visit_not(node)
646
+ node("not") do
647
+ field("statement", node.statement)
648
+ comments(node)
649
+ end
650
+ end
651
+
652
+ def visit_op(node)
653
+ visit_token("op", node)
654
+ end
655
+
656
+ def visit_opassign(node)
657
+ node("opassign") do
658
+ field("target", node.target)
659
+ field("operator", node.operator)
660
+ field("value", node.value)
661
+ comments(node)
662
+ end
663
+ end
664
+
665
+ def visit_params(node)
666
+ node("params") do
667
+ list("requireds", node.requireds) if node.requireds.any?
668
+
669
+ if node.optionals.any?
670
+ q.breakable
671
+ q.group(2, "(", ")") do
672
+ q.seplist(node.optionals) do |(name, default)|
673
+ name.pretty_print(q)
674
+ q.text("=")
675
+ q.group(2) do
676
+ q.breakable("")
677
+ default.pretty_print(q)
678
+ end
679
+ end
680
+ end
681
+ end
682
+
683
+ field("rest", node.rest) if node.rest
684
+ list("posts", node.posts) if node.posts.any?
685
+
686
+ if node.keywords.any?
687
+ q.breakable
688
+ q.group(2, "(", ")") do
689
+ q.seplist(node.keywords) do |(name, default)|
690
+ name.pretty_print(q)
691
+
692
+ if default
693
+ q.text("=")
694
+ q.group(2) do
695
+ q.breakable("")
696
+ default.pretty_print(q)
697
+ end
698
+ end
699
+ end
700
+ end
701
+ end
702
+
703
+ field("keyword_rest", node.keyword_rest) if node.keyword_rest
704
+ field("block", node.block) if node.block
705
+ comments(node)
706
+ end
707
+ end
708
+
709
+ def visit_paren(node)
710
+ node("paren") do
711
+ field("contents", node.contents)
712
+ comments(node)
713
+ end
714
+ end
715
+
716
+ def visit_period(node)
717
+ visit_token("period", node)
718
+ end
719
+
720
+ def visit_pinned_begin(node)
721
+ node("pinned_begin") do
722
+ field("statement", node.statement)
723
+ comments(node)
724
+ end
725
+ end
726
+
727
+ def visit_pinned_var_ref(node)
728
+ node("pinned_var_ref") do
729
+ field("value", node.value)
730
+ comments(node)
731
+ end
732
+ end
733
+
734
+ def visit_program(node)
735
+ node("program") do
736
+ field("statements", node.statements)
737
+ comments(node)
738
+ end
739
+ end
740
+
741
+ def visit_qsymbols(node)
742
+ node("qsymbols") do
743
+ list("elements", node.elements)
744
+ comments(node)
745
+ end
746
+ end
747
+
748
+ def visit_qsymbols_beg(node)
749
+ node("qsymbols_beg") do
750
+ field("value", node.value)
751
+ end
752
+ end
753
+
754
+ def visit_qwords(node)
755
+ node("qwords") do
756
+ list("elements", node.elements)
757
+ comments(node)
758
+ end
759
+ end
760
+
761
+ def visit_qwords_beg(node)
762
+ node("qwords_beg") do
763
+ field("value", node.value)
764
+ end
765
+ end
766
+
767
+ def visit_rassign(node)
768
+ node("rassign") do
769
+ field("value", node.value)
770
+ field("operator", node.operator)
771
+ field("pattern", node.pattern)
772
+ comments(node)
773
+ end
774
+ end
775
+
776
+ def visit_rational(node)
777
+ visit_token("rational", node)
778
+ end
779
+
780
+ def visit_rbrace(node)
781
+ node("rbrace") do
782
+ field("value", node.value)
783
+ end
784
+ end
785
+
786
+ def visit_rbracket(node)
787
+ node("rbracket") do
788
+ field("value", node.value)
789
+ end
790
+ end
791
+
792
+ def visit_redo(node)
793
+ visit_token("redo", node)
794
+ end
795
+
796
+ def visit_regexp_beg(node)
797
+ node("regexp_beg") do
798
+ field("value", node.value)
799
+ end
800
+ end
801
+
802
+ def visit_regexp_content(node)
803
+ node("regexp_content") do
804
+ list("parts", node.parts)
805
+ end
806
+ end
807
+
808
+ def visit_regexp_end(node)
809
+ node("regexp_end") do
810
+ field("value", node.value)
811
+ end
812
+ end
813
+
814
+ def visit_regexp_literal(node)
815
+ node("regexp_literal") do
816
+ list("parts", node.parts)
817
+ comments(node)
818
+ end
819
+ end
820
+
821
+ def visit_rescue(node)
822
+ node("rescue") do
823
+ field("exception", node.exception) if node.exception
824
+ field("statements", node.statements)
825
+ field("consequent", node.consequent) if node.consequent
826
+ comments(node)
827
+ end
828
+ end
829
+
830
+ def visit_rescue_ex(node)
831
+ node("rescue_ex") do
832
+ field("exceptions", node.exceptions)
833
+ field("variable", node.variable)
834
+ comments(node)
835
+ end
836
+ end
837
+
838
+ def visit_rescue_mod(node)
839
+ node("rescue_mod") do
840
+ field("statement", node.statement)
841
+ field("value", node.value)
842
+ comments(node)
843
+ end
844
+ end
845
+
846
+ def visit_rest_param(node)
847
+ node("rest_param") do
848
+ field("name", node.name)
849
+ comments(node)
850
+ end
851
+ end
852
+
853
+ def visit_retry(node)
854
+ visit_token("retry", node)
855
+ end
856
+
857
+ def visit_return(node)
858
+ node("return") do
859
+ field("arguments", node.arguments)
860
+ comments(node)
861
+ end
862
+ end
863
+
864
+ def visit_return0(node)
865
+ visit_token("return0", node)
866
+ end
867
+
868
+ def visit_rparen(node)
869
+ node("rparen") do
870
+ field("value", node.value)
871
+ end
872
+ end
873
+
874
+ def visit_sclass(node)
875
+ node("sclass") do
876
+ field("target", node.target)
877
+ field("bodystmt", node.bodystmt)
878
+ comments(node)
879
+ end
880
+ end
881
+
882
+ def visit_statements(node)
883
+ node("statements") do
884
+ list("body", node.body)
885
+ comments(node)
886
+ end
887
+ end
888
+
889
+ def visit_string_concat(node)
890
+ node("string_concat") do
891
+ field("left", node.left)
892
+ field("right", node.right)
893
+ comments(node)
894
+ end
895
+ end
896
+
897
+ def visit_string_content(node)
898
+ node("string_content") do
899
+ list("parts", node.parts)
900
+ end
901
+ end
902
+
903
+ def visit_string_dvar(node)
904
+ node("string_dvar") do
905
+ field("variable", node.variable)
906
+ comments(node)
907
+ end
908
+ end
909
+
910
+ def visit_string_embexpr(node)
911
+ node("string_embexpr") do
912
+ field("statements", node.statements)
913
+ comments(node)
914
+ end
915
+ end
916
+
917
+ def visit_string_literal(node)
918
+ node("string_literal") do
919
+ list("parts", node.parts)
920
+ comments(node)
921
+ end
922
+ end
923
+
924
+ def visit_super(node)
925
+ node("super") do
926
+ field("arguments", node.arguments)
927
+ comments(node)
928
+ end
929
+ end
930
+
931
+ def visit_symbeg(node)
932
+ node("symbeg") do
933
+ field("value", node.value)
934
+ end
935
+ end
936
+
937
+ def visit_symbol_content(node)
938
+ node("symbol_content") do
939
+ field("value", node.value)
940
+ end
941
+ end
942
+
943
+ def visit_symbol_literal(node)
944
+ node("symbol_literal") do
945
+ field("value", node.value)
946
+ comments(node)
947
+ end
948
+ end
949
+
950
+ def visit_symbols(node)
951
+ node("symbols") do
952
+ list("elements", node.elements)
953
+ comments(node)
954
+ end
955
+ end
956
+
957
+ def visit_symbols_beg(node)
958
+ node("symbols_beg") do
959
+ field("value", node.value)
960
+ end
961
+ end
962
+
963
+ def visit_tlambda(node)
964
+ node("tlambda") do
965
+ field("value", node.value)
966
+ end
967
+ end
968
+
969
+ def visit_tlambeg(node)
970
+ node("tlambeg") do
971
+ field("value", node.value)
972
+ end
973
+ end
974
+
975
+ def visit_top_const_field(node)
976
+ node("top_const_field") do
977
+ field("constant", node.constant)
978
+ comments(node)
979
+ end
980
+ end
981
+
982
+ def visit_top_const_ref(node)
983
+ node("top_const_ref") do
984
+ field("constant", node.constant)
985
+ comments(node)
986
+ end
987
+ end
988
+
989
+ def visit_tstring_beg(node)
990
+ node("tstring_beg") do
991
+ field("value", node.value)
992
+ end
993
+ end
994
+
995
+ def visit_tstring_content(node)
996
+ visit_token("tstring_content", node)
997
+ end
998
+
999
+ def visit_tstring_end(node)
1000
+ node("tstring_end") do
1001
+ field("value", node.value)
1002
+ end
1003
+ end
1004
+
1005
+ def visit_unary(node)
1006
+ node("unary") do
1007
+ field("operator", node.operator)
1008
+ field("statement", node.statement)
1009
+ comments(node)
1010
+ end
1011
+ end
1012
+
1013
+ def visit_undef(node)
1014
+ node("undef") do
1015
+ list("symbols", node.symbols)
1016
+ comments(node)
1017
+ end
1018
+ end
1019
+
1020
+ def visit_unless(node)
1021
+ node("unless") do
1022
+ field("predicate", node.predicate)
1023
+ field("statements", node.statements)
1024
+ field("consequent", node.consequent) if node.consequent
1025
+ comments(node)
1026
+ end
1027
+ end
1028
+
1029
+ def visit_unless_mod(node)
1030
+ node("unless_mod") do
1031
+ field("statement", node.statement)
1032
+ field("predicate", node.predicate)
1033
+ comments(node)
1034
+ end
1035
+ end
1036
+
1037
+ def visit_until(node)
1038
+ node("until") do
1039
+ field("predicate", node.predicate)
1040
+ field("statements", node.statements)
1041
+ comments(node)
1042
+ end
1043
+ end
1044
+
1045
+ def visit_until_mod(node)
1046
+ node("until_mod") do
1047
+ field("statement", node.statement)
1048
+ field("predicate", node.predicate)
1049
+ comments(node)
1050
+ end
1051
+ end
1052
+
1053
+ def visit_var_alias(node)
1054
+ node("var_alias") do
1055
+ field("left", node.left)
1056
+ field("right", node.right)
1057
+ comments(node)
1058
+ end
1059
+ end
1060
+
1061
+ def visit_var_field(node)
1062
+ node("var_field") do
1063
+ field("value", node.value)
1064
+ comments(node)
1065
+ end
1066
+ end
1067
+
1068
+ def visit_var_ref(node)
1069
+ node("var_ref") do
1070
+ field("value", node.value)
1071
+ comments(node)
1072
+ end
1073
+ end
1074
+
1075
+ def visit_vcall(node)
1076
+ node("vcall") do
1077
+ field("value", node.value)
1078
+ comments(node)
1079
+ end
1080
+ end
1081
+
1082
+ def visit_void_stmt(node)
1083
+ node("void_stmt") do
1084
+ comments(node)
1085
+ end
1086
+ end
1087
+
1088
+ def visit_when(node)
1089
+ node("when") do
1090
+ field("arguments", node.arguments)
1091
+ field("statements", node.statements)
1092
+ field("consequent", node.consequent) if node.consequent
1093
+ comments(node)
1094
+ end
1095
+ end
1096
+
1097
+ def visit_while(node)
1098
+ node("while") do
1099
+ field("predicate", node.predicate)
1100
+ field("statements", node.statements)
1101
+ comments(node)
1102
+ end
1103
+ end
1104
+
1105
+ def visit_while_mod(node)
1106
+ node("while_mod") do
1107
+ field("statement", node.statement)
1108
+ field("predicate", node.predicate)
1109
+ comments(node)
1110
+ end
1111
+ end
1112
+
1113
+ def visit_word(node)
1114
+ node("word") do
1115
+ list("parts", node.parts)
1116
+ comments(node)
1117
+ end
1118
+ end
1119
+
1120
+ def visit_words(node)
1121
+ node("words") do
1122
+ list("elements", node.elements)
1123
+ comments(node)
1124
+ end
1125
+ end
1126
+
1127
+ def visit_words_beg(node)
1128
+ node("words_beg") do
1129
+ field("value", node.value)
1130
+ end
1131
+ end
1132
+
1133
+ def visit_xstring(node)
1134
+ node("xstring") do
1135
+ list("parts", node.parts)
1136
+ end
1137
+ end
1138
+
1139
+ def visit_xstring_literal(node)
1140
+ node("xstring_literal") do
1141
+ list("parts", node.parts)
1142
+ comments(node)
1143
+ end
1144
+ end
1145
+
1146
+ def visit_yield(node)
1147
+ node("yield") do
1148
+ field("arguments", node.arguments)
1149
+ comments(node)
1150
+ end
1151
+ end
1152
+
1153
+ def visit_yield0(node)
1154
+ visit_token("yield0", node)
1155
+ end
1156
+
1157
+ def visit_zsuper(node)
1158
+ visit_token("zsuper", node)
1159
+ end
1160
+
1161
+ def visit___end__(node)
1162
+ visit_token("__end__", node)
1163
+ end
1164
+
1165
+ private
1166
+
1167
+ def comments(node)
1168
+ return if node.comments.empty?
1169
+
1170
+ q.breakable
1171
+ q.group(2, "(", ")") do
1172
+ q.seplist(node.comments) { |comment| comment.pretty_print(q) }
1173
+ end
1174
+ end
1175
+
1176
+ def field(_name, value)
1177
+ q.breakable
1178
+
1179
+ # I don't entirely know why this is necessary, but in Ruby 2.7 there is
1180
+ # an issue with calling q.pp on strings that somehow involves inspect
1181
+ # keys. I'm purposefully avoiding the inspect key stuff here because I
1182
+ # know the tree does not contain any cycles.
1183
+ value.is_a?(String) ? q.text(value.inspect) : value.pretty_print(q)
1184
+ end
1185
+
1186
+ def list(_name, values)
1187
+ q.breakable
1188
+ q.group(2, "(", ")") do
1189
+ q.seplist(values) { |value| value.pretty_print(q) }
1190
+ end
1191
+ end
1192
+
1193
+ def node(type)
1194
+ q.group(2, "(", ")") do
1195
+ q.text(type)
1196
+ yield
1197
+ end
1198
+ end
1199
+
1200
+ def text(_name, value)
1201
+ q.breakable
1202
+ q.text(value)
1203
+ end
1204
+
1205
+ def visit_token(type, node)
1206
+ node(type) do
1207
+ field("value", node.value)
1208
+ comments(node)
1209
+ end
1210
+ end
1211
+ end
1212
+ end
1213
+ end