syntax_tree 2.3.1 → 2.4.0

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