unparser 0.4.9 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +19 -4
  3. data/bin/unparser +1 -1
  4. data/lib/unparser.rb +115 -61
  5. data/lib/unparser/ast.rb +0 -1
  6. data/lib/unparser/ast/local_variable_scope.rb +6 -76
  7. data/lib/unparser/buffer.rb +19 -16
  8. data/lib/unparser/cli.rb +26 -39
  9. data/lib/unparser/color.rb +0 -3
  10. data/lib/unparser/comments.rb +0 -26
  11. data/lib/unparser/constants.rb +4 -53
  12. data/lib/unparser/diff.rb +0 -17
  13. data/lib/unparser/dsl.rb +0 -32
  14. data/lib/unparser/emitter.rb +23 -425
  15. data/lib/unparser/emitter/alias.rb +2 -8
  16. data/lib/unparser/emitter/args.rb +45 -0
  17. data/lib/unparser/emitter/argument.rb +8 -166
  18. data/lib/unparser/emitter/array.rb +27 -0
  19. data/lib/unparser/emitter/array_pattern.rb +29 -0
  20. data/lib/unparser/emitter/assignment.rb +36 -127
  21. data/lib/unparser/emitter/begin.rb +9 -84
  22. data/lib/unparser/emitter/binary.rb +7 -20
  23. data/lib/unparser/emitter/block.rb +57 -41
  24. data/lib/unparser/emitter/case.rb +6 -48
  25. data/lib/unparser/emitter/case_guard.rb +27 -0
  26. data/lib/unparser/emitter/case_match.rb +40 -0
  27. data/lib/unparser/emitter/cbase.rb +1 -3
  28. data/lib/unparser/emitter/class.rb +6 -26
  29. data/lib/unparser/emitter/const_pattern.rb +24 -0
  30. data/lib/unparser/emitter/def.rb +7 -51
  31. data/lib/unparser/emitter/defined.rb +2 -12
  32. data/lib/unparser/emitter/dstr.rb +22 -0
  33. data/lib/unparser/emitter/dsym.rb +41 -0
  34. data/lib/unparser/emitter/flipflop.rb +11 -10
  35. data/lib/unparser/emitter/float.rb +29 -0
  36. data/lib/unparser/emitter/flow_modifier.rb +11 -55
  37. data/lib/unparser/emitter/for.rb +5 -19
  38. data/lib/unparser/emitter/hash.rb +74 -0
  39. data/lib/unparser/emitter/hash_pattern.rb +67 -0
  40. data/lib/unparser/emitter/hookexe.rb +5 -11
  41. data/lib/unparser/emitter/if.rb +15 -71
  42. data/lib/unparser/emitter/in_match.rb +21 -0
  43. data/lib/unparser/emitter/in_pattern.rb +34 -0
  44. data/lib/unparser/emitter/index.rb +21 -88
  45. data/lib/unparser/emitter/kwbegin.rb +31 -0
  46. data/lib/unparser/emitter/lambda.rb +0 -8
  47. data/lib/unparser/emitter/masgn.rb +20 -0
  48. data/lib/unparser/emitter/match.rb +3 -17
  49. data/lib/unparser/emitter/match_alt.rb +23 -0
  50. data/lib/unparser/emitter/match_as.rb +21 -0
  51. data/lib/unparser/emitter/match_rest.rb +26 -0
  52. data/lib/unparser/emitter/match_var.rb +19 -0
  53. data/lib/unparser/emitter/mlhs.rb +40 -0
  54. data/lib/unparser/emitter/module.rb +3 -9
  55. data/lib/unparser/emitter/op_assign.rb +12 -27
  56. data/lib/unparser/emitter/pin.rb +19 -0
  57. data/lib/unparser/emitter/primitive.rb +93 -0
  58. data/lib/unparser/emitter/range.rb +35 -0
  59. data/lib/unparser/emitter/regexp.rb +35 -0
  60. data/lib/unparser/emitter/repetition.rb +17 -57
  61. data/lib/unparser/emitter/rescue.rb +1 -97
  62. data/lib/unparser/emitter/root.rb +17 -1
  63. data/lib/unparser/emitter/send.rb +10 -219
  64. data/lib/unparser/emitter/simple.rb +33 -0
  65. data/lib/unparser/emitter/splat.rb +2 -18
  66. data/lib/unparser/emitter/super.rb +1 -29
  67. data/lib/unparser/emitter/undef.rb +1 -9
  68. data/lib/unparser/emitter/variable.rb +1 -31
  69. data/lib/unparser/emitter/xstr.rb +72 -0
  70. data/lib/unparser/emitter/yield.rb +1 -9
  71. data/lib/unparser/generation.rb +250 -0
  72. data/lib/unparser/node_details.rb +21 -0
  73. data/lib/unparser/node_details/send.rb +62 -0
  74. data/lib/unparser/node_helpers.rb +46 -6
  75. data/lib/unparser/validation.rb +58 -35
  76. data/lib/unparser/writer.rb +15 -0
  77. data/lib/unparser/writer/binary.rb +99 -0
  78. data/lib/unparser/writer/dynamic_string.rb +233 -0
  79. data/lib/unparser/writer/resbody.rb +40 -0
  80. data/lib/unparser/writer/rescue.rb +39 -0
  81. data/lib/unparser/writer/send.rb +124 -0
  82. data/lib/unparser/{emitter → writer}/send/attribute_assignment.rb +11 -26
  83. data/lib/unparser/writer/send/binary.rb +27 -0
  84. data/lib/unparser/writer/send/conditional.rb +25 -0
  85. data/lib/unparser/writer/send/regular.rb +33 -0
  86. data/lib/unparser/{emitter → writer}/send/unary.rb +10 -17
  87. metadata +66 -34
  88. data/lib/unparser/emitter/empty.rb +0 -23
  89. data/lib/unparser/emitter/ensure.rb +0 -37
  90. data/lib/unparser/emitter/literal.rb +0 -10
  91. data/lib/unparser/emitter/literal/array.rb +0 -29
  92. data/lib/unparser/emitter/literal/dynamic.rb +0 -53
  93. data/lib/unparser/emitter/literal/dynamic_body.rb +0 -132
  94. data/lib/unparser/emitter/literal/execute_string.rb +0 -38
  95. data/lib/unparser/emitter/literal/hash.rb +0 -156
  96. data/lib/unparser/emitter/literal/primitive.rb +0 -145
  97. data/lib/unparser/emitter/literal/range.rb +0 -36
  98. data/lib/unparser/emitter/literal/regexp.rb +0 -114
  99. data/lib/unparser/emitter/literal/singleton.rb +0 -26
  100. data/lib/unparser/emitter/meta.rb +0 -16
  101. data/lib/unparser/emitter/redo.rb +0 -25
  102. data/lib/unparser/emitter/resbody.rb +0 -76
  103. data/lib/unparser/emitter/retry.rb +0 -25
  104. data/lib/unparser/emitter/send/binary.rb +0 -57
  105. data/lib/unparser/emitter/send/conditional.rb +0 -40
  106. data/lib/unparser/emitter/send/regular.rb +0 -40
  107. data/lib/unparser/preprocessor.rb +0 -159
@@ -1,145 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
-
7
- # Base class for primitive emitters
8
- class Primitive < self
9
-
10
- children :value
11
-
12
- # Emitter for primitives based on Object#inspect
13
- class Inspect < self
14
-
15
- handle :sym, :str
16
-
17
- private
18
-
19
- # Dispatch value
20
- #
21
- # @return [undefined]
22
- #
23
- # @api private
24
- #
25
- def dispatch
26
- write(value.inspect)
27
- end
28
-
29
- end # Inspect
30
-
31
- # Emitter for complex literals
32
- class Complex < self
33
-
34
- handle :complex
35
-
36
- RATIONAL_FORMAT = 'i'.freeze
37
-
38
- MAP =
39
- if 0.class.equal?(Integer)
40
- IceNine.deep_freeze(
41
- Float => :float,
42
- Rational => :rational,
43
- Integer => :int
44
- )
45
- else
46
- IceNine.deep_freeze(
47
- Float => :float,
48
- Rational => :rational,
49
- Fixnum => :int,
50
- Bignum => :int
51
- )
52
- end
53
-
54
- private
55
-
56
- # Dispatch value
57
- #
58
- # @return [undefined]
59
- #
60
- def dispatch
61
- emit_imaginary
62
- write(RATIONAL_FORMAT)
63
- end
64
-
65
- # Emit imaginary component
66
- #
67
- # @return [undefined]
68
- #
69
- # @api private
70
- #
71
- def emit_imaginary
72
- visit(imaginary_node)
73
- end
74
-
75
- # Return imaginary node
76
- #
77
- # @return [Parser::AST::Node]
78
- #
79
- # @api private
80
- #
81
- def imaginary_node
82
- imaginary = value.imaginary
83
- s(MAP.fetch(imaginary.class), imaginary)
84
- end
85
-
86
- end # Rational
87
-
88
- # Emitter for rational literals
89
- class Rational < self
90
-
91
- handle :rational
92
-
93
- RATIONAL_FORMAT = 'r'.freeze
94
-
95
- private
96
-
97
- # Dispatch value
98
- #
99
- # @return [undefined]
100
- #
101
- def dispatch
102
- integer = value.to_i
103
- float = value.to_f
104
-
105
- write_rational(integer.to_f.eql?(float) ? integer : float)
106
- end
107
-
108
- # Write rational format
109
- #
110
- # @param [#to_s] value
111
- #
112
- # @return [undefined]
113
- #
114
- # @api private
115
- #
116
- def write_rational(value)
117
- write(value.to_s, RATIONAL_FORMAT)
118
- end
119
-
120
- end # Rational
121
-
122
- # Emiter for numeric literals
123
- class Numeric < self
124
-
125
- handle :int, :float
126
-
127
- private
128
-
129
- # Dispatch value
130
- #
131
- # @return [undefined]
132
- #
133
- # @api private
134
- #
135
- def dispatch
136
- conditional_parentheses(parent.is_a?(Send) && value.negative?) do
137
- write(value.inspect)
138
- end
139
- end
140
-
141
- end # Numeric
142
- end # Primitive
143
- end # Literal
144
- end # Emitter
145
- end # Unparser
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
- # Abstract base class for literal range emitter
7
- class Range < self
8
- include Unterminated
9
-
10
- TOKENS = IceNine.deep_freeze(
11
- irange: '..',
12
- erange: '...'
13
- )
14
-
15
- handle(*TOKENS.keys)
16
-
17
- children :begin_node, :end_node
18
-
19
- private
20
-
21
- # Perform dispatch
22
- #
23
- # @return [undefined]
24
- #
25
- # @api private
26
- #
27
- def dispatch
28
- visit(begin_node)
29
- write(TOKENS.fetch(node.type))
30
- visit(end_node) if end_node
31
- end
32
-
33
- end # Range
34
- end # Literal
35
- end # Emitter
36
- end # Unparser
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
-
7
- # Emitter for regexp literals
8
- class Regexp < self
9
- DELIMITER = '/'.freeze
10
- ESCAPED_DELIMITER = '\/'.freeze
11
-
12
- handle :regexp
13
-
14
- private
15
-
16
- # Perform dispatch
17
- #
18
- # @return [undefined]
19
- #
20
- # @api private
21
- #
22
- def dispatch
23
- parentheses(DELIMITER, DELIMITER) do
24
- body.each(&method(:write_body))
25
- end
26
- visit(children.last)
27
- end
28
-
29
- # Return non regopt children
30
- #
31
- # @return [Array<Parser::AST::Node>]
32
- #
33
- # @api private
34
- #
35
- def body
36
- children[0..-2]
37
- end
38
-
39
- # Write specific body component
40
- #
41
- # @param [Parser::AST::Node] node
42
- #
43
- # @return [undefined]
44
- #
45
- # @api private
46
- #
47
- def write_body(node)
48
- case node.type
49
- when :str
50
- buffer.append_without_prefix(escape(node).children.first)
51
- else
52
- visit(s(:interpolated, node))
53
- end
54
- end
55
-
56
- # Return dynamic body
57
- #
58
- # @return [undefined]
59
- #
60
- # @api private
61
- #
62
- def dynamic_body
63
- Parser::AST::Node.new(:dyn_regexp_body, dynamic_body_children)
64
- end
65
-
66
- # Return dynamic body children
67
- #
68
- # @return [Enumerable<Parser::AST::Node>]
69
- #
70
- # @api private
71
- #
72
- def dynamic_body_children
73
- children[0..-2].map do |child|
74
- escape(child)
75
- end
76
- end
77
-
78
- # Return escaped child
79
- #
80
- # @param [Parser::AST::Node] child
81
- #
82
- # @return [Parser::AST::Node]
83
- #
84
- # @api private
85
- #
86
- def escape(child)
87
- source = child.children.first
88
- s(:str, source.gsub(DELIMITER, ESCAPED_DELIMITER))
89
- end
90
-
91
- end # Regexp
92
-
93
- # Emitter for regexp options
94
- class Regopt < self
95
-
96
- handle :regopt
97
-
98
- private
99
-
100
- # Perform dispatch
101
- #
102
- # @return [undefined]
103
- #
104
- # @api private
105
- #
106
- def dispatch
107
- write(children.map(&:to_s).join)
108
- end
109
-
110
- end # Regopt
111
-
112
- end # Literal
113
- end # Emitter
114
- end # Unparser
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- class Literal
6
- # Emiter for literal singletons
7
- class Singleton < self
8
-
9
- handle :self, :true, :false, :nil
10
-
11
- private
12
-
13
- # Perform dispatco
14
- #
15
- # @return [undefined]
16
- #
17
- # @api private
18
- #
19
- def dispatch
20
- buffer.append(node.type.to_s)
21
- end
22
-
23
- end # Singleton
24
- end # Literal
25
- end # Emitter
26
- end # Unparser
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- # Namespace class for meta emitters
6
- class Meta < self
7
- include Terminated
8
-
9
- handle(:__ENCODING__, :__FILE__, :__LINE__)
10
-
11
- def dispatch
12
- write(node.type.to_s)
13
- end
14
- end # Meta
15
- end # Emitter
16
- end # Unparser
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- # Emitter for redo nodes
6
- class Redo < self
7
- include Terminated
8
-
9
- handle :redo
10
-
11
- private
12
-
13
- # Perform dispatch
14
- #
15
- # @return [undefined]
16
- #
17
- # @api private
18
- #
19
- def dispatch
20
- write(K_REDO)
21
- end
22
-
23
- end # Redo
24
- end # Emitter
25
- end # Unparser
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- # Emitter for rescue body nodes
6
- class Resbody < self
7
-
8
- children :exception, :assignment, :body
9
-
10
- # Emitter for resbody in standalone form
11
- class Standalone < self
12
-
13
- private
14
-
15
- # Perform dispatch
16
- #
17
- # @return [undefined]
18
- #
19
- # @api private
20
- #
21
- def dispatch
22
- write(K_RESCUE, WS)
23
- visit_plain(body)
24
- end
25
- end
26
-
27
- # Emitter for resbody in keyworkd-embedded form
28
- class Embedded < self
29
-
30
- handle :resbody
31
-
32
- private
33
-
34
- # Perform dispatch
35
- #
36
- # @return [undefined]
37
- #
38
- # @api private
39
- #
40
- def dispatch
41
- write(K_RESCUE)
42
- emit_exception
43
- emit_assignment
44
- emit_body
45
- end
46
-
47
- # Emit exception
48
- #
49
- # @return [undefined]
50
- #
51
- # @api private
52
- #
53
- def emit_exception
54
- return unless exception
55
-
56
- ws
57
- delimited(exception.children)
58
- end
59
-
60
- # Emit assignment
61
- #
62
- # @return [undefined]
63
- #
64
- # @api private
65
- #
66
- def emit_assignment
67
- return unless assignment
68
-
69
- write(WS, T_ASR, WS)
70
- visit(assignment)
71
- end
72
-
73
- end # Resbody
74
- end
75
- end # Emitter
76
- end # Unparser
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Unparser
4
- class Emitter
5
- # Emitter for retry nodes
6
- class Retry < self
7
- include Terminated
8
-
9
- handle :retry
10
-
11
- private
12
-
13
- # Perform dispatch
14
- #
15
- # @return [undefined]
16
- #
17
- # @api private
18
- #
19
- def dispatch
20
- write(K_RETRY)
21
- end
22
-
23
- end # Break
24
- end # Emitter
25
- end # Unparser