jruby-prism-parser 0.23.0.pre.SNAPSHOT-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +401 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +62 -0
- data/LICENSE.md +7 -0
- data/Makefile +101 -0
- data/README.md +98 -0
- data/config.yml +2902 -0
- data/docs/build_system.md +91 -0
- data/docs/configuration.md +64 -0
- data/docs/cruby_compilation.md +27 -0
- data/docs/design.md +53 -0
- data/docs/encoding.md +121 -0
- data/docs/fuzzing.md +88 -0
- data/docs/heredocs.md +36 -0
- data/docs/javascript.md +118 -0
- data/docs/local_variable_depth.md +229 -0
- data/docs/mapping.md +117 -0
- data/docs/parser_translation.md +34 -0
- data/docs/parsing_rules.md +19 -0
- data/docs/releasing.md +98 -0
- data/docs/ripper.md +36 -0
- data/docs/ruby_api.md +43 -0
- data/docs/ruby_parser_translation.md +19 -0
- data/docs/serialization.md +209 -0
- data/docs/testing.md +55 -0
- data/ext/prism/api_node.c +5098 -0
- data/ext/prism/api_pack.c +267 -0
- data/ext/prism/extconf.rb +110 -0
- data/ext/prism/extension.c +1155 -0
- data/ext/prism/extension.h +18 -0
- data/include/prism/ast.h +5807 -0
- data/include/prism/defines.h +102 -0
- data/include/prism/diagnostic.h +339 -0
- data/include/prism/encoding.h +265 -0
- data/include/prism/node.h +57 -0
- data/include/prism/options.h +230 -0
- data/include/prism/pack.h +152 -0
- data/include/prism/parser.h +732 -0
- data/include/prism/prettyprint.h +26 -0
- data/include/prism/regexp.h +33 -0
- data/include/prism/util/pm_buffer.h +155 -0
- data/include/prism/util/pm_char.h +205 -0
- data/include/prism/util/pm_constant_pool.h +209 -0
- data/include/prism/util/pm_list.h +97 -0
- data/include/prism/util/pm_memchr.h +29 -0
- data/include/prism/util/pm_newline_list.h +93 -0
- data/include/prism/util/pm_state_stack.h +42 -0
- data/include/prism/util/pm_string.h +150 -0
- data/include/prism/util/pm_string_list.h +44 -0
- data/include/prism/util/pm_strncasecmp.h +32 -0
- data/include/prism/util/pm_strpbrk.h +46 -0
- data/include/prism/version.h +29 -0
- data/include/prism.h +289 -0
- data/jruby-prism.jar +0 -0
- data/lib/prism/compiler.rb +486 -0
- data/lib/prism/debug.rb +206 -0
- data/lib/prism/desugar_compiler.rb +207 -0
- data/lib/prism/dispatcher.rb +2150 -0
- data/lib/prism/dot_visitor.rb +4634 -0
- data/lib/prism/dsl.rb +785 -0
- data/lib/prism/ffi.rb +346 -0
- data/lib/prism/lex_compat.rb +908 -0
- data/lib/prism/mutation_compiler.rb +753 -0
- data/lib/prism/node.rb +17864 -0
- data/lib/prism/node_ext.rb +212 -0
- data/lib/prism/node_inspector.rb +68 -0
- data/lib/prism/pack.rb +224 -0
- data/lib/prism/parse_result/comments.rb +177 -0
- data/lib/prism/parse_result/newlines.rb +64 -0
- data/lib/prism/parse_result.rb +498 -0
- data/lib/prism/pattern.rb +250 -0
- data/lib/prism/serialize.rb +1354 -0
- data/lib/prism/translation/parser/compiler.rb +1838 -0
- data/lib/prism/translation/parser/lexer.rb +335 -0
- data/lib/prism/translation/parser/rubocop.rb +37 -0
- data/lib/prism/translation/parser.rb +178 -0
- data/lib/prism/translation/ripper.rb +577 -0
- data/lib/prism/translation/ruby_parser.rb +1521 -0
- data/lib/prism/translation.rb +11 -0
- data/lib/prism/version.rb +3 -0
- data/lib/prism/visitor.rb +495 -0
- data/lib/prism.rb +99 -0
- data/prism.gemspec +135 -0
- data/rbi/prism.rbi +7767 -0
- data/rbi/prism_static.rbi +207 -0
- data/sig/prism.rbs +4773 -0
- data/sig/prism_static.rbs +201 -0
- data/src/diagnostic.c +400 -0
- data/src/encoding.c +5132 -0
- data/src/node.c +2786 -0
- data/src/options.c +213 -0
- data/src/pack.c +493 -0
- data/src/prettyprint.c +8881 -0
- data/src/prism.c +18406 -0
- data/src/regexp.c +638 -0
- data/src/serialize.c +1554 -0
- data/src/token_type.c +700 -0
- data/src/util/pm_buffer.c +190 -0
- data/src/util/pm_char.c +318 -0
- data/src/util/pm_constant_pool.c +322 -0
- data/src/util/pm_list.c +49 -0
- data/src/util/pm_memchr.c +35 -0
- data/src/util/pm_newline_list.c +84 -0
- data/src/util/pm_state_stack.c +25 -0
- data/src/util/pm_string.c +203 -0
- data/src/util/pm_string_list.c +28 -0
- data/src/util/pm_strncasecmp.c +24 -0
- data/src/util/pm_strpbrk.c +180 -0
- metadata +156 -0
@@ -0,0 +1,207 @@
|
|
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
|
+
0,
|
161
|
+
read_class.new(*arguments, node.name_loc),
|
162
|
+
nil,
|
163
|
+
node.operator_loc.slice.chomp("="),
|
164
|
+
node.operator_loc.copy(length: node.operator_loc.length - 1),
|
165
|
+
nil,
|
166
|
+
ArgumentsNode.new(0, [node.value], node.value.location),
|
167
|
+
nil,
|
168
|
+
nil,
|
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
|
+
node.operator_loc,
|
192
|
+
StatementsNode.new([read_class.new(*arguments, node.name_loc)], node.location),
|
193
|
+
ElseNode.new(
|
194
|
+
node.operator_loc,
|
195
|
+
StatementsNode.new(
|
196
|
+
[write_class.new(*arguments, node.name_loc, node.value, node.operator_loc, node.location)],
|
197
|
+
node.location
|
198
|
+
),
|
199
|
+
node.operator_loc,
|
200
|
+
node.location
|
201
|
+
),
|
202
|
+
node.operator_loc,
|
203
|
+
node.location
|
204
|
+
)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|