yarp 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -1
  3. data/CONTRIBUTING.md +7 -0
  4. data/Makefile +5 -1
  5. data/config.yml +308 -166
  6. data/docs/configuration.md +0 -1
  7. data/docs/encoding.md +5 -5
  8. data/docs/mapping.md +91 -91
  9. data/docs/serialization.md +25 -22
  10. data/ext/yarp/api_node.c +1210 -483
  11. data/ext/yarp/extension.c +22 -8
  12. data/ext/yarp/extension.h +2 -2
  13. data/include/yarp/ast.h +692 -183
  14. data/include/yarp/defines.h +2 -1
  15. data/include/yarp/diagnostic.h +200 -3
  16. data/include/yarp/enc/yp_encoding.h +10 -10
  17. data/include/yarp/node.h +0 -4
  18. data/include/yarp/parser.h +19 -19
  19. data/include/yarp/regexp.h +1 -1
  20. data/include/yarp/unescape.h +4 -4
  21. data/include/yarp/util/yp_buffer.h +3 -0
  22. data/include/yarp/util/yp_char.h +16 -16
  23. data/include/yarp/util/yp_constant_pool.h +12 -5
  24. data/include/yarp/util/yp_newline_list.h +5 -5
  25. data/include/yarp/util/yp_string.h +4 -4
  26. data/include/yarp/util/yp_string_list.h +0 -3
  27. data/include/yarp/util/yp_strpbrk.h +1 -1
  28. data/include/yarp/version.h +2 -2
  29. data/include/yarp.h +5 -4
  30. data/lib/yarp/desugar_visitor.rb +59 -122
  31. data/lib/yarp/mutation_visitor.rb +22 -12
  32. data/lib/yarp/node.rb +3081 -501
  33. data/lib/yarp/parse_result/comments.rb +172 -0
  34. data/lib/yarp/parse_result/newlines.rb +60 -0
  35. data/lib/yarp/pattern.rb +239 -0
  36. data/lib/yarp/serialize.rb +152 -129
  37. data/lib/yarp.rb +109 -49
  38. data/src/diagnostic.c +254 -2
  39. data/src/enc/yp_big5.c +15 -42
  40. data/src/enc/yp_euc_jp.c +16 -43
  41. data/src/enc/yp_gbk.c +19 -46
  42. data/src/enc/yp_shift_jis.c +16 -43
  43. data/src/enc/yp_tables.c +36 -38
  44. data/src/enc/yp_unicode.c +20 -25
  45. data/src/enc/yp_windows_31j.c +16 -43
  46. data/src/node.c +1871 -1466
  47. data/src/prettyprint.c +463 -230
  48. data/src/regexp.c +21 -21
  49. data/src/serialize.c +352 -184
  50. data/src/unescape.c +152 -122
  51. data/src/util/yp_buffer.c +7 -2
  52. data/src/util/yp_char.c +35 -40
  53. data/src/util/yp_constant_pool.c +45 -12
  54. data/src/util/yp_memchr.c +1 -1
  55. data/src/util/yp_newline_list.c +10 -5
  56. data/src/util/yp_string.c +22 -20
  57. data/src/util/yp_string_list.c +4 -7
  58. data/src/util/yp_strncasecmp.c +3 -6
  59. data/src/util/yp_strpbrk.c +8 -8
  60. data/src/yarp.c +1288 -1021
  61. data/yarp.gemspec +4 -1
  62. metadata +6 -3
@@ -24,6 +24,6 @@
24
24
  // characters that are trailing bytes of multi-byte characters. For example, in
25
25
  // Shift-JIS, the backslash character can be a trailing byte. In that case we
26
26
  // need to take a slower path and iterate one multi-byte character at a time.
27
- const char * yp_strpbrk(yp_parser_t *parser, const char *source, const char *charset, ptrdiff_t length);
27
+ const uint8_t * yp_strpbrk(yp_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length);
28
28
 
29
29
  #endif
@@ -1,4 +1,4 @@
1
1
  #define YP_VERSION_MAJOR 0
2
- #define YP_VERSION_MINOR 9
2
+ #define YP_VERSION_MINOR 11
3
3
  #define YP_VERSION_PATCH 0
4
- #define YP_VERSION "0.9.0"
4
+ #define YP_VERSION "0.11.0"
data/include/yarp.h CHANGED
@@ -16,6 +16,7 @@
16
16
  #include "yarp/version.h"
17
17
 
18
18
  #include <assert.h>
19
+ #include <errno.h>
19
20
  #include <stdarg.h>
20
21
  #include <stdbool.h>
21
22
  #include <stdint.h>
@@ -40,7 +41,7 @@ void yp_scope_node_init(yp_node_t *node, yp_scope_node_t *dest);
40
41
  YP_EXPORTED_FUNCTION const char * yp_version(void);
41
42
 
42
43
  // Initialize a parser with the given start and end pointers.
43
- YP_EXPORTED_FUNCTION void yp_parser_init(yp_parser_t *parser, const char *source, size_t size, const char *filepath);
44
+ YP_EXPORTED_FUNCTION void yp_parser_init(yp_parser_t *parser, const uint8_t *source, size_t size, const char *filepath);
44
45
 
45
46
  // Register a callback that will be called whenever YARP changes the encoding it
46
47
  // is using to parse based on the magic comment.
@@ -66,14 +67,14 @@ YP_EXPORTED_FUNCTION void yp_prettyprint(yp_parser_t *parser, yp_node_t *node, y
66
67
  YP_EXPORTED_FUNCTION void yp_serialize(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer);
67
68
 
68
69
  // Parse the given source to the AST and serialize the AST to the given buffer.
69
- YP_EXPORTED_FUNCTION void yp_parse_serialize(const char *source, size_t size, yp_buffer_t *buffer, const char *metadata);
70
+ YP_EXPORTED_FUNCTION void yp_parse_serialize(const uint8_t *source, size_t size, yp_buffer_t *buffer, const char *metadata);
70
71
 
71
72
  // Lex the given source and serialize to the given buffer.
72
- YP_EXPORTED_FUNCTION void yp_lex_serialize(const char *source, size_t size, const char *filepath, yp_buffer_t *buffer);
73
+ YP_EXPORTED_FUNCTION void yp_lex_serialize(const uint8_t *source, size_t size, const char *filepath, yp_buffer_t *buffer);
73
74
 
74
75
  // Parse and serialize both the AST and the tokens represented by the given
75
76
  // source to the given buffer.
76
- YP_EXPORTED_FUNCTION void yp_parse_lex_serialize(const char *source, size_t size, yp_buffer_t *buffer, const char *metadata);
77
+ YP_EXPORTED_FUNCTION void yp_parse_lex_serialize(const uint8_t *source, size_t size, yp_buffer_t *buffer, const char *metadata);
77
78
 
78
79
  // Returns a string representation of the given token type.
79
80
  YP_EXPORTED_FUNCTION const char * yp_token_type_to_str(yp_token_type_t token_type);
@@ -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, 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, 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, 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, node.name)
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, node.name)
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, node.name)
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, node.name)
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, node.name)
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, node.name)
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, 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, 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, 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, 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, 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, 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
@@ -65,6 +65,11 @@ module YARP
65
65
  node.copy(expression: visit(node.expression))
66
66
  end
67
67
 
68
+ # Copy a BlockLocalVariableNode node
69
+ def visit_block_local_variable_node(node)
70
+ node.copy
71
+ end
72
+
68
73
  # Copy a BlockNode node
69
74
  def visit_block_node(node)
70
75
  node.copy(parameters: visit(node.parameters), body: visit(node.body))
@@ -77,7 +82,7 @@ module YARP
77
82
 
78
83
  # Copy a BlockParametersNode node
79
84
  def visit_block_parameters_node(node)
80
- node.copy(parameters: visit(node.parameters))
85
+ node.copy(parameters: visit(node.parameters), locals: visit_all(node.locals))
81
86
  end
82
87
 
83
88
  # Copy a BreakNode node
@@ -85,24 +90,24 @@ module YARP
85
90
  node.copy(arguments: visit(node.arguments))
86
91
  end
87
92
 
93
+ # Copy a CallAndWriteNode node
94
+ def visit_call_and_write_node(node)
95
+ node.copy(receiver: visit(node.receiver), arguments: visit(node.arguments), value: visit(node.value))
96
+ end
97
+
88
98
  # Copy a CallNode node
89
99
  def visit_call_node(node)
90
100
  node.copy(receiver: visit(node.receiver), arguments: visit(node.arguments), block: visit(node.block))
91
101
  end
92
102
 
93
- # Copy a CallOperatorAndWriteNode node
94
- def visit_call_operator_and_write_node(node)
95
- node.copy(target: visit(node.target), value: visit(node.value))
96
- end
97
-
98
- # Copy a CallOperatorOrWriteNode node
99
- def visit_call_operator_or_write_node(node)
100
- node.copy(target: visit(node.target), value: visit(node.value))
101
- end
102
-
103
103
  # Copy a CallOperatorWriteNode node
104
104
  def visit_call_operator_write_node(node)
105
- node.copy(target: visit(node.target), value: visit(node.value))
105
+ node.copy(receiver: visit(node.receiver), arguments: visit(node.arguments), value: visit(node.value))
106
+ end
107
+
108
+ # Copy a CallOrWriteNode node
109
+ def visit_call_or_write_node(node)
110
+ node.copy(receiver: visit(node.receiver), arguments: visit(node.arguments), value: visit(node.value))
106
111
  end
107
112
 
108
113
  # Copy a CapturePatternNode node
@@ -460,6 +465,11 @@ module YARP
460
465
  node.copy(constant_path: visit(node.constant_path), body: visit(node.body))
461
466
  end
462
467
 
468
+ # Copy a MultiTargetNode node
469
+ def visit_multi_target_node(node)
470
+ node.copy(targets: visit_all(node.targets))
471
+ end
472
+
463
473
  # Copy a MultiWriteNode node
464
474
  def visit_multi_write_node(node)
465
475
  node.copy(targets: visit_all(node.targets), value: visit(node.value))