syntax_tree 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,548 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ class Visitor
5
+ # This is raised when you use the Visitor.visit_method method and it fails.
6
+ # It is correctable to through DidYouMean.
7
+ class VisitMethodError < StandardError
8
+ attr_reader :visit_method
9
+
10
+ def initialize(visit_method)
11
+ @visit_method = visit_method
12
+ super("Invalid visit method: #{visit_method}")
13
+ end
14
+ end
15
+
16
+ # This class is used by DidYouMean to offer corrections to invalid visit
17
+ # method names.
18
+ class VisitMethodChecker
19
+ attr_reader :visit_method
20
+
21
+ def initialize(error)
22
+ @visit_method = error.visit_method
23
+ end
24
+
25
+ def corrections
26
+ @corrections ||=
27
+ DidYouMean::SpellChecker.new(dictionary: Visitor.visit_methods).correct(visit_method)
28
+ end
29
+
30
+ DidYouMean.correct_error(VisitMethodError, self)
31
+ end
32
+
33
+ class << self
34
+ # This method is here to help folks write visitors.
35
+ #
36
+ # It's not always easy to ensure you're writing the correct method name in
37
+ # the visitor since it's perfectly valid to define methods that don't
38
+ # override these parent methods.
39
+ #
40
+ # If you use this method, you can ensure you're writing the correct method
41
+ # name. It will raise an error if the visit method you're defining isn't
42
+ # actually a method on the parent visitor.
43
+ def visit_method(method_name)
44
+ return if visit_methods.include?(method_name)
45
+
46
+ raise VisitMethodError, method_name
47
+ end
48
+
49
+ # This is the list of all of the valid visit methods.
50
+ def visit_methods
51
+ @visit_methods ||=
52
+ Visitor.instance_methods.grep(/^visit_(?!child_nodes)/)
53
+ end
54
+ end
55
+
56
+ def visit(node)
57
+ node&.accept(self)
58
+ end
59
+
60
+ def visit_all(nodes)
61
+ nodes.map { |node| visit(node) }
62
+ end
63
+
64
+ def visit_child_nodes(node)
65
+ visit_all(node.child_nodes)
66
+ end
67
+
68
+ # Visit an ARef node.
69
+ alias visit_aref visit_child_nodes
70
+
71
+ # Visit an ARefField node.
72
+ alias visit_aref_field visit_child_nodes
73
+
74
+ # Visit an Alias node.
75
+ alias visit_alias visit_child_nodes
76
+
77
+ # Visit an ArgBlock node.
78
+ alias visit_arg_block visit_child_nodes
79
+
80
+ # Visit an ArgParen node.
81
+ alias visit_arg_paren visit_child_nodes
82
+
83
+ # Visit an ArgStar node.
84
+ alias visit_arg_star visit_child_nodes
85
+
86
+ # Visit an Args node.
87
+ alias visit_args visit_child_nodes
88
+
89
+ # Visit an ArgsForward node.
90
+ alias visit_args_forward visit_child_nodes
91
+
92
+ # Visit an ArrayLiteral node.
93
+ alias visit_array visit_child_nodes
94
+
95
+ # Visit an AryPtn node.
96
+ alias visit_aryptn visit_child_nodes
97
+
98
+ # Visit an Assign node.
99
+ alias visit_assign visit_child_nodes
100
+
101
+ # Visit an Assoc node.
102
+ alias visit_assoc visit_child_nodes
103
+
104
+ # Visit an AssocSplat node.
105
+ alias visit_assoc_splat visit_child_nodes
106
+
107
+ # Visit a Backref node.
108
+ alias visit_backref visit_child_nodes
109
+
110
+ # Visit a Backtick node.
111
+ alias visit_backtick visit_child_nodes
112
+
113
+ # Visit a BareAssocHash node.
114
+ alias visit_bare_assoc_hash visit_child_nodes
115
+
116
+ # Visit a BEGINBlock node.
117
+ alias visit_BEGIN visit_child_nodes
118
+
119
+ # Visit a Begin node.
120
+ alias visit_begin visit_child_nodes
121
+
122
+ # Visit a Binary node.
123
+ alias visit_binary visit_child_nodes
124
+
125
+ # Visit a BlockArg node.
126
+ alias visit_blockarg visit_child_nodes
127
+
128
+ # Visit a BlockVar node.
129
+ alias visit_block_var visit_child_nodes
130
+
131
+ # Visit a BodyStmt node.
132
+ alias visit_bodystmt visit_child_nodes
133
+
134
+ # Visit a BraceBlock node.
135
+ alias visit_brace_block visit_child_nodes
136
+
137
+ # Visit a Break node.
138
+ alias visit_break visit_child_nodes
139
+
140
+ # Visit a Call node.
141
+ alias visit_call visit_child_nodes
142
+
143
+ # Visit a Case node.
144
+ alias visit_case visit_child_nodes
145
+
146
+ # Visit a CHAR node.
147
+ alias visit_CHAR visit_child_nodes
148
+
149
+ # Visit a ClassDeclaration node.
150
+ alias visit_class visit_child_nodes
151
+
152
+ # Visit a Comma node.
153
+ alias visit_comma visit_child_nodes
154
+
155
+ # Visit a Command node.
156
+ alias visit_command visit_child_nodes
157
+
158
+ # Visit a CommandCall node.
159
+ alias visit_command_call visit_child_nodes
160
+
161
+ # Visit a Comment node.
162
+ alias visit_comment visit_child_nodes
163
+
164
+ # Visit a Const node.
165
+ alias visit_const visit_child_nodes
166
+
167
+ # Visit a ConstPathField node.
168
+ alias visit_const_path_field visit_child_nodes
169
+
170
+ # Visit a ConstPathRef node.
171
+ alias visit_const_path_ref visit_child_nodes
172
+
173
+ # Visit a ConstRef node.
174
+ alias visit_const_ref visit_child_nodes
175
+
176
+ # Visit a CVar node.
177
+ alias visit_cvar visit_child_nodes
178
+
179
+ # Visit a Def node.
180
+ alias visit_def visit_child_nodes
181
+
182
+ # Visit a DefEndless node.
183
+ alias visit_def_endless visit_child_nodes
184
+
185
+ # Visit a Defined node.
186
+ alias visit_defined visit_child_nodes
187
+
188
+ # Visit a Defs node.
189
+ alias visit_defs visit_child_nodes
190
+
191
+ # Visit a DoBlock node.
192
+ alias visit_do_block visit_child_nodes
193
+
194
+ # Visit a Dot2 node.
195
+ alias visit_dot2 visit_child_nodes
196
+
197
+ # Visit a Dot3 node.
198
+ alias visit_dot3 visit_child_nodes
199
+
200
+ # Visit a DynaSymbol node.
201
+ alias visit_dyna_symbol visit_child_nodes
202
+
203
+ # Visit an ENDBlock node.
204
+ alias visit_END visit_child_nodes
205
+
206
+ # Visit an Else node.
207
+ alias visit_else visit_child_nodes
208
+
209
+ # Visit an Elsif node.
210
+ alias visit_elsif visit_child_nodes
211
+
212
+ # Visit an EmbDoc node.
213
+ alias visit_embdoc visit_child_nodes
214
+
215
+ # Visit an EmbExprBeg node.
216
+ alias visit_embexpr_beg visit_child_nodes
217
+
218
+ # Visit an EmbExprEnd node.
219
+ alias visit_embexpr_end visit_child_nodes
220
+
221
+ # Visit an EmbVar node.
222
+ alias visit_embvar visit_child_nodes
223
+
224
+ # Visit an Ensure node.
225
+ alias visit_ensure visit_child_nodes
226
+
227
+ # Visit an ExcessedComma node.
228
+ alias visit_excessed_comma visit_child_nodes
229
+
230
+ # Visit a FCall node.
231
+ alias visit_fcall visit_child_nodes
232
+
233
+ # Visit a Field node.
234
+ alias visit_field visit_child_nodes
235
+
236
+ # Visit a FloatLiteral node.
237
+ alias visit_float visit_child_nodes
238
+
239
+ # Visit a FndPtn node.
240
+ alias visit_fndptn visit_child_nodes
241
+
242
+ # Visit a For node.
243
+ alias visit_for visit_child_nodes
244
+
245
+ # Visit a GVar node.
246
+ alias visit_gvar visit_child_nodes
247
+
248
+ # Visit a HashLiteral node.
249
+ alias visit_hash visit_child_nodes
250
+
251
+ # Visit a Heredoc node.
252
+ alias visit_heredoc visit_child_nodes
253
+
254
+ # Visit a HeredocBeg node.
255
+ alias visit_heredoc_beg visit_child_nodes
256
+
257
+ # Visit a HshPtn node.
258
+ alias visit_hshptn visit_child_nodes
259
+
260
+ # Visit an Ident node.
261
+ alias visit_ident visit_child_nodes
262
+
263
+ # Visit an If node.
264
+ alias visit_if visit_child_nodes
265
+
266
+ # Visit an IfMod node.
267
+ alias visit_if_mod visit_child_nodes
268
+
269
+ # Visit an IfOp node.
270
+ alias visit_if_op visit_child_nodes
271
+
272
+ # Visit an Imaginary node.
273
+ alias visit_imaginary visit_child_nodes
274
+
275
+ # Visit an In node.
276
+ alias visit_in visit_child_nodes
277
+
278
+ # Visit an Int node.
279
+ alias visit_int visit_child_nodes
280
+
281
+ # Visit an IVar node.
282
+ alias visit_ivar visit_child_nodes
283
+
284
+ # Visit a Kw node.
285
+ alias visit_kw visit_child_nodes
286
+
287
+ # Visit a KwRestParam node.
288
+ alias visit_kwrest_param visit_child_nodes
289
+
290
+ # Visit a Label node.
291
+ alias visit_label visit_child_nodes
292
+
293
+ # Visit a LabelEnd node.
294
+ alias visit_label_end visit_child_nodes
295
+
296
+ # Visit a Lambda node.
297
+ alias visit_lambda visit_child_nodes
298
+
299
+ # Visit a LBrace node.
300
+ alias visit_lbrace visit_child_nodes
301
+
302
+ # Visit a LBracket node.
303
+ alias visit_lbracket visit_child_nodes
304
+
305
+ # Visit a LParen node.
306
+ alias visit_lparen visit_child_nodes
307
+
308
+ # Visit a MAssign node.
309
+ alias visit_massign visit_child_nodes
310
+
311
+ # Visit a MethodAddBlock node.
312
+ alias visit_method_add_block visit_child_nodes
313
+
314
+ # Visit a MLHS node.
315
+ alias visit_mlhs visit_child_nodes
316
+
317
+ # Visit a MLHSParen node.
318
+ alias visit_mlhs_paren visit_child_nodes
319
+
320
+ # Visit a ModuleDeclaration node.
321
+ alias visit_module visit_child_nodes
322
+
323
+ # Visit a MRHS node.
324
+ alias visit_mrhs visit_child_nodes
325
+
326
+ # Visit a Next node.
327
+ alias visit_next visit_child_nodes
328
+
329
+ # Visit a Not node.
330
+ alias visit_not visit_child_nodes
331
+
332
+ # Visit an Op node.
333
+ alias visit_op visit_child_nodes
334
+
335
+ # Visit an OpAssign node.
336
+ alias visit_opassign visit_child_nodes
337
+
338
+ # Visit a Params node.
339
+ alias visit_params visit_child_nodes
340
+
341
+ # Visit a Paren node.
342
+ alias visit_paren visit_child_nodes
343
+
344
+ # Visit a Period node.
345
+ alias visit_period visit_child_nodes
346
+
347
+ # Visit a PinnedBegin node.
348
+ alias visit_pinned_begin visit_child_nodes
349
+
350
+ # Visit a PinnedVarRef node.
351
+ alias visit_pinned_var_ref visit_child_nodes
352
+
353
+ # Visit a Program node.
354
+ alias visit_program visit_child_nodes
355
+
356
+ # Visit a QSymbols node.
357
+ alias visit_qsymbols visit_child_nodes
358
+
359
+ # Visit a QSymbolsBeg node.
360
+ alias visit_qsymbols_beg visit_child_nodes
361
+
362
+ # Visit a QWords node.
363
+ alias visit_qwords visit_child_nodes
364
+
365
+ # Visit a QWordsBeg node.
366
+ alias visit_qwords_beg visit_child_nodes
367
+
368
+ # Visit a RAssign node.
369
+ alias visit_rassign visit_child_nodes
370
+
371
+ # Visit a RationalLiteral node.
372
+ alias visit_rational visit_child_nodes
373
+
374
+ # Visit a RBrace node.
375
+ alias visit_rbrace visit_child_nodes
376
+
377
+ # Visit a RBracket node.
378
+ alias visit_rbracket visit_child_nodes
379
+
380
+ # Visit a Redo node.
381
+ alias visit_redo visit_child_nodes
382
+
383
+ # Visit a RegexpBeg node.
384
+ alias visit_regexp_beg visit_child_nodes
385
+
386
+ # Visit a RegexpContent node.
387
+ alias visit_regexp_content visit_child_nodes
388
+
389
+ # Visit a RegexpEnd node.
390
+ alias visit_regexp_end visit_child_nodes
391
+
392
+ # Visit a RegexpLiteral node.
393
+ alias visit_regexp_literal visit_child_nodes
394
+
395
+ # Visit a Rescue node.
396
+ alias visit_rescue visit_child_nodes
397
+
398
+ # Visit a RescueEx node.
399
+ alias visit_rescue_ex visit_child_nodes
400
+
401
+ # Visit a RescueMod node.
402
+ alias visit_rescue_mod visit_child_nodes
403
+
404
+ # Visit a RestParam node.
405
+ alias visit_rest_param visit_child_nodes
406
+
407
+ # Visit a Retry node.
408
+ alias visit_retry visit_child_nodes
409
+
410
+ # Visit a Return node.
411
+ alias visit_return visit_child_nodes
412
+
413
+ # Visit a Return0 node.
414
+ alias visit_return0 visit_child_nodes
415
+
416
+ # Visit a RParen node.
417
+ alias visit_rparen visit_child_nodes
418
+
419
+ # Visit a SClass node.
420
+ alias visit_sclass visit_child_nodes
421
+
422
+ # Visit a Statements node.
423
+ alias visit_statements visit_child_nodes
424
+
425
+ # Visit a StringConcat node.
426
+ alias visit_string_concat visit_child_nodes
427
+
428
+ # Visit a StringContent node.
429
+ alias visit_string_content visit_child_nodes
430
+
431
+ # Visit a StringDVar node.
432
+ alias visit_string_dvar visit_child_nodes
433
+
434
+ # Visit a StringEmbExpr node.
435
+ alias visit_string_embexpr visit_child_nodes
436
+
437
+ # Visit a StringLiteral node.
438
+ alias visit_string_literal visit_child_nodes
439
+
440
+ # Visit a Super node.
441
+ alias visit_super visit_child_nodes
442
+
443
+ # Visit a SymBeg node.
444
+ alias visit_symbeg visit_child_nodes
445
+
446
+ # Visit a SymbolContent node.
447
+ alias visit_symbol_content visit_child_nodes
448
+
449
+ # Visit a SymbolLiteral node.
450
+ alias visit_symbol_literal visit_child_nodes
451
+
452
+ # Visit a Symbols node.
453
+ alias visit_symbols visit_child_nodes
454
+
455
+ # Visit a SymbolsBeg node.
456
+ alias visit_symbols_beg visit_child_nodes
457
+
458
+ # Visit a TLambda node.
459
+ alias visit_tlambda visit_child_nodes
460
+
461
+ # Visit a TLamBeg node.
462
+ alias visit_tlambeg visit_child_nodes
463
+
464
+ # Visit a TopConstField node.
465
+ alias visit_top_const_field visit_child_nodes
466
+
467
+ # Visit a TopConstRef node.
468
+ alias visit_top_const_ref visit_child_nodes
469
+
470
+ # Visit a TStringBeg node.
471
+ alias visit_tstring_beg visit_child_nodes
472
+
473
+ # Visit a TStringContent node.
474
+ alias visit_tstring_content visit_child_nodes
475
+
476
+ # Visit a TStringEnd node.
477
+ alias visit_tstring_end visit_child_nodes
478
+
479
+ # Visit an Unary node.
480
+ alias visit_unary visit_child_nodes
481
+
482
+ # Visit an Undef node.
483
+ alias visit_undef visit_child_nodes
484
+
485
+ # Visit an Unless node.
486
+ alias visit_unless visit_child_nodes
487
+
488
+ # Visit an UnlessMod node.
489
+ alias visit_unless_mod visit_child_nodes
490
+
491
+ # Visit an Until node.
492
+ alias visit_until visit_child_nodes
493
+
494
+ # Visit an UntilMod node.
495
+ alias visit_until_mod visit_child_nodes
496
+
497
+ # Visit a VarAlias node.
498
+ alias visit_var_alias visit_child_nodes
499
+
500
+ # Visit a VarField node.
501
+ alias visit_var_field visit_child_nodes
502
+
503
+ # Visit a VarRef node.
504
+ alias visit_var_ref visit_child_nodes
505
+
506
+ # Visit a VCall node.
507
+ alias visit_vcall visit_child_nodes
508
+
509
+ # Visit a VoidStmt node.
510
+ alias visit_void_stmt visit_child_nodes
511
+
512
+ # Visit a When node.
513
+ alias visit_when visit_child_nodes
514
+
515
+ # Visit a While node.
516
+ alias visit_while visit_child_nodes
517
+
518
+ # Visit a WhileMod node.
519
+ alias visit_while_mod visit_child_nodes
520
+
521
+ # Visit a Word node.
522
+ alias visit_word visit_child_nodes
523
+
524
+ # Visit a Words node.
525
+ alias visit_words visit_child_nodes
526
+
527
+ # Visit a WordsBeg node.
528
+ alias visit_words_beg visit_child_nodes
529
+
530
+ # Visit a XString node.
531
+ alias visit_xstring visit_child_nodes
532
+
533
+ # Visit a XStringLiteral node.
534
+ alias visit_xstring_literal visit_child_nodes
535
+
536
+ # Visit a Yield node.
537
+ alias visit_yield visit_child_nodes
538
+
539
+ # Visit a Yield0 node.
540
+ alias visit_yield0 visit_child_nodes
541
+
542
+ # Visit a ZSuper node.
543
+ alias visit_zsuper visit_child_nodes
544
+
545
+ # Visit an EndContent node.
546
+ alias visit___end__ visit_child_nodes
547
+ end
548
+ end
data/lib/syntax_tree.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require "pp"
4
5
  require "prettyprint"
5
6
  require "ripper"
@@ -9,6 +10,9 @@ require_relative "syntax_tree/formatter"
9
10
  require_relative "syntax_tree/node"
10
11
  require_relative "syntax_tree/parser"
11
12
  require_relative "syntax_tree/version"
13
+ require_relative "syntax_tree/visitor"
14
+ require_relative "syntax_tree/visitor/json_visitor"
15
+ require_relative "syntax_tree/visitor/pretty_print_visitor"
12
16
 
13
17
  # If PrettyPrint::Align isn't defined, then we haven't gotten the updated
14
18
  # version of prettyprint. In that case we'll define our own. This is going to
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-31 00:00:00.000000000 Z
11
+ date: 2022-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,7 +88,6 @@ files:
88
88
  - bin/bench
89
89
  - bin/console
90
90
  - bin/profile
91
- - bin/setup
92
91
  - doc/logo.svg
93
92
  - exe/stree
94
93
  - lib/syntax_tree.rb
@@ -100,6 +99,9 @@ files:
100
99
  - lib/syntax_tree/parser.rb
101
100
  - lib/syntax_tree/prettyprint.rb
102
101
  - lib/syntax_tree/version.rb
102
+ - lib/syntax_tree/visitor.rb
103
+ - lib/syntax_tree/visitor/json_visitor.rb
104
+ - lib/syntax_tree/visitor/pretty_print_visitor.rb
103
105
  - syntax_tree.gemspec
104
106
  homepage: https://github.com/kddnewton/syntax_tree
105
107
  licenses:
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  - !ruby/object:Gem::Version
122
124
  version: '0'
123
125
  requirements: []
124
- rubygems_version: 3.4.0.dev
126
+ rubygems_version: 3.3.3
125
127
  signing_key:
126
128
  specification_version: 4
127
129
  summary: A parser based on ripper
data/bin/setup DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install