flog-rs 0.1.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.
- checksums.yaml +7 -0
- data/Cargo.lock +486 -0
- data/Cargo.toml +22 -0
- data/DIVERGENCES.md +14 -0
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/THIRD_PARTY_NOTICES.md +45 -0
- data/crates/flog-cli/Cargo.toml +23 -0
- data/crates/flog-cli/src/main.rs +224 -0
- data/crates/flog-core/Cargo.toml +18 -0
- data/crates/flog-core/src/attach_comments.rs +271 -0
- data/crates/flog-core/src/lib.rs +307 -0
- data/crates/flog-core/src/literal.rs +217 -0
- data/crates/flog-core/src/ruby_format.rs +131 -0
- data/crates/flog-core/src/ruby_sort.rs +192 -0
- data/crates/flog-core/src/scorer.rs +1851 -0
- data/crates/flog-core/src/scores.rs +96 -0
- data/crates/flog-core/src/syntax_error.rs +145 -0
- data/crates/flog-core/src/translation.rs +487 -0
- data/ext/flog_rs/Cargo.toml +18 -0
- data/ext/flog_rs/extconf.rb +6 -0
- data/ext/flog_rs/src/flog.rs +493 -0
- data/ext/flog_rs/src/lib.rs +39 -0
- data/lib/flog_rs/version.rb +8 -0
- data/lib/flog_rs.rb +16 -0
- metadata +86 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
//! What `Prism::Translation::RubyParser` (prism 2.1.0) would produce for a
|
|
2
|
+
//! Prism node, without building that tree.
|
|
3
|
+
//!
|
|
4
|
+
//! Flog 4.10.0 scores the ruby_parser tree that this translator builds. A few
|
|
5
|
+
//! Flog rules look at the translated node's type (`sexp_type`) or at a literal's
|
|
6
|
+
//! value, so this module answers those questions directly from the Prism node.
|
|
7
|
+
//! Each function cites the translator method it mirrors.
|
|
8
|
+
|
|
9
|
+
use ruby_prism::Node;
|
|
10
|
+
|
|
11
|
+
/// The translator's sexp type for `node`, or `None` where it returns Ruby `nil`
|
|
12
|
+
/// or a bare value (a Symbol for a required parameter, for example).
|
|
13
|
+
pub fn sexp_type(node: &Node<'_>, in_pattern: bool) -> Option<&'static str> {
|
|
14
|
+
let kind = match node {
|
|
15
|
+
Node::AliasGlobalVariableNode { .. } => "valias",
|
|
16
|
+
Node::AliasMethodNode { .. } => "alias",
|
|
17
|
+
Node::AlternationPatternNode { .. } => "or",
|
|
18
|
+
Node::AndNode { .. } => "and",
|
|
19
|
+
Node::ArrayNode { .. } => {
|
|
20
|
+
if in_pattern {
|
|
21
|
+
"array_pat"
|
|
22
|
+
} else {
|
|
23
|
+
"array"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
Node::ArrayPatternNode { .. } => "array_pat",
|
|
27
|
+
Node::AssocSplatNode { .. } => "kwsplat",
|
|
28
|
+
Node::BackReferenceReadNode { .. } => "back_ref",
|
|
29
|
+
Node::BeginNode { .. } => return begin_type(node, in_pattern),
|
|
30
|
+
Node::BlockArgumentNode { .. } => "block_pass",
|
|
31
|
+
Node::BlockParametersNode { .. } => "args",
|
|
32
|
+
Node::BreakNode { .. } => "break",
|
|
33
|
+
Node::CallNode { .. } => return Some(call_type(&node.as_call_node().unwrap())),
|
|
34
|
+
Node::CallAndWriteNode { .. } => {
|
|
35
|
+
let write = node.as_call_and_write_node().unwrap();
|
|
36
|
+
call_operator_write_type(
|
|
37
|
+
write.call_operator_loc().map(|loc| loc.as_slice()),
|
|
38
|
+
&write.value(),
|
|
39
|
+
write.is_safe_navigation(),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
Node::CallOrWriteNode { .. } => {
|
|
43
|
+
let write = node.as_call_or_write_node().unwrap();
|
|
44
|
+
call_operator_write_type(
|
|
45
|
+
write.call_operator_loc().map(|loc| loc.as_slice()),
|
|
46
|
+
&write.value(),
|
|
47
|
+
write.is_safe_navigation(),
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
Node::CallOperatorWriteNode { .. } => {
|
|
51
|
+
let write = node.as_call_operator_write_node().unwrap();
|
|
52
|
+
call_operator_write_type(
|
|
53
|
+
write.call_operator_loc().map(|loc| loc.as_slice()),
|
|
54
|
+
&write.value(),
|
|
55
|
+
write.is_safe_navigation(),
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
Node::CallTargetNode { .. } => "attrasgn",
|
|
59
|
+
Node::CapturePatternNode { .. } => "lasgn",
|
|
60
|
+
Node::CaseMatchNode { .. } | Node::CaseNode { .. } => "case",
|
|
61
|
+
Node::ClassNode { .. } => "class",
|
|
62
|
+
Node::ClassVariableReadNode { .. } => "cvar",
|
|
63
|
+
// cvasgn inside a method definition; the difference never affects a score.
|
|
64
|
+
Node::ClassVariableWriteNode { .. }
|
|
65
|
+
| Node::ClassVariableOperatorWriteNode { .. }
|
|
66
|
+
| Node::ClassVariableTargetNode { .. } => "cvdecl",
|
|
67
|
+
Node::ClassVariableAndWriteNode { .. } => "op_asgn_and",
|
|
68
|
+
Node::ClassVariableOrWriteNode { .. } => "op_asgn_or",
|
|
69
|
+
Node::ConstantReadNode { .. } => "const",
|
|
70
|
+
Node::ConstantWriteNode { .. }
|
|
71
|
+
| Node::ConstantOperatorWriteNode { .. }
|
|
72
|
+
| Node::ConstantTargetNode { .. } => "cdecl",
|
|
73
|
+
Node::ConstantAndWriteNode { .. } => "op_asgn_and",
|
|
74
|
+
Node::ConstantOrWriteNode { .. } => "op_asgn_or",
|
|
75
|
+
Node::ConstantPathNode { .. } => {
|
|
76
|
+
if node.as_constant_path_node().unwrap().parent().is_none() {
|
|
77
|
+
"colon3"
|
|
78
|
+
} else {
|
|
79
|
+
"colon2"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
Node::ConstantPathWriteNode { .. } => "cdecl",
|
|
83
|
+
Node::ConstantPathOperatorWriteNode { .. } => "op_asgn",
|
|
84
|
+
Node::ConstantPathAndWriteNode { .. } => "op_asgn_and",
|
|
85
|
+
Node::ConstantPathOrWriteNode { .. } => "op_asgn_or",
|
|
86
|
+
Node::ConstantPathTargetNode { .. } => "const",
|
|
87
|
+
Node::DefNode { .. } => {
|
|
88
|
+
if node.as_def_node().unwrap().receiver().is_none() {
|
|
89
|
+
"defn"
|
|
90
|
+
} else {
|
|
91
|
+
"defs"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
Node::DefinedNode { .. } => "defined",
|
|
95
|
+
Node::ElseNode { .. } => {
|
|
96
|
+
let statements = node.as_else_node().unwrap().statements()?;
|
|
97
|
+
return sexp_type(&statements.as_node(), in_pattern);
|
|
98
|
+
}
|
|
99
|
+
Node::EmbeddedStatementsNode { .. } | Node::EmbeddedVariableNode { .. } => "evstr",
|
|
100
|
+
Node::EnsureNode { .. } => match node.as_ensure_node().unwrap().statements() {
|
|
101
|
+
None => "nil",
|
|
102
|
+
Some(statements) => return sexp_type(&statements.as_node(), in_pattern),
|
|
103
|
+
},
|
|
104
|
+
Node::FalseNode { .. } => "false",
|
|
105
|
+
Node::FindPatternNode { .. } => "find_pat",
|
|
106
|
+
Node::FlipFlopNode { .. } => {
|
|
107
|
+
let flip_flop = node.as_flip_flop_node().unwrap();
|
|
108
|
+
let integer_bounds = matches!(flip_flop.left(), Some(Node::IntegerNode { .. }))
|
|
109
|
+
&& matches!(flip_flop.right(), Some(Node::IntegerNode { .. }));
|
|
110
|
+
if integer_bounds {
|
|
111
|
+
"lit"
|
|
112
|
+
} else if flip_flop.is_exclude_end() {
|
|
113
|
+
"flip3"
|
|
114
|
+
} else {
|
|
115
|
+
"flip2"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
Node::FloatNode { .. } => "lit",
|
|
119
|
+
Node::ForNode { .. } => "for",
|
|
120
|
+
Node::ForwardingArgumentsNode { .. } | Node::ForwardingParameterNode { .. } => {
|
|
121
|
+
"forward_args"
|
|
122
|
+
}
|
|
123
|
+
Node::ForwardingSuperNode { .. } => {
|
|
124
|
+
if node.as_forwarding_super_node().unwrap().block().is_some() {
|
|
125
|
+
"iter"
|
|
126
|
+
} else {
|
|
127
|
+
"zsuper"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
Node::GlobalVariableReadNode { .. } => "gvar",
|
|
131
|
+
Node::GlobalVariableWriteNode { .. }
|
|
132
|
+
| Node::GlobalVariableOperatorWriteNode { .. }
|
|
133
|
+
| Node::GlobalVariableTargetNode { .. } => "gasgn",
|
|
134
|
+
Node::GlobalVariableAndWriteNode { .. } => "op_asgn_and",
|
|
135
|
+
Node::GlobalVariableOrWriteNode { .. } => "op_asgn_or",
|
|
136
|
+
Node::HashNode { .. } | Node::KeywordHashNode { .. } => "hash",
|
|
137
|
+
Node::HashPatternNode { .. } => "hash_pat",
|
|
138
|
+
Node::IfNode { .. } | Node::UnlessNode { .. } => "if",
|
|
139
|
+
Node::ImaginaryNode { .. } => "lit",
|
|
140
|
+
Node::ImplicitNode { .. } | Node::ImplicitRestNode { .. } => return None,
|
|
141
|
+
Node::InNode { .. } => "in",
|
|
142
|
+
Node::IndexAndWriteNode { .. }
|
|
143
|
+
| Node::IndexOperatorWriteNode { .. }
|
|
144
|
+
| Node::IndexOrWriteNode { .. } => "op_asgn1",
|
|
145
|
+
Node::IndexTargetNode { .. } => "attrasgn",
|
|
146
|
+
Node::InstanceVariableReadNode { .. } => "ivar",
|
|
147
|
+
Node::InstanceVariableWriteNode { .. }
|
|
148
|
+
| Node::InstanceVariableOperatorWriteNode { .. }
|
|
149
|
+
| Node::InstanceVariableTargetNode { .. } => "iasgn",
|
|
150
|
+
Node::InstanceVariableAndWriteNode { .. } => "op_asgn_and",
|
|
151
|
+
Node::InstanceVariableOrWriteNode { .. } => "op_asgn_or",
|
|
152
|
+
Node::IntegerNode { .. } => "lit",
|
|
153
|
+
Node::InterpolatedMatchLastLineNode { .. } => "match",
|
|
154
|
+
Node::InterpolatedRegularExpressionNode { .. } => {
|
|
155
|
+
let regexp = node.as_interpolated_regular_expression_node().unwrap();
|
|
156
|
+
if interpolated_string_value(®exp.parts()).is_some() {
|
|
157
|
+
"lit"
|
|
158
|
+
} else {
|
|
159
|
+
"dregx"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
Node::InterpolatedStringNode { .. } => {
|
|
163
|
+
let string = node.as_interpolated_string_node().unwrap();
|
|
164
|
+
if interpolated_string_value(&string.parts()).is_some() {
|
|
165
|
+
"str"
|
|
166
|
+
} else {
|
|
167
|
+
"dstr"
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
Node::InterpolatedSymbolNode { .. } => {
|
|
171
|
+
let symbol = node.as_interpolated_symbol_node().unwrap();
|
|
172
|
+
if interpolated_string_value(&symbol.parts()).is_some() {
|
|
173
|
+
"lit"
|
|
174
|
+
} else {
|
|
175
|
+
"dsym"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
Node::InterpolatedXStringNode { .. } => {
|
|
179
|
+
let xstring = node.as_interpolated_x_string_node().unwrap();
|
|
180
|
+
if interpolated_string_value(&xstring.parts()).is_some() {
|
|
181
|
+
"xstr"
|
|
182
|
+
} else {
|
|
183
|
+
"dxstr"
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// Flog 4.10.0 patches the translator so `it` and `_1` read as lvar.
|
|
187
|
+
Node::ItLocalVariableReadNode { .. } | Node::LocalVariableReadNode { .. } => "lvar",
|
|
188
|
+
Node::LambdaNode { .. } => "iter",
|
|
189
|
+
Node::LocalVariableWriteNode { .. }
|
|
190
|
+
| Node::LocalVariableOperatorWriteNode { .. }
|
|
191
|
+
| Node::LocalVariableTargetNode { .. } => "lasgn",
|
|
192
|
+
Node::LocalVariableAndWriteNode { .. } => "op_asgn_and",
|
|
193
|
+
Node::LocalVariableOrWriteNode { .. } => "op_asgn_or",
|
|
194
|
+
Node::MatchLastLineNode { .. } => "match",
|
|
195
|
+
Node::MatchPredicateNode { .. } | Node::MatchRequiredNode { .. } => "case",
|
|
196
|
+
Node::MatchWriteNode { .. } => "match2",
|
|
197
|
+
Node::ModuleNode { .. } => "module",
|
|
198
|
+
Node::MultiTargetNode { .. } | Node::MultiWriteNode { .. } => "masgn",
|
|
199
|
+
Node::NextNode { .. } => "next",
|
|
200
|
+
Node::NilNode { .. } => "nil",
|
|
201
|
+
Node::NoKeywordsParameterNode { .. } => {
|
|
202
|
+
if in_pattern {
|
|
203
|
+
"kwrest"
|
|
204
|
+
} else {
|
|
205
|
+
return None;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
Node::NumberedReferenceReadNode { .. } => "nth_ref",
|
|
209
|
+
Node::OptionalKeywordParameterNode { .. } | Node::RequiredKeywordParameterNode { .. } => {
|
|
210
|
+
"kwarg"
|
|
211
|
+
}
|
|
212
|
+
Node::OptionalParameterNode { .. } => "lasgn",
|
|
213
|
+
Node::OrNode { .. } => "or",
|
|
214
|
+
Node::ParametersNode { .. } => "args",
|
|
215
|
+
Node::ParenthesesNode { .. } => match node.as_parentheses_node().unwrap().body() {
|
|
216
|
+
None => "nil",
|
|
217
|
+
Some(body) => return sexp_type(&body, in_pattern),
|
|
218
|
+
},
|
|
219
|
+
Node::PinnedExpressionNode { .. } => {
|
|
220
|
+
return sexp_type(&node.as_pinned_expression_node().unwrap().expression(), false)
|
|
221
|
+
}
|
|
222
|
+
Node::PinnedVariableNode { .. } => {
|
|
223
|
+
return sexp_type(&node.as_pinned_variable_node().unwrap().variable(), in_pattern)
|
|
224
|
+
}
|
|
225
|
+
Node::PostExecutionNode { .. } | Node::PreExecutionNode { .. } => "iter",
|
|
226
|
+
Node::ProgramNode { .. } => {
|
|
227
|
+
let statements = node.as_program_node().unwrap().statements();
|
|
228
|
+
return sexp_type(&statements.as_node(), in_pattern);
|
|
229
|
+
}
|
|
230
|
+
Node::RangeNode { .. } => {
|
|
231
|
+
let range = node.as_range_node().unwrap();
|
|
232
|
+
if range_is_literal(&range, in_pattern) {
|
|
233
|
+
"lit"
|
|
234
|
+
} else if range.is_exclude_end() {
|
|
235
|
+
"dot3"
|
|
236
|
+
} else {
|
|
237
|
+
"dot2"
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
Node::RationalNode { .. } => "lit",
|
|
241
|
+
Node::RedoNode { .. } => "redo",
|
|
242
|
+
Node::RegularExpressionNode { .. } => "lit",
|
|
243
|
+
Node::RescueModifierNode { .. } => "rescue",
|
|
244
|
+
Node::RescueNode { .. } => "resbody",
|
|
245
|
+
Node::RetryNode { .. } => "retry",
|
|
246
|
+
Node::ReturnNode { .. } => "return",
|
|
247
|
+
Node::SelfNode { .. } => "self",
|
|
248
|
+
Node::ShareableConstantNode { .. } => {
|
|
249
|
+
return sexp_type(&node.as_shareable_constant_node().unwrap().write(), in_pattern)
|
|
250
|
+
}
|
|
251
|
+
Node::SingletonClassNode { .. } => "sclass",
|
|
252
|
+
Node::SourceEncodingNode { .. } => "colon2",
|
|
253
|
+
Node::SourceFileNode { .. } => "str",
|
|
254
|
+
Node::SourceLineNode { .. } => "lit",
|
|
255
|
+
Node::SplatNode { .. } => "splat",
|
|
256
|
+
Node::StatementsNode { .. } => {
|
|
257
|
+
let body = node.as_statements_node().unwrap().body();
|
|
258
|
+
return match body.len() {
|
|
259
|
+
0 => None,
|
|
260
|
+
1 => sexp_type(&body.first().unwrap(), in_pattern),
|
|
261
|
+
_ => Some("block"),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
Node::StringNode { .. } => "str",
|
|
265
|
+
Node::SuperNode { .. } => {
|
|
266
|
+
if matches!(node.as_super_node().unwrap().block(), Some(Node::BlockNode { .. })) {
|
|
267
|
+
"iter"
|
|
268
|
+
} else {
|
|
269
|
+
"super"
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
Node::SymbolNode { .. } => "lit",
|
|
273
|
+
Node::TrueNode { .. } => "true",
|
|
274
|
+
Node::UndefNode { .. } => {
|
|
275
|
+
if node.as_undef_node().unwrap().names().len() == 1 {
|
|
276
|
+
"undef"
|
|
277
|
+
} else {
|
|
278
|
+
"block"
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
Node::UntilNode { .. } => "until",
|
|
282
|
+
Node::WhenNode { .. } => "when",
|
|
283
|
+
Node::WhileNode { .. } => "while",
|
|
284
|
+
Node::XStringNode { .. } => "xstr",
|
|
285
|
+
Node::YieldNode { .. } => "yield",
|
|
286
|
+
// These return bare values (Symbols), not sexps.
|
|
287
|
+
Node::BlockLocalVariableNode { .. }
|
|
288
|
+
| Node::BlockParameterNode { .. }
|
|
289
|
+
| Node::KeywordRestParameterNode { .. }
|
|
290
|
+
| Node::NoBlockParameterNode { .. }
|
|
291
|
+
| Node::RequiredParameterNode { .. }
|
|
292
|
+
| Node::RestParameterNode { .. } => return None,
|
|
293
|
+
// The translator never visits these directly.
|
|
294
|
+
Node::ArgumentsNode { .. }
|
|
295
|
+
| Node::AssocNode { .. }
|
|
296
|
+
| Node::BlockNode { .. }
|
|
297
|
+
| Node::ErrorRecoveryNode { .. }
|
|
298
|
+
| Node::ItParametersNode { .. }
|
|
299
|
+
| Node::NumberedParametersNode { .. } => return None,
|
|
300
|
+
};
|
|
301
|
+
Some(kind)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/// `visit_begin_node`: the outermost sexp is `rescue` or `ensure` when those
|
|
305
|
+
/// clauses exist, otherwise the statements' own type (or `nil`).
|
|
306
|
+
fn begin_type(node: &Node<'_>, in_pattern: bool) -> Option<&'static str> {
|
|
307
|
+
let begin = node.as_begin_node().unwrap();
|
|
308
|
+
if begin.ensure_clause().is_some() {
|
|
309
|
+
return Some("ensure");
|
|
310
|
+
}
|
|
311
|
+
if begin.rescue_clause().is_some() {
|
|
312
|
+
return Some("rescue");
|
|
313
|
+
}
|
|
314
|
+
match begin.statements() {
|
|
315
|
+
None => Some("nil"),
|
|
316
|
+
Some(statements) => sexp_type(&statements.as_node(), in_pattern),
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/// `visit_call_node`: `!~` becomes `not`, some `=~` calls become `match2` or
|
|
321
|
+
/// `match3`, a literal block makes an `iter`, and otherwise `call` or
|
|
322
|
+
/// `attrasgn`, prefixed `safe_` for `&.`.
|
|
323
|
+
pub fn call_type(call: &ruby_prism::CallNode<'_>) -> &'static str {
|
|
324
|
+
let name = call.name().as_slice();
|
|
325
|
+
if name == b"!~" {
|
|
326
|
+
return "not";
|
|
327
|
+
}
|
|
328
|
+
if name == b"=~" {
|
|
329
|
+
if let Some(form) = match_form(call) {
|
|
330
|
+
return form.sexp_type();
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if matches!(call.block(), Some(Node::BlockNode { .. })) {
|
|
334
|
+
return "iter";
|
|
335
|
+
}
|
|
336
|
+
plain_call_type(call)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/// The call's own type before a literal block wraps it in an `iter`.
|
|
340
|
+
pub fn plain_call_type(call: &ruby_prism::CallNode<'_>) -> &'static str {
|
|
341
|
+
match (call.is_attribute_write(), call.is_safe_navigation()) {
|
|
342
|
+
(true, true) => "safe_attrasgn",
|
|
343
|
+
(true, false) => "attrasgn",
|
|
344
|
+
(false, true) => "safe_call",
|
|
345
|
+
(false, false) => "call",
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/// The two `=~` rewrites in `visit_call_node`.
|
|
350
|
+
pub enum MatchForm {
|
|
351
|
+
/// `s(:match2, receiver, argument)`: the receiver is a regexp literal.
|
|
352
|
+
ReceiverRegexp,
|
|
353
|
+
/// `s(:match3, argument, receiver)`: the receiver is a string literal, or
|
|
354
|
+
/// the argument is a regexp literal.
|
|
355
|
+
ArgumentFirst,
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
impl MatchForm {
|
|
359
|
+
pub fn sexp_type(&self) -> &'static str {
|
|
360
|
+
match self {
|
|
361
|
+
MatchForm::ReceiverRegexp => "match2",
|
|
362
|
+
MatchForm::ArgumentFirst => "match3",
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/// Which `=~` rewrite applies, if any. Only called for calls named `=~`.
|
|
368
|
+
pub fn match_form(call: &ruby_prism::CallNode<'_>) -> Option<MatchForm> {
|
|
369
|
+
let arguments = call.arguments()?.arguments();
|
|
370
|
+
if arguments.len() != 1 || call.block().is_some() {
|
|
371
|
+
return None;
|
|
372
|
+
}
|
|
373
|
+
match call.receiver() {
|
|
374
|
+
Some(Node::StringNode { .. }) => return Some(MatchForm::ArgumentFirst),
|
|
375
|
+
Some(Node::RegularExpressionNode { .. })
|
|
376
|
+
| Some(Node::InterpolatedRegularExpressionNode { .. }) => {
|
|
377
|
+
return Some(MatchForm::ReceiverRegexp)
|
|
378
|
+
}
|
|
379
|
+
_ => {}
|
|
380
|
+
}
|
|
381
|
+
match arguments.first() {
|
|
382
|
+
Some(Node::RegularExpressionNode { .. })
|
|
383
|
+
| Some(Node::InterpolatedRegularExpressionNode { .. }) => Some(MatchForm::ArgumentFirst),
|
|
384
|
+
_ => None,
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/// `op_asgn?` in the translator: `op_asgn` when the call operator is `::` or
|
|
389
|
+
/// the value is a call with arguments and no parentheses; otherwise `op_asgn2`.
|
|
390
|
+
fn call_operator_write_type(
|
|
391
|
+
call_operator: Option<&[u8]>,
|
|
392
|
+
value: &Node<'_>,
|
|
393
|
+
safe_navigation: bool,
|
|
394
|
+
) -> &'static str {
|
|
395
|
+
if is_op_asgn(call_operator, value) {
|
|
396
|
+
if safe_navigation {
|
|
397
|
+
"safe_op_asgn"
|
|
398
|
+
} else {
|
|
399
|
+
"op_asgn"
|
|
400
|
+
}
|
|
401
|
+
} else if safe_navigation {
|
|
402
|
+
"safe_op_asgn2"
|
|
403
|
+
} else {
|
|
404
|
+
"op_asgn2"
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/// `op_asgn?` in the translator.
|
|
409
|
+
pub fn is_op_asgn(call_operator: Option<&[u8]>, value: &Node<'_>) -> bool {
|
|
410
|
+
if call_operator == Some(b"::".as_slice()) {
|
|
411
|
+
return true;
|
|
412
|
+
}
|
|
413
|
+
match value.as_call_node() {
|
|
414
|
+
Some(call) => call.opening_loc().is_none() && call.arguments().is_some(),
|
|
415
|
+
None => false,
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/// `visit_range_node`: a range of integer or `nil` literals, outside a
|
|
420
|
+
/// pattern, becomes one Range literal.
|
|
421
|
+
pub fn range_is_literal(range: &ruby_prism::RangeNode<'_>, in_pattern: bool) -> bool {
|
|
422
|
+
if in_pattern {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
let literal_bound = |bound: Option<Node<'_>>| {
|
|
426
|
+
matches!(bound, Some(Node::IntegerNode { .. }) | Some(Node::NilNode { .. }))
|
|
427
|
+
};
|
|
428
|
+
literal_bound(range.left()) && literal_bound(range.right())
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/// `visit_interpolated_parts`: when every part is plain string content, the
|
|
432
|
+
/// translator returns one string. Returns that string's bytes, or `None` when
|
|
433
|
+
/// the result keeps interpolated parts (`dstr`, `dsym`, `dregx`, `dxstr`).
|
|
434
|
+
pub fn interpolated_string_value(parts: &ruby_prism::NodeList<'_>) -> Option<Vec<u8>> {
|
|
435
|
+
let mut value = Vec::new();
|
|
436
|
+
for part in parts.iter() {
|
|
437
|
+
match &part {
|
|
438
|
+
Node::EmbeddedStatementsNode { .. } => {
|
|
439
|
+
// `#{}` stays an evstr; `#{"a"}` contributes its string.
|
|
440
|
+
let statements = part.as_embedded_statements_node().unwrap().statements()?;
|
|
441
|
+
value.extend(string_value(&statements.as_node())?);
|
|
442
|
+
}
|
|
443
|
+
_ => value.extend(string_value(&part)?),
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
Some(value)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/// The bytes of a node the translator turns into `s(:str, value)`.
|
|
450
|
+
pub fn string_value(node: &Node<'_>) -> Option<Vec<u8>> {
|
|
451
|
+
match node {
|
|
452
|
+
Node::StringNode { .. } => Some(node.as_string_node().unwrap().unescaped().to_vec()),
|
|
453
|
+
Node::SourceFileNode { .. } => {
|
|
454
|
+
Some(node.as_source_file_node().unwrap().filepath().to_vec())
|
|
455
|
+
}
|
|
456
|
+
Node::InterpolatedStringNode { .. } => {
|
|
457
|
+
interpolated_string_value(&node.as_interpolated_string_node().unwrap().parts())
|
|
458
|
+
}
|
|
459
|
+
_ => transparent_child(node).and_then(|child| string_value(&child)),
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/// The single node a wrapper translates to, for wrappers that add no sexp of
|
|
464
|
+
/// their own: parentheses, a one-statement list, and a plain `begin`.
|
|
465
|
+
pub fn transparent_child<'pr>(node: &Node<'pr>) -> Option<Node<'pr>> {
|
|
466
|
+
match node {
|
|
467
|
+
Node::ParenthesesNode { .. } => node.as_parentheses_node().unwrap().body(),
|
|
468
|
+
Node::StatementsNode { .. } => {
|
|
469
|
+
let body = node.as_statements_node().unwrap().body();
|
|
470
|
+
if body.len() == 1 {
|
|
471
|
+
body.first()
|
|
472
|
+
} else {
|
|
473
|
+
None
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
Node::BeginNode { .. } => {
|
|
477
|
+
let begin = node.as_begin_node().unwrap();
|
|
478
|
+
let plain = begin.rescue_clause().is_none() && begin.ensure_clause().is_none();
|
|
479
|
+
if plain {
|
|
480
|
+
begin.statements().map(|statements| statements.as_node())
|
|
481
|
+
} else {
|
|
482
|
+
None
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
_ => None,
|
|
486
|
+
}
|
|
487
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "flog_rs"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
authors.workspace = true
|
|
8
|
+
homepage.workspace = true
|
|
9
|
+
repository.workspace = true
|
|
10
|
+
publish = false
|
|
11
|
+
|
|
12
|
+
[lib]
|
|
13
|
+
crate-type = ["cdylib"]
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
flog-core = { path = "../../crates/flog-core" }
|
|
17
|
+
magnus = "0.9"
|
|
18
|
+
rayon.workspace = true
|