yarp 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -1
  3. data/Makefile +5 -1
  4. data/config.yml +156 -125
  5. data/docs/encoding.md +5 -5
  6. data/docs/serialization.md +2 -2
  7. data/ext/yarp/api_node.c +142 -98
  8. data/ext/yarp/extension.c +21 -7
  9. data/ext/yarp/extension.h +1 -1
  10. data/include/yarp/ast.h +327 -18
  11. data/include/yarp/defines.h +2 -1
  12. data/include/yarp/diagnostic.h +3 -3
  13. data/include/yarp/enc/yp_encoding.h +10 -10
  14. data/include/yarp/parser.h +19 -19
  15. data/include/yarp/regexp.h +1 -1
  16. data/include/yarp/unescape.h +4 -4
  17. data/include/yarp/util/yp_buffer.h +3 -0
  18. data/include/yarp/util/yp_char.h +16 -16
  19. data/include/yarp/util/yp_constant_pool.h +2 -2
  20. data/include/yarp/util/yp_newline_list.h +5 -5
  21. data/include/yarp/util/yp_string.h +4 -4
  22. data/include/yarp/util/yp_string_list.h +0 -3
  23. data/include/yarp/util/yp_strpbrk.h +1 -1
  24. data/include/yarp/version.h +2 -2
  25. data/include/yarp.h +5 -4
  26. data/lib/yarp/desugar_visitor.rb +59 -122
  27. data/lib/yarp/node.rb +230 -240
  28. data/lib/yarp/serialize.rb +16 -16
  29. data/lib/yarp.rb +5 -5
  30. data/src/diagnostic.c +1 -1
  31. data/src/enc/yp_big5.c +15 -42
  32. data/src/enc/yp_euc_jp.c +16 -43
  33. data/src/enc/yp_gbk.c +19 -46
  34. data/src/enc/yp_shift_jis.c +16 -43
  35. data/src/enc/yp_tables.c +36 -38
  36. data/src/enc/yp_unicode.c +20 -25
  37. data/src/enc/yp_windows_31j.c +16 -43
  38. data/src/node.c +1271 -899
  39. data/src/prettyprint.c +87 -48
  40. data/src/regexp.c +21 -21
  41. data/src/serialize.c +28 -15
  42. data/src/unescape.c +151 -121
  43. data/src/util/yp_buffer.c +7 -2
  44. data/src/util/yp_char.c +34 -34
  45. data/src/util/yp_constant_pool.c +4 -4
  46. data/src/util/yp_memchr.c +1 -1
  47. data/src/util/yp_newline_list.c +5 -4
  48. data/src/util/yp_string.c +22 -20
  49. data/src/util/yp_string_list.c +0 -6
  50. data/src/util/yp_strncasecmp.c +3 -6
  51. data/src/util/yp_strpbrk.c +8 -8
  52. data/src/yarp.c +355 -216
  53. data/yarp.gemspec +1 -1
  54. metadata +2 -2
@@ -8,26 +8,16 @@ module YARP
8
8
  #
9
9
  # @@foo && @@foo = bar
10
10
  def visit_class_variable_and_write_node(node)
11
- AndNode.new(
12
- ClassVariableReadNode.new(node.name_loc),
13
- ClassVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
14
- node.operator_loc,
15
- node.location
16
- )
11
+ desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
17
12
  end
18
13
 
19
14
  # @@foo ||= bar
20
15
  #
21
16
  # becomes
22
17
  #
23
- # @@foo || @@foo = bar
18
+ # defined?(@@foo) ? @@foo : @@foo = bar
24
19
  def visit_class_variable_or_write_node(node)
25
- OrNode.new(
26
- ClassVariableReadNode.new(node.name_loc),
27
- ClassVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
28
- node.operator_loc,
29
- node.location
30
- )
20
+ desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
31
21
  end
32
22
 
33
23
  # @@foo += bar
@@ -36,7 +26,7 @@ module YARP
36
26
  #
37
27
  # @@foo = @@foo + bar
38
28
  def visit_class_variable_operator_write_node(node)
39
- desugar_operator_write_node(node, ClassVariableWriteNode, ClassVariableReadNode)
29
+ desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, arguments: [node.name])
40
30
  end
41
31
 
42
32
  # Foo &&= bar
@@ -45,26 +35,16 @@ module YARP
45
35
  #
46
36
  # Foo && Foo = bar
47
37
  def visit_constant_and_write_node(node)
48
- AndNode.new(
49
- ConstantReadNode.new(node.name_loc),
50
- ConstantWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
51
- node.operator_loc,
52
- node.location
53
- )
38
+ desugar_and_write_node(node, ConstantReadNode, ConstantWriteNode)
54
39
  end
55
40
 
56
41
  # Foo ||= bar
57
42
  #
58
43
  # becomes
59
44
  #
60
- # Foo || Foo = bar
45
+ # defined?(Foo) ? Foo : Foo = bar
61
46
  def visit_constant_or_write_node(node)
62
- OrNode.new(
63
- ConstantReadNode.new(node.name_loc),
64
- ConstantWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
65
- node.operator_loc,
66
- node.location
67
- )
47
+ desugar_or_write_defined_node(node, ConstantReadNode, ConstantWriteNode)
68
48
  end
69
49
 
70
50
  # Foo += bar
@@ -73,60 +53,7 @@ module YARP
73
53
  #
74
54
  # Foo = Foo + bar
75
55
  def visit_constant_operator_write_node(node)
76
- desugar_operator_write_node(node, ConstantWriteNode, ConstantReadNode)
77
- end
78
-
79
- # Foo::Bar &&= baz
80
- #
81
- # becomes
82
- #
83
- # Foo::Bar && Foo::Bar = baz
84
- def visit_constant_path_and_write_node(node)
85
- AndNode.new(
86
- node.target,
87
- ConstantPathWriteNode.new(node.target, node.value, node.operator_loc, node.location),
88
- node.operator_loc,
89
- node.location
90
- )
91
- end
92
-
93
- # Foo::Bar ||= baz
94
- #
95
- # becomes
96
- #
97
- # Foo::Bar || Foo::Bar = baz
98
- def visit_constant_path_or_write_node(node)
99
- OrNode.new(
100
- node.target,
101
- ConstantPathWriteNode.new(node.target, node.value, node.operator_loc, node.location),
102
- node.operator_loc,
103
- node.location
104
- )
105
- end
106
-
107
- # Foo::Bar += baz
108
- #
109
- # becomes
110
- #
111
- # Foo::Bar = Foo::Bar + baz
112
- def visit_constant_path_operator_write_node(node)
113
- ConstantPathWriteNode.new(
114
- node.target,
115
- CallNode.new(
116
- node.target,
117
- nil,
118
- node.operator_loc.copy(length: node.operator_loc.length - 1),
119
- nil,
120
- ArgumentsNode.new([node.value], node.value.location),
121
- nil,
122
- nil,
123
- 0,
124
- node.operator_loc.slice.chomp("="),
125
- node.location
126
- ),
127
- node.operator_loc.copy(start_offset: node.operator_loc.end_offset - 1, length: 1),
128
- node.location
129
- )
56
+ desugar_operator_write_node(node, ConstantReadNode, ConstantWriteNode)
130
57
  end
131
58
 
132
59
  # $foo &&= bar
@@ -135,26 +62,16 @@ module YARP
135
62
  #
136
63
  # $foo && $foo = bar
137
64
  def visit_global_variable_and_write_node(node)
138
- AndNode.new(
139
- GlobalVariableReadNode.new(node.name_loc),
140
- GlobalVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
141
- node.operator_loc,
142
- node.location
143
- )
65
+ desugar_and_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
144
66
  end
145
67
 
146
68
  # $foo ||= bar
147
69
  #
148
70
  # becomes
149
71
  #
150
- # $foo || $foo = bar
72
+ # defined?($foo) ? $foo : $foo = bar
151
73
  def visit_global_variable_or_write_node(node)
152
- OrNode.new(
153
- GlobalVariableReadNode.new(node.name_loc),
154
- GlobalVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
155
- node.operator_loc,
156
- node.location
157
- )
74
+ desugar_or_write_defined_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
158
75
  end
159
76
 
160
77
  # $foo += bar
@@ -163,7 +80,7 @@ module YARP
163
80
  #
164
81
  # $foo = $foo + bar
165
82
  def visit_global_variable_operator_write_node(node)
166
- desugar_operator_write_node(node, GlobalVariableWriteNode, GlobalVariableReadNode)
83
+ desugar_operator_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode)
167
84
  end
168
85
 
169
86
  # @foo &&= bar
@@ -172,12 +89,7 @@ module YARP
172
89
  #
173
90
  # @foo && @foo = bar
174
91
  def visit_instance_variable_and_write_node(node)
175
- AndNode.new(
176
- InstanceVariableReadNode.new(node.name_loc),
177
- InstanceVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
178
- node.operator_loc,
179
- node.location
180
- )
92
+ desugar_and_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, arguments: [node.name])
181
93
  end
182
94
 
183
95
  # @foo ||= bar
@@ -186,12 +98,7 @@ module YARP
186
98
  #
187
99
  # @foo || @foo = bar
188
100
  def visit_instance_variable_or_write_node(node)
189
- OrNode.new(
190
- InstanceVariableReadNode.new(node.name_loc),
191
- InstanceVariableWriteNode.new(node.name_loc, node.value, node.operator_loc, node.location),
192
- node.operator_loc,
193
- node.location
194
- )
101
+ desugar_or_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, arguments: [node.name])
195
102
  end
196
103
 
197
104
  # @foo += bar
@@ -200,7 +107,7 @@ module YARP
200
107
  #
201
108
  # @foo = @foo + bar
202
109
  def visit_instance_variable_operator_write_node(node)
203
- desugar_operator_write_node(node, InstanceVariableWriteNode, InstanceVariableReadNode)
110
+ desugar_operator_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, arguments: [node.name])
204
111
  end
205
112
 
206
113
  # foo &&= bar
@@ -209,12 +116,7 @@ module YARP
209
116
  #
210
117
  # foo && foo = bar
211
118
  def visit_local_variable_and_write_node(node)
212
- AndNode.new(
213
- LocalVariableReadNode.new(node.constant_id, node.depth, node.name_loc),
214
- LocalVariableWriteNode.new(node.constant_id, node.depth, node.name_loc, node.value, node.operator_loc, node.location),
215
- node.operator_loc,
216
- node.location
217
- )
119
+ desugar_and_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, arguments: [node.name, node.depth])
218
120
  end
219
121
 
220
122
  # foo ||= bar
@@ -223,12 +125,7 @@ module YARP
223
125
  #
224
126
  # foo || foo = bar
225
127
  def visit_local_variable_or_write_node(node)
226
- OrNode.new(
227
- LocalVariableReadNode.new(node.constant_id, node.depth, node.name_loc),
228
- LocalVariableWriteNode.new(node.constant_id, node.depth, node.name_loc, node.value, node.operator_loc, node.location),
229
- node.operator_loc,
230
- node.location
231
- )
128
+ desugar_or_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, arguments: [node.name, node.depth])
232
129
  end
233
130
 
234
131
  # foo += bar
@@ -237,13 +134,23 @@ module YARP
237
134
  #
238
135
  # foo = foo + bar
239
136
  def visit_local_variable_operator_write_node(node)
240
- desugar_operator_write_node(node, LocalVariableWriteNode, LocalVariableReadNode, arguments: [node.constant_id, node.depth])
137
+ desugar_operator_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, arguments: [node.name, node.depth])
241
138
  end
242
139
 
243
140
  private
244
141
 
142
+ # Desugar `x &&= y` to `x && x = y`
143
+ def desugar_and_write_node(node, read_class, write_class, arguments: [])
144
+ AndNode.new(
145
+ read_class.new(*arguments, node.name_loc),
146
+ write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location),
147
+ node.operator_loc,
148
+ node.location
149
+ )
150
+ end
151
+
245
152
  # Desugar `x += y` to `x = x + y`
246
- def desugar_operator_write_node(node, write_class, read_class, arguments: [])
153
+ def desugar_operator_write_node(node, read_class, write_class, arguments: [])
247
154
  write_class.new(
248
155
  *arguments,
249
156
  node.name_loc,
@@ -263,5 +170,35 @@ module YARP
263
170
  node.location
264
171
  )
265
172
  end
173
+
174
+ # Desugar `x ||= y` to `x || x = y`
175
+ def desugar_or_write_node(node, read_class, write_class, arguments: [])
176
+ OrNode.new(
177
+ read_class.new(*arguments, node.name_loc),
178
+ write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location),
179
+ node.operator_loc,
180
+ node.location
181
+ )
182
+ end
183
+
184
+ # Desugar `x ||= y` to `defined?(x) ? x : x = y`
185
+ def desugar_or_write_defined_node(node, read_class, write_class, arguments: [])
186
+ IfNode.new(
187
+ node.operator_loc,
188
+ DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc),
189
+ StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
190
+ ElseNode.new(
191
+ node.operator_loc,
192
+ StatementsNode.new(
193
+ [write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
194
+ node.location
195
+ ),
196
+ node.operator_loc,
197
+ node.location
198
+ ),
199
+ node.operator_loc,
200
+ node.location
201
+ )
202
+ end
266
203
  end
267
204
  end