prism 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +172 -0
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +62 -0
  5. data/LICENSE.md +7 -0
  6. data/Makefile +84 -0
  7. data/README.md +89 -0
  8. data/config.yml +2481 -0
  9. data/docs/build_system.md +74 -0
  10. data/docs/building.md +22 -0
  11. data/docs/configuration.md +60 -0
  12. data/docs/design.md +53 -0
  13. data/docs/encoding.md +117 -0
  14. data/docs/fuzzing.md +93 -0
  15. data/docs/heredocs.md +36 -0
  16. data/docs/mapping.md +117 -0
  17. data/docs/ripper.md +36 -0
  18. data/docs/ruby_api.md +25 -0
  19. data/docs/serialization.md +181 -0
  20. data/docs/testing.md +55 -0
  21. data/ext/prism/api_node.c +4725 -0
  22. data/ext/prism/api_pack.c +256 -0
  23. data/ext/prism/extconf.rb +136 -0
  24. data/ext/prism/extension.c +626 -0
  25. data/ext/prism/extension.h +18 -0
  26. data/include/prism/ast.h +1932 -0
  27. data/include/prism/defines.h +45 -0
  28. data/include/prism/diagnostic.h +231 -0
  29. data/include/prism/enc/pm_encoding.h +95 -0
  30. data/include/prism/node.h +41 -0
  31. data/include/prism/pack.h +141 -0
  32. data/include/prism/parser.h +418 -0
  33. data/include/prism/regexp.h +19 -0
  34. data/include/prism/unescape.h +48 -0
  35. data/include/prism/util/pm_buffer.h +51 -0
  36. data/include/prism/util/pm_char.h +91 -0
  37. data/include/prism/util/pm_constant_pool.h +78 -0
  38. data/include/prism/util/pm_list.h +67 -0
  39. data/include/prism/util/pm_memchr.h +14 -0
  40. data/include/prism/util/pm_newline_list.h +61 -0
  41. data/include/prism/util/pm_state_stack.h +24 -0
  42. data/include/prism/util/pm_string.h +61 -0
  43. data/include/prism/util/pm_string_list.h +25 -0
  44. data/include/prism/util/pm_strpbrk.h +29 -0
  45. data/include/prism/version.h +4 -0
  46. data/include/prism.h +82 -0
  47. data/lib/prism/compiler.rb +465 -0
  48. data/lib/prism/debug.rb +157 -0
  49. data/lib/prism/desugar_compiler.rb +206 -0
  50. data/lib/prism/dispatcher.rb +2051 -0
  51. data/lib/prism/dsl.rb +750 -0
  52. data/lib/prism/ffi.rb +251 -0
  53. data/lib/prism/lex_compat.rb +838 -0
  54. data/lib/prism/mutation_compiler.rb +718 -0
  55. data/lib/prism/node.rb +14540 -0
  56. data/lib/prism/node_ext.rb +55 -0
  57. data/lib/prism/node_inspector.rb +68 -0
  58. data/lib/prism/pack.rb +185 -0
  59. data/lib/prism/parse_result/comments.rb +172 -0
  60. data/lib/prism/parse_result/newlines.rb +60 -0
  61. data/lib/prism/parse_result.rb +266 -0
  62. data/lib/prism/pattern.rb +239 -0
  63. data/lib/prism/ripper_compat.rb +174 -0
  64. data/lib/prism/serialize.rb +662 -0
  65. data/lib/prism/visitor.rb +470 -0
  66. data/lib/prism.rb +64 -0
  67. data/prism.gemspec +113 -0
  68. data/src/diagnostic.c +287 -0
  69. data/src/enc/pm_big5.c +52 -0
  70. data/src/enc/pm_euc_jp.c +58 -0
  71. data/src/enc/pm_gbk.c +61 -0
  72. data/src/enc/pm_shift_jis.c +56 -0
  73. data/src/enc/pm_tables.c +507 -0
  74. data/src/enc/pm_unicode.c +2324 -0
  75. data/src/enc/pm_windows_31j.c +56 -0
  76. data/src/node.c +2633 -0
  77. data/src/pack.c +493 -0
  78. data/src/prettyprint.c +2136 -0
  79. data/src/prism.c +14587 -0
  80. data/src/regexp.c +580 -0
  81. data/src/serialize.c +1899 -0
  82. data/src/token_type.c +349 -0
  83. data/src/unescape.c +637 -0
  84. data/src/util/pm_buffer.c +103 -0
  85. data/src/util/pm_char.c +272 -0
  86. data/src/util/pm_constant_pool.c +252 -0
  87. data/src/util/pm_list.c +41 -0
  88. data/src/util/pm_memchr.c +33 -0
  89. data/src/util/pm_newline_list.c +134 -0
  90. data/src/util/pm_state_stack.c +19 -0
  91. data/src/util/pm_string.c +200 -0
  92. data/src/util/pm_string_list.c +29 -0
  93. data/src/util/pm_strncasecmp.c +17 -0
  94. data/src/util/pm_strpbrk.c +66 -0
  95. metadata +138 -0
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prism
4
+ # DesugarCompiler is a compiler that desugars Ruby code into a more primitive
5
+ # form. This is useful for consumers that want to deal with fewer node types.
6
+ class DesugarCompiler < MutationCompiler
7
+ # @@foo &&= bar
8
+ #
9
+ # becomes
10
+ #
11
+ # @@foo && @@foo = bar
12
+ def visit_class_variable_and_write_node(node)
13
+ desugar_and_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, node.name)
14
+ end
15
+
16
+ # @@foo ||= bar
17
+ #
18
+ # becomes
19
+ #
20
+ # defined?(@@foo) ? @@foo : @@foo = bar
21
+ def visit_class_variable_or_write_node(node)
22
+ desugar_or_write_defined_node(node, ClassVariableReadNode, ClassVariableWriteNode, node.name)
23
+ end
24
+
25
+ # @@foo += bar
26
+ #
27
+ # becomes
28
+ #
29
+ # @@foo = @@foo + bar
30
+ def visit_class_variable_operator_write_node(node)
31
+ desugar_operator_write_node(node, ClassVariableReadNode, ClassVariableWriteNode, node.name)
32
+ end
33
+
34
+ # Foo &&= bar
35
+ #
36
+ # becomes
37
+ #
38
+ # Foo && Foo = bar
39
+ def visit_constant_and_write_node(node)
40
+ desugar_and_write_node(node, ConstantReadNode, ConstantWriteNode, node.name)
41
+ end
42
+
43
+ # Foo ||= bar
44
+ #
45
+ # becomes
46
+ #
47
+ # defined?(Foo) ? Foo : Foo = bar
48
+ def visit_constant_or_write_node(node)
49
+ desugar_or_write_defined_node(node, ConstantReadNode, ConstantWriteNode, node.name)
50
+ end
51
+
52
+ # Foo += bar
53
+ #
54
+ # becomes
55
+ #
56
+ # Foo = Foo + bar
57
+ def visit_constant_operator_write_node(node)
58
+ desugar_operator_write_node(node, ConstantReadNode, ConstantWriteNode, node.name)
59
+ end
60
+
61
+ # $foo &&= bar
62
+ #
63
+ # becomes
64
+ #
65
+ # $foo && $foo = bar
66
+ def visit_global_variable_and_write_node(node)
67
+ desugar_and_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, node.name)
68
+ end
69
+
70
+ # $foo ||= bar
71
+ #
72
+ # becomes
73
+ #
74
+ # defined?($foo) ? $foo : $foo = bar
75
+ def visit_global_variable_or_write_node(node)
76
+ desugar_or_write_defined_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, node.name)
77
+ end
78
+
79
+ # $foo += bar
80
+ #
81
+ # becomes
82
+ #
83
+ # $foo = $foo + bar
84
+ def visit_global_variable_operator_write_node(node)
85
+ desugar_operator_write_node(node, GlobalVariableReadNode, GlobalVariableWriteNode, node.name)
86
+ end
87
+
88
+ # @foo &&= bar
89
+ #
90
+ # becomes
91
+ #
92
+ # @foo && @foo = bar
93
+ def visit_instance_variable_and_write_node(node)
94
+ desugar_and_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, node.name)
95
+ end
96
+
97
+ # @foo ||= bar
98
+ #
99
+ # becomes
100
+ #
101
+ # @foo || @foo = bar
102
+ def visit_instance_variable_or_write_node(node)
103
+ desugar_or_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, node.name)
104
+ end
105
+
106
+ # @foo += bar
107
+ #
108
+ # becomes
109
+ #
110
+ # @foo = @foo + bar
111
+ def visit_instance_variable_operator_write_node(node)
112
+ desugar_operator_write_node(node, InstanceVariableReadNode, InstanceVariableWriteNode, node.name)
113
+ end
114
+
115
+ # foo &&= bar
116
+ #
117
+ # becomes
118
+ #
119
+ # foo && foo = bar
120
+ def visit_local_variable_and_write_node(node)
121
+ desugar_and_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, node.name, node.depth)
122
+ end
123
+
124
+ # foo ||= bar
125
+ #
126
+ # becomes
127
+ #
128
+ # foo || foo = bar
129
+ def visit_local_variable_or_write_node(node)
130
+ desugar_or_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, node.name, node.depth)
131
+ end
132
+
133
+ # foo += bar
134
+ #
135
+ # becomes
136
+ #
137
+ # foo = foo + bar
138
+ def visit_local_variable_operator_write_node(node)
139
+ desugar_operator_write_node(node, LocalVariableReadNode, LocalVariableWriteNode, node.name, node.depth)
140
+ end
141
+
142
+ private
143
+
144
+ # Desugar `x &&= y` to `x && x = y`
145
+ def desugar_and_write_node(node, read_class, write_class, *arguments)
146
+ AndNode.new(
147
+ read_class.new(*arguments, node.name_loc),
148
+ write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location),
149
+ node.operator_loc,
150
+ node.location
151
+ )
152
+ end
153
+
154
+ # Desugar `x += y` to `x = x + y`
155
+ def desugar_operator_write_node(node, read_class, write_class, *arguments)
156
+ write_class.new(
157
+ *arguments,
158
+ node.name_loc,
159
+ CallNode.new(
160
+ read_class.new(*arguments, node.name_loc),
161
+ nil,
162
+ node.operator_loc.copy(length: node.operator_loc.length - 1),
163
+ nil,
164
+ ArgumentsNode.new([node.value], node.value.location),
165
+ nil,
166
+ nil,
167
+ 0,
168
+ node.operator_loc.slice.chomp("="),
169
+ node.location
170
+ ),
171
+ node.operator_loc.copy(start_offset: node.operator_loc.end_offset - 1, length: 1),
172
+ node.location
173
+ )
174
+ end
175
+
176
+ # Desugar `x ||= y` to `x || x = y`
177
+ def desugar_or_write_node(node, read_class, write_class, *arguments)
178
+ OrNode.new(
179
+ read_class.new(*arguments, node.name_loc),
180
+ write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location),
181
+ node.operator_loc,
182
+ node.location
183
+ )
184
+ end
185
+
186
+ # Desugar `x ||= y` to `defined?(x) ? x : x = y`
187
+ def desugar_or_write_defined_node(node, read_class, write_class, *arguments)
188
+ IfNode.new(
189
+ node.operator_loc,
190
+ DefinedNode.new(nil, read_class.new(*arguments, node.name_loc), nil, node.operator_loc, node.name_loc),
191
+ StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
192
+ ElseNode.new(
193
+ node.operator_loc,
194
+ StatementsNode.new(
195
+ [write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
196
+ node.location
197
+ ),
198
+ node.operator_loc,
199
+ node.location
200
+ ),
201
+ node.operator_loc,
202
+ node.location
203
+ )
204
+ end
205
+ end
206
+ end