reek-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 +614 -0
- data/Cargo.toml +24 -0
- data/DIVERGENCES.md +15 -0
- data/LICENSE +22 -0
- data/README.md +125 -0
- data/THIRD_PARTY_NOTICES.md +57 -0
- data/crates/reek-cli/Cargo.toml +22 -0
- data/crates/reek-cli/src/main.rs +277 -0
- data/crates/reek-core/Cargo.toml +21 -0
- data/crates/reek-core/src/ast.rs +449 -0
- data/crates/reek-core/src/code_comment.rs +245 -0
- data/crates/reek-core/src/comments.rs +179 -0
- data/crates/reek-core/src/config.rs +803 -0
- data/crates/reek-core/src/context.rs +891 -0
- data/crates/reek-core/src/detectors/attribute.rs +13 -0
- data/crates/reek-core/src/detectors/boolean_parameter.rs +20 -0
- data/crates/reek-core/src/detectors/class_variable.rs +25 -0
- data/crates/reek-core/src/detectors/control_parameter.rs +110 -0
- data/crates/reek-core/src/detectors/data_clump.rs +114 -0
- data/crates/reek-core/src/detectors/duplicate_method_call.rs +63 -0
- data/crates/reek-core/src/detectors/feature_envy.rs +32 -0
- data/crates/reek-core/src/detectors/instance_variable_assumption.rs +39 -0
- data/crates/reek-core/src/detectors/irresponsible_module.rs +12 -0
- data/crates/reek-core/src/detectors/long_parameter_list.rs +16 -0
- data/crates/reek-core/src/detectors/long_yield_list.rs +20 -0
- data/crates/reek-core/src/detectors/manual_dispatch.rs +18 -0
- data/crates/reek-core/src/detectors/missing_safe_method.rs +34 -0
- data/crates/reek-core/src/detectors/mod.rs +479 -0
- data/crates/reek-core/src/detectors/module_initialize.rs +15 -0
- data/crates/reek-core/src/detectors/nested_iterators.rs +82 -0
- data/crates/reek-core/src/detectors/nil_check.rs +40 -0
- data/crates/reek-core/src/detectors/repeated_conditional.rs +51 -0
- data/crates/reek-core/src/detectors/subclassed_from_core_class.rs +22 -0
- data/crates/reek-core/src/detectors/too_many_constants.rs +18 -0
- data/crates/reek-core/src/detectors/too_many_instance_variables.rs +25 -0
- data/crates/reek-core/src/detectors/too_many_methods.rs +15 -0
- data/crates/reek-core/src/detectors/too_many_statements.rs +16 -0
- data/crates/reek-core/src/detectors/uncommunicative_method_name.rs +32 -0
- data/crates/reek-core/src/detectors/uncommunicative_module_name.rs +33 -0
- data/crates/reek-core/src/detectors/uncommunicative_parameter_name.rs +49 -0
- data/crates/reek-core/src/detectors/uncommunicative_variable_name.rs +138 -0
- data/crates/reek-core/src/detectors/unused_parameters.rs +21 -0
- data/crates/reek-core/src/detectors/unused_private_method.rs +59 -0
- data/crates/reek-core/src/detectors/utility_function.rs +27 -0
- data/crates/reek-core/src/encodings.rs +180 -0
- data/crates/reek-core/src/examiner.rs +165 -0
- data/crates/reek-core/src/lib.rs +21 -0
- data/crates/reek-core/src/node.rs +423 -0
- data/crates/reek-core/src/ruby_format.rs +60 -0
- data/crates/reek-core/src/ruby_inspect.rs +172 -0
- data/crates/reek-core/src/ruby_regexp.rs +243 -0
- data/crates/reek-core/src/single_byte_encodings.rs +6007 -0
- data/crates/reek-core/src/source.rs +280 -0
- data/crates/reek-core/src/translate.rs +3112 -0
- data/crates/reek-core/src/yaml.rs +520 -0
- data/ext/reek_rs/Cargo.toml +18 -0
- data/ext/reek_rs/extconf.rb +6 -0
- data/ext/reek_rs/src/lib.rs +155 -0
- data/lib/reek_rs/version.rb +8 -0
- data/lib/reek_rs.rb +390 -0
- metadata +123 -0
|
@@ -0,0 +1,3112 @@
|
|
|
1
|
+
//! Builds the `parser` gem's tree from Prism's tree.
|
|
2
|
+
//!
|
|
3
|
+
//! This ports `Prism::Translation::Parser::Compiler` (prism 2.1.0,
|
|
4
|
+
//! `translation/parser/compiler.rb`) together with the parts of
|
|
5
|
+
//! `Parser::Builders::Default` (parser 3.3.14.0) it calls, configured as
|
|
6
|
+
//! Reek's `Reek::AST::Builder` is: `emit_lambda` on and every other `emit_*`
|
|
7
|
+
//! flag off, so calls to `[]` stay `send`, `...` parameters become
|
|
8
|
+
//! `forward_args`, keyword arguments stay `hash`, `__ENCODING__` becomes
|
|
9
|
+
//! `(const (const nil :Encoding) :UTF_8)` and block parameters never become
|
|
10
|
+
//! `procarg0`. `__FILE__` and `__LINE__` become literals.
|
|
11
|
+
//!
|
|
12
|
+
//! Compiler methods are named `visit_*` after the methods they port; builder
|
|
13
|
+
//! methods are named `b_*` after `Parser::Builders::Default` methods.
|
|
14
|
+
|
|
15
|
+
use ruby_prism::{Location, Node};
|
|
16
|
+
|
|
17
|
+
use crate::ast::{Child, Loc, NodeId, Range, Tree};
|
|
18
|
+
|
|
19
|
+
/// The compiler's `forwarding`, `in_destructure` and `in_pattern` state
|
|
20
|
+
/// (`copy_compiler`).
|
|
21
|
+
#[derive(Clone, Copy, Debug, Default)]
|
|
22
|
+
struct Scope {
|
|
23
|
+
forward_rest: bool,
|
|
24
|
+
forward_kwrest: bool,
|
|
25
|
+
forward_block: bool,
|
|
26
|
+
forward_all: bool,
|
|
27
|
+
in_destructure: bool,
|
|
28
|
+
in_pattern: bool,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
impl Scope {
|
|
32
|
+
fn without_forwarding(self) -> Self {
|
|
33
|
+
Scope { forward_rest: false, forward_kwrest: false, forward_block: false, forward_all: false, ..self }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fn destructure(self) -> Self {
|
|
37
|
+
Scope { in_destructure: true, ..self }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
fn pattern(self, in_pattern: bool) -> Self {
|
|
41
|
+
Scope { in_pattern, ..self }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// A token: its text and range (`[value, range]` in the builder).
|
|
46
|
+
#[derive(Clone, Copy, Debug)]
|
|
47
|
+
struct Tok<'a> {
|
|
48
|
+
text: &'a [u8],
|
|
49
|
+
range: Range,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// A part of a string for `visit_heredoc`: a `StringNode` (real or made by
|
|
53
|
+
/// `StringNode#to_interpolated`) or any other node.
|
|
54
|
+
enum Part<'pr> {
|
|
55
|
+
Str { unescaped: Vec<u8>, content: Range, location: Range },
|
|
56
|
+
Node(Node<'pr>),
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// Translates a Prism program. `file_name` is the buffer name `__FILE__`
|
|
60
|
+
/// becomes. Returns `None` for a program without statements.
|
|
61
|
+
pub fn translate(tree: &mut Tree, program: &Node<'_>, file_name: &[u8]) -> Option<NodeId> {
|
|
62
|
+
let mut translator = Translator { tree, file_name: file_name.to_vec(), sign_fold: None };
|
|
63
|
+
translator.visit_node(program, Scope::default())
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
struct Translator<'t> {
|
|
67
|
+
tree: &'t mut Tree,
|
|
68
|
+
file_name: Vec<u8>,
|
|
69
|
+
/// A unary `-` or `+` separated from a numeric literal by whitespace,
|
|
70
|
+
/// waiting for that literal to be built (see [`SignFold`]).
|
|
71
|
+
sign_fold: Option<SignFold>,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/// The parser gem's lexer reads `-`, whitespace and a number as
|
|
75
|
+
/// `tUMINUS_NUM` and folds the sign into the literal (`- 1.abs` is
|
|
76
|
+
/// `(-1).abs`); Prism reads it as a unary operator call on the whole operand
|
|
77
|
+
/// (`-(1.abs)`). The literal starting at `literal_start` takes the sign and
|
|
78
|
+
/// begins at `operator`.
|
|
79
|
+
#[derive(Clone, Copy, Debug)]
|
|
80
|
+
struct SignFold {
|
|
81
|
+
literal_start: u32,
|
|
82
|
+
operator: Range,
|
|
83
|
+
negative: bool,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fn range(location: &Location<'_>) -> Range {
|
|
87
|
+
Range::new(location.start(), location.end())
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fn lines(bytes: &[u8]) -> Vec<&[u8]> {
|
|
91
|
+
// String#lines: split after each "\n".
|
|
92
|
+
let mut out = Vec::new();
|
|
93
|
+
let mut start = 0;
|
|
94
|
+
for (index, byte) in bytes.iter().enumerate() {
|
|
95
|
+
if *byte == b'\n' {
|
|
96
|
+
out.push(&bytes[start..=index]);
|
|
97
|
+
start = index + 1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if start < bytes.len() {
|
|
101
|
+
out.push(&bytes[start..]);
|
|
102
|
+
}
|
|
103
|
+
out
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fn starts_with_any(text: Option<&[u8]>, prefixes: &[&[u8]]) -> bool {
|
|
107
|
+
text.is_some_and(|text| prefixes.iter().any(|prefix| text.starts_with(prefix)))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// `Integer#to_s` for a Prism integer (sign and base-2^32 digits).
|
|
111
|
+
pub fn integer_text(negative: bool, digits: &[u32]) -> String {
|
|
112
|
+
let mut limbs: Vec<u32> = digits.to_vec();
|
|
113
|
+
while limbs.last() == Some(&0) {
|
|
114
|
+
limbs.pop();
|
|
115
|
+
}
|
|
116
|
+
if limbs.is_empty() {
|
|
117
|
+
return "0".to_string();
|
|
118
|
+
}
|
|
119
|
+
let mut chunks = Vec::new();
|
|
120
|
+
while !limbs.is_empty() {
|
|
121
|
+
let mut remainder: u64 = 0;
|
|
122
|
+
for limb in limbs.iter_mut().rev() {
|
|
123
|
+
let current = (remainder << 32) | u64::from(*limb);
|
|
124
|
+
*limb = (current / 1_000_000_000) as u32;
|
|
125
|
+
remainder = current % 1_000_000_000;
|
|
126
|
+
}
|
|
127
|
+
chunks.push(remainder as u32);
|
|
128
|
+
while limbs.last() == Some(&0) {
|
|
129
|
+
limbs.pop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
let mut out = String::new();
|
|
133
|
+
if negative {
|
|
134
|
+
out.push('-');
|
|
135
|
+
}
|
|
136
|
+
out.push_str(&chunks.last().unwrap().to_string());
|
|
137
|
+
for chunk in chunks.iter().rev().skip(1) {
|
|
138
|
+
out.push_str(&format!("{chunk:09}"));
|
|
139
|
+
}
|
|
140
|
+
out
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fn integer_child(value: ruby_prism::Integer<'_>) -> (bool, Vec<u32>) {
|
|
144
|
+
let (negative, digits) = value.to_u32_digits();
|
|
145
|
+
(negative, digits.to_vec())
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
fn gcd_u128(mut a: u128, mut b: u128) -> u128 {
|
|
149
|
+
while b != 0 {
|
|
150
|
+
(a, b) = (b, a % b);
|
|
151
|
+
}
|
|
152
|
+
a
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fn digits_to_u128(digits: &[u32]) -> Option<u128> {
|
|
156
|
+
let mut value: u128 = 0;
|
|
157
|
+
for (index, digit) in digits.iter().enumerate() {
|
|
158
|
+
if *digit == 0 {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if index >= 4 {
|
|
162
|
+
return None;
|
|
163
|
+
}
|
|
164
|
+
value |= u128::from(*digit) << (32 * index);
|
|
165
|
+
}
|
|
166
|
+
Some(value)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/// `Rational(numerator, denominator).to_s`, reduced when it fits in 128 bits.
|
|
170
|
+
fn rational_text(numerator: (bool, Vec<u32>), denominator: (bool, Vec<u32>)) -> String {
|
|
171
|
+
let negative = numerator.0 != denominator.0;
|
|
172
|
+
match (digits_to_u128(&numerator.1), digits_to_u128(&denominator.1)) {
|
|
173
|
+
(Some(n), Some(d)) if d != 0 => {
|
|
174
|
+
let g = gcd_u128(n, d).max(1);
|
|
175
|
+
let sign = if negative && n != 0 { "-" } else { "" };
|
|
176
|
+
format!("{sign}{}/{}", n / g, d / g)
|
|
177
|
+
}
|
|
178
|
+
_ => format!(
|
|
179
|
+
"{}/{}",
|
|
180
|
+
integer_text(negative, &numerator.1),
|
|
181
|
+
integer_text(false, &denominator.1)
|
|
182
|
+
),
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// `Complex(0, imaginary).to_s` given `imaginary.to_s` and its sign.
|
|
187
|
+
fn complex_text(imaginary: &str) -> String {
|
|
188
|
+
match imaginary.strip_prefix('-') {
|
|
189
|
+
Some(magnitude) => format!("0-{magnitude}i"),
|
|
190
|
+
None => format!("0+{imaginary}i"),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
impl<'t> Translator<'t> {
|
|
195
|
+
// =====================================================================
|
|
196
|
+
// Tokens and ranges
|
|
197
|
+
|
|
198
|
+
fn tok<'a>(&'a self, location: Option<Location<'_>>) -> Option<Tok<'a>> {
|
|
199
|
+
location.map(|location| {
|
|
200
|
+
let range = range(&location);
|
|
201
|
+
Tok { text: &self.tree.source[range.start as usize..range.end as usize], range }
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fn text(&self, range: Range) -> &[u8] {
|
|
206
|
+
&self.tree.source[range.start as usize..range.end as usize]
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn tok_at(&self, range: Range) -> Tok<'_> {
|
|
210
|
+
Tok { text: self.text(range), range }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/// `srange_semicolon`: the `;` right after `start` (after whitespace),
|
|
214
|
+
/// used as a `then` token.
|
|
215
|
+
fn srange_semicolon(&self, start: u32, end: Option<u32>) -> Option<Range> {
|
|
216
|
+
let end = end?;
|
|
217
|
+
if end < start {
|
|
218
|
+
return None;
|
|
219
|
+
}
|
|
220
|
+
let bytes = &self.tree.source[start as usize..end as usize];
|
|
221
|
+
// The parser gem's `then` is the first terminator: a `;` after a
|
|
222
|
+
// newline belongs to the (empty) statements instead.
|
|
223
|
+
let spaces = bytes.iter().take_while(|byte| matches!(byte, b' ' | b'\t' | 0x0b | 0x0c | b'\r')).count();
|
|
224
|
+
if bytes.get(spaces) == Some(&b';') {
|
|
225
|
+
let finish = start + spaces as u32 + 1;
|
|
226
|
+
Some(Range::new(finish - 1, finish))
|
|
227
|
+
} else {
|
|
228
|
+
None
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
fn owned_tok(&self, location: Option<Location<'_>>) -> Option<(Vec<u8>, Range)> {
|
|
233
|
+
self.tok(location).map(|tok| (tok.text.to_vec(), tok.range))
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// =====================================================================
|
|
237
|
+
// Node helpers
|
|
238
|
+
|
|
239
|
+
fn n(&mut self, ty: &'static str, children: Vec<Child>, loc: Loc) -> NodeId {
|
|
240
|
+
self.tree.add(ty, children, Some(loc))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
fn ty(&self, id: NodeId) -> &'static str {
|
|
244
|
+
self.tree.ty(id)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fn expr(&self, id: NodeId) -> Option<Range> {
|
|
248
|
+
self.tree.expression(id)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
fn join_opt(a: Option<Range>, b: Option<Range>) -> Option<Range> {
|
|
252
|
+
match (a, b) {
|
|
253
|
+
(Some(a), Some(b)) => Some(a.join(b)),
|
|
254
|
+
(a, b) => a.or(b),
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
fn join_exprs(&self, left: NodeId, right: NodeId) -> Option<Range> {
|
|
259
|
+
Self::join_opt(self.expr(left), self.expr(right))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
fn nodes(ids: &[NodeId]) -> Vec<Child> {
|
|
263
|
+
ids.iter().map(|id| Child::Node(*id)).collect()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
fn opt(id: Option<NodeId>) -> Child {
|
|
267
|
+
id.map_or(Child::Nil, Child::Node)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn loc_begin(&self, id: NodeId) -> Option<Range> {
|
|
271
|
+
self.tree.loc(id).and_then(|loc| loc.begin)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn loc_end(&self, id: NodeId) -> Option<Range> {
|
|
275
|
+
self.tree.loc(id).and_then(|loc| loc.end)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
fn str_value(&self, id: NodeId) -> Option<&[u8]> {
|
|
279
|
+
match self.tree.children(id).first() {
|
|
280
|
+
Some(Child::Str(bytes)) => Some(bytes),
|
|
281
|
+
_ => None,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// =====================================================================
|
|
286
|
+
// Source maps (Parser::Builders::Default private methods)
|
|
287
|
+
|
|
288
|
+
fn collection_map(&self, begin: Option<Range>, parts: &[NodeId], end: Option<Range>) -> Loc {
|
|
289
|
+
let expression = match (begin, end) {
|
|
290
|
+
(Some(begin), Some(end)) => Some(begin.join(end)),
|
|
291
|
+
_ => match (parts.first(), parts.last()) {
|
|
292
|
+
(Some(first), Some(last)) => self.join_exprs(*first, *last),
|
|
293
|
+
_ => begin.or(end),
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
Loc { expression, begin, end, heredoc: false }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fn string_map(&self, begin: Option<Tok<'_>>, parts: &[NodeId], end: Option<Range>) -> Loc {
|
|
300
|
+
if let Some(begin) = begin {
|
|
301
|
+
if begin.text.starts_with(b"<<") {
|
|
302
|
+
return Loc { expression: Some(begin.range), begin: None, end: None, heredoc: true };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
self.collection_map(begin.map(|t| t.range), parts, end)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
fn keyword_map(&self, keyword: Range, args: &[Option<NodeId>], end: Option<Range>) -> Loc {
|
|
309
|
+
let end_l = if end.is_some() {
|
|
310
|
+
end
|
|
311
|
+
} else if args.iter().any(Option::is_some) && args.last().unwrap().is_some() {
|
|
312
|
+
self.expr(args.last().unwrap().unwrap())
|
|
313
|
+
} else if args.iter().any(Option::is_some) && args.len() > 1 {
|
|
314
|
+
args[args.len() - 2].and_then(|id| self.expr(id))
|
|
315
|
+
} else {
|
|
316
|
+
Some(keyword)
|
|
317
|
+
};
|
|
318
|
+
Loc::expr(Self::join_opt(Some(keyword), end_l))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
fn send_map(
|
|
322
|
+
&self,
|
|
323
|
+
receiver: Option<NodeId>,
|
|
324
|
+
selector: Option<Range>,
|
|
325
|
+
rparen: Option<Range>,
|
|
326
|
+
args: &[NodeId],
|
|
327
|
+
) -> Loc {
|
|
328
|
+
let begin_l = match receiver {
|
|
329
|
+
Some(receiver) => self.expr(receiver),
|
|
330
|
+
None => selector,
|
|
331
|
+
};
|
|
332
|
+
let end_l = if rparen.is_some() {
|
|
333
|
+
rparen
|
|
334
|
+
} else if let Some(last) = args.last() {
|
|
335
|
+
self.expr(*last)
|
|
336
|
+
} else {
|
|
337
|
+
selector
|
|
338
|
+
};
|
|
339
|
+
Loc::expr(Self::join_opt(begin_l, end_l))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
fn unary_op_map(&self, op: Range, arg: Option<NodeId>) -> Loc {
|
|
343
|
+
Loc::expr(match arg {
|
|
344
|
+
Some(arg) => Self::join_opt(Some(op), self.expr(arg)),
|
|
345
|
+
None => Some(op),
|
|
346
|
+
})
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
fn binary_op_map(&self, left: NodeId, right: NodeId) -> Loc {
|
|
350
|
+
Loc::expr(self.join_exprs(left, right))
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
fn token_map(range: Range) -> Loc {
|
|
354
|
+
Loc::expr(Some(range))
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// =====================================================================
|
|
358
|
+
// Builder methods
|
|
359
|
+
|
|
360
|
+
fn b_string_internal(&mut self, value: Vec<u8>, range: Range) -> NodeId {
|
|
361
|
+
self.n("str", vec![Child::Str(value.into())], Loc::expr(Some(range)))
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
fn collapse_string_parts(&self, parts: &[NodeId]) -> bool {
|
|
365
|
+
parts.len() == 1 && matches!(self.ty(parts[0]), "str" | "dstr")
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
fn b_string_compose(&mut self, begin: Option<Tok<'_>>, parts: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
369
|
+
if self.collapse_string_parts(&parts) {
|
|
370
|
+
if begin.is_none() && end.is_none() {
|
|
371
|
+
parts[0]
|
|
372
|
+
} else {
|
|
373
|
+
let children = self.tree.children(parts[0]).to_vec();
|
|
374
|
+
let loc = self.string_map(begin, &parts, end);
|
|
375
|
+
self.n("str", children, loc)
|
|
376
|
+
}
|
|
377
|
+
} else {
|
|
378
|
+
let loc = self.string_map(begin, &parts, end);
|
|
379
|
+
self.n("dstr", Self::nodes(&parts), loc)
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
fn b_symbol_compose(&mut self, begin: Option<Range>, parts: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
384
|
+
if self.collapse_string_parts(&parts) {
|
|
385
|
+
let value = self.str_value(parts[0]).unwrap_or_default().to_vec();
|
|
386
|
+
let expression = match (begin, end) {
|
|
387
|
+
(Some(begin), Some(end)) => Some(begin.join(end)),
|
|
388
|
+
_ => self.expr(parts[0]),
|
|
389
|
+
};
|
|
390
|
+
self.n("sym", vec![Child::Sym(value.into())], Loc { expression, begin, end, heredoc: false })
|
|
391
|
+
} else {
|
|
392
|
+
let loc = self.collection_map(begin, &parts, end);
|
|
393
|
+
self.n("dsym", Self::nodes(&parts), loc)
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
fn b_xstring_compose(&mut self, begin: Option<Tok<'_>>, parts: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
398
|
+
let loc = self.string_map(begin, &parts, end);
|
|
399
|
+
self.n("xstr", Self::nodes(&parts), loc)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
fn b_regexp_compose(&mut self, begin: Range, mut parts: Vec<NodeId>, end: Range, options: NodeId) -> NodeId {
|
|
403
|
+
let expression = Self::join_opt(Some(begin), self.expr(options));
|
|
404
|
+
parts.push(options);
|
|
405
|
+
self.n("regexp", Self::nodes(&parts), Loc { expression, begin: Some(begin), end: Some(end), heredoc: false })
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
fn b_regexp_options(&mut self, text: &[u8], range: Range) -> NodeId {
|
|
409
|
+
let mut options: Vec<u8> = text.to_vec();
|
|
410
|
+
options.sort_unstable();
|
|
411
|
+
options.dedup();
|
|
412
|
+
let children = options.iter().map(|byte| Child::Sym(vec![*byte].into())).collect();
|
|
413
|
+
self.n("regopt", children, Self::token_map(range))
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
fn b_array(&mut self, begin: Option<Range>, elements: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
417
|
+
let loc = self.collection_map(begin, &elements, end);
|
|
418
|
+
self.n("array", Self::nodes(&elements), loc)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
fn b_splat(&mut self, star: Range, arg: Option<NodeId>) -> NodeId {
|
|
422
|
+
let loc = self.unary_op_map(star, arg);
|
|
423
|
+
let children = arg.map(|arg| vec![Child::Node(arg)]).unwrap_or_default();
|
|
424
|
+
self.n("splat", children, loc)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
fn b_pair(&mut self, key: NodeId, value: NodeId) -> NodeId {
|
|
428
|
+
let loc = self.binary_op_map(key, value);
|
|
429
|
+
self.n("pair", vec![Child::Node(key), Child::Node(value)], loc)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
fn b_pair_keyword(&mut self, key: &[u8], key_range: Range, value: NodeId) -> NodeId {
|
|
433
|
+
let key_l = Range::new(key_range.start, key_range.end - 1);
|
|
434
|
+
let key_node = self.n("sym", vec![Child::sym_name(key)], Loc::expr(Some(key_l)));
|
|
435
|
+
let expression = Self::join_opt(Some(key_range), self.expr(value));
|
|
436
|
+
self.n("pair", vec![Child::Node(key_node), Child::Node(value)], Loc::expr(expression))
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
fn b_pair_quoted(&mut self, begin: Range, parts: Vec<NodeId>, end: Range, value: NodeId) -> NodeId {
|
|
440
|
+
let quote_l = Range::new(end.end - 2, end.end - 1);
|
|
441
|
+
let key = self.b_symbol_compose(Some(begin), parts, Some(quote_l));
|
|
442
|
+
let expression = Self::join_opt(Some(begin), self.expr(value));
|
|
443
|
+
self.n("pair", vec![Child::Node(key), Child::Node(value)], Loc::expr(expression))
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
fn b_kwsplat(&mut self, dstar: Range, arg: NodeId) -> NodeId {
|
|
447
|
+
let loc = self.unary_op_map(dstar, Some(arg));
|
|
448
|
+
self.n("kwsplat", vec![Child::Node(arg)], loc)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
fn b_associate(&mut self, begin: Option<Range>, pairs: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
452
|
+
let loc = self.collection_map(begin, &pairs, end);
|
|
453
|
+
self.n("hash", Self::nodes(&pairs), loc)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
fn b_range(&mut self, ty: &'static str, left: Option<NodeId>, op: Range, right: Option<NodeId>) -> NodeId {
|
|
457
|
+
let expression = match (left, right) {
|
|
458
|
+
(Some(left), Some(right)) => self.join_exprs(left, right),
|
|
459
|
+
(Some(left), None) => Self::join_opt(self.expr(left), Some(op)),
|
|
460
|
+
(None, Some(right)) => Self::join_opt(Some(op), self.expr(right)),
|
|
461
|
+
(None, None) => None,
|
|
462
|
+
};
|
|
463
|
+
self.n(ty, vec![Self::opt(left), Self::opt(right)], Loc::expr(expression))
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
fn b_leaf(&mut self, ty: &'static str, range: Range) -> NodeId {
|
|
467
|
+
self.n(ty, Vec::new(), Self::token_map(range))
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
fn b_named(&mut self, ty: &'static str, name: &[u8], range: Range) -> NodeId {
|
|
471
|
+
self.n(ty, vec![Child::sym_name(name)], Self::token_map(range))
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
fn b_const(&mut self, name: &[u8], range: Range) -> NodeId {
|
|
475
|
+
self.n("const", vec![Child::Nil, Child::sym_name(name)], Self::token_map(range))
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
fn b_const_global(&mut self, colon3: Range, name: &[u8], name_range: Range) -> NodeId {
|
|
479
|
+
let cbase = self.b_leaf("cbase", colon3);
|
|
480
|
+
let expression = Some(colon3.join(name_range));
|
|
481
|
+
self.n("const", vec![Child::Node(cbase), Child::sym_name(name)], Loc::expr(expression))
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
fn b_const_fetch(&mut self, scope: NodeId, name: &[u8], name_range: Range) -> NodeId {
|
|
485
|
+
let expression = Self::join_opt(self.expr(scope), Some(name_range));
|
|
486
|
+
self.n("const", vec![Child::Node(scope), Child::sym_name(name)], Loc::expr(expression))
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/// `assignable`: the write form of a variable or constant node.
|
|
490
|
+
fn b_assignable(&mut self, node: NodeId) -> NodeId {
|
|
491
|
+
let ty = match self.ty(node) {
|
|
492
|
+
"cvar" => "cvasgn",
|
|
493
|
+
"ivar" => "ivasgn",
|
|
494
|
+
"gvar" => "gvasgn",
|
|
495
|
+
"const" => "casgn",
|
|
496
|
+
"ident" => "lvasgn",
|
|
497
|
+
_ => return node,
|
|
498
|
+
};
|
|
499
|
+
self.tree.updated(node, Some(ty), None, None)
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
fn b_assign(&mut self, lhs: NodeId, rhs: Option<NodeId>) -> NodeId {
|
|
503
|
+
let mut children = self.tree.children(lhs).to_vec();
|
|
504
|
+
children.push(Self::opt(rhs));
|
|
505
|
+
let expression = match rhs {
|
|
506
|
+
Some(rhs) => self.join_exprs(lhs, rhs),
|
|
507
|
+
None => self.expr(lhs),
|
|
508
|
+
};
|
|
509
|
+
let loc = Loc::expr(expression);
|
|
510
|
+
self.tree.updated(lhs, None, Some(children), Some(Some(loc)))
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
fn b_op_assign(&mut self, lhs: NodeId, operator: &[u8], rhs: NodeId) -> NodeId {
|
|
514
|
+
let expression = self.join_exprs(lhs, rhs);
|
|
515
|
+
let loc = Loc::expr(expression);
|
|
516
|
+
match operator {
|
|
517
|
+
b"&&" => self.n("and_asgn", vec![Child::Node(lhs), Child::Node(rhs)], loc),
|
|
518
|
+
b"||" => self.n("or_asgn", vec![Child::Node(lhs), Child::Node(rhs)], loc),
|
|
519
|
+
_ => self.n("op_asgn", vec![Child::Node(lhs), Child::sym_name(operator), Child::Node(rhs)], loc),
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
fn b_multi_lhs(&mut self, begin: Option<Range>, items: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
524
|
+
let loc = self.collection_map(begin, &items, end);
|
|
525
|
+
self.n("mlhs", Self::nodes(&items), loc)
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
fn b_args(&mut self, begin: Option<Range>, args: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
529
|
+
let loc = self.collection_map(begin, &args, end);
|
|
530
|
+
if args.len() == 1 && self.ty(args[0]) == "forward_arg" {
|
|
531
|
+
self.n("forward_args", Vec::new(), loc)
|
|
532
|
+
} else {
|
|
533
|
+
self.n("args", Self::nodes(&args), loc)
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
fn b_arg_prefix(&mut self, ty: &'static str, op: Range, name: Option<(&[u8], Range)>) -> NodeId {
|
|
538
|
+
match name {
|
|
539
|
+
Some((name, name_range)) => self.n(ty, vec![Child::sym_name(name)], Loc::expr(Some(op.join(name_range)))),
|
|
540
|
+
None => self.n(ty, Vec::new(), Loc::expr(Some(op))),
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
fn b_call_method(
|
|
545
|
+
&mut self,
|
|
546
|
+
receiver: Option<NodeId>,
|
|
547
|
+
dot: Option<&[u8]>,
|
|
548
|
+
selector: Option<(&[u8], Range)>,
|
|
549
|
+
lparen: Option<Range>,
|
|
550
|
+
args: Vec<NodeId>,
|
|
551
|
+
rparen: Option<Range>,
|
|
552
|
+
) -> NodeId {
|
|
553
|
+
let ty = if dot == Some(b"&.") { "csend" } else { "send" };
|
|
554
|
+
let name: &[u8] = selector.map_or(b"call", |(name, _)| name);
|
|
555
|
+
let loc = self.send_map(receiver, selector.map(|(_, r)| r), rparen, &args);
|
|
556
|
+
let _ = lparen;
|
|
557
|
+
let mut children = vec![Self::opt(receiver), Child::sym_name(name)];
|
|
558
|
+
children.extend(args.iter().map(|id| Child::Node(*id)));
|
|
559
|
+
self.n(ty, children, loc)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
fn b_block(&mut self, call: NodeId, begin: Range, args: Child, body: Option<NodeId>, end: Range) -> NodeId {
|
|
563
|
+
let numbered = matches!(args, Child::Int(_));
|
|
564
|
+
let ty = if numbered { "numblock" } else { "block" };
|
|
565
|
+
if matches!(self.ty(call), "send" | "csend" | "index" | "super" | "zsuper" | "lambda") {
|
|
566
|
+
let expression = Self::join_opt(self.expr(call), Some(end));
|
|
567
|
+
self.n(ty, vec![Child::Node(call), args, Self::opt(body)], Loc { expression, begin: Some(begin), end: Some(end), heredoc: false })
|
|
568
|
+
} else {
|
|
569
|
+
let actual = self.tree.child_node(call, 0).expect("block on a wrapped call");
|
|
570
|
+
let expression = Self::join_opt(self.expr(actual), Some(end));
|
|
571
|
+
let block = self.n(ty, vec![Child::Node(actual), args, Self::opt(body)], Loc { expression, begin: Some(begin), end: Some(end), heredoc: false });
|
|
572
|
+
let outer = self.join_exprs(call, block);
|
|
573
|
+
let call_ty = self.ty(call);
|
|
574
|
+
self.n(call_ty, vec![Child::Node(block)], Loc::expr(outer))
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
fn b_block_pass(&mut self, amper: Range, arg: Option<NodeId>) -> NodeId {
|
|
579
|
+
let loc = self.unary_op_map(amper, arg);
|
|
580
|
+
self.n("block_pass", vec![Self::opt(arg)], loc)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
fn b_attr_asgn(&mut self, receiver: Option<NodeId>, dot: Option<&[u8]>, selector: (&[u8], Range)) -> NodeId {
|
|
584
|
+
let mut name = selector.0.to_vec();
|
|
585
|
+
name.push(b'=');
|
|
586
|
+
let ty = if dot == Some(b"&.") { "csend" } else { "send" };
|
|
587
|
+
let loc = self.send_map(receiver, Some(selector.1), None, &[]);
|
|
588
|
+
self.n(ty, vec![Self::opt(receiver), Child::Sym(name.into())], loc)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
fn b_index(&mut self, ty_name: &[u8], receiver: Option<NodeId>, indexes: Vec<NodeId>, rbrack: Option<Range>) -> NodeId {
|
|
592
|
+
let expression = Self::join_opt(receiver.and_then(|r| self.expr(r)), rbrack);
|
|
593
|
+
let mut children = vec![Self::opt(receiver), Child::sym_name(ty_name)];
|
|
594
|
+
children.extend(indexes.iter().map(|id| Child::Node(*id)));
|
|
595
|
+
self.n("send", children, Loc::expr(expression))
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
fn b_match_op(&mut self, receiver: NodeId, arg: NodeId) -> NodeId {
|
|
599
|
+
let loc = self.binary_op_map(receiver, arg);
|
|
600
|
+
if self.static_regexp_node(receiver) {
|
|
601
|
+
self.n("match_with_lvasgn", vec![Child::Node(receiver), Child::Node(arg)], loc)
|
|
602
|
+
} else {
|
|
603
|
+
self.n("send", vec![Child::Node(receiver), Child::sym_name(b"=~"), Child::Node(arg)], loc)
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/// `static_regexp_node(node)` is truthy: a regexp whose parts are all
|
|
608
|
+
/// plain strings (version 33).
|
|
609
|
+
fn static_regexp_node(&self, node: NodeId) -> bool {
|
|
610
|
+
if self.ty(node) != "regexp" {
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
let children = self.tree.children(node);
|
|
614
|
+
children[..children.len() - 1].iter().all(|child| child.node().is_some_and(|id| self.ty(id) == "str"))
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
fn b_not_op(&mut self, not: Range, begin: Option<Range>, receiver: Option<NodeId>, end: Option<Range>) -> NodeId {
|
|
618
|
+
match receiver {
|
|
619
|
+
None => {
|
|
620
|
+
let nil_loc = self.collection_map(begin, &[], end);
|
|
621
|
+
let nil_node = self.n("begin", Vec::new(), nil_loc);
|
|
622
|
+
let loc = self.unary_op_map(not, Some(nil_node));
|
|
623
|
+
self.n("send", vec![Child::Node(nil_node), Child::sym_name(b"!")], loc)
|
|
624
|
+
}
|
|
625
|
+
Some(receiver) => {
|
|
626
|
+
let condition = self.check_condition(receiver);
|
|
627
|
+
let loc = self.send_map(None, Some(not), end, &[receiver]);
|
|
628
|
+
self.n("send", vec![Child::Node(condition), Child::sym_name(b"!")], loc)
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
fn b_logical_op(&mut self, ty: &'static str, left: NodeId, right: NodeId) -> NodeId {
|
|
634
|
+
let loc = self.binary_op_map(left, right);
|
|
635
|
+
self.n(ty, vec![Child::Node(left), Child::Node(right)], loc)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
#[allow(clippy::too_many_arguments)]
|
|
639
|
+
fn b_condition(
|
|
640
|
+
&mut self,
|
|
641
|
+
keyword: Range,
|
|
642
|
+
cond: NodeId,
|
|
643
|
+
then: Option<Range>,
|
|
644
|
+
if_true: Option<NodeId>,
|
|
645
|
+
else_t: Option<Range>,
|
|
646
|
+
if_false: Option<NodeId>,
|
|
647
|
+
end: Option<Range>,
|
|
648
|
+
) -> NodeId {
|
|
649
|
+
let condition = self.check_condition(cond);
|
|
650
|
+
let loc = self.condition_map(keyword, cond, then, if_true, else_t, if_false, end);
|
|
651
|
+
self.n("if", vec![Child::Node(condition), Self::opt(if_true), Self::opt(if_false)], loc)
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
#[allow(clippy::too_many_arguments)]
|
|
655
|
+
fn condition_map(
|
|
656
|
+
&self,
|
|
657
|
+
keyword: Range,
|
|
658
|
+
cond: NodeId,
|
|
659
|
+
begin: Option<Range>,
|
|
660
|
+
body: Option<NodeId>,
|
|
661
|
+
else_t: Option<Range>,
|
|
662
|
+
else_e: Option<NodeId>,
|
|
663
|
+
end: Option<Range>,
|
|
664
|
+
) -> Loc {
|
|
665
|
+
let else_expr = else_e.and_then(|id| self.expr(id));
|
|
666
|
+
let body_expr = body.and_then(|id| self.expr(id));
|
|
667
|
+
let end_l = if end.is_some() {
|
|
668
|
+
end
|
|
669
|
+
} else if else_expr.is_some() {
|
|
670
|
+
else_expr
|
|
671
|
+
} else if else_t.is_some() {
|
|
672
|
+
else_t
|
|
673
|
+
} else if body_expr.is_some() {
|
|
674
|
+
body_expr
|
|
675
|
+
} else if begin.is_some() {
|
|
676
|
+
begin
|
|
677
|
+
} else {
|
|
678
|
+
self.expr(cond)
|
|
679
|
+
};
|
|
680
|
+
Loc::expr(Self::join_opt(Some(keyword), end_l))
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
fn b_condition_mod(&mut self, if_true: Option<NodeId>, if_false: Option<NodeId>, cond: NodeId) -> NodeId {
|
|
684
|
+
let condition = self.check_condition(cond);
|
|
685
|
+
let pre = if_true.or(if_false).expect("modifier body");
|
|
686
|
+
let loc = Loc::expr(self.join_exprs(pre, cond));
|
|
687
|
+
self.n("if", vec![Child::Node(condition), Self::opt(if_true), Self::opt(if_false)], loc)
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
fn b_ternary(&mut self, cond: NodeId, if_true: Option<NodeId>, if_false: Option<NodeId>) -> NodeId {
|
|
691
|
+
let condition = self.check_condition(cond);
|
|
692
|
+
let expression = Self::join_opt(self.expr(cond), if_false.and_then(|id| self.expr(id)));
|
|
693
|
+
self.n("if", vec![Child::Node(condition), Self::opt(if_true), Self::opt(if_false)], Loc::expr(expression))
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/// `check_condition`: conditions become flip-flops and
|
|
697
|
+
/// `match_current_line` where Ruby treats them so.
|
|
698
|
+
fn check_condition(&mut self, cond: NodeId) -> NodeId {
|
|
699
|
+
match self.ty(cond) {
|
|
700
|
+
"begin" => {
|
|
701
|
+
if self.tree.children(cond).len() == 1 {
|
|
702
|
+
let child = self.tree.child_node(cond, 0).unwrap();
|
|
703
|
+
let checked = self.check_condition(child);
|
|
704
|
+
self.tree.updated(cond, None, Some(vec![Child::Node(checked)]), None)
|
|
705
|
+
} else {
|
|
706
|
+
cond
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
"and" | "or" => {
|
|
710
|
+
let left = self.tree.child_node(cond, 0).unwrap();
|
|
711
|
+
let right = self.tree.child_node(cond, 1).unwrap();
|
|
712
|
+
let left = self.check_condition(left);
|
|
713
|
+
let right = self.check_condition(right);
|
|
714
|
+
self.tree.updated(cond, None, Some(vec![Child::Node(left), Child::Node(right)]), None)
|
|
715
|
+
}
|
|
716
|
+
ty @ ("irange" | "erange") => {
|
|
717
|
+
let flip = if ty == "irange" { "iflipflop" } else { "eflipflop" };
|
|
718
|
+
let left = self.tree.child_node(cond, 0);
|
|
719
|
+
let right = self.tree.child_node(cond, 1);
|
|
720
|
+
let left = left.map(|id| self.check_condition(id));
|
|
721
|
+
let right = right.map(|id| self.check_condition(id));
|
|
722
|
+
self.tree.updated(cond, Some(flip), Some(vec![Self::opt(left), Self::opt(right)]), None)
|
|
723
|
+
}
|
|
724
|
+
"regexp" => {
|
|
725
|
+
let expression = self.expr(cond);
|
|
726
|
+
self.n("match_current_line", vec![Child::Node(cond)], Loc::expr(expression))
|
|
727
|
+
}
|
|
728
|
+
_ => cond,
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
fn b_when(&mut self, keyword: Range, patterns: Vec<NodeId>, body: Option<NodeId>) -> NodeId {
|
|
733
|
+
let mut args: Vec<Option<NodeId>> = patterns.iter().map(|id| Some(*id)).collect();
|
|
734
|
+
args.push(body);
|
|
735
|
+
let loc = self.keyword_map(keyword, &args, None);
|
|
736
|
+
let children = args.iter().map(|id| Self::opt(*id)).collect();
|
|
737
|
+
self.n("when", children, loc)
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
#[allow(clippy::too_many_arguments)]
|
|
741
|
+
fn b_case(
|
|
742
|
+
&mut self,
|
|
743
|
+
ty: &'static str,
|
|
744
|
+
keyword: Range,
|
|
745
|
+
expr: Option<NodeId>,
|
|
746
|
+
bodies: Vec<NodeId>,
|
|
747
|
+
else_t: Option<Range>,
|
|
748
|
+
else_body: Option<NodeId>,
|
|
749
|
+
end: Range,
|
|
750
|
+
) -> NodeId {
|
|
751
|
+
let expression = Some(keyword.join(end));
|
|
752
|
+
let mut children = vec![Self::opt(expr)];
|
|
753
|
+
children.extend(bodies.iter().map(|id| Child::Node(*id)));
|
|
754
|
+
let else_body = match else_t {
|
|
755
|
+
Some(else_t) if ty == "case_match" && else_body.is_none() => {
|
|
756
|
+
Some(self.n("empty_else", Vec::new(), Self::token_map(else_t)))
|
|
757
|
+
}
|
|
758
|
+
_ => else_body,
|
|
759
|
+
};
|
|
760
|
+
children.push(Self::opt(else_body));
|
|
761
|
+
self.n(ty, children, Loc::expr(expression))
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
fn b_loop(&mut self, ty: &'static str, keyword: Range, cond: NodeId, body: Option<NodeId>, end: Range) -> NodeId {
|
|
765
|
+
let condition = self.check_condition(cond);
|
|
766
|
+
self.n(ty, vec![Child::Node(condition), Self::opt(body)], Loc::expr(Some(keyword.join(end))))
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
fn b_loop_mod(&mut self, ty: &'static str, body: NodeId, cond: NodeId) -> NodeId {
|
|
770
|
+
let ty = if self.ty(body) == "kwbegin" {
|
|
771
|
+
if ty == "while" {
|
|
772
|
+
"while_post"
|
|
773
|
+
} else {
|
|
774
|
+
"until_post"
|
|
775
|
+
}
|
|
776
|
+
} else {
|
|
777
|
+
ty
|
|
778
|
+
};
|
|
779
|
+
let condition = self.check_condition(cond);
|
|
780
|
+
let loc = Loc::expr(self.join_exprs(body, cond));
|
|
781
|
+
self.n(ty, vec![Child::Node(condition), Child::Node(body)], loc)
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
fn b_keyword_cmd(
|
|
785
|
+
&mut self,
|
|
786
|
+
ty: &'static str,
|
|
787
|
+
keyword: Range,
|
|
788
|
+
args: Vec<NodeId>,
|
|
789
|
+
rparen: Option<Range>,
|
|
790
|
+
) -> NodeId {
|
|
791
|
+
let optional: Vec<Option<NodeId>> = args.iter().map(|id| Some(*id)).collect();
|
|
792
|
+
let loc = self.keyword_map(keyword, &optional, rparen);
|
|
793
|
+
self.n(ty, Self::nodes(&args), loc)
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
fn b_rescue_body(
|
|
797
|
+
&mut self,
|
|
798
|
+
keyword: Range,
|
|
799
|
+
exc_list: Option<NodeId>,
|
|
800
|
+
exc_var: Option<NodeId>,
|
|
801
|
+
then: Option<Range>,
|
|
802
|
+
body: Option<NodeId>,
|
|
803
|
+
) -> NodeId {
|
|
804
|
+
let end_l = body
|
|
805
|
+
.and_then(|id| self.expr(id))
|
|
806
|
+
.or(then)
|
|
807
|
+
.or_else(|| exc_var.and_then(|id| self.expr(id)))
|
|
808
|
+
.or_else(|| exc_list.and_then(|id| self.expr(id)))
|
|
809
|
+
.unwrap_or(keyword);
|
|
810
|
+
let children = vec![Self::opt(exc_list), Self::opt(exc_var), Self::opt(body)];
|
|
811
|
+
self.n("resbody", children, Loc::expr(Some(keyword.join(end_l))))
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
fn b_begin_body(
|
|
815
|
+
&mut self,
|
|
816
|
+
compound: Option<NodeId>,
|
|
817
|
+
rescue_bodies: Vec<NodeId>,
|
|
818
|
+
else_t: Option<Range>,
|
|
819
|
+
else_body: Option<NodeId>,
|
|
820
|
+
ensure_t: Option<Range>,
|
|
821
|
+
ensure_body: Option<NodeId>,
|
|
822
|
+
) -> Option<NodeId> {
|
|
823
|
+
let mut compound = compound;
|
|
824
|
+
if !rescue_bodies.is_empty() {
|
|
825
|
+
let begin_l = compound.and_then(|id| self.expr(id)).or_else(|| self.expr(rescue_bodies[0]));
|
|
826
|
+
let end_l = if let Some(else_t) = else_t {
|
|
827
|
+
else_body.and_then(|id| self.expr(id)).or(Some(else_t))
|
|
828
|
+
} else {
|
|
829
|
+
self.expr(*rescue_bodies.last().unwrap())
|
|
830
|
+
};
|
|
831
|
+
let mut children = vec![Self::opt(compound)];
|
|
832
|
+
children.extend(rescue_bodies.iter().map(|id| Child::Node(*id)));
|
|
833
|
+
children.push(if else_t.is_some() { Self::opt(else_body) } else { Child::Nil });
|
|
834
|
+
compound = Some(self.n("rescue", children, Loc::expr(Self::join_opt(begin_l, end_l))));
|
|
835
|
+
} else if let Some(else_t) = else_t {
|
|
836
|
+
let mut statements = Vec::new();
|
|
837
|
+
if let Some(stmt) = compound {
|
|
838
|
+
if self.ty(stmt) == "begin" {
|
|
839
|
+
statements.extend(self.tree.child_nodes(stmt));
|
|
840
|
+
} else {
|
|
841
|
+
statements.push(stmt);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
let else_loc = self.collection_map(Some(else_t), &else_body.into_iter().collect::<Vec<_>>(), None);
|
|
845
|
+
let else_node = self.n("begin", vec![Self::opt(else_body)], else_loc);
|
|
846
|
+
statements.push(else_node);
|
|
847
|
+
let loc = self.collection_map(None, &statements, None);
|
|
848
|
+
compound = Some(self.n("begin", Self::nodes(&statements), loc));
|
|
849
|
+
}
|
|
850
|
+
if let Some(ensure_t) = ensure_t {
|
|
851
|
+
let begin_l = match compound {
|
|
852
|
+
Some(id) => self.expr(id),
|
|
853
|
+
None => Some(ensure_t),
|
|
854
|
+
};
|
|
855
|
+
let end_l = match ensure_body {
|
|
856
|
+
Some(id) => self.expr(id),
|
|
857
|
+
None => Some(ensure_t),
|
|
858
|
+
};
|
|
859
|
+
compound = Some(self.n(
|
|
860
|
+
"ensure",
|
|
861
|
+
vec![Self::opt(compound), Self::opt(ensure_body)],
|
|
862
|
+
Loc::expr(Self::join_opt(begin_l, end_l)),
|
|
863
|
+
));
|
|
864
|
+
}
|
|
865
|
+
compound
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
fn b_compstmt(&mut self, statements: Vec<NodeId>) -> Option<NodeId> {
|
|
869
|
+
match statements.len() {
|
|
870
|
+
0 => None,
|
|
871
|
+
1 => Some(statements[0]),
|
|
872
|
+
_ => {
|
|
873
|
+
let loc = self.collection_map(None, &statements, None);
|
|
874
|
+
Some(self.n("begin", Self::nodes(&statements), loc))
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
fn b_begin(&mut self, begin: Option<Range>, body: Option<NodeId>, end: Option<Range>) -> NodeId {
|
|
880
|
+
match body {
|
|
881
|
+
None => {
|
|
882
|
+
let loc = self.collection_map(begin, &[], end);
|
|
883
|
+
self.n("begin", Vec::new(), loc)
|
|
884
|
+
}
|
|
885
|
+
Some(body)
|
|
886
|
+
if self.ty(body) == "mlhs"
|
|
887
|
+
|| (self.ty(body) == "begin" && self.loc_begin(body).is_none() && self.loc_end(body).is_none()) =>
|
|
888
|
+
{
|
|
889
|
+
let children: Vec<NodeId> = self.tree.child_nodes(body).collect();
|
|
890
|
+
let all = self.tree.children(body).to_vec();
|
|
891
|
+
let loc = self.collection_map(begin, &children, end);
|
|
892
|
+
let ty = self.ty(body);
|
|
893
|
+
self.n(ty, all, loc)
|
|
894
|
+
}
|
|
895
|
+
Some(body) => {
|
|
896
|
+
let loc = self.collection_map(begin, &[body], end);
|
|
897
|
+
self.n("begin", vec![Child::Node(body)], loc)
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
fn b_begin_keyword(&mut self, begin: Range, body: Option<NodeId>, end: Option<Range>) -> NodeId {
|
|
903
|
+
match body {
|
|
904
|
+
None => {
|
|
905
|
+
let loc = self.collection_map(Some(begin), &[], end);
|
|
906
|
+
self.n("kwbegin", Vec::new(), loc)
|
|
907
|
+
}
|
|
908
|
+
Some(body) if self.ty(body) == "begin" && self.loc_begin(body).is_none() && self.loc_end(body).is_none() => {
|
|
909
|
+
let children: Vec<NodeId> = self.tree.child_nodes(body).collect();
|
|
910
|
+
let all = self.tree.children(body).to_vec();
|
|
911
|
+
let loc = self.collection_map(Some(begin), &children, end);
|
|
912
|
+
self.n("kwbegin", all, loc)
|
|
913
|
+
}
|
|
914
|
+
Some(body) => {
|
|
915
|
+
let loc = self.collection_map(Some(begin), &[body], end);
|
|
916
|
+
self.n("kwbegin", vec![Child::Node(body)], loc)
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
fn b_binary(&mut self, ty: &'static str, left: NodeId, right: NodeId) -> NodeId {
|
|
922
|
+
let loc = self.binary_op_map(left, right);
|
|
923
|
+
self.n(ty, vec![Child::Node(left), Child::Node(right)], loc)
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
fn b_match_var(&mut self, name: &[u8], range: Range) -> NodeId {
|
|
927
|
+
self.n("match_var", vec![Child::sym_name(name)], Loc::expr(Some(range)))
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
fn b_match_rest(&mut self, star: Range, name: Option<(&[u8], Range)>) -> NodeId {
|
|
931
|
+
match name {
|
|
932
|
+
None => self.n("match_rest", Vec::new(), Loc::expr(Some(star))),
|
|
933
|
+
Some((name, range)) => {
|
|
934
|
+
let var = self.b_match_var(name, range);
|
|
935
|
+
let loc = self.unary_op_map(star, Some(var));
|
|
936
|
+
self.n("match_rest", vec![Child::Node(var)], loc)
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
fn b_pattern_collection(&mut self, ty: &'static str, begin: Option<Range>, elements: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
942
|
+
let loc = self.collection_map(begin, &elements, end);
|
|
943
|
+
self.n(ty, Self::nodes(&elements), loc)
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
fn b_array_pattern(&mut self, begin: Option<Range>, elements: Vec<NodeId>, end: Option<Range>) -> NodeId {
|
|
947
|
+
let mut trailing_comma = false;
|
|
948
|
+
let node_elements: Vec<NodeId> = elements
|
|
949
|
+
.iter()
|
|
950
|
+
.map(|element| {
|
|
951
|
+
if self.ty(*element) == "match_with_trailing_comma" {
|
|
952
|
+
trailing_comma = true;
|
|
953
|
+
self.tree.child_node(*element, 0).unwrap()
|
|
954
|
+
} else {
|
|
955
|
+
trailing_comma = false;
|
|
956
|
+
*element
|
|
957
|
+
}
|
|
958
|
+
})
|
|
959
|
+
.collect();
|
|
960
|
+
let ty = if trailing_comma { "array_pattern_with_tail" } else { "array_pattern" };
|
|
961
|
+
let loc = self.collection_map(begin, &elements, end);
|
|
962
|
+
self.n(ty, Self::nodes(&node_elements), loc)
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
fn b_const_pattern(&mut self, constant: NodeId, ldelim: Option<Range>, pattern: NodeId, rdelim: Option<Range>) -> NodeId {
|
|
966
|
+
let expression = Self::join_opt(self.expr(constant), rdelim);
|
|
967
|
+
self.n("const_pattern", vec![Child::Node(constant), Child::Node(pattern)], Loc { expression, begin: ldelim, end: rdelim, heredoc: false })
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// =====================================================================
|
|
971
|
+
// Visiting
|
|
972
|
+
|
|
973
|
+
fn visit(&mut self, node: Option<Node<'_>>, scope: Scope) -> Option<NodeId> {
|
|
974
|
+
node.and_then(|node| self.visit_node(&node, scope))
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
fn visit_req(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
978
|
+
self.visit_node(node, scope).expect("node compiles to nothing")
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
fn visit_all<'pr>(&mut self, nodes: impl IntoIterator<Item = Node<'pr>>, scope: Scope) -> Vec<NodeId> {
|
|
982
|
+
nodes.into_iter().filter_map(|node| self.visit_node(&node, scope)).collect()
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
fn visit_statements(&mut self, statements: Option<ruby_prism::StatementsNode<'_>>, scope: Scope) -> Option<NodeId> {
|
|
986
|
+
let statements = statements?;
|
|
987
|
+
let body = self.visit_all(statements.body().iter(), scope);
|
|
988
|
+
self.b_compstmt(body)
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
fn visit_arguments(&mut self, arguments: Option<ruby_prism::ArgumentsNode<'_>>, scope: Scope) -> Vec<NodeId> {
|
|
992
|
+
match arguments {
|
|
993
|
+
Some(arguments) => self.visit_all(arguments.arguments().iter(), scope),
|
|
994
|
+
None => Vec::new(),
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
fn visit_node(&mut self, node: &Node<'_>, scope: Scope) -> Option<NodeId> {
|
|
999
|
+
Some(match node {
|
|
1000
|
+
Node::AliasMethodNode { .. } => {
|
|
1001
|
+
let alias = node.as_alias_method_node().unwrap();
|
|
1002
|
+
let new = self.visit_req(&alias.new_name(), scope);
|
|
1003
|
+
let old = self.visit_req(&alias.old_name(), scope);
|
|
1004
|
+
self.alias(range(&alias.keyword_loc()), new, old)
|
|
1005
|
+
}
|
|
1006
|
+
Node::AliasGlobalVariableNode { .. } => {
|
|
1007
|
+
let alias = node.as_alias_global_variable_node().unwrap();
|
|
1008
|
+
let new = self.visit_req(&alias.new_name(), scope);
|
|
1009
|
+
let old = self.visit_req(&alias.old_name(), scope);
|
|
1010
|
+
self.alias(range(&alias.keyword_loc()), new, old)
|
|
1011
|
+
}
|
|
1012
|
+
Node::AlternationPatternNode { .. } => {
|
|
1013
|
+
let alt = node.as_alternation_pattern_node().unwrap();
|
|
1014
|
+
let left = self.visit_req(&alt.left(), scope);
|
|
1015
|
+
let right = self.visit_req(&alt.right(), scope);
|
|
1016
|
+
self.b_binary("match_alt", left, right)
|
|
1017
|
+
}
|
|
1018
|
+
Node::AndNode { .. } => {
|
|
1019
|
+
let and = node.as_and_node().unwrap();
|
|
1020
|
+
let left = self.visit_req(&and.left(), scope);
|
|
1021
|
+
let right = self.visit_req(&and.right(), scope);
|
|
1022
|
+
self.b_logical_op("and", left, right)
|
|
1023
|
+
}
|
|
1024
|
+
Node::OrNode { .. } => {
|
|
1025
|
+
let or = node.as_or_node().unwrap();
|
|
1026
|
+
let left = self.visit_req(&or.left(), scope);
|
|
1027
|
+
let right = self.visit_req(&or.right(), scope);
|
|
1028
|
+
self.b_logical_op("or", left, right)
|
|
1029
|
+
}
|
|
1030
|
+
Node::ArrayNode { .. } => self.visit_array_node(node, scope),
|
|
1031
|
+
Node::ArrayPatternNode { .. } => self.visit_array_pattern_node(node, scope),
|
|
1032
|
+
Node::AssocNode { .. } => self.visit_assoc_node(node, scope),
|
|
1033
|
+
Node::AssocSplatNode { .. } => {
|
|
1034
|
+
let splat = node.as_assoc_splat_node().unwrap();
|
|
1035
|
+
let op = range(&splat.operator_loc());
|
|
1036
|
+
if scope.in_pattern {
|
|
1037
|
+
let name = splat.value().map(|value| {
|
|
1038
|
+
let r = range(&value.location());
|
|
1039
|
+
(self.text(r).to_vec(), r)
|
|
1040
|
+
});
|
|
1041
|
+
self.b_match_rest(op, name.as_ref().map(|(n, r)| (n.as_slice(), *r)))
|
|
1042
|
+
} else if splat.value().is_none() && scope.forward_kwrest {
|
|
1043
|
+
self.b_leaf("forwarded_kwrestarg", op)
|
|
1044
|
+
} else {
|
|
1045
|
+
let value = self.visit(splat.value(), scope);
|
|
1046
|
+
match value {
|
|
1047
|
+
Some(value) => self.b_kwsplat(op, value),
|
|
1048
|
+
None => self.n("kwsplat", vec![Child::Nil], Loc::expr(Some(op))),
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
Node::BackReferenceReadNode { .. } => {
|
|
1053
|
+
let r = range(&node.location());
|
|
1054
|
+
let name = self.text(r).to_vec();
|
|
1055
|
+
self.b_named("back_ref", &name, r)
|
|
1056
|
+
}
|
|
1057
|
+
Node::BeginNode { .. } => return self.visit_begin_node(node, scope),
|
|
1058
|
+
Node::BlockArgumentNode { .. } => {
|
|
1059
|
+
let arg = node.as_block_argument_node().unwrap();
|
|
1060
|
+
let value = self.visit(arg.expression(), scope);
|
|
1061
|
+
self.b_block_pass(range(&arg.operator_loc()), value)
|
|
1062
|
+
}
|
|
1063
|
+
Node::BlockLocalVariableNode { .. } => {
|
|
1064
|
+
let r = range(&node.location());
|
|
1065
|
+
let name = self.text(r).to_vec();
|
|
1066
|
+
self.b_named("shadowarg", &name, r)
|
|
1067
|
+
}
|
|
1068
|
+
Node::BlockParameterNode { .. } => {
|
|
1069
|
+
let param = node.as_block_parameter_node().unwrap();
|
|
1070
|
+
let op = range(¶m.operator_loc());
|
|
1071
|
+
let name = param.name_loc().map(|loc| {
|
|
1072
|
+
let r = range(&loc);
|
|
1073
|
+
(self.text(r).to_vec(), r)
|
|
1074
|
+
});
|
|
1075
|
+
match name {
|
|
1076
|
+
Some((name, r)) => self.n("blockarg", vec![Child::sym_name(&name)], Loc::expr(Some(op.join(r)))),
|
|
1077
|
+
None => self.n("blockarg", vec![Child::Nil], Loc::expr(Some(op))),
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
Node::BreakNode { .. } => {
|
|
1081
|
+
let brk = node.as_break_node().unwrap();
|
|
1082
|
+
let args = self.visit_arguments(brk.arguments(), scope);
|
|
1083
|
+
self.b_keyword_cmd("break", range(&brk.keyword_loc()), args, None)
|
|
1084
|
+
}
|
|
1085
|
+
Node::NextNode { .. } => {
|
|
1086
|
+
let next = node.as_next_node().unwrap();
|
|
1087
|
+
let args = self.visit_arguments(next.arguments(), scope);
|
|
1088
|
+
self.b_keyword_cmd("next", range(&next.keyword_loc()), args, None)
|
|
1089
|
+
}
|
|
1090
|
+
Node::ReturnNode { .. } => {
|
|
1091
|
+
let ret = node.as_return_node().unwrap();
|
|
1092
|
+
let args = self.visit_arguments(ret.arguments(), scope);
|
|
1093
|
+
self.b_keyword_cmd("return", range(&ret.keyword_loc()), args, None)
|
|
1094
|
+
}
|
|
1095
|
+
Node::CallNode { .. } => self.visit_call_node(node, scope),
|
|
1096
|
+
Node::CallOperatorWriteNode { .. } => {
|
|
1097
|
+
let write = node.as_call_operator_write_node().unwrap();
|
|
1098
|
+
let receiver = self.visit(write.receiver(), scope);
|
|
1099
|
+
let dot = self.owned_tok(write.call_operator_loc()).map(|t| t.0);
|
|
1100
|
+
let selector = self.owned_tok(write.message_loc());
|
|
1101
|
+
let lhs = self.b_call_method(
|
|
1102
|
+
receiver,
|
|
1103
|
+
dot.as_deref(),
|
|
1104
|
+
selector.as_ref().map(|_| (write.read_name().as_slice(), selector.as_ref().unwrap().1)),
|
|
1105
|
+
None,
|
|
1106
|
+
Vec::new(),
|
|
1107
|
+
None,
|
|
1108
|
+
);
|
|
1109
|
+
let op = self.binary_operator(write.binary_operator_loc());
|
|
1110
|
+
let value = self.visit_req(&write.value(), scope);
|
|
1111
|
+
self.b_op_assign(lhs, &op, value)
|
|
1112
|
+
}
|
|
1113
|
+
Node::CallAndWriteNode { .. } | Node::CallOrWriteNode { .. } => {
|
|
1114
|
+
let (receiver_node, operator_loc, message_loc, read_name, value_node, call_operator_loc) =
|
|
1115
|
+
if let Some(write) = node.as_call_and_write_node() {
|
|
1116
|
+
(write.receiver(), write.operator_loc(), write.message_loc(), write.read_name(), write.value(), write.call_operator_loc())
|
|
1117
|
+
} else {
|
|
1118
|
+
let write = node.as_call_or_write_node().unwrap();
|
|
1119
|
+
(write.receiver(), write.operator_loc(), write.message_loc(), write.read_name(), write.value(), write.call_operator_loc())
|
|
1120
|
+
};
|
|
1121
|
+
let receiver = self.visit(receiver_node, scope);
|
|
1122
|
+
let dot = self.owned_tok(call_operator_loc).map(|t| t.0);
|
|
1123
|
+
let selector = self.owned_tok(message_loc).map(|t| t.1);
|
|
1124
|
+
let lhs = self.b_call_method(
|
|
1125
|
+
receiver,
|
|
1126
|
+
dot.as_deref(),
|
|
1127
|
+
selector.map(|r| (read_name.as_slice(), r)),
|
|
1128
|
+
None,
|
|
1129
|
+
Vec::new(),
|
|
1130
|
+
None,
|
|
1131
|
+
);
|
|
1132
|
+
let op = self.binary_operator(operator_loc);
|
|
1133
|
+
let value = self.visit_req(&value_node, scope);
|
|
1134
|
+
self.b_op_assign(lhs, &op, value)
|
|
1135
|
+
}
|
|
1136
|
+
Node::CallTargetNode { .. } => {
|
|
1137
|
+
let target = node.as_call_target_node().unwrap();
|
|
1138
|
+
let receiver = self.visit_node(&target.receiver(), scope);
|
|
1139
|
+
let dot = self.owned_tok(Some(target.call_operator_loc())).map(|t| t.0);
|
|
1140
|
+
let selector = self.owned_tok(Some(target.message_loc())).unwrap();
|
|
1141
|
+
self.b_attr_asgn(receiver, dot.as_deref(), (&selector.0, selector.1))
|
|
1142
|
+
}
|
|
1143
|
+
Node::CapturePatternNode { .. } => {
|
|
1144
|
+
let capture = node.as_capture_pattern_node().unwrap();
|
|
1145
|
+
let value = self.visit_req(&capture.value(), scope);
|
|
1146
|
+
let target = self.visit_req(&capture.target().as_node(), scope);
|
|
1147
|
+
self.b_binary("match_as", value, target)
|
|
1148
|
+
}
|
|
1149
|
+
Node::CaseNode { .. } => {
|
|
1150
|
+
let case = node.as_case_node().unwrap();
|
|
1151
|
+
let predicate = self.visit(case.predicate(), scope);
|
|
1152
|
+
let conditions = self.visit_all(case.conditions().iter(), scope);
|
|
1153
|
+
let else_clause = case.else_clause();
|
|
1154
|
+
let else_t = else_clause.as_ref().map(|e| range(&e.else_keyword_loc()));
|
|
1155
|
+
let else_body = self.visit_statements(else_clause.and_then(|e| e.statements()), scope);
|
|
1156
|
+
self.b_case("case", range(&case.case_keyword_loc()), predicate, conditions, else_t, else_body, range(&case.end_keyword_loc()))
|
|
1157
|
+
}
|
|
1158
|
+
Node::CaseMatchNode { .. } => {
|
|
1159
|
+
let case = node.as_case_match_node().unwrap();
|
|
1160
|
+
let predicate = self.visit(case.predicate(), scope);
|
|
1161
|
+
let conditions = self.visit_all(case.conditions().iter(), scope);
|
|
1162
|
+
let else_clause = case.else_clause();
|
|
1163
|
+
let else_t = else_clause.as_ref().map(|e| range(&e.else_keyword_loc()));
|
|
1164
|
+
let else_body = self.visit_statements(else_clause.and_then(|e| e.statements()), scope);
|
|
1165
|
+
self.b_case("case_match", range(&case.case_keyword_loc()), predicate, conditions, else_t, else_body, range(&case.end_keyword_loc()))
|
|
1166
|
+
}
|
|
1167
|
+
Node::ClassNode { .. } => {
|
|
1168
|
+
let class = node.as_class_node().unwrap();
|
|
1169
|
+
let name = self.visit_req(&class.constant_path(), scope);
|
|
1170
|
+
let superclass = self.visit(class.superclass(), scope);
|
|
1171
|
+
let body = self.visit(class.body(), scope.without_forwarding());
|
|
1172
|
+
let keyword = range(&class.class_keyword_loc());
|
|
1173
|
+
let end = range(&class.end_keyword_loc());
|
|
1174
|
+
self.n("class", vec![Child::Node(name), Self::opt(superclass), Self::opt(body)], Loc::expr(Some(keyword.join(end))))
|
|
1175
|
+
}
|
|
1176
|
+
Node::ModuleNode { .. } => {
|
|
1177
|
+
let module = node.as_module_node().unwrap();
|
|
1178
|
+
let name = self.visit_req(&module.constant_path(), scope);
|
|
1179
|
+
let body = self.visit(module.body(), scope.without_forwarding());
|
|
1180
|
+
let keyword = range(&module.module_keyword_loc());
|
|
1181
|
+
let end = range(&module.end_keyword_loc());
|
|
1182
|
+
self.n("module", vec![Child::Node(name), Self::opt(body)], Loc::expr(Some(keyword.join(end))))
|
|
1183
|
+
}
|
|
1184
|
+
Node::SingletonClassNode { .. } => {
|
|
1185
|
+
let sclass = node.as_singleton_class_node().unwrap();
|
|
1186
|
+
let expr = self.visit_req(&sclass.expression(), scope);
|
|
1187
|
+
let body = self.visit(sclass.body(), scope.without_forwarding());
|
|
1188
|
+
let keyword = range(&sclass.class_keyword_loc());
|
|
1189
|
+
let end = range(&sclass.end_keyword_loc());
|
|
1190
|
+
self.n("sclass", vec![Child::Node(expr), Self::opt(body)], Loc::expr(Some(keyword.join(end))))
|
|
1191
|
+
}
|
|
1192
|
+
Node::ClassVariableReadNode { .. } => self.variable("cvar", node),
|
|
1193
|
+
Node::InstanceVariableReadNode { .. } => self.variable("ivar", node),
|
|
1194
|
+
Node::GlobalVariableReadNode { .. } => self.variable("gvar", node),
|
|
1195
|
+
Node::ClassVariableTargetNode { .. } => {
|
|
1196
|
+
let var = self.variable("cvar", node);
|
|
1197
|
+
self.b_assignable(var)
|
|
1198
|
+
}
|
|
1199
|
+
Node::InstanceVariableTargetNode { .. } => {
|
|
1200
|
+
let var = self.variable("ivar", node);
|
|
1201
|
+
self.b_assignable(var)
|
|
1202
|
+
}
|
|
1203
|
+
Node::GlobalVariableTargetNode { .. } => {
|
|
1204
|
+
let var = self.variable("gvar", node);
|
|
1205
|
+
self.b_assignable(var)
|
|
1206
|
+
}
|
|
1207
|
+
Node::ClassVariableWriteNode { .. } => {
|
|
1208
|
+
let write = node.as_class_variable_write_node().unwrap();
|
|
1209
|
+
self.variable_write("cvar", write.name_loc(), Some(write.value()), scope)
|
|
1210
|
+
}
|
|
1211
|
+
Node::InstanceVariableWriteNode { .. } => {
|
|
1212
|
+
let write = node.as_instance_variable_write_node().unwrap();
|
|
1213
|
+
self.variable_write("ivar", write.name_loc(), Some(write.value()), scope)
|
|
1214
|
+
}
|
|
1215
|
+
Node::GlobalVariableWriteNode { .. } => {
|
|
1216
|
+
let write = node.as_global_variable_write_node().unwrap();
|
|
1217
|
+
self.variable_write("gvar", write.name_loc(), Some(write.value()), scope)
|
|
1218
|
+
}
|
|
1219
|
+
Node::LocalVariableWriteNode { .. } => {
|
|
1220
|
+
let write = node.as_local_variable_write_node().unwrap();
|
|
1221
|
+
self.variable_write("ident", write.name_loc(), Some(write.value()), scope)
|
|
1222
|
+
}
|
|
1223
|
+
Node::ConstantWriteNode { .. } => {
|
|
1224
|
+
let write = node.as_constant_write_node().unwrap();
|
|
1225
|
+
let r = range(&write.name_loc());
|
|
1226
|
+
let name = self.text(r).to_vec();
|
|
1227
|
+
let constant = self.b_const(&name, r);
|
|
1228
|
+
let lhs = self.b_assignable(constant);
|
|
1229
|
+
let value = self.visit(Some(write.value()), scope);
|
|
1230
|
+
self.b_assign(lhs, value)
|
|
1231
|
+
}
|
|
1232
|
+
Node::ClassVariableOperatorWriteNode { .. } => {
|
|
1233
|
+
let w = node.as_class_variable_operator_write_node().unwrap();
|
|
1234
|
+
self.variable_op_write("cvar", w.name_loc(), w.binary_operator_loc(), w.value(), scope)
|
|
1235
|
+
}
|
|
1236
|
+
Node::ClassVariableAndWriteNode { .. } => {
|
|
1237
|
+
let w = node.as_class_variable_and_write_node().unwrap();
|
|
1238
|
+
self.variable_op_write("cvar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1239
|
+
}
|
|
1240
|
+
Node::ClassVariableOrWriteNode { .. } => {
|
|
1241
|
+
let w = node.as_class_variable_or_write_node().unwrap();
|
|
1242
|
+
self.variable_op_write("cvar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1243
|
+
}
|
|
1244
|
+
Node::InstanceVariableOperatorWriteNode { .. } => {
|
|
1245
|
+
let w = node.as_instance_variable_operator_write_node().unwrap();
|
|
1246
|
+
self.variable_op_write("ivar", w.name_loc(), w.binary_operator_loc(), w.value(), scope)
|
|
1247
|
+
}
|
|
1248
|
+
Node::InstanceVariableAndWriteNode { .. } => {
|
|
1249
|
+
let w = node.as_instance_variable_and_write_node().unwrap();
|
|
1250
|
+
self.variable_op_write("ivar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1251
|
+
}
|
|
1252
|
+
Node::InstanceVariableOrWriteNode { .. } => {
|
|
1253
|
+
let w = node.as_instance_variable_or_write_node().unwrap();
|
|
1254
|
+
self.variable_op_write("ivar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1255
|
+
}
|
|
1256
|
+
Node::GlobalVariableOperatorWriteNode { .. } => {
|
|
1257
|
+
let w = node.as_global_variable_operator_write_node().unwrap();
|
|
1258
|
+
self.variable_op_write("gvar", w.name_loc(), w.binary_operator_loc(), w.value(), scope)
|
|
1259
|
+
}
|
|
1260
|
+
Node::GlobalVariableAndWriteNode { .. } => {
|
|
1261
|
+
let w = node.as_global_variable_and_write_node().unwrap();
|
|
1262
|
+
self.variable_op_write("gvar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1263
|
+
}
|
|
1264
|
+
Node::GlobalVariableOrWriteNode { .. } => {
|
|
1265
|
+
let w = node.as_global_variable_or_write_node().unwrap();
|
|
1266
|
+
self.variable_op_write("gvar", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1267
|
+
}
|
|
1268
|
+
Node::LocalVariableOperatorWriteNode { .. } => {
|
|
1269
|
+
let w = node.as_local_variable_operator_write_node().unwrap();
|
|
1270
|
+
self.variable_op_write("ident", w.name_loc(), w.binary_operator_loc(), w.value(), scope)
|
|
1271
|
+
}
|
|
1272
|
+
Node::LocalVariableAndWriteNode { .. } => {
|
|
1273
|
+
let w = node.as_local_variable_and_write_node().unwrap();
|
|
1274
|
+
self.variable_op_write("ident", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1275
|
+
}
|
|
1276
|
+
Node::LocalVariableOrWriteNode { .. } => {
|
|
1277
|
+
let w = node.as_local_variable_or_write_node().unwrap();
|
|
1278
|
+
self.variable_op_write("ident", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1279
|
+
}
|
|
1280
|
+
Node::ConstantOperatorWriteNode { .. } => {
|
|
1281
|
+
let w = node.as_constant_operator_write_node().unwrap();
|
|
1282
|
+
self.variable_op_write("const", w.name_loc(), w.binary_operator_loc(), w.value(), scope)
|
|
1283
|
+
}
|
|
1284
|
+
Node::ConstantAndWriteNode { .. } => {
|
|
1285
|
+
let w = node.as_constant_and_write_node().unwrap();
|
|
1286
|
+
self.variable_op_write("const", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1287
|
+
}
|
|
1288
|
+
Node::ConstantOrWriteNode { .. } => {
|
|
1289
|
+
let w = node.as_constant_or_write_node().unwrap();
|
|
1290
|
+
self.variable_op_write("const", w.name_loc(), w.operator_loc(), w.value(), scope)
|
|
1291
|
+
}
|
|
1292
|
+
Node::ConstantReadNode { .. } => {
|
|
1293
|
+
let r = range(&node.location());
|
|
1294
|
+
let name = self.text(r).to_vec();
|
|
1295
|
+
self.b_const(&name, r)
|
|
1296
|
+
}
|
|
1297
|
+
Node::ConstantTargetNode { .. } => {
|
|
1298
|
+
let r = range(&node.location());
|
|
1299
|
+
let name = self.text(r).to_vec();
|
|
1300
|
+
let constant = self.b_const(&name, r);
|
|
1301
|
+
self.b_assignable(constant)
|
|
1302
|
+
}
|
|
1303
|
+
Node::ConstantPathNode { .. } => {
|
|
1304
|
+
let path = node.as_constant_path_node().unwrap();
|
|
1305
|
+
self.constant_path(path.parent(), path.delimiter_loc(), path.name_loc(), scope)
|
|
1306
|
+
}
|
|
1307
|
+
Node::ConstantPathTargetNode { .. } => {
|
|
1308
|
+
let path = node.as_constant_path_target_node().unwrap();
|
|
1309
|
+
let constant = self.constant_path(path.parent(), path.delimiter_loc(), path.name_loc(), scope);
|
|
1310
|
+
self.b_assignable(constant)
|
|
1311
|
+
}
|
|
1312
|
+
Node::ConstantPathWriteNode { .. } => {
|
|
1313
|
+
let write = node.as_constant_path_write_node().unwrap();
|
|
1314
|
+
let target = write.target();
|
|
1315
|
+
let constant = self.constant_path(target.parent(), target.delimiter_loc(), target.name_loc(), scope);
|
|
1316
|
+
let lhs = self.b_assignable(constant);
|
|
1317
|
+
let value = self.visit(Some(write.value()), scope);
|
|
1318
|
+
self.b_assign(lhs, value)
|
|
1319
|
+
}
|
|
1320
|
+
Node::ConstantPathOperatorWriteNode { .. } => {
|
|
1321
|
+
let w = node.as_constant_path_operator_write_node().unwrap();
|
|
1322
|
+
let t = w.target();
|
|
1323
|
+
let constant = self.constant_path(t.parent(), t.delimiter_loc(), t.name_loc(), scope);
|
|
1324
|
+
let lhs = self.b_assignable(constant);
|
|
1325
|
+
let op = self.binary_operator(w.binary_operator_loc());
|
|
1326
|
+
let value = self.visit_req(&w.value(), scope);
|
|
1327
|
+
self.b_op_assign(lhs, &op, value)
|
|
1328
|
+
}
|
|
1329
|
+
Node::ConstantPathAndWriteNode { .. } => {
|
|
1330
|
+
let w = node.as_constant_path_and_write_node().unwrap();
|
|
1331
|
+
let t = w.target();
|
|
1332
|
+
let constant = self.constant_path(t.parent(), t.delimiter_loc(), t.name_loc(), scope);
|
|
1333
|
+
let lhs = self.b_assignable(constant);
|
|
1334
|
+
let op = self.binary_operator(w.operator_loc());
|
|
1335
|
+
let value = self.visit_req(&w.value(), scope);
|
|
1336
|
+
self.b_op_assign(lhs, &op, value)
|
|
1337
|
+
}
|
|
1338
|
+
Node::ConstantPathOrWriteNode { .. } => {
|
|
1339
|
+
let w = node.as_constant_path_or_write_node().unwrap();
|
|
1340
|
+
let t = w.target();
|
|
1341
|
+
let constant = self.constant_path(t.parent(), t.delimiter_loc(), t.name_loc(), scope);
|
|
1342
|
+
let lhs = self.b_assignable(constant);
|
|
1343
|
+
let op = self.binary_operator(w.operator_loc());
|
|
1344
|
+
let value = self.visit_req(&w.value(), scope);
|
|
1345
|
+
self.b_op_assign(lhs, &op, value)
|
|
1346
|
+
}
|
|
1347
|
+
Node::DefNode { .. } => self.visit_def_node(node, scope),
|
|
1348
|
+
Node::DefinedNode { .. } => {
|
|
1349
|
+
let defined = node.as_defined_node().unwrap();
|
|
1350
|
+
let keyword = range(&defined.keyword_loc());
|
|
1351
|
+
let lparen = defined.lparen_loc().map(|l| range(&l));
|
|
1352
|
+
let rparen = defined.rparen_loc().map(|l| range(&l));
|
|
1353
|
+
let value = self.visit_req(&defined.value(), scope);
|
|
1354
|
+
let multiline = lparen.is_some_and(|lp| self.text(keyword.join(lp)).contains(&b'\n'));
|
|
1355
|
+
if multiline {
|
|
1356
|
+
let inner = self.b_begin(lparen, Some(value), rparen);
|
|
1357
|
+
self.b_keyword_cmd("defined?", keyword, vec![inner], None)
|
|
1358
|
+
} else {
|
|
1359
|
+
self.b_keyword_cmd("defined?", keyword, vec![value], rparen)
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
Node::ElseNode { .. } => {
|
|
1363
|
+
return self.visit_statements(node.as_else_node().unwrap().statements(), scope);
|
|
1364
|
+
}
|
|
1365
|
+
Node::EmbeddedStatementsNode { .. } => {
|
|
1366
|
+
let embedded = node.as_embedded_statements_node().unwrap();
|
|
1367
|
+
let body = self.visit_statements(embedded.statements(), scope);
|
|
1368
|
+
self.b_begin(Some(range(&embedded.opening_loc())), body, Some(range(&embedded.closing_loc())))
|
|
1369
|
+
}
|
|
1370
|
+
Node::EmbeddedVariableNode { .. } => {
|
|
1371
|
+
let variable = node.as_embedded_variable_node().unwrap().variable();
|
|
1372
|
+
// The parser gem's lexer emits `#$&` and friends as `tGVAR`.
|
|
1373
|
+
if let Node::BackReferenceReadNode { .. } = variable {
|
|
1374
|
+
let r = range(&variable.location());
|
|
1375
|
+
let name = self.text(r).to_vec();
|
|
1376
|
+
return Some(self.b_named("gvar", &name, r));
|
|
1377
|
+
}
|
|
1378
|
+
return self.visit_node(&variable, scope);
|
|
1379
|
+
}
|
|
1380
|
+
Node::FalseNode { .. } => self.b_leaf("false", range(&node.location())),
|
|
1381
|
+
Node::TrueNode { .. } => self.b_leaf("true", range(&node.location())),
|
|
1382
|
+
Node::NilNode { .. } => self.b_leaf("nil", range(&node.location())),
|
|
1383
|
+
Node::SelfNode { .. } => self.b_leaf("self", range(&node.location())),
|
|
1384
|
+
Node::RedoNode { .. } => self.b_keyword_cmd("redo", range(&node.location()), Vec::new(), None),
|
|
1385
|
+
Node::RetryNode { .. } => self.b_keyword_cmd("retry", range(&node.location()), Vec::new(), None),
|
|
1386
|
+
Node::FindPatternNode { .. } => {
|
|
1387
|
+
let find = node.as_find_pattern_node().unwrap();
|
|
1388
|
+
let mut elements = vec![self.visit_req(&find.left().as_node(), scope)];
|
|
1389
|
+
elements.extend(self.visit_all(find.requireds().iter(), scope));
|
|
1390
|
+
elements.push(self.visit_req(&find.right().as_node(), scope));
|
|
1391
|
+
let opening = find.opening_loc().map(|l| range(&l));
|
|
1392
|
+
let closing = find.closing_loc().map(|l| range(&l));
|
|
1393
|
+
match find.constant() {
|
|
1394
|
+
Some(constant) => {
|
|
1395
|
+
let constant = self.visit_req(&constant, scope);
|
|
1396
|
+
let pattern = self.b_pattern_collection("find_pattern", None, elements, None);
|
|
1397
|
+
self.b_const_pattern(constant, opening, pattern, closing)
|
|
1398
|
+
}
|
|
1399
|
+
None => self.b_pattern_collection("find_pattern", opening, elements, closing),
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
Node::FloatNode { .. } => {
|
|
1403
|
+
let value = node.as_float_node().unwrap().value();
|
|
1404
|
+
let literal = self.n("float", vec![Child::Float(value)], Loc::expr(Some(range(&node.location()))));
|
|
1405
|
+
self.apply_sign_fold(literal)
|
|
1406
|
+
}
|
|
1407
|
+
Node::IntegerNode { .. } => {
|
|
1408
|
+
let (negative, digits) = integer_child(node.as_integer_node().unwrap().value());
|
|
1409
|
+
let text = integer_text(negative, &digits);
|
|
1410
|
+
let literal = self.n("int", vec![Child::Int(text.into())], Loc::expr(Some(range(&node.location()))));
|
|
1411
|
+
self.apply_sign_fold(literal)
|
|
1412
|
+
}
|
|
1413
|
+
Node::RationalNode { .. } => {
|
|
1414
|
+
let text = self.rational_value(node);
|
|
1415
|
+
let literal = self.n("rational", vec![Child::Rational(text.into())], Loc::expr(Some(range(&node.location()))));
|
|
1416
|
+
self.apply_sign_fold(literal)
|
|
1417
|
+
}
|
|
1418
|
+
Node::ImaginaryNode { .. } => {
|
|
1419
|
+
let numeric = node.as_imaginary_node().unwrap().numeric();
|
|
1420
|
+
let imaginary = match &numeric {
|
|
1421
|
+
Node::IntegerNode { .. } => {
|
|
1422
|
+
let (negative, digits) = integer_child(numeric.as_integer_node().unwrap().value());
|
|
1423
|
+
integer_text(negative, &digits)
|
|
1424
|
+
}
|
|
1425
|
+
Node::FloatNode { .. } => crate::ruby_format::float_to_s(numeric.as_float_node().unwrap().value()),
|
|
1426
|
+
Node::RationalNode { .. } => self.rational_value(&numeric),
|
|
1427
|
+
_ => "0".to_string(),
|
|
1428
|
+
};
|
|
1429
|
+
let literal = self.n("complex", vec![Child::Complex(complex_text(&imaginary).into())], Loc::expr(Some(range(&node.location()))));
|
|
1430
|
+
self.apply_sign_fold(literal)
|
|
1431
|
+
}
|
|
1432
|
+
Node::ForNode { .. } => {
|
|
1433
|
+
let for_node = node.as_for_node().unwrap();
|
|
1434
|
+
let index = self.visit_req(&for_node.index(), scope);
|
|
1435
|
+
let collection = self.visit_req(&for_node.collection(), scope);
|
|
1436
|
+
let body = self.visit_statements(for_node.statements(), scope);
|
|
1437
|
+
let keyword = range(&for_node.for_keyword_loc());
|
|
1438
|
+
let end = range(&for_node.end_keyword_loc());
|
|
1439
|
+
self.n("for", vec![Child::Node(index), Child::Node(collection), Self::opt(body)], Loc::expr(Some(keyword.join(end))))
|
|
1440
|
+
}
|
|
1441
|
+
Node::ForwardingArgumentsNode { .. } => self.b_leaf("forwarded_args", range(&node.location())),
|
|
1442
|
+
Node::ForwardingParameterNode { .. } => self.b_leaf("forward_arg", range(&node.location())),
|
|
1443
|
+
Node::ForwardingSuperNode { .. } => {
|
|
1444
|
+
let zsuper = node.as_forwarding_super_node().unwrap();
|
|
1445
|
+
let start = node.location().start();
|
|
1446
|
+
let call = self.b_keyword_cmd("zsuper", Range::new(start, start + 5), Vec::new(), None);
|
|
1447
|
+
match zsuper.block() {
|
|
1448
|
+
Some(block) => self.visit_block(call, &block, scope),
|
|
1449
|
+
None => call,
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
Node::HashNode { .. } => {
|
|
1453
|
+
let hash = node.as_hash_node().unwrap();
|
|
1454
|
+
let elements = self.visit_all(hash.elements().iter(), scope);
|
|
1455
|
+
self.b_associate(Some(range(&hash.opening_loc())), elements, Some(range(&hash.closing_loc())))
|
|
1456
|
+
}
|
|
1457
|
+
Node::KeywordHashNode { .. } => {
|
|
1458
|
+
let hash = node.as_keyword_hash_node().unwrap();
|
|
1459
|
+
let elements = self.visit_all(hash.elements().iter(), scope);
|
|
1460
|
+
self.b_associate(None, elements, None)
|
|
1461
|
+
}
|
|
1462
|
+
Node::HashPatternNode { .. } => {
|
|
1463
|
+
let pattern = node.as_hash_pattern_node().unwrap();
|
|
1464
|
+
let mut elements = self.visit_all(pattern.elements().iter(), scope);
|
|
1465
|
+
if let Some(rest) = pattern.rest() {
|
|
1466
|
+
elements.extend(self.visit_node(&rest, scope));
|
|
1467
|
+
}
|
|
1468
|
+
let opening = pattern.opening_loc().map(|l| range(&l));
|
|
1469
|
+
let closing = pattern.closing_loc().map(|l| range(&l));
|
|
1470
|
+
match pattern.constant() {
|
|
1471
|
+
Some(constant) => {
|
|
1472
|
+
let constant = self.visit_req(&constant, scope);
|
|
1473
|
+
let inner = self.b_pattern_collection("hash_pattern", None, elements, None);
|
|
1474
|
+
self.b_const_pattern(constant, opening, inner, closing)
|
|
1475
|
+
}
|
|
1476
|
+
None => self.b_pattern_collection("hash_pattern", opening, elements, closing),
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
Node::IfNode { .. } => self.visit_if_node(node, scope),
|
|
1480
|
+
Node::UnlessNode { .. } => self.visit_unless_node(node, scope),
|
|
1481
|
+
Node::InNode { .. } => self.visit_in_node(node, scope),
|
|
1482
|
+
Node::IndexOperatorWriteNode { .. } | Node::IndexAndWriteNode { .. } | Node::IndexOrWriteNode { .. } => {
|
|
1483
|
+
self.visit_index_write(node, scope)
|
|
1484
|
+
}
|
|
1485
|
+
Node::IndexTargetNode { .. } => {
|
|
1486
|
+
let target = node.as_index_target_node().unwrap();
|
|
1487
|
+
let receiver = self.visit_req(&target.receiver(), scope);
|
|
1488
|
+
let args = self.visit_arguments(target.arguments(), scope);
|
|
1489
|
+
self.b_index(b"[]=", Some(receiver), args, Some(range(&target.closing_loc())))
|
|
1490
|
+
}
|
|
1491
|
+
Node::InterpolatedRegularExpressionNode { .. } => {
|
|
1492
|
+
let regexp = node.as_interpolated_regular_expression_node().unwrap();
|
|
1493
|
+
let opening = range(®exp.opening_loc());
|
|
1494
|
+
let parts = self.string_nodes_from_interpolation(regexp.parts().iter().collect(), Some(self.text(opening).to_vec()), scope);
|
|
1495
|
+
self.regexp(opening, parts, range(®exp.closing_loc()))
|
|
1496
|
+
}
|
|
1497
|
+
Node::InterpolatedMatchLastLineNode { .. } => {
|
|
1498
|
+
let regexp = node.as_interpolated_match_last_line_node().unwrap();
|
|
1499
|
+
let opening = range(®exp.opening_loc());
|
|
1500
|
+
let parts = self.string_nodes_from_interpolation(regexp.parts().iter().collect(), Some(self.text(opening).to_vec()), scope);
|
|
1501
|
+
self.regexp(opening, parts, range(®exp.closing_loc()))
|
|
1502
|
+
}
|
|
1503
|
+
Node::InterpolatedStringNode { .. } => {
|
|
1504
|
+
let string = node.as_interpolated_string_node().unwrap();
|
|
1505
|
+
let opening = string.opening_loc().map(|l| range(&l));
|
|
1506
|
+
let opening_text = opening.map(|r| self.text(r).to_vec());
|
|
1507
|
+
if opening_text.as_deref().is_some_and(|t| t.starts_with(b"<<")) {
|
|
1508
|
+
let parts = string.parts().iter().map(Part::Node).collect();
|
|
1509
|
+
return Some(self.visit_heredoc("dstr", opening.unwrap(), parts, range(&string.closing_loc().unwrap()), scope));
|
|
1510
|
+
}
|
|
1511
|
+
let parts = self.string_nodes_from_interpolation(string.parts().iter().collect(), opening_text, scope);
|
|
1512
|
+
let closing = string.closing_loc().map(|l| range(&l));
|
|
1513
|
+
let begin = opening.map(|r| self.tok_at(r));
|
|
1514
|
+
let begin = begin.map(|t| Tok { text: t.text, range: t.range });
|
|
1515
|
+
let begin_owned = begin.map(|t| (t.text.to_vec(), t.range));
|
|
1516
|
+
self.b_string_compose(begin_owned.as_ref().map(|(text, r)| Tok { text, range: *r }), parts, closing)
|
|
1517
|
+
}
|
|
1518
|
+
Node::InterpolatedSymbolNode { .. } => {
|
|
1519
|
+
let symbol = node.as_interpolated_symbol_node().unwrap();
|
|
1520
|
+
let opening = symbol.opening_loc().map(|l| range(&l));
|
|
1521
|
+
let opening_text = opening.map(|r| self.text(r).to_vec());
|
|
1522
|
+
let parts = self.string_nodes_from_interpolation(symbol.parts().iter().collect(), opening_text, scope);
|
|
1523
|
+
self.b_symbol_compose(opening, parts, symbol.closing_loc().map(|l| range(&l)))
|
|
1524
|
+
}
|
|
1525
|
+
Node::InterpolatedXStringNode { .. } => {
|
|
1526
|
+
let xstring = node.as_interpolated_x_string_node().unwrap();
|
|
1527
|
+
let opening = range(&xstring.opening_loc());
|
|
1528
|
+
if self.text(opening).starts_with(b"<<") {
|
|
1529
|
+
let parts = xstring.parts().iter().map(Part::Node).collect();
|
|
1530
|
+
return Some(self.visit_heredoc("xstr", opening, parts, range(&xstring.closing_loc()), scope));
|
|
1531
|
+
}
|
|
1532
|
+
let opening_text = self.text(opening).to_vec();
|
|
1533
|
+
let parts = self.string_nodes_from_interpolation(xstring.parts().iter().collect(), Some(opening_text.clone()), scope);
|
|
1534
|
+
self.b_xstring_compose(Some(Tok { text: &opening_text, range: opening }), parts, Some(range(&xstring.closing_loc())))
|
|
1535
|
+
}
|
|
1536
|
+
Node::ItLocalVariableReadNode { .. } => {
|
|
1537
|
+
self.n("lvar", vec![Child::sym_name(b"it")], Loc::expr(Some(range(&node.location()))))
|
|
1538
|
+
}
|
|
1539
|
+
Node::ItParametersNode { .. } => self.b_args(None, Vec::new(), None),
|
|
1540
|
+
Node::KeywordRestParameterNode { .. } => {
|
|
1541
|
+
let param = node.as_keyword_rest_parameter_node().unwrap();
|
|
1542
|
+
let op = range(¶m.operator_loc());
|
|
1543
|
+
let name = param.name_loc().map(|l| {
|
|
1544
|
+
let r = range(&l);
|
|
1545
|
+
(self.text(r).to_vec(), r)
|
|
1546
|
+
});
|
|
1547
|
+
self.b_arg_prefix("kwrestarg", op, name.as_ref().map(|(n, r)| (n.as_slice(), *r)))
|
|
1548
|
+
}
|
|
1549
|
+
Node::LambdaNode { .. } => self.visit_lambda_node(node, scope),
|
|
1550
|
+
Node::LocalVariableReadNode { .. } => {
|
|
1551
|
+
let r = range(&node.location());
|
|
1552
|
+
let name = node.as_local_variable_read_node().unwrap().name();
|
|
1553
|
+
self.n("lvar", vec![Child::sym_name(name.as_slice())], Loc::expr(Some(r)))
|
|
1554
|
+
}
|
|
1555
|
+
Node::LocalVariableTargetNode { .. } => {
|
|
1556
|
+
let r = range(&node.location());
|
|
1557
|
+
let name = node.as_local_variable_target_node().unwrap().name();
|
|
1558
|
+
if scope.in_pattern {
|
|
1559
|
+
self.b_match_var(name.as_slice(), r)
|
|
1560
|
+
} else {
|
|
1561
|
+
let ident = self.n("ident", vec![Child::sym_name(self.text(r).to_vec().as_slice())], Loc::expr(Some(r)));
|
|
1562
|
+
self.b_assignable(ident)
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
Node::MatchPredicateNode { .. } => {
|
|
1566
|
+
let m = node.as_match_predicate_node().unwrap();
|
|
1567
|
+
let value = self.visit_req(&m.value(), scope);
|
|
1568
|
+
let pattern = self.visit_req(&m.pattern(), scope.pattern(true));
|
|
1569
|
+
self.b_binary("match_pattern_p", value, pattern)
|
|
1570
|
+
}
|
|
1571
|
+
Node::MatchRequiredNode { .. } => {
|
|
1572
|
+
let m = node.as_match_required_node().unwrap();
|
|
1573
|
+
let value = self.visit_req(&m.value(), scope);
|
|
1574
|
+
let pattern = self.visit_req(&m.pattern(), scope.pattern(true));
|
|
1575
|
+
self.b_binary("match_pattern", value, pattern)
|
|
1576
|
+
}
|
|
1577
|
+
Node::MatchWriteNode { .. } => {
|
|
1578
|
+
let call = node.as_match_write_node().unwrap().call();
|
|
1579
|
+
let receiver = self.visit_req(&call.receiver().unwrap(), scope);
|
|
1580
|
+
let arg = self.visit_req(&call.arguments().unwrap().arguments().iter().next().unwrap(), scope);
|
|
1581
|
+
self.b_match_op(receiver, arg)
|
|
1582
|
+
}
|
|
1583
|
+
Node::ErrorRecoveryNode { .. } => self.b_leaf("missing", range(&node.location())),
|
|
1584
|
+
Node::MultiTargetNode { .. } => {
|
|
1585
|
+
let target = node.as_multi_target_node().unwrap();
|
|
1586
|
+
// In an assignment the parser gem reads `((a, b))` as one
|
|
1587
|
+
// `mlhs` of `a` and `b` spanning the outer parentheses;
|
|
1588
|
+
// parameters keep the nesting.
|
|
1589
|
+
let mut innermost = node.as_multi_target_node().unwrap();
|
|
1590
|
+
if !scope.in_destructure {
|
|
1591
|
+
while let Some(inner) = sole_nested_multi_target(&innermost) {
|
|
1592
|
+
innermost = inner;
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
let mut elements: Vec<Node<'_>> = innermost.lefts().iter().collect();
|
|
1596
|
+
if let Some(rest) = innermost.rest() {
|
|
1597
|
+
if !matches!(rest, Node::ImplicitRestNode { .. }) {
|
|
1598
|
+
elements.push(rest);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
elements.extend(innermost.rights().iter());
|
|
1602
|
+
let items = self.visit_all(elements, scope);
|
|
1603
|
+
self.b_multi_lhs(target.lparen_loc().map(|l| range(&l)), items, target.rparen_loc().map(|l| range(&l)))
|
|
1604
|
+
}
|
|
1605
|
+
Node::MultiWriteNode { .. } => self.visit_multi_write_node(node, scope),
|
|
1606
|
+
Node::NoKeywordsParameterNode { .. } => {
|
|
1607
|
+
let param = node.as_no_keywords_parameter_node().unwrap();
|
|
1608
|
+
let r = range(¶m.operator_loc()).join(range(¶m.keyword_loc()));
|
|
1609
|
+
if scope.in_pattern {
|
|
1610
|
+
self.b_leaf("match_nil_pattern", r)
|
|
1611
|
+
} else {
|
|
1612
|
+
self.b_leaf("kwnilarg", r)
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
Node::NoBlockParameterNode { .. } => {
|
|
1616
|
+
let param = node.as_no_block_parameter_node().unwrap();
|
|
1617
|
+
let r = range(¶m.operator_loc()).join(range(¶m.keyword_loc()));
|
|
1618
|
+
self.b_leaf("blocknilarg", r)
|
|
1619
|
+
}
|
|
1620
|
+
Node::NumberedReferenceReadNode { .. } => {
|
|
1621
|
+
let number = node.as_numbered_reference_read_node().unwrap().number();
|
|
1622
|
+
self.n("nth_ref", vec![Child::Int(number.to_string().into())], Loc::expr(Some(range(&node.location()))))
|
|
1623
|
+
}
|
|
1624
|
+
Node::OptionalKeywordParameterNode { .. } => {
|
|
1625
|
+
let param = node.as_optional_keyword_parameter_node().unwrap();
|
|
1626
|
+
let name_range = range(¶m.name_loc());
|
|
1627
|
+
let value = self.visit_req(¶m.value(), scope);
|
|
1628
|
+
let expression = Self::join_opt(Some(name_range), self.expr(value));
|
|
1629
|
+
self.n("kwoptarg", vec![Child::sym_name(param.name().as_slice()), Child::Node(value)], Loc::expr(expression))
|
|
1630
|
+
}
|
|
1631
|
+
Node::OptionalParameterNode { .. } => {
|
|
1632
|
+
let param = node.as_optional_parameter_node().unwrap();
|
|
1633
|
+
let name_range = range(¶m.name_loc());
|
|
1634
|
+
let name = self.text(name_range).to_vec();
|
|
1635
|
+
let value = self.visit_req(¶m.value(), scope);
|
|
1636
|
+
let expression = Self::join_opt(Some(name_range), self.expr(value));
|
|
1637
|
+
self.n("optarg", vec![Child::sym_name(&name), Child::Node(value)], Loc::expr(expression))
|
|
1638
|
+
}
|
|
1639
|
+
Node::RequiredKeywordParameterNode { .. } => {
|
|
1640
|
+
let param = node.as_required_keyword_parameter_node().unwrap();
|
|
1641
|
+
let r = range(¶m.name_loc());
|
|
1642
|
+
self.n("kwarg", vec![Child::sym_name(param.name().as_slice())], Loc::expr(Some(r)))
|
|
1643
|
+
}
|
|
1644
|
+
Node::RequiredParameterNode { .. } => {
|
|
1645
|
+
let r = range(&node.location());
|
|
1646
|
+
let name = self.text(r).to_vec();
|
|
1647
|
+
self.n("arg", vec![Child::sym_name(&name)], Loc::expr(Some(r)))
|
|
1648
|
+
}
|
|
1649
|
+
Node::RestParameterNode { .. } => {
|
|
1650
|
+
let param = node.as_rest_parameter_node().unwrap();
|
|
1651
|
+
let op = range(¶m.operator_loc());
|
|
1652
|
+
let name = param.name_loc().map(|l| {
|
|
1653
|
+
let r = range(&l);
|
|
1654
|
+
(self.text(r).to_vec(), r)
|
|
1655
|
+
});
|
|
1656
|
+
self.b_arg_prefix("restarg", op, name.as_ref().map(|(n, r)| (n.as_slice(), *r)))
|
|
1657
|
+
}
|
|
1658
|
+
Node::ParenthesesNode { .. } => {
|
|
1659
|
+
let parens = node.as_parentheses_node().unwrap();
|
|
1660
|
+
let body = self.visit(parens.body(), scope);
|
|
1661
|
+
self.b_begin(Some(range(&parens.opening_loc())), body, Some(range(&parens.closing_loc())))
|
|
1662
|
+
}
|
|
1663
|
+
Node::PinnedExpressionNode { .. } => {
|
|
1664
|
+
let pinned = node.as_pinned_expression_node().unwrap();
|
|
1665
|
+
let inner = self.visit_req(&pinned.expression(), scope.pattern(false));
|
|
1666
|
+
let expression = self.b_begin(Some(range(&pinned.lparen_loc())), Some(inner), Some(range(&pinned.rparen_loc())));
|
|
1667
|
+
let loc = self.unary_op_map(range(&pinned.operator_loc()), Some(expression));
|
|
1668
|
+
self.n("pin", vec![Child::Node(expression)], loc)
|
|
1669
|
+
}
|
|
1670
|
+
Node::PinnedVariableNode { .. } => {
|
|
1671
|
+
let pinned = node.as_pinned_variable_node().unwrap();
|
|
1672
|
+
let var = self.visit_req(&pinned.variable(), scope);
|
|
1673
|
+
let loc = self.unary_op_map(range(&pinned.operator_loc()), Some(var));
|
|
1674
|
+
self.n("pin", vec![Child::Node(var)], loc)
|
|
1675
|
+
}
|
|
1676
|
+
Node::PostExecutionNode { .. } => {
|
|
1677
|
+
let post = node.as_post_execution_node().unwrap();
|
|
1678
|
+
let body = self.visit_statements(post.statements(), scope);
|
|
1679
|
+
let keyword = range(&post.keyword_loc());
|
|
1680
|
+
let loc = Loc::expr(Some(keyword.join(range(&post.closing_loc()))));
|
|
1681
|
+
self.n("postexe", vec![Self::opt(body)], loc)
|
|
1682
|
+
}
|
|
1683
|
+
Node::PreExecutionNode { .. } => {
|
|
1684
|
+
let pre = node.as_pre_execution_node().unwrap();
|
|
1685
|
+
let body = self.visit_statements(pre.statements(), scope);
|
|
1686
|
+
let keyword = range(&pre.keyword_loc());
|
|
1687
|
+
let loc = Loc::expr(Some(keyword.join(range(&pre.closing_loc()))));
|
|
1688
|
+
self.n("preexe", vec![Self::opt(body)], loc)
|
|
1689
|
+
}
|
|
1690
|
+
Node::ProgramNode { .. } => {
|
|
1691
|
+
return self.visit_statements(Some(node.as_program_node().unwrap().statements()), scope);
|
|
1692
|
+
}
|
|
1693
|
+
Node::StatementsNode { .. } => {
|
|
1694
|
+
return self.visit_statements(node.as_statements_node(), scope);
|
|
1695
|
+
}
|
|
1696
|
+
Node::RangeNode { .. } => {
|
|
1697
|
+
let r = node.as_range_node().unwrap();
|
|
1698
|
+
let left = self.visit(r.left(), scope);
|
|
1699
|
+
let right = self.visit(r.right(), scope);
|
|
1700
|
+
let ty = if r.is_exclude_end() { "erange" } else { "irange" };
|
|
1701
|
+
self.b_range(ty, left, range(&r.operator_loc()), right)
|
|
1702
|
+
}
|
|
1703
|
+
Node::FlipFlopNode { .. } => {
|
|
1704
|
+
let r = node.as_flip_flop_node().unwrap();
|
|
1705
|
+
let left = self.visit(r.left(), scope);
|
|
1706
|
+
let right = self.visit(r.right(), scope);
|
|
1707
|
+
let ty = if r.is_exclude_end() { "erange" } else { "irange" };
|
|
1708
|
+
self.b_range(ty, left, range(&r.operator_loc()), right)
|
|
1709
|
+
}
|
|
1710
|
+
Node::RegularExpressionNode { .. } => {
|
|
1711
|
+
let regexp = node.as_regular_expression_node().unwrap();
|
|
1712
|
+
self.regular_expression(regexp.opening_loc(), regexp.content_loc(), regexp.closing_loc())
|
|
1713
|
+
}
|
|
1714
|
+
Node::MatchLastLineNode { .. } => {
|
|
1715
|
+
let regexp = node.as_match_last_line_node().unwrap();
|
|
1716
|
+
self.regular_expression(regexp.opening_loc(), regexp.content_loc(), regexp.closing_loc())
|
|
1717
|
+
}
|
|
1718
|
+
Node::RescueModifierNode { .. } => {
|
|
1719
|
+
let rescue = node.as_rescue_modifier_node().unwrap();
|
|
1720
|
+
let expression = self.visit(Some(rescue.expression()), scope);
|
|
1721
|
+
let rescue_expression = self.visit(Some(rescue.rescue_expression()), scope);
|
|
1722
|
+
let body = self.b_rescue_body(range(&rescue.keyword_loc()), None, None, None, rescue_expression);
|
|
1723
|
+
return self.b_begin_body(expression, vec![body], None, None, None, None);
|
|
1724
|
+
}
|
|
1725
|
+
Node::ShareableConstantNode { .. } => {
|
|
1726
|
+
return self.visit_node(&node.as_shareable_constant_node().unwrap().write(), scope);
|
|
1727
|
+
}
|
|
1728
|
+
Node::SourceEncodingNode { .. } => {
|
|
1729
|
+
let r = range(&node.location());
|
|
1730
|
+
let inner = self.tree.add("const", vec![Child::Nil, Child::sym_name(b"Encoding")], None);
|
|
1731
|
+
self.n("const", vec![Child::Node(inner), Child::sym_name(b"UTF_8")], Loc::expr(Some(r)))
|
|
1732
|
+
}
|
|
1733
|
+
Node::SourceFileNode { .. } => {
|
|
1734
|
+
let r = range(&node.location());
|
|
1735
|
+
let name = self.file_name.clone();
|
|
1736
|
+
self.n("str", vec![Child::Str(name.into())], Loc::expr(Some(r)))
|
|
1737
|
+
}
|
|
1738
|
+
Node::SourceLineNode { .. } => {
|
|
1739
|
+
let r = range(&node.location());
|
|
1740
|
+
let line = self.tree.line_for(r.start);
|
|
1741
|
+
self.n("int", vec![Child::Int(line.to_string().into())], Loc::expr(Some(r)))
|
|
1742
|
+
}
|
|
1743
|
+
Node::SplatNode { .. } => {
|
|
1744
|
+
let splat = node.as_splat_node().unwrap();
|
|
1745
|
+
let op = range(&splat.operator_loc());
|
|
1746
|
+
if splat.expression().is_none() && scope.forward_rest {
|
|
1747
|
+
self.b_leaf("forwarded_restarg", op)
|
|
1748
|
+
} else if scope.in_destructure {
|
|
1749
|
+
let name = splat.expression().map(|e| {
|
|
1750
|
+
let r = range(&e.location());
|
|
1751
|
+
(self.text(r).to_vec(), r)
|
|
1752
|
+
});
|
|
1753
|
+
self.b_arg_prefix("restarg", op, name.as_ref().map(|(n, r)| (n.as_slice(), *r)))
|
|
1754
|
+
} else if scope.in_pattern {
|
|
1755
|
+
let name = splat.expression().map(|e| {
|
|
1756
|
+
let r = range(&e.location());
|
|
1757
|
+
(self.text(r).to_vec(), r)
|
|
1758
|
+
});
|
|
1759
|
+
self.b_match_rest(op, name.as_ref().map(|(n, r)| (n.as_slice(), *r)))
|
|
1760
|
+
} else {
|
|
1761
|
+
let value = self.visit(splat.expression(), scope);
|
|
1762
|
+
self.b_splat(op, value)
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
Node::StringNode { .. } => self.visit_string_node(node, scope),
|
|
1766
|
+
Node::SuperNode { .. } => {
|
|
1767
|
+
let sup = node.as_super_node().unwrap();
|
|
1768
|
+
let mut args = self.visit_arguments(sup.arguments(), scope);
|
|
1769
|
+
let mut block = sup.block();
|
|
1770
|
+
if let Some(Node::BlockArgumentNode { .. }) = block {
|
|
1771
|
+
args.extend(self.visit_node(&block.take().unwrap(), scope));
|
|
1772
|
+
}
|
|
1773
|
+
let call = self.b_keyword_cmd("super", range(&sup.keyword_loc()), args, sup.rparen_loc().map(|l| range(&l)));
|
|
1774
|
+
match block {
|
|
1775
|
+
Some(block) => {
|
|
1776
|
+
let block = block.as_block_node().unwrap();
|
|
1777
|
+
self.visit_block(call, &block, scope)
|
|
1778
|
+
}
|
|
1779
|
+
None => call,
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
Node::SymbolNode { .. } => self.visit_symbol_node(node),
|
|
1783
|
+
Node::UndefNode { .. } => {
|
|
1784
|
+
let undef = node.as_undef_node().unwrap();
|
|
1785
|
+
let names = self.visit_all(undef.names().iter(), scope);
|
|
1786
|
+
self.b_keyword_cmd("undef", range(&undef.keyword_loc()), names, None)
|
|
1787
|
+
}
|
|
1788
|
+
Node::UntilNode { .. } => {
|
|
1789
|
+
let until = node.as_until_node().unwrap();
|
|
1790
|
+
let keyword = range(&until.until_keyword_loc());
|
|
1791
|
+
let predicate = self.visit_req(&until.predicate(), scope);
|
|
1792
|
+
let body = self.visit_statements(until.statements(), scope);
|
|
1793
|
+
if node.location().start() == keyword.start {
|
|
1794
|
+
self.b_loop("until", keyword, predicate, body, range(&until.end_keyword_loc().unwrap()))
|
|
1795
|
+
} else {
|
|
1796
|
+
self.b_loop_mod("until", body.unwrap(), predicate)
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
Node::WhileNode { .. } => {
|
|
1800
|
+
let w = node.as_while_node().unwrap();
|
|
1801
|
+
let keyword = range(&w.while_keyword_loc());
|
|
1802
|
+
let predicate = self.visit_req(&w.predicate(), scope);
|
|
1803
|
+
let body = self.visit_statements(w.statements(), scope);
|
|
1804
|
+
if node.location().start() == keyword.start {
|
|
1805
|
+
self.b_loop("while", keyword, predicate, body, range(&w.end_keyword_loc().unwrap()))
|
|
1806
|
+
} else {
|
|
1807
|
+
self.b_loop_mod("while", body.unwrap(), predicate)
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
Node::WhenNode { .. } => {
|
|
1811
|
+
let when = node.as_when_node().unwrap();
|
|
1812
|
+
let conditions = self.visit_all(when.conditions().iter(), scope);
|
|
1813
|
+
let body = self.visit_statements(when.statements(), scope);
|
|
1814
|
+
self.b_when(range(&when.when_keyword_loc()), conditions, body)
|
|
1815
|
+
}
|
|
1816
|
+
Node::XStringNode { .. } => {
|
|
1817
|
+
let xstring = node.as_x_string_node().unwrap();
|
|
1818
|
+
let opening = range(&xstring.opening_loc());
|
|
1819
|
+
if self.text(opening).starts_with(b"<<") {
|
|
1820
|
+
let content = range(&xstring.content_loc());
|
|
1821
|
+
let part = Part::Str { unescaped: xstring.unescaped().to_vec(), content, location: content };
|
|
1822
|
+
return Some(self.visit_heredoc("xstr", opening, vec![part], range(&xstring.closing_loc()), scope));
|
|
1823
|
+
}
|
|
1824
|
+
let content = range(&xstring.content_loc());
|
|
1825
|
+
let opening_text = self.text(opening).to_vec();
|
|
1826
|
+
let parts = if content.start == content.end {
|
|
1827
|
+
Vec::new()
|
|
1828
|
+
} else if self.text(content).contains(&b'\n') {
|
|
1829
|
+
let escaped = self.text(content).to_vec();
|
|
1830
|
+
self.string_nodes_from_line_continuations(xstring.unescaped(), &escaped, content.start, Some(&opening_text))
|
|
1831
|
+
} else {
|
|
1832
|
+
vec![self.b_string_internal(xstring.unescaped().to_vec(), content)]
|
|
1833
|
+
};
|
|
1834
|
+
self.b_xstring_compose(Some(Tok { text: &opening_text, range: opening }), parts, Some(range(&xstring.closing_loc())))
|
|
1835
|
+
}
|
|
1836
|
+
Node::YieldNode { .. } => {
|
|
1837
|
+
let y = node.as_yield_node().unwrap();
|
|
1838
|
+
let args = self.visit_arguments(y.arguments(), scope);
|
|
1839
|
+
self.b_keyword_cmd("yield", range(&y.keyword_loc()), args, y.rparen_loc().map(|l| range(&l)))
|
|
1840
|
+
}
|
|
1841
|
+
Node::ImplicitNode { .. } | Node::ImplicitRestNode { .. } => return None,
|
|
1842
|
+
Node::BlockNode { .. }
|
|
1843
|
+
| Node::BlockParametersNode { .. }
|
|
1844
|
+
| Node::ParametersNode { .. }
|
|
1845
|
+
| Node::ArgumentsNode { .. }
|
|
1846
|
+
| Node::EnsureNode { .. }
|
|
1847
|
+
| Node::RescueNode { .. }
|
|
1848
|
+
| Node::NumberedParametersNode { .. } => {
|
|
1849
|
+
panic!("cannot directly compile {node:?}")
|
|
1850
|
+
}
|
|
1851
|
+
})
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// ---------------------------------------------------------------------
|
|
1855
|
+
// Visitor helpers
|
|
1856
|
+
|
|
1857
|
+
fn alias(&mut self, keyword: Range, new: NodeId, old: NodeId) -> NodeId {
|
|
1858
|
+
let loc = self.keyword_map(keyword, &[Some(new), Some(old)], None);
|
|
1859
|
+
self.n("alias", vec![Child::Node(new), Child::Node(old)], loc)
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
fn binary_operator(&self, location: Location<'_>) -> Vec<u8> {
|
|
1863
|
+
let mut text = self.text(range(&location)).to_vec();
|
|
1864
|
+
if text.last() == Some(&b'=') {
|
|
1865
|
+
text.pop();
|
|
1866
|
+
}
|
|
1867
|
+
text
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
fn variable(&mut self, ty: &'static str, node: &Node<'_>) -> NodeId {
|
|
1871
|
+
let r = range(&node.location());
|
|
1872
|
+
let name = self.text(r).to_vec();
|
|
1873
|
+
self.b_named(ty, &name, r)
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
fn variable_write(&mut self, ty: &'static str, name_loc: Location<'_>, value: Option<Node<'_>>, scope: Scope) -> NodeId {
|
|
1877
|
+
let r = range(&name_loc);
|
|
1878
|
+
let name = self.text(r).to_vec();
|
|
1879
|
+
let var = self.b_named(ty, &name, r);
|
|
1880
|
+
let lhs = self.b_assignable(var);
|
|
1881
|
+
let value = self.visit(value, scope);
|
|
1882
|
+
self.b_assign(lhs, value)
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
fn variable_op_write(&mut self, ty: &'static str, name_loc: Location<'_>, operator: Location<'_>, value: Node<'_>, scope: Scope) -> NodeId {
|
|
1886
|
+
let r = range(&name_loc);
|
|
1887
|
+
let name = self.text(r).to_vec();
|
|
1888
|
+
let var = if ty == "const" { self.b_const(&name, r) } else { self.b_named(ty, &name, r) };
|
|
1889
|
+
let lhs = self.b_assignable(var);
|
|
1890
|
+
let op = self.binary_operator(operator);
|
|
1891
|
+
let value = self.visit_req(&value, scope);
|
|
1892
|
+
self.b_op_assign(lhs, &op, value)
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
fn constant_path(&mut self, parent: Option<Node<'_>>, delimiter: Location<'_>, name_loc: Location<'_>, scope: Scope) -> NodeId {
|
|
1896
|
+
let name_range = range(&name_loc);
|
|
1897
|
+
let name = self.text(name_range).to_vec();
|
|
1898
|
+
match parent {
|
|
1899
|
+
None => self.b_const_global(range(&delimiter), &name, name_range),
|
|
1900
|
+
Some(parent) => {
|
|
1901
|
+
let scope_node = self.visit_req(&parent, scope);
|
|
1902
|
+
self.b_const_fetch(scope_node, &name, name_range)
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
fn rational_value(&self, node: &Node<'_>) -> String {
|
|
1908
|
+
let rational = node.as_rational_node().unwrap();
|
|
1909
|
+
rational_text(integer_child(rational.numerator()), integer_child(rational.denominator()))
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
fn regexp(&mut self, opening: Range, parts: Vec<NodeId>, closing: Range) -> NodeId {
|
|
1913
|
+
let end = Range::new(closing.start, closing.start + 1);
|
|
1914
|
+
let options_range = Range::new(closing.start + 1, closing.end);
|
|
1915
|
+
let options_text = self.text(options_range).to_vec();
|
|
1916
|
+
let options = self.b_regexp_options(&options_text, options_range);
|
|
1917
|
+
self.b_regexp_compose(opening, parts, end, options)
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
fn regular_expression(&mut self, opening: Location<'_>, content: Location<'_>, closing: Location<'_>) -> NodeId {
|
|
1921
|
+
let opening = range(&opening);
|
|
1922
|
+
let content = range(&content);
|
|
1923
|
+
let opening_text = self.text(opening).to_vec();
|
|
1924
|
+
let unescaped = regexp_value(self.text(content), &opening_text);
|
|
1925
|
+
let parts = if content.start == content.end {
|
|
1926
|
+
Vec::new()
|
|
1927
|
+
} else if self.text(content).contains(&b'\n') {
|
|
1928
|
+
let escaped = self.text(content).to_vec();
|
|
1929
|
+
self.string_nodes_from_line_continuations(&unescaped, &escaped, content.start, Some(&opening_text))
|
|
1930
|
+
} else {
|
|
1931
|
+
vec![self.b_string_internal(unescaped, content)]
|
|
1932
|
+
};
|
|
1933
|
+
self.regexp(opening, parts, range(&closing))
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
fn visit_array_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
1937
|
+
let array = node.as_array_node().unwrap();
|
|
1938
|
+
let opening = array.opening_loc().map(|l| range(&l));
|
|
1939
|
+
let opening_text = opening.map(|r| self.text(r).to_vec());
|
|
1940
|
+
let percent = starts_with_any(opening_text.as_deref(), &[b"%w", b"%W", b"%i", b"%I"]);
|
|
1941
|
+
let elements = if percent {
|
|
1942
|
+
let mut elements = Vec::new();
|
|
1943
|
+
for element in array.elements().iter() {
|
|
1944
|
+
match &element {
|
|
1945
|
+
Node::StringNode { .. } => {
|
|
1946
|
+
let string = element.as_string_node().unwrap();
|
|
1947
|
+
let content = range(&string.content_loc());
|
|
1948
|
+
if self.text(content).contains(&b'\n') {
|
|
1949
|
+
let escaped = self.text(content).to_vec();
|
|
1950
|
+
let nodes = self.string_nodes_from_line_continuations(string.unescaped(), &escaped, content.start, opening_text.as_deref());
|
|
1951
|
+
elements.extend(nodes);
|
|
1952
|
+
} else {
|
|
1953
|
+
elements.push(self.b_string_internal(string.unescaped().to_vec(), content));
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
Node::InterpolatedStringNode { .. } => {
|
|
1957
|
+
let string = element.as_interpolated_string_node().unwrap();
|
|
1958
|
+
let parts = self.string_nodes_from_interpolation(string.parts().iter().collect(), opening_text.clone(), scope);
|
|
1959
|
+
let begin = string.opening_loc().map(|l| range(&l));
|
|
1960
|
+
let begin_text = begin.map(|r| self.text(r).to_vec());
|
|
1961
|
+
let tok = begin.map(|r| (begin_text.clone().unwrap(), r));
|
|
1962
|
+
let composed = self.b_string_compose(tok.as_ref().map(|(t, r)| Tok { text: t, range: *r }), parts, string.closing_loc().map(|l| range(&l)));
|
|
1963
|
+
elements.push(composed);
|
|
1964
|
+
}
|
|
1965
|
+
_ => elements.extend(self.visit_node(&element, scope)),
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
elements
|
|
1969
|
+
} else {
|
|
1970
|
+
self.visit_all(array.elements().iter(), scope)
|
|
1971
|
+
};
|
|
1972
|
+
self.b_array(opening, elements, array.closing_loc().map(|l| range(&l)))
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
fn visit_array_pattern_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
1976
|
+
let pattern = node.as_array_pattern_node().unwrap();
|
|
1977
|
+
let mut elements: Vec<Node<'_>> = pattern.requireds().iter().collect();
|
|
1978
|
+
let rest = pattern.rest();
|
|
1979
|
+
let implicit_rest = matches!(rest, Some(Node::ImplicitRestNode { .. }));
|
|
1980
|
+
if let Some(explicit_rest) = pattern.rest().filter(|_| !implicit_rest) {
|
|
1981
|
+
elements.push(explicit_rest);
|
|
1982
|
+
}
|
|
1983
|
+
elements.extend(pattern.posts().iter());
|
|
1984
|
+
let mut visited = self.visit_all(elements, scope);
|
|
1985
|
+
if implicit_rest {
|
|
1986
|
+
let comma = range(&rest.unwrap().location());
|
|
1987
|
+
let last = visited.pop().unwrap();
|
|
1988
|
+
let expression = Self::join_opt(self.expr(last), Some(comma));
|
|
1989
|
+
visited.push(self.n("match_with_trailing_comma", vec![Child::Node(last)], Loc::expr(expression)));
|
|
1990
|
+
}
|
|
1991
|
+
let opening = pattern.opening_loc().map(|l| range(&l));
|
|
1992
|
+
let closing = pattern.closing_loc().map(|l| range(&l));
|
|
1993
|
+
match pattern.constant() {
|
|
1994
|
+
Some(constant) => {
|
|
1995
|
+
let constant = self.visit_req(&constant, scope);
|
|
1996
|
+
let inner = if visited.is_empty() {
|
|
1997
|
+
self.b_array_pattern(opening, visited, closing)
|
|
1998
|
+
} else {
|
|
1999
|
+
self.b_array_pattern(None, visited, None)
|
|
2000
|
+
};
|
|
2001
|
+
self.b_const_pattern(constant, opening, inner, closing)
|
|
2002
|
+
}
|
|
2003
|
+
None => self.b_array_pattern(opening, visited, closing),
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
fn visit_assoc_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2008
|
+
let assoc = node.as_assoc_node().unwrap();
|
|
2009
|
+
let key = assoc.key();
|
|
2010
|
+
let value = assoc.value();
|
|
2011
|
+
if let Node::ImplicitNode { .. } = value {
|
|
2012
|
+
if scope.in_pattern {
|
|
2013
|
+
if let Some(symbol) = key.as_symbol_node() {
|
|
2014
|
+
if symbol.opening_loc().is_none() {
|
|
2015
|
+
let r = range(&key.location());
|
|
2016
|
+
let name_l = Range::new(r.start, r.end - 1);
|
|
2017
|
+
return self.n("match_var", vec![Child::sym_name(symbol.unescaped())], Loc::expr(Some(name_l)).with_expression(Some(r)));
|
|
2018
|
+
}
|
|
2019
|
+
let content = range(&symbol.content_loc());
|
|
2020
|
+
let opening = range(&symbol.opening_loc().unwrap());
|
|
2021
|
+
let closing = range(&symbol.closing_loc().unwrap());
|
|
2022
|
+
let expression = Some(opening.join(content).join(closing));
|
|
2023
|
+
return self.n("match_var", vec![Child::sym_name(symbol.unescaped())], Loc::expr(expression));
|
|
2024
|
+
}
|
|
2025
|
+
// Interpolated keys are a syntax error in parser.
|
|
2026
|
+
let r = range(&key.location());
|
|
2027
|
+
return self.n("match_var", vec![Child::sym_name(self.text(r).to_vec().as_slice())], Loc::expr(Some(r)));
|
|
2028
|
+
}
|
|
2029
|
+
let implicit = value.as_implicit_node().unwrap().value();
|
|
2030
|
+
let symbol = key.as_symbol_node().unwrap();
|
|
2031
|
+
let content = range(&symbol.content_loc());
|
|
2032
|
+
let implicit_value = match &implicit {
|
|
2033
|
+
Node::CallNode { .. } => {
|
|
2034
|
+
let call = implicit.as_call_node().unwrap();
|
|
2035
|
+
let message = range(&call.message_loc().unwrap());
|
|
2036
|
+
self.b_call_method(None, None, Some((call.name().as_slice(), message)), None, Vec::new(), None)
|
|
2037
|
+
}
|
|
2038
|
+
Node::ConstantReadNode { .. } => {
|
|
2039
|
+
let name = implicit.as_constant_read_node().unwrap().name();
|
|
2040
|
+
self.b_const(name.as_slice(), content)
|
|
2041
|
+
}
|
|
2042
|
+
Node::LocalVariableReadNode { .. } => {
|
|
2043
|
+
let name = implicit.as_local_variable_read_node().unwrap().name();
|
|
2044
|
+
self.n("lvar", vec![Child::sym_name(name.as_slice())], Loc::expr(Some(content)))
|
|
2045
|
+
}
|
|
2046
|
+
_ => {
|
|
2047
|
+
let r = range(&implicit.location());
|
|
2048
|
+
self.n("lvar", vec![Child::sym_name(self.text(content).to_vec().as_slice())], Loc::expr(Some(r)))
|
|
2049
|
+
}
|
|
2050
|
+
};
|
|
2051
|
+
return self.b_pair_keyword(symbol.unescaped(), range(&key.location()), implicit_value);
|
|
2052
|
+
}
|
|
2053
|
+
if assoc.operator_loc().is_some() {
|
|
2054
|
+
let key = self.visit_req(&key, scope);
|
|
2055
|
+
let value = self.visit_req(&value, scope);
|
|
2056
|
+
return self.b_pair(key, value);
|
|
2057
|
+
}
|
|
2058
|
+
if let Some(symbol) = key.as_symbol_node() {
|
|
2059
|
+
if symbol.opening_loc().is_none() {
|
|
2060
|
+
let value = self.visit_req(&value, scope);
|
|
2061
|
+
return self.b_pair_keyword(symbol.unescaped(), range(&key.location()), value);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
let (opening, closing, parts) = if let Some(symbol) = key.as_symbol_node() {
|
|
2065
|
+
let opening = range(&symbol.opening_loc().unwrap());
|
|
2066
|
+
let opening_text = self.text(opening).to_vec();
|
|
2067
|
+
let content = range(&symbol.content_loc());
|
|
2068
|
+
let parts = if content.start == content.end {
|
|
2069
|
+
Vec::new()
|
|
2070
|
+
} else if self.text(content).contains(&b'\n') {
|
|
2071
|
+
let escaped = self.text(content).to_vec();
|
|
2072
|
+
self.string_nodes_from_line_continuations(symbol.unescaped(), &escaped, content.start, Some(&opening_text))
|
|
2073
|
+
} else {
|
|
2074
|
+
vec![self.b_string_internal(symbol.unescaped().to_vec(), content)]
|
|
2075
|
+
};
|
|
2076
|
+
(opening, range(&symbol.closing_loc().unwrap()), parts)
|
|
2077
|
+
} else {
|
|
2078
|
+
let symbol = key.as_interpolated_symbol_node().unwrap();
|
|
2079
|
+
let opening = range(&symbol.opening_loc().unwrap());
|
|
2080
|
+
let parts = self.visit_all(symbol.parts().iter(), scope);
|
|
2081
|
+
(opening, range(&symbol.closing_loc().unwrap()), parts)
|
|
2082
|
+
};
|
|
2083
|
+
let value = self.visit_req(&value, scope);
|
|
2084
|
+
self.b_pair_quoted(opening, parts, closing, value)
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
fn visit_begin_node(&mut self, node: &Node<'_>, scope: Scope) -> Option<NodeId> {
|
|
2088
|
+
let begin = node.as_begin_node().unwrap();
|
|
2089
|
+
let mut rescue_bodies = Vec::new();
|
|
2090
|
+
let mut rescue_clause = begin.rescue_clause();
|
|
2091
|
+
while let Some(clause) = rescue_clause {
|
|
2092
|
+
let find_start = clause
|
|
2093
|
+
.reference()
|
|
2094
|
+
.map(|r| r.location().end())
|
|
2095
|
+
.or_else(|| clause.exceptions().last().map(|e| e.location().end()))
|
|
2096
|
+
.unwrap_or_else(|| clause.keyword_loc().end());
|
|
2097
|
+
let find_end = clause
|
|
2098
|
+
.statements()
|
|
2099
|
+
.map(|s| s.location().start())
|
|
2100
|
+
.or_else(|| clause.subsequent().map(|s| s.location().start()))
|
|
2101
|
+
.or_else(|| begin.else_clause().map(|e| e.location().start()))
|
|
2102
|
+
.or_else(|| begin.ensure_clause().map(|e| e.location().start()))
|
|
2103
|
+
.or_else(|| begin.end_keyword_loc().map(|l| l.start()))
|
|
2104
|
+
.unwrap_or(find_start + 1);
|
|
2105
|
+
let exceptions = if clause.exceptions().is_empty() {
|
|
2106
|
+
None
|
|
2107
|
+
} else {
|
|
2108
|
+
let list = self.visit_all(clause.exceptions().iter(), scope);
|
|
2109
|
+
Some(self.b_array(None, list, None))
|
|
2110
|
+
};
|
|
2111
|
+
let reference = self.visit(clause.reference(), scope);
|
|
2112
|
+
let then = self.srange_semicolon(find_start, Some(find_end));
|
|
2113
|
+
let statements = self.visit_statements(clause.statements(), scope);
|
|
2114
|
+
rescue_bodies.push(self.b_rescue_body(range(&clause.keyword_loc()), exceptions, reference, then, statements));
|
|
2115
|
+
rescue_clause = clause.subsequent();
|
|
2116
|
+
}
|
|
2117
|
+
let statements = self.visit_statements(begin.statements(), scope);
|
|
2118
|
+
let else_clause = begin.else_clause();
|
|
2119
|
+
let else_t = else_clause.as_ref().map(|e| range(&e.else_keyword_loc()));
|
|
2120
|
+
let else_body = self.visit_statements(else_clause.and_then(|e| e.statements()), scope);
|
|
2121
|
+
let ensure_clause = begin.ensure_clause();
|
|
2122
|
+
let ensure_t = ensure_clause.as_ref().map(|e| range(&e.ensure_keyword_loc()));
|
|
2123
|
+
let ensure_body = self.visit_statements(ensure_clause.and_then(|e| e.statements()), scope);
|
|
2124
|
+
let body = self.b_begin_body(statements, rescue_bodies, else_t, else_body, ensure_t, ensure_body);
|
|
2125
|
+
match begin.begin_keyword_loc() {
|
|
2126
|
+
Some(keyword) => Some(self.b_begin_keyword(range(&keyword), body, begin.end_keyword_loc().map(|l| range(&l)))),
|
|
2127
|
+
None => body,
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
|
|
2131
|
+
/// The [`SignFold`] for a unary `-@` or `+@` call written as an operator
|
|
2132
|
+
/// followed by whitespace and an operand that starts with a numeric
|
|
2133
|
+
/// literal, unless the operand is `literal ** x` (which the parser gem
|
|
2134
|
+
/// also reads as `-(literal ** x)`).
|
|
2135
|
+
fn sign_fold_for(&self, call: &ruby_prism::CallNode<'_>, name: &[u8]) -> Option<SignFold> {
|
|
2136
|
+
if !matches!(name, b"-@" | b"+@") || call.call_operator_loc().is_some() || call.arguments().is_some() || call.block().is_some() {
|
|
2137
|
+
return None;
|
|
2138
|
+
}
|
|
2139
|
+
let operator = range(&call.message_loc()?);
|
|
2140
|
+
if !matches!(self.text(operator), b"-" | b"+") {
|
|
2141
|
+
return None;
|
|
2142
|
+
}
|
|
2143
|
+
let receiver = call.receiver()?;
|
|
2144
|
+
let between = &self.tree.source[operator.end as usize..receiver.location().start() as usize];
|
|
2145
|
+
if between.is_empty() || !between.iter().all(|byte| matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | 0x0b | 0x0c)) {
|
|
2146
|
+
return None;
|
|
2147
|
+
}
|
|
2148
|
+
if let Some(power) = receiver.as_call_node() {
|
|
2149
|
+
if power.name().as_slice() == b"**" && power.receiver().as_ref().is_some_and(is_numeric_literal) {
|
|
2150
|
+
return None;
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
let literal_start = leftmost_numeric_literal(&receiver)?;
|
|
2154
|
+
Some(SignFold { literal_start, operator, negative: name == b"-@" })
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
/// Gives a numeric literal the pending [`SignFold`] when it is the
|
|
2158
|
+
/// literal the fold is waiting for.
|
|
2159
|
+
fn apply_sign_fold(&mut self, literal: NodeId) -> NodeId {
|
|
2160
|
+
let Some(fold) = self.sign_fold else { return literal };
|
|
2161
|
+
let expression = self.expr(literal).expect("numeric literals have a location");
|
|
2162
|
+
if expression.start != fold.literal_start {
|
|
2163
|
+
return literal;
|
|
2164
|
+
}
|
|
2165
|
+
self.sign_fold = None;
|
|
2166
|
+
let mut children = self.tree.children(literal).to_vec();
|
|
2167
|
+
if fold.negative {
|
|
2168
|
+
children[0] = negate_numeric(&children[0]);
|
|
2169
|
+
}
|
|
2170
|
+
let loc = Loc::expr(Some(fold.operator.join(expression)));
|
|
2171
|
+
self.tree.updated(literal, None, Some(children), Some(Some(loc)))
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
fn visit_call_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2175
|
+
let call = node.as_call_node().unwrap();
|
|
2176
|
+
let name = call.name();
|
|
2177
|
+
let message = call.message_loc().map(|l| self.text(range(&l)).to_vec());
|
|
2178
|
+
let name = operator_method_name(name.as_slice(), message.as_deref());
|
|
2179
|
+
let name = name.as_slice();
|
|
2180
|
+
let mut arguments: Vec<Node<'_>> = call.arguments().map(|a| a.arguments().iter().collect()).unwrap_or_default();
|
|
2181
|
+
let mut block = call.block();
|
|
2182
|
+
if let Some(Node::BlockArgumentNode { .. }) = block {
|
|
2183
|
+
arguments.push(block.take().unwrap());
|
|
2184
|
+
}
|
|
2185
|
+
let block = block.map(|b| b.as_block_node().unwrap());
|
|
2186
|
+
if let Some(fold) = self.sign_fold_for(&call, name) {
|
|
2187
|
+
self.sign_fold = Some(fold);
|
|
2188
|
+
let operand = self.visit_req(&call.receiver().unwrap(), scope);
|
|
2189
|
+
assert!(self.sign_fold.take().is_none(), "unary sign was not folded into a numeric literal");
|
|
2190
|
+
return operand;
|
|
2191
|
+
}
|
|
2192
|
+
if call.call_operator_loc().is_none() {
|
|
2193
|
+
match name {
|
|
2194
|
+
b"!" => {
|
|
2195
|
+
let receiver = self.visit(call.receiver(), scope);
|
|
2196
|
+
let not = self.b_not_op(
|
|
2197
|
+
range(&call.message_loc().unwrap()),
|
|
2198
|
+
call.opening_loc().map(|l| range(&l)),
|
|
2199
|
+
receiver,
|
|
2200
|
+
call.closing_loc().map(|l| range(&l)),
|
|
2201
|
+
);
|
|
2202
|
+
return self.visit_block_opt(not, block, scope);
|
|
2203
|
+
}
|
|
2204
|
+
b"=~" => {
|
|
2205
|
+
if let Some(Node::RegularExpressionNode { .. }) = call.receiver() {
|
|
2206
|
+
let receiver = self.visit_req(&call.receiver().unwrap(), scope);
|
|
2207
|
+
let arg = self.visit_req(&call.arguments().unwrap().arguments().iter().next().unwrap(), scope);
|
|
2208
|
+
return self.b_match_op(receiver, arg);
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
b"[]" => {
|
|
2212
|
+
let receiver = self.visit(call.receiver(), scope);
|
|
2213
|
+
let args = self.visit_all(arguments, scope);
|
|
2214
|
+
let index = self.b_index(b"[]", receiver, args, call.closing_loc().map(|l| range(&l)));
|
|
2215
|
+
return self.visit_block_opt(index, block, scope);
|
|
2216
|
+
}
|
|
2217
|
+
b"[]=" => {
|
|
2218
|
+
let message = call.message_loc().map(|l| self.text(range(&l)).to_vec());
|
|
2219
|
+
if message.as_deref() != Some(b"[]=") && call.arguments().is_some() && block.is_none() && !call.is_safe_navigation() {
|
|
2220
|
+
let mut index_args: Vec<Node<'_>> = call.arguments().unwrap().arguments().iter().collect();
|
|
2221
|
+
let value = index_args.pop();
|
|
2222
|
+
let receiver = self.visit(call.receiver(), scope);
|
|
2223
|
+
let args = self.visit_all(index_args, scope);
|
|
2224
|
+
let lhs = self.b_index(b"[]=", receiver, args, call.closing_loc().map(|l| range(&l)));
|
|
2225
|
+
let value = self.visit(value, scope);
|
|
2226
|
+
return self.b_assign(lhs, value);
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
_ => {}
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
let message_loc = call.message_loc();
|
|
2233
|
+
let dot = call.call_operator_loc().map(|l| self.text(range(&l)).to_vec());
|
|
2234
|
+
let receiver = self.visit(call.receiver(), scope);
|
|
2235
|
+
let message_text = message_loc.as_ref().map(|l| self.text(range(l)).to_vec());
|
|
2236
|
+
let attribute_write = name.ends_with(b"=")
|
|
2237
|
+
&& !message_text.as_deref().is_some_and(|m| m.ends_with(b"="))
|
|
2238
|
+
&& call.arguments().is_some()
|
|
2239
|
+
&& block.is_none();
|
|
2240
|
+
let result = if attribute_write {
|
|
2241
|
+
let selector = message_text.clone().unwrap();
|
|
2242
|
+
let lhs = self.b_attr_asgn(receiver, dot.as_deref(), (&selector, range(&message_loc.unwrap())));
|
|
2243
|
+
let last = call.arguments().unwrap().arguments().iter().last();
|
|
2244
|
+
let value = self.visit(last, scope);
|
|
2245
|
+
self.b_assign(lhs, value)
|
|
2246
|
+
} else {
|
|
2247
|
+
let args = self.visit_all(arguments, scope);
|
|
2248
|
+
let selector = message_loc.map(|l| range(&l));
|
|
2249
|
+
self.b_call_method(
|
|
2250
|
+
receiver,
|
|
2251
|
+
dot.as_deref(),
|
|
2252
|
+
selector.map(|r| (name, r)),
|
|
2253
|
+
call.opening_loc().map(|l| range(&l)),
|
|
2254
|
+
args,
|
|
2255
|
+
call.closing_loc().map(|l| range(&l)),
|
|
2256
|
+
)
|
|
2257
|
+
};
|
|
2258
|
+
self.visit_block_opt(result, block, scope)
|
|
2259
|
+
}
|
|
2260
|
+
|
|
2261
|
+
fn visit_block_opt(&mut self, call: NodeId, block: Option<ruby_prism::BlockNode<'_>>, scope: Scope) -> NodeId {
|
|
2262
|
+
match block {
|
|
2263
|
+
Some(block) => self.visit_block(call, &block, scope),
|
|
2264
|
+
None => call,
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
/// `procarg0?(parameters)`: exactly one required parameter.
|
|
2269
|
+
fn procarg0(parameters: &ruby_prism::ParametersNode<'_>) -> bool {
|
|
2270
|
+
parameters.requireds().len() == 1
|
|
2271
|
+
&& parameters.optionals().is_empty()
|
|
2272
|
+
&& parameters.rest().is_none()
|
|
2273
|
+
&& parameters.posts().is_empty()
|
|
2274
|
+
&& parameters.keywords().is_empty()
|
|
2275
|
+
&& parameters.keyword_rest().is_none()
|
|
2276
|
+
&& parameters.block().is_none()
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
fn visit_block(&mut self, call: NodeId, block: &ruby_prism::BlockNode<'_>, scope: Scope) -> NodeId {
|
|
2280
|
+
let args = match block.parameters() {
|
|
2281
|
+
None => Child::Node(self.b_args(None, Vec::new(), None)),
|
|
2282
|
+
Some(Node::NumberedParametersNode { .. }) => {
|
|
2283
|
+
let maximum = block.parameters().unwrap().as_numbered_parameters_node().unwrap().maximum();
|
|
2284
|
+
Child::Int(maximum.to_string().into())
|
|
2285
|
+
}
|
|
2286
|
+
Some(Node::ItParametersNode { .. }) => Child::Node(self.b_args(None, Vec::new(), None)),
|
|
2287
|
+
Some(parameters) => {
|
|
2288
|
+
let parameters = parameters.as_block_parameters_node().unwrap();
|
|
2289
|
+
let list = self.visit_block_parameters(¶meters, scope, true);
|
|
2290
|
+
Child::Node(self.b_args(
|
|
2291
|
+
parameters.opening_loc().map(|l| range(&l)),
|
|
2292
|
+
list,
|
|
2293
|
+
parameters.closing_loc().map(|l| range(&l)),
|
|
2294
|
+
))
|
|
2295
|
+
}
|
|
2296
|
+
};
|
|
2297
|
+
let body = self.visit(block.body(), scope);
|
|
2298
|
+
self.b_block(call, range(&block.opening_loc()), args, body, range(&block.closing_loc()))
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
fn visit_block_parameters(&mut self, parameters: &ruby_prism::BlockParametersNode<'_>, scope: Scope, procarg0: bool) -> Vec<NodeId> {
|
|
2302
|
+
let mut list = Vec::new();
|
|
2303
|
+
if let Some(params) = parameters.parameters() {
|
|
2304
|
+
if procarg0 && Self::procarg0(¶ms) {
|
|
2305
|
+
let parameter = params.requireds().iter().next().unwrap();
|
|
2306
|
+
let visited = if let Node::RequiredParameterNode { .. } = parameter {
|
|
2307
|
+
self.visit_req(¶meter, scope)
|
|
2308
|
+
} else {
|
|
2309
|
+
self.visit_req(¶meter, scope.destructure())
|
|
2310
|
+
};
|
|
2311
|
+
list.push(visited);
|
|
2312
|
+
} else {
|
|
2313
|
+
list.extend(self.visit_parameters(¶ms, scope));
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
list.extend(self.visit_all(parameters.locals().iter(), scope));
|
|
2317
|
+
list
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
fn visit_parameters(&mut self, node: &ruby_prism::ParametersNode<'_>, scope: Scope) -> Vec<NodeId> {
|
|
2321
|
+
let mut params = Vec::new();
|
|
2322
|
+
for required in node.requireds().iter() {
|
|
2323
|
+
let scope = if matches!(required, Node::RequiredParameterNode { .. }) { scope } else { scope.destructure() };
|
|
2324
|
+
params.extend(self.visit_node(&required, scope));
|
|
2325
|
+
}
|
|
2326
|
+
params.extend(self.visit_all(node.optionals().iter(), scope));
|
|
2327
|
+
if let Some(rest) = node.rest() {
|
|
2328
|
+
if !matches!(rest, Node::ImplicitRestNode { .. }) {
|
|
2329
|
+
params.extend(self.visit_node(&rest, scope));
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
for post in node.posts().iter() {
|
|
2333
|
+
let scope = if matches!(post, Node::RequiredParameterNode { .. }) { scope } else { scope.destructure() };
|
|
2334
|
+
params.extend(self.visit_node(&post, scope));
|
|
2335
|
+
}
|
|
2336
|
+
params.extend(self.visit_all(node.keywords().iter(), scope));
|
|
2337
|
+
if let Some(keyword_rest) = node.keyword_rest() {
|
|
2338
|
+
params.extend(self.visit_node(&keyword_rest, scope));
|
|
2339
|
+
}
|
|
2340
|
+
if let Some(block) = node.block() {
|
|
2341
|
+
params.extend(self.visit_node(&block, scope));
|
|
2342
|
+
}
|
|
2343
|
+
params
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
/// `find_forwarding(parameters)`: which anonymous parameters a method
|
|
2347
|
+
/// body may forward.
|
|
2348
|
+
fn find_forwarding(parameters: Option<&ruby_prism::ParametersNode<'_>>, scope: Scope) -> Scope {
|
|
2349
|
+
let mut scope = scope.without_forwarding();
|
|
2350
|
+
let Some(parameters) = parameters else { return scope };
|
|
2351
|
+
if let Some(Node::RestParameterNode { .. }) = parameters.rest() {
|
|
2352
|
+
if parameters.rest().unwrap().as_rest_parameter_node().unwrap().name().is_none() {
|
|
2353
|
+
scope.forward_rest = true;
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
match parameters.keyword_rest() {
|
|
2357
|
+
Some(Node::KeywordRestParameterNode { .. }) => {
|
|
2358
|
+
if parameters.keyword_rest().unwrap().as_keyword_rest_parameter_node().unwrap().name().is_none() {
|
|
2359
|
+
scope.forward_kwrest = true;
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
Some(Node::ForwardingParameterNode { .. }) => {
|
|
2363
|
+
scope.forward_block = true;
|
|
2364
|
+
scope.forward_all = true;
|
|
2365
|
+
}
|
|
2366
|
+
_ => {}
|
|
2367
|
+
}
|
|
2368
|
+
if let Some(block) = parameters.block() {
|
|
2369
|
+
if block.as_block_parameter_node().is_some_and(|b| b.name().is_none()) {
|
|
2370
|
+
scope.forward_block = true;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
scope
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
fn visit_def_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2377
|
+
let def = node.as_def_node().unwrap();
|
|
2378
|
+
let receiver = def.receiver().map(|receiver| match receiver.as_parentheses_node() {
|
|
2379
|
+
Some(parens) => self.visit(parens.body(), scope).expect("parenthesized definee"),
|
|
2380
|
+
None => self.visit_req(&receiver, scope),
|
|
2381
|
+
});
|
|
2382
|
+
let parameters = def.parameters();
|
|
2383
|
+
let list = match ¶meters {
|
|
2384
|
+
Some(parameters) => self.visit_parameters(parameters, scope),
|
|
2385
|
+
None => Vec::new(),
|
|
2386
|
+
};
|
|
2387
|
+
let args = self.b_args(def.lparen_loc().map(|l| range(&l)), list, def.rparen_loc().map(|l| range(&l)));
|
|
2388
|
+
let body_scope = Self::find_forwarding(parameters.as_ref(), scope);
|
|
2389
|
+
let body = self.visit(def.body(), body_scope);
|
|
2390
|
+
let keyword = range(&def.def_keyword_loc());
|
|
2391
|
+
let name = operator_method_name(def.name().as_slice(), Some(self.text(range(&def.name_loc()))));
|
|
2392
|
+
let name_child = Child::sym_name(&name);
|
|
2393
|
+
let expression = match def.end_keyword_loc() {
|
|
2394
|
+
Some(end) => Some(keyword.join(range(&end))),
|
|
2395
|
+
None => Self::join_opt(Some(keyword), body.and_then(|b| self.expr(b))),
|
|
2396
|
+
};
|
|
2397
|
+
match receiver {
|
|
2398
|
+
Some(receiver) => self.n("defs", vec![Child::Node(receiver), name_child, Child::Node(args), Self::opt(body)], Loc::expr(expression)),
|
|
2399
|
+
None => self.n("def", vec![name_child, Child::Node(args), Self::opt(body)], Loc::expr(expression)),
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
fn visit_if_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2404
|
+
let if_node = node.as_if_node().unwrap();
|
|
2405
|
+
let predicate_node = if_node.predicate();
|
|
2406
|
+
match if_node.if_keyword_loc() {
|
|
2407
|
+
None => {
|
|
2408
|
+
let predicate = self.visit_req(&predicate_node, scope);
|
|
2409
|
+
let if_true = self.visit_statements(if_node.statements(), scope);
|
|
2410
|
+
let if_false = self.visit(if_node.subsequent(), scope);
|
|
2411
|
+
self.b_ternary(predicate, if_true, if_false)
|
|
2412
|
+
}
|
|
2413
|
+
Some(keyword) if keyword.start() == node.location().start() => {
|
|
2414
|
+
let predicate = self.visit_req(&predicate_node, scope);
|
|
2415
|
+
let then = match if_node.then_keyword_loc() {
|
|
2416
|
+
Some(then) => Some(range(&then)),
|
|
2417
|
+
None => {
|
|
2418
|
+
let end = if_node
|
|
2419
|
+
.statements()
|
|
2420
|
+
.map(|s| s.location().start())
|
|
2421
|
+
.or_else(|| if_node.subsequent().map(|s| s.location().start()))
|
|
2422
|
+
.or_else(|| if_node.end_keyword_loc().map(|l| l.start()));
|
|
2423
|
+
self.srange_semicolon(predicate_node.location().end(), end)
|
|
2424
|
+
}
|
|
2425
|
+
};
|
|
2426
|
+
let if_true = self.visit_statements(if_node.statements(), scope);
|
|
2427
|
+
let subsequent = if_node.subsequent();
|
|
2428
|
+
let else_t = match &subsequent {
|
|
2429
|
+
Some(Node::IfNode { .. }) => subsequent.as_ref().unwrap().as_if_node().unwrap().if_keyword_loc().map(|l| range(&l)),
|
|
2430
|
+
Some(Node::ElseNode { .. }) => Some(range(&subsequent.as_ref().unwrap().as_else_node().unwrap().else_keyword_loc())),
|
|
2431
|
+
_ => None,
|
|
2432
|
+
};
|
|
2433
|
+
let if_false = self.visit(subsequent, scope);
|
|
2434
|
+
let is_elsif = self.text(range(&keyword)) == b"elsif";
|
|
2435
|
+
let end = if is_elsif { None } else { if_node.end_keyword_loc().map(|l| range(&l)) };
|
|
2436
|
+
self.b_condition(range(&keyword), predicate, then, if_true, else_t, if_false, end)
|
|
2437
|
+
}
|
|
2438
|
+
Some(_) => {
|
|
2439
|
+
let if_true = self.visit_statements(if_node.statements(), scope);
|
|
2440
|
+
let if_false = self.visit(if_node.subsequent(), scope);
|
|
2441
|
+
let predicate = self.visit_req(&predicate_node, scope);
|
|
2442
|
+
self.b_condition_mod(if_true, if_false, predicate)
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
fn visit_unless_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2448
|
+
let unless = node.as_unless_node().unwrap();
|
|
2449
|
+
let keyword = range(&unless.unless_keyword_loc());
|
|
2450
|
+
let predicate_node = unless.predicate();
|
|
2451
|
+
if keyword.start == node.location().start() {
|
|
2452
|
+
let predicate = self.visit_req(&predicate_node, scope);
|
|
2453
|
+
let then = match unless.then_keyword_loc() {
|
|
2454
|
+
Some(then) => Some(range(&then)),
|
|
2455
|
+
None => {
|
|
2456
|
+
let end = unless
|
|
2457
|
+
.statements()
|
|
2458
|
+
.map(|s| s.location().start())
|
|
2459
|
+
.or_else(|| unless.else_clause().map(|s| s.location().start()))
|
|
2460
|
+
.or_else(|| unless.end_keyword_loc().map(|l| l.start()));
|
|
2461
|
+
self.srange_semicolon(predicate_node.location().end(), end)
|
|
2462
|
+
}
|
|
2463
|
+
};
|
|
2464
|
+
let else_clause = unless.else_clause();
|
|
2465
|
+
let else_t = else_clause.as_ref().map(|e| range(&e.else_keyword_loc()));
|
|
2466
|
+
let if_true = self.visit_statements(else_clause.and_then(|e| e.statements()), scope);
|
|
2467
|
+
let if_false = self.visit_statements(unless.statements(), scope);
|
|
2468
|
+
self.b_condition(keyword, predicate, then, if_true, else_t, if_false, unless.end_keyword_loc().map(|l| range(&l)))
|
|
2469
|
+
} else {
|
|
2470
|
+
let if_true = self.visit_statements(unless.else_clause().and_then(|e| e.statements()), scope);
|
|
2471
|
+
let if_false = self.visit_statements(unless.statements(), scope);
|
|
2472
|
+
let predicate = self.visit_req(&predicate_node, scope);
|
|
2473
|
+
self.b_condition_mod(if_true, if_false, predicate)
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
fn visit_in_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2478
|
+
let in_node = node.as_in_node().unwrap();
|
|
2479
|
+
let pattern_node = in_node.pattern();
|
|
2480
|
+
let (pattern, guard) = match &pattern_node {
|
|
2481
|
+
Node::IfNode { .. } => {
|
|
2482
|
+
let guard_node = pattern_node.as_if_node().unwrap();
|
|
2483
|
+
let pattern = self.visit_statements(guard_node.statements(), scope.pattern(true));
|
|
2484
|
+
let keyword = range(&guard_node.if_keyword_loc().unwrap());
|
|
2485
|
+
let body = self.visit_req(&guard_node.predicate(), scope);
|
|
2486
|
+
let expression = Self::join_opt(Some(keyword), self.expr(body));
|
|
2487
|
+
(pattern, Some(self.n("if_guard", vec![Child::Node(body)], Loc::expr(expression))))
|
|
2488
|
+
}
|
|
2489
|
+
Node::UnlessNode { .. } => {
|
|
2490
|
+
let guard_node = pattern_node.as_unless_node().unwrap();
|
|
2491
|
+
let pattern = self.visit_statements(guard_node.statements(), scope.pattern(true));
|
|
2492
|
+
let keyword = range(&guard_node.unless_keyword_loc());
|
|
2493
|
+
let body = self.visit_req(&guard_node.predicate(), scope);
|
|
2494
|
+
let expression = Self::join_opt(Some(keyword), self.expr(body));
|
|
2495
|
+
(pattern, Some(self.n("unless_guard", vec![Child::Node(body)], Loc::expr(expression))))
|
|
2496
|
+
}
|
|
2497
|
+
_ => (self.visit_node(&pattern_node, scope.pattern(true)), None),
|
|
2498
|
+
};
|
|
2499
|
+
let body = self.visit_statements(in_node.statements(), scope);
|
|
2500
|
+
let keyword = range(&in_node.in_keyword_loc());
|
|
2501
|
+
let args = [pattern, guard, body];
|
|
2502
|
+
let compact: Vec<Option<NodeId>> = args.iter().filter(|id| id.is_some()).cloned().collect();
|
|
2503
|
+
let loc = self.keyword_map(keyword, &compact, None);
|
|
2504
|
+
self.n("in_pattern", vec![Self::opt(pattern), Self::opt(guard), Self::opt(body)], loc)
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
fn visit_index_write(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2508
|
+
let (receiver, arguments, block, closing, operator, value) = if let Some(w) = node.as_index_operator_write_node() {
|
|
2509
|
+
(w.receiver(), w.arguments(), w.block(), w.closing_loc(), w.binary_operator_loc(), w.value())
|
|
2510
|
+
} else if let Some(w) = node.as_index_and_write_node() {
|
|
2511
|
+
(w.receiver(), w.arguments(), w.block(), w.closing_loc(), w.operator_loc(), w.value())
|
|
2512
|
+
} else {
|
|
2513
|
+
let w = node.as_index_or_write_node().unwrap();
|
|
2514
|
+
(w.receiver(), w.arguments(), w.block(), w.closing_loc(), w.operator_loc(), w.value())
|
|
2515
|
+
};
|
|
2516
|
+
let receiver = self.visit(receiver, scope);
|
|
2517
|
+
let mut args = self.visit_arguments(arguments, scope);
|
|
2518
|
+
if let Some(block) = block {
|
|
2519
|
+
args.extend(self.visit_node(&block.as_node(), scope));
|
|
2520
|
+
}
|
|
2521
|
+
let lhs = self.b_index(b"[]", receiver, args, Some(range(&closing)));
|
|
2522
|
+
let op = self.binary_operator(operator);
|
|
2523
|
+
let value = self.visit_req(&value, scope);
|
|
2524
|
+
self.b_op_assign(lhs, &op, value)
|
|
2525
|
+
}
|
|
2526
|
+
|
|
2527
|
+
fn visit_lambda_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2528
|
+
let lambda = node.as_lambda_node().unwrap();
|
|
2529
|
+
let call = self.n("lambda", Vec::new(), Loc::expr(Some(range(&lambda.operator_loc()))));
|
|
2530
|
+
let args = match lambda.parameters() {
|
|
2531
|
+
None => Child::Node(self.b_args(None, Vec::new(), None)),
|
|
2532
|
+
Some(Node::NumberedParametersNode { .. }) => {
|
|
2533
|
+
let maximum = lambda.parameters().unwrap().as_numbered_parameters_node().unwrap().maximum();
|
|
2534
|
+
Child::Int(maximum.to_string().into())
|
|
2535
|
+
}
|
|
2536
|
+
Some(Node::ItParametersNode { .. }) => Child::Node(self.b_args(None, Vec::new(), None)),
|
|
2537
|
+
Some(parameters) => {
|
|
2538
|
+
let parameters = parameters.as_block_parameters_node().unwrap();
|
|
2539
|
+
let list = self.visit_block_parameters(¶meters, scope, false);
|
|
2540
|
+
Child::Node(self.b_args(
|
|
2541
|
+
parameters.opening_loc().map(|l| range(&l)),
|
|
2542
|
+
list,
|
|
2543
|
+
parameters.closing_loc().map(|l| range(&l)),
|
|
2544
|
+
))
|
|
2545
|
+
}
|
|
2546
|
+
};
|
|
2547
|
+
let body = self.visit(lambda.body(), scope);
|
|
2548
|
+
self.b_block(call, range(&lambda.opening_loc()), args, body, range(&lambda.closing_loc()))
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
fn visit_multi_write_node(&mut self, node: &Node<'_>, scope: Scope) -> NodeId {
|
|
2552
|
+
let write = node.as_multi_write_node().unwrap();
|
|
2553
|
+
let mut elements: Vec<Node<'_>> = write.lefts().iter().collect();
|
|
2554
|
+
let rest = write.rest();
|
|
2555
|
+
if let Some(explicit_rest) = write.rest().filter(|rest| !matches!(rest, Node::ImplicitRestNode { .. })) {
|
|
2556
|
+
elements.push(explicit_rest);
|
|
2557
|
+
}
|
|
2558
|
+
elements.extend(write.rights().iter());
|
|
2559
|
+
if elements.len() == 1 && rest.is_none() {
|
|
2560
|
+
if let Some(inner) = elements[0].as_multi_target_node() {
|
|
2561
|
+
let mut inner_elements: Vec<Node<'_>> = inner.lefts().iter().collect();
|
|
2562
|
+
if let Some(inner_rest) = inner.rest() {
|
|
2563
|
+
if !matches!(inner_rest, Node::ImplicitRestNode { .. }) {
|
|
2564
|
+
inner_elements.push(inner_rest);
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
inner_elements.extend(inner.rights().iter());
|
|
2568
|
+
elements = inner_elements;
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2571
|
+
let items = self.visit_all(elements, scope);
|
|
2572
|
+
let lhs = self.b_multi_lhs(write.lparen_loc().map(|l| range(&l)), items, write.rparen_loc().map(|l| range(&l)));
|
|
2573
|
+
let value = self.visit_req(&write.value(), scope);
|
|
2574
|
+
self.b_binary("masgn", lhs, value)
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
fn visit_string_node(&mut self, node: &Node<'_>, _scope: Scope) -> NodeId {
|
|
2578
|
+
let string = node.as_string_node().unwrap();
|
|
2579
|
+
let opening = string.opening_loc().map(|l| range(&l));
|
|
2580
|
+
let opening_text = opening.map(|r| self.text(r).to_vec());
|
|
2581
|
+
let content = range(&string.content_loc());
|
|
2582
|
+
if opening_text.as_deref().is_some_and(|t| t.starts_with(b"<<")) {
|
|
2583
|
+
let part = Part::Str { unescaped: string.unescaped().to_vec(), content, location: content };
|
|
2584
|
+
return self.visit_heredoc("dstr", opening.unwrap(), vec![part], range(&string.closing_loc().unwrap()), _scope);
|
|
2585
|
+
}
|
|
2586
|
+
if opening_text.as_deref() == Some(b"?") {
|
|
2587
|
+
let r = range(&node.location());
|
|
2588
|
+
return self.n("str", vec![Child::Str(string.unescaped().into())], Loc { expression: Some(r), begin: Some(Range::new(r.start, r.start + 1)), end: None, heredoc: false });
|
|
2589
|
+
}
|
|
2590
|
+
let closing = string.closing_loc().map(|l| range(&l));
|
|
2591
|
+
let begin_tok = opening.map(|r| (opening_text.clone().unwrap(), r));
|
|
2592
|
+
if opening_text.as_deref().is_some_and(|t| t.starts_with(b"%")) && string.unescaped().is_empty() {
|
|
2593
|
+
return self.b_string_compose(begin_tok.as_ref().map(|(t, r)| Tok { text: t, range: *r }), Vec::new(), closing);
|
|
2594
|
+
}
|
|
2595
|
+
let parts = if self.text(content).contains(&b'\n') {
|
|
2596
|
+
let escaped = self.text(content).to_vec();
|
|
2597
|
+
self.string_nodes_from_line_continuations(string.unescaped(), &escaped, content.start, opening_text.as_deref())
|
|
2598
|
+
} else {
|
|
2599
|
+
vec![self.b_string_internal(string.unescaped().to_vec(), content)]
|
|
2600
|
+
};
|
|
2601
|
+
self.b_string_compose(begin_tok.as_ref().map(|(t, r)| Tok { text: t, range: *r }), parts, closing)
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
fn visit_symbol_node(&mut self, node: &Node<'_>) -> NodeId {
|
|
2605
|
+
let symbol = node.as_symbol_node().unwrap();
|
|
2606
|
+
let r = range(&node.location());
|
|
2607
|
+
match (symbol.opening_loc(), symbol.closing_loc()) {
|
|
2608
|
+
(None, None) => self.n("sym", vec![Child::sym_name(symbol.unescaped())], Loc::expr(Some(r))),
|
|
2609
|
+
(Some(_), None) => self.n("sym", vec![Child::sym_name(symbol.unescaped())], Loc { expression: Some(r), begin: Some(Range::new(r.start, r.start + 1)), end: None, heredoc: false }),
|
|
2610
|
+
(opening, Some(closing)) => {
|
|
2611
|
+
let opening = opening.map(|l| range(&l));
|
|
2612
|
+
let opening_text = opening.map(|o| self.text(o).to_vec());
|
|
2613
|
+
let content = range(&symbol.content_loc());
|
|
2614
|
+
let parts = if content.start == content.end {
|
|
2615
|
+
Vec::new()
|
|
2616
|
+
} else if self.text(content).contains(&b'\n') {
|
|
2617
|
+
let escaped = self.text(content).to_vec();
|
|
2618
|
+
self.string_nodes_from_line_continuations(symbol.unescaped(), &escaped, content.start, opening_text.as_deref())
|
|
2619
|
+
} else {
|
|
2620
|
+
vec![self.b_string_internal(symbol.unescaped().to_vec(), content)]
|
|
2621
|
+
};
|
|
2622
|
+
self.b_symbol_compose(opening, parts, Some(range(&closing)))
|
|
2623
|
+
}
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2627
|
+
fn string_nodes_from_interpolation(&mut self, parts: Vec<Node<'_>>, opening: Option<Vec<u8>>, scope: Scope) -> Vec<NodeId> {
|
|
2628
|
+
let mut out = Vec::new();
|
|
2629
|
+
let regex = opening.as_deref().is_some_and(|opening| opening == b"/" || opening.starts_with(b"%r"));
|
|
2630
|
+
for part in parts {
|
|
2631
|
+
if let Some(string) = part.as_string_node() {
|
|
2632
|
+
let content = range(&string.content_loc());
|
|
2633
|
+
let unescaped = match &opening {
|
|
2634
|
+
Some(opening) if regex => regexp_value(self.text(content), opening),
|
|
2635
|
+
_ => string.unescaped().to_vec(),
|
|
2636
|
+
};
|
|
2637
|
+
if self.text(content).contains(&b'\n') && string.opening_loc().is_none() {
|
|
2638
|
+
let escaped = self.text(content).to_vec();
|
|
2639
|
+
out.extend(self.string_nodes_from_line_continuations(&unescaped, &escaped, content.start, opening.as_deref()));
|
|
2640
|
+
continue;
|
|
2641
|
+
}
|
|
2642
|
+
if regex {
|
|
2643
|
+
out.push(self.b_string_internal(unescaped, content));
|
|
2644
|
+
continue;
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
out.extend(self.visit_node(&part, scope));
|
|
2648
|
+
}
|
|
2649
|
+
out
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
/// `string_nodes_from_line_continuations`: one `str` per source line
|
|
2653
|
+
/// (joining lines continued with a backslash), each with its own range.
|
|
2654
|
+
fn string_nodes_from_line_continuations(&mut self, unescaped: &[u8], escaped: &[u8], start_offset: u32, opening: Option<&[u8]>) -> Vec<NodeId> {
|
|
2655
|
+
let unescaped_lines = lines(unescaped);
|
|
2656
|
+
let escaped_lines = lines(escaped);
|
|
2657
|
+
let percent_array = starts_with_any(opening, &[b"%w", b"%W", b"%i", b"%I"]);
|
|
2658
|
+
let regex = opening == Some(b"/") || starts_with_any(opening, &[b"%r"]);
|
|
2659
|
+
let mut start_offset = start_offset;
|
|
2660
|
+
let mut out = Vec::new();
|
|
2661
|
+
let single_quoted = opening.is_some_and(|o| o.ends_with(b"'")) || starts_with_any(opening, &[b"%q", b"%s", b"%w", b"%i"]);
|
|
2662
|
+
if single_quoted {
|
|
2663
|
+
let mut current_length: u32 = 0;
|
|
2664
|
+
let mut current_line: Vec<u8> = Vec::new();
|
|
2665
|
+
let count = escaped_lines.len();
|
|
2666
|
+
for (index, escaped_line) in escaped_lines.iter().enumerate() {
|
|
2667
|
+
let unescaped_line = unescaped_lines.get(index).copied().unwrap_or(b"");
|
|
2668
|
+
current_length += escaped_line.len() as u32;
|
|
2669
|
+
current_line.extend_from_slice(unescaped_line);
|
|
2670
|
+
// escaped_line[/(\\)*\n$/, 1]: a backslash right before the newline.
|
|
2671
|
+
if percent_array && escaped_line.ends_with(b"\\\n") && index != count - 1 {
|
|
2672
|
+
continue;
|
|
2673
|
+
}
|
|
2674
|
+
let node = self.b_string_internal(std::mem::take(&mut current_line), Range::new(start_offset, start_offset + current_length));
|
|
2675
|
+
out.push(node);
|
|
2676
|
+
start_offset += escaped_line.len() as u32;
|
|
2677
|
+
current_length = 0;
|
|
2678
|
+
}
|
|
2679
|
+
return out;
|
|
2680
|
+
}
|
|
2681
|
+
|
|
2682
|
+
let mut escaped_lengths: Vec<u32> = Vec::new();
|
|
2683
|
+
let mut normalized_lengths: Vec<u32> = Vec::new();
|
|
2684
|
+
let mut do_next_tokens: Vec<bool> = Vec::new();
|
|
2685
|
+
// chunk_while { before ends with an odd number of backslashes before "\n" }
|
|
2686
|
+
let mut chunks: Vec<Vec<&[u8]>> = Vec::new();
|
|
2687
|
+
for line in &escaped_lines {
|
|
2688
|
+
let continues = chunks.last().and_then(|chunk| chunk.last()).is_some_and(|before| ends_with_odd_backslashes_newline(before));
|
|
2689
|
+
if continues {
|
|
2690
|
+
chunks.last_mut().unwrap().push(line);
|
|
2691
|
+
} else {
|
|
2692
|
+
chunks.push(vec![line]);
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
for chunk in &chunks {
|
|
2696
|
+
escaped_lengths.push(chunk.iter().map(|line| line.len() as u32).sum());
|
|
2697
|
+
let unescaped_lines_count: usize = if regex {
|
|
2698
|
+
0
|
|
2699
|
+
} else {
|
|
2700
|
+
chunk.iter().map(|line| escaped_newline_count(line)).sum()
|
|
2701
|
+
};
|
|
2702
|
+
let extra = if percent_array { chunk.len() } else { 1 };
|
|
2703
|
+
let total = unescaped_lines_count + extra;
|
|
2704
|
+
normalized_lengths.extend(std::iter::repeat_n(0, total));
|
|
2705
|
+
*normalized_lengths.last_mut().unwrap() = chunk.iter().map(|line| line.len() as u32).sum();
|
|
2706
|
+
do_next_tokens.extend(std::iter::repeat_n(false, total));
|
|
2707
|
+
*do_next_tokens.last_mut().unwrap() = true;
|
|
2708
|
+
}
|
|
2709
|
+
let mut current_line: Vec<u8> = Vec::new();
|
|
2710
|
+
let mut current_normalized_length: u32 = 0;
|
|
2711
|
+
let mut emitted_count = 0;
|
|
2712
|
+
for (index, unescaped_line) in unescaped_lines.iter().enumerate() {
|
|
2713
|
+
current_line.extend_from_slice(unescaped_line);
|
|
2714
|
+
current_normalized_length += normalized_lengths.get(index).copied().unwrap_or(0);
|
|
2715
|
+
if do_next_tokens.get(index).copied().unwrap_or(false) {
|
|
2716
|
+
let node = self.b_string_internal(
|
|
2717
|
+
std::mem::take(&mut current_line),
|
|
2718
|
+
Range::new(start_offset, start_offset + current_normalized_length),
|
|
2719
|
+
);
|
|
2720
|
+
out.push(node);
|
|
2721
|
+
start_offset += escaped_lengths.get(emitted_count).copied().unwrap_or(0);
|
|
2722
|
+
current_normalized_length = 0;
|
|
2723
|
+
emitted_count += 1;
|
|
2724
|
+
}
|
|
2725
|
+
}
|
|
2726
|
+
out
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
/// The `str` nodes of one string part of a heredoc: one per source line
|
|
2730
|
+
/// when the part spans lines.
|
|
2731
|
+
fn heredoc_string_part(&mut self, unescaped: Vec<u8>, content: Range, start: u32, single_quoted: bool, opening: &[u8]) -> Vec<NodeId> {
|
|
2732
|
+
let unescaped = if single_quoted { unescape_backslashes(&unescaped) } else { unescaped };
|
|
2733
|
+
if self.text(content).contains(&b'\n') {
|
|
2734
|
+
let escaped = self.text(content).to_vec();
|
|
2735
|
+
self.string_nodes_from_line_continuations(&unescaped, &escaped, start, Some(opening))
|
|
2736
|
+
} else {
|
|
2737
|
+
vec![self.b_string_internal(unescaped, content)]
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
/// `visit_heredoc`: the parts of a heredoc, with adjacent `str` parts on
|
|
2742
|
+
/// one line joined and empty ones dropped.
|
|
2743
|
+
fn visit_heredoc(&mut self, ty: &'static str, opening: Range, parts: Vec<Part<'_>>, closing: Range, scope: Scope) -> NodeId {
|
|
2744
|
+
let opening_text = self.text(opening).to_vec();
|
|
2745
|
+
let mut children: Vec<NodeId> = Vec::new();
|
|
2746
|
+
let mut indented = false;
|
|
2747
|
+
let first_is_string_node = match parts.first() {
|
|
2748
|
+
Some(Part::Str { .. }) => true,
|
|
2749
|
+
Some(Part::Node(first)) => first.as_string_node().is_some(),
|
|
2750
|
+
None => false,
|
|
2751
|
+
};
|
|
2752
|
+
if opening_text.starts_with(b"<<~") && !parts.is_empty() && !first_is_string_node {
|
|
2753
|
+
if let Part::Node(first) = &parts[0] {
|
|
2754
|
+
let location = range(&first.location());
|
|
2755
|
+
let line_start = self.tree.source[..location.start as usize].iter().rposition(|b| *b == b'\n').map_or(0, |i| i + 1) as u32;
|
|
2756
|
+
let r = Range::new(line_start, location.end);
|
|
2757
|
+
let text = self.text(r).to_vec();
|
|
2758
|
+
children.push(self.b_string_internal(text, r));
|
|
2759
|
+
indented = true;
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
// The parser gem's lexer reads `\\` in a single-quoted heredoc as an
|
|
2763
|
+
// escaped backslash; Ruby (and Prism) keep both characters.
|
|
2764
|
+
let single_quoted = opening_text.strip_prefix(b"<<").is_some_and(|rest| {
|
|
2765
|
+
let rest = rest.strip_prefix(b"-").or_else(|| rest.strip_prefix(b"~")).unwrap_or(rest);
|
|
2766
|
+
rest.starts_with(b"'")
|
|
2767
|
+
});
|
|
2768
|
+
for part in parts {
|
|
2769
|
+
let pushing: Vec<NodeId> = match part {
|
|
2770
|
+
Part::Str { unescaped, content, location } => {
|
|
2771
|
+
self.heredoc_string_part(unescaped, content, location.start, single_quoted, &opening_text)
|
|
2772
|
+
}
|
|
2773
|
+
Part::Node(node) => match node.as_string_node() {
|
|
2774
|
+
Some(string) if single_quoted || self.text(range(&string.content_loc())).contains(&b'\n') => {
|
|
2775
|
+
let content = range(&string.content_loc());
|
|
2776
|
+
let start = node.location().start();
|
|
2777
|
+
self.heredoc_string_part(string.unescaped().to_vec(), content, start, single_quoted, &opening_text)
|
|
2778
|
+
}
|
|
2779
|
+
_ => self.visit_node(&node, scope).into_iter().collect(),
|
|
2780
|
+
},
|
|
2781
|
+
};
|
|
2782
|
+
for child in pushing {
|
|
2783
|
+
let is_str = self.ty(child) == "str";
|
|
2784
|
+
if is_str && self.str_value(child) == Some(b"") {
|
|
2785
|
+
continue;
|
|
2786
|
+
}
|
|
2787
|
+
let last = children.last().copied();
|
|
2788
|
+
let mergeable = is_str
|
|
2789
|
+
&& last.is_some_and(|last| self.ty(last) == "str" && !self.str_value(last).unwrap_or_default().ends_with(b"\n"));
|
|
2790
|
+
if mergeable {
|
|
2791
|
+
let appendee = last.unwrap();
|
|
2792
|
+
let mut value = self.str_value(appendee).unwrap_or_default().to_vec();
|
|
2793
|
+
value.extend_from_slice(self.str_value(child).unwrap_or_default());
|
|
2794
|
+
let mut loc = *self.tree.loc(appendee).unwrap();
|
|
2795
|
+
loc.expression = self.join_exprs(appendee, child);
|
|
2796
|
+
let merged = self.tree.updated(appendee, Some("str"), Some(vec![Child::Str(value.into())]), Some(Some(loc)));
|
|
2797
|
+
*children.last_mut().unwrap() = merged;
|
|
2798
|
+
} else {
|
|
2799
|
+
children.push(child);
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
// The parser gem builds these nodes before dedenting their strings.
|
|
2804
|
+
let undedented: Vec<Option<Vec<u8>>> = if opening_text.starts_with(b"<<~") {
|
|
2805
|
+
children.iter().map(|child| self.undedented_value(*child)).collect()
|
|
2806
|
+
} else {
|
|
2807
|
+
Vec::new()
|
|
2808
|
+
};
|
|
2809
|
+
let closing_text = self.text(closing).to_vec();
|
|
2810
|
+
let trailing = closing_text.iter().rev().take_while(|b| matches!(b, b' ' | b'\t' | b'\n' | 0x0b | 0x0c | b'\r')).count() as u32;
|
|
2811
|
+
let closing_t = Range::new(closing.start, closing.end - trailing);
|
|
2812
|
+
let begin = Tok { text: &opening_text, range: opening };
|
|
2813
|
+
let begin = Some(Tok { text: begin.text, range: begin.range });
|
|
2814
|
+
let composed = if ty == "xstr" {
|
|
2815
|
+
let opening_copy = opening_text.clone();
|
|
2816
|
+
self.b_xstring_compose(Some(Tok { text: &opening_copy, range: opening }), children.clone(), Some(closing_t))
|
|
2817
|
+
} else {
|
|
2818
|
+
let opening_copy = opening_text.clone();
|
|
2819
|
+
let _ = begin;
|
|
2820
|
+
self.b_string_compose(Some(Tok { text: &opening_copy, range: opening }), children.clone(), Some(closing_t))
|
|
2821
|
+
};
|
|
2822
|
+
for (child, value) in children.iter().zip(&undedented) {
|
|
2823
|
+
if let Some(value) = value {
|
|
2824
|
+
self.tree.nodes[child.0 as usize].hashed_value = Some(value.as_slice().into());
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
if let ([_], [Some(value)]) = (children.as_slice(), undedented.as_slice()) {
|
|
2828
|
+
self.tree.nodes[composed.0 as usize].hashed_value = Some(value.as_slice().into());
|
|
2829
|
+
}
|
|
2830
|
+
if indented {
|
|
2831
|
+
let rest = Self::nodes(&children[1..]);
|
|
2832
|
+
self.tree.updated(composed, None, Some(rest), None)
|
|
2833
|
+
} else {
|
|
2834
|
+
composed
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
|
|
2838
|
+
/// A `<<~` heredoc `str`'s value before dedenting, when dedenting
|
|
2839
|
+
/// removed whitespace from its start: the whitespace its source line
|
|
2840
|
+
/// starts with, less what the value still starts with, then the value.
|
|
2841
|
+
fn undedented_value(&self, node: NodeId) -> Option<Vec<u8>> {
|
|
2842
|
+
if self.ty(node) != "str" {
|
|
2843
|
+
return None;
|
|
2844
|
+
}
|
|
2845
|
+
let value = self.str_value(node)?;
|
|
2846
|
+
let source = self.text(self.expr(node)?);
|
|
2847
|
+
let is_indentation = |byte: &u8| matches!(byte, b' ' | b'\t');
|
|
2848
|
+
let source_indentation = source.iter().take_while(|byte| is_indentation(byte)).count();
|
|
2849
|
+
let value_indentation = value.iter().take_while(|byte| is_indentation(byte)).count();
|
|
2850
|
+
if source_indentation <= value_indentation || !source[..source_indentation].ends_with(&value[..value_indentation]) {
|
|
2851
|
+
return None;
|
|
2852
|
+
}
|
|
2853
|
+
let mut original = source[..source_indentation - value_indentation].to_vec();
|
|
2854
|
+
original.extend_from_slice(value);
|
|
2855
|
+
Some(original)
|
|
2856
|
+
}
|
|
2857
|
+
}
|
|
2858
|
+
|
|
2859
|
+
/// The target list a parenthesized target list consists of, as in
|
|
2860
|
+
/// `((a, b))`.
|
|
2861
|
+
fn sole_nested_multi_target<'pr>(target: &ruby_prism::MultiTargetNode<'pr>) -> Option<ruby_prism::MultiTargetNode<'pr>> {
|
|
2862
|
+
if target.rest().is_some() || target.rights().iter().next().is_some() {
|
|
2863
|
+
return None;
|
|
2864
|
+
}
|
|
2865
|
+
let mut lefts = target.lefts().iter();
|
|
2866
|
+
let only = lefts.next()?;
|
|
2867
|
+
if lefts.next().is_some() {
|
|
2868
|
+
return None;
|
|
2869
|
+
}
|
|
2870
|
+
only.as_multi_target_node()
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2873
|
+
fn is_numeric_literal(node: &Node<'_>) -> bool {
|
|
2874
|
+
matches!(node, Node::IntegerNode { .. } | Node::FloatNode { .. } | Node::RationalNode { .. } | Node::ImaginaryNode { .. })
|
|
2875
|
+
}
|
|
2876
|
+
|
|
2877
|
+
/// The start of the numeric literal an expression begins with, following
|
|
2878
|
+
/// call receivers (`1.abs.to_s` begins with `1`).
|
|
2879
|
+
fn leftmost_numeric_literal(node: &Node<'_>) -> Option<u32> {
|
|
2880
|
+
if is_numeric_literal(node) {
|
|
2881
|
+
return Some(node.location().start());
|
|
2882
|
+
}
|
|
2883
|
+
let call = node.as_call_node()?;
|
|
2884
|
+
let receiver = call.receiver()?;
|
|
2885
|
+
if receiver.location().start() != node.location().start() {
|
|
2886
|
+
return None;
|
|
2887
|
+
}
|
|
2888
|
+
leftmost_numeric_literal(&receiver)
|
|
2889
|
+
}
|
|
2890
|
+
|
|
2891
|
+
/// A numeric literal's value negated, in the literal's `to_s` form.
|
|
2892
|
+
fn negate_numeric(value: &Child) -> Child {
|
|
2893
|
+
fn toggle_sign(text: &str) -> String {
|
|
2894
|
+
match text.strip_prefix('-') {
|
|
2895
|
+
Some(positive) => positive.to_string(),
|
|
2896
|
+
None => format!("-{text}"),
|
|
2897
|
+
}
|
|
2898
|
+
}
|
|
2899
|
+
match value {
|
|
2900
|
+
Child::Int(text) if &**text == "0" => value.clone(),
|
|
2901
|
+
Child::Int(text) => Child::Int(toggle_sign(text).into()),
|
|
2902
|
+
Child::Float(number) => Child::Float(-number),
|
|
2903
|
+
Child::Rational(text) if text.starts_with("0/") => value.clone(),
|
|
2904
|
+
Child::Rational(text) => Child::Rational(toggle_sign(text).into()),
|
|
2905
|
+
Child::Complex(text) => {
|
|
2906
|
+
// `0+X` or `0-X`: the real part is always 0, which stays 0.
|
|
2907
|
+
let (real, imaginary) = text.split_at(1);
|
|
2908
|
+
let (sign, magnitude) = imaginary.split_at(1);
|
|
2909
|
+
let integer_zero = magnitude == "0i" || magnitude.starts_with("0/");
|
|
2910
|
+
let sign = match (sign, integer_zero) {
|
|
2911
|
+
(_, true) => sign,
|
|
2912
|
+
("+", false) => "-",
|
|
2913
|
+
_ => "+",
|
|
2914
|
+
};
|
|
2915
|
+
Child::Complex(format!("{real}{sign}{magnitude}").into())
|
|
2916
|
+
}
|
|
2917
|
+
other => panic!("not a numeric literal value: {other:?}"),
|
|
2918
|
+
}
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
/// Replaces each `\\` with `\`, left to right.
|
|
2922
|
+
fn unescape_backslashes(text: &[u8]) -> Vec<u8> {
|
|
2923
|
+
let mut out = Vec::with_capacity(text.len());
|
|
2924
|
+
let mut index = 0;
|
|
2925
|
+
while index < text.len() {
|
|
2926
|
+
out.push(text[index]);
|
|
2927
|
+
index += if text[index] == b'\\' && text.get(index + 1) == Some(&b'\\') { 2 } else { 1 };
|
|
2928
|
+
}
|
|
2929
|
+
out
|
|
2930
|
+
}
|
|
2931
|
+
|
|
2932
|
+
/// A method name as the parser gem gives it: the token's text for `~@` and
|
|
2933
|
+
/// `!@`, which Prism names `~` and `!`.
|
|
2934
|
+
fn operator_method_name(name: &[u8], token: Option<&[u8]>) -> Vec<u8> {
|
|
2935
|
+
match token {
|
|
2936
|
+
Some(token @ (b"~@" | b"!@")) => token.to_vec(),
|
|
2937
|
+
_ => name.to_vec(),
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
/// A regexp literal's string content as the parser gem's lexer builds it
|
|
2942
|
+
/// (`extend_string_escaped`): escapes stay as written, except that an
|
|
2943
|
+
/// escaped delimiter loses its backslash unless it is a regexp
|
|
2944
|
+
/// metacharacter, a backslash-newline disappears, and `\c`, `\C-` and `\M-`
|
|
2945
|
+
/// escapes become the bytes they denote. Prism writes those as `\xHH`.
|
|
2946
|
+
fn regexp_value(content: &[u8], opening: &[u8]) -> Vec<u8> {
|
|
2947
|
+
const REGEXP_META_CHARACTERS: &[u8] = b"\\$()*+.<>?[]^{|}";
|
|
2948
|
+
let start_delimiter = *opening.last().unwrap_or(&b'/');
|
|
2949
|
+
let end_delimiter = match start_delimiter {
|
|
2950
|
+
b'(' => b')',
|
|
2951
|
+
b'[' => b']',
|
|
2952
|
+
b'{' => b'}',
|
|
2953
|
+
b'<' => b'>',
|
|
2954
|
+
other => other,
|
|
2955
|
+
};
|
|
2956
|
+
let mut out = Vec::with_capacity(content.len());
|
|
2957
|
+
let mut index = 0;
|
|
2958
|
+
while index < content.len() {
|
|
2959
|
+
let byte = content[index];
|
|
2960
|
+
if byte != b'\\' || index + 1 >= content.len() {
|
|
2961
|
+
out.push(byte);
|
|
2962
|
+
index += 1;
|
|
2963
|
+
continue;
|
|
2964
|
+
}
|
|
2965
|
+
let escaped = content[index + 1];
|
|
2966
|
+
if escaped == b'\\' || escaped == start_delimiter || escaped == end_delimiter {
|
|
2967
|
+
if REGEXP_META_CHARACTERS.contains(&escaped) {
|
|
2968
|
+
out.extend_from_slice(&[b'\\', escaped]);
|
|
2969
|
+
} else {
|
|
2970
|
+
out.push(escaped);
|
|
2971
|
+
}
|
|
2972
|
+
index += 2;
|
|
2973
|
+
} else if escaped == b'\n' {
|
|
2974
|
+
index += 2;
|
|
2975
|
+
} else if matches!(escaped, b'c' | b'C' | b'M') {
|
|
2976
|
+
let (value, next) = control_meta_escape(content, index);
|
|
2977
|
+
out.push(value);
|
|
2978
|
+
index = next;
|
|
2979
|
+
} else {
|
|
2980
|
+
out.extend_from_slice(&[b'\\', escaped]);
|
|
2981
|
+
index += 2;
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
out
|
|
2985
|
+
}
|
|
2986
|
+
|
|
2987
|
+
/// Reads the character at `index`, decoding `\cX`, `\C-X` and `\M-X`
|
|
2988
|
+
/// (nested in any order) and simple escapes, and returns its byte and the
|
|
2989
|
+
/// index after it.
|
|
2990
|
+
fn control_meta_escape(content: &[u8], index: usize) -> (u8, usize) {
|
|
2991
|
+
let Some(&byte) = content.get(index) else { return (0, index) };
|
|
2992
|
+
if byte != b'\\' {
|
|
2993
|
+
return (byte, index + 1);
|
|
2994
|
+
}
|
|
2995
|
+
match (content.get(index + 1), content.get(index + 2)) {
|
|
2996
|
+
(Some(b'M'), Some(b'-')) => {
|
|
2997
|
+
let (value, next) = control_meta_escape(content, index + 3);
|
|
2998
|
+
(value | 0x80, next)
|
|
2999
|
+
}
|
|
3000
|
+
(Some(b'C'), Some(b'-')) => {
|
|
3001
|
+
let (value, next) = control_meta_escape(content, index + 3);
|
|
3002
|
+
(control(value), next)
|
|
3003
|
+
}
|
|
3004
|
+
(Some(b'c'), _) => {
|
|
3005
|
+
let (value, next) = control_meta_escape(content, index + 2);
|
|
3006
|
+
(control(value), next)
|
|
3007
|
+
}
|
|
3008
|
+
(Some(&escaped), _) => {
|
|
3009
|
+
let value = match escaped {
|
|
3010
|
+
b'n' => b'\n',
|
|
3011
|
+
b't' => b'\t',
|
|
3012
|
+
b'r' => b'\r',
|
|
3013
|
+
b'f' => 0x0c,
|
|
3014
|
+
b'v' => 0x0b,
|
|
3015
|
+
b'a' => 0x07,
|
|
3016
|
+
b'b' => 0x08,
|
|
3017
|
+
b'e' => 0x1b,
|
|
3018
|
+
b's' => b' ',
|
|
3019
|
+
other => other,
|
|
3020
|
+
};
|
|
3021
|
+
(value, index + 2)
|
|
3022
|
+
}
|
|
3023
|
+
(None, _) => (byte, index + 1),
|
|
3024
|
+
}
|
|
3025
|
+
}
|
|
3026
|
+
|
|
3027
|
+
/// A control character: `\c?` is DEL, otherwise the low five bits (and the
|
|
3028
|
+
/// meta bit).
|
|
3029
|
+
fn control(value: u8) -> u8 {
|
|
3030
|
+
if value == b'?' {
|
|
3031
|
+
0x7f
|
|
3032
|
+
} else {
|
|
3033
|
+
value & 0x9f
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
|
|
3037
|
+
/// `before[/(\\*)\r?\n$/, 1]&.length&.odd?`
|
|
3038
|
+
fn ends_with_odd_backslashes_newline(line: &[u8]) -> bool {
|
|
3039
|
+
let Some(rest) = line.strip_suffix(b"\n") else { return false };
|
|
3040
|
+
let rest = rest.strip_suffix(b"\r").unwrap_or(rest);
|
|
3041
|
+
let backslashes = rest.iter().rev().take_while(|b| **b == b'\\').count();
|
|
3042
|
+
backslashes % 2 == 1
|
|
3043
|
+
}
|
|
3044
|
+
|
|
3045
|
+
/// The number of `\n` escapes in a line, less one when the line ends with
|
|
3046
|
+
/// one (`string_nodes_from_line_continuations`).
|
|
3047
|
+
fn escaped_newline_count(line: &[u8]) -> usize {
|
|
3048
|
+
// line.scan(/(\\*)n/).count { |(backslashes)| backslashes&.length&.odd? }
|
|
3049
|
+
let mut count = 0;
|
|
3050
|
+
let mut index = 0;
|
|
3051
|
+
while index < line.len() {
|
|
3052
|
+
// Find the next match of /(\\*)n/ starting at index (leftmost).
|
|
3053
|
+
let mut position = index;
|
|
3054
|
+
let mut found = None;
|
|
3055
|
+
while position < line.len() {
|
|
3056
|
+
let mut cursor = position;
|
|
3057
|
+
while cursor < line.len() && line[cursor] == b'\\' {
|
|
3058
|
+
cursor += 1;
|
|
3059
|
+
}
|
|
3060
|
+
if cursor < line.len() && line[cursor] == b'n' {
|
|
3061
|
+
found = Some((position, cursor));
|
|
3062
|
+
break;
|
|
3063
|
+
}
|
|
3064
|
+
position += 1;
|
|
3065
|
+
}
|
|
3066
|
+
let Some((start, n_index)) = found else { break };
|
|
3067
|
+
if (n_index - start) % 2 == 1 {
|
|
3068
|
+
count += 1;
|
|
3069
|
+
}
|
|
3070
|
+
index = n_index + 1;
|
|
3071
|
+
}
|
|
3072
|
+
// line.match?(/(?:\A|[^\\])(?:\\\\)*\\n\z/)
|
|
3073
|
+
if count > 0 && line.ends_with(b"\\n") {
|
|
3074
|
+
let before = &line[..line.len() - 2];
|
|
3075
|
+
let backslashes = before.iter().rev().take_while(|b| **b == b'\\').count();
|
|
3076
|
+
if backslashes % 2 == 0 {
|
|
3077
|
+
count -= 1;
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
count
|
|
3081
|
+
}
|
|
3082
|
+
|
|
3083
|
+
trait LocExt {
|
|
3084
|
+
fn with_expression(self, expression: Option<Range>) -> Self;
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
impl LocExt for Loc {
|
|
3088
|
+
fn with_expression(mut self, expression: Option<Range>) -> Self {
|
|
3089
|
+
self.expression = expression;
|
|
3090
|
+
self
|
|
3091
|
+
}
|
|
3092
|
+
}
|
|
3093
|
+
|
|
3094
|
+
#[cfg(test)]
|
|
3095
|
+
mod tests {
|
|
3096
|
+
use super::*;
|
|
3097
|
+
|
|
3098
|
+
#[test]
|
|
3099
|
+
fn integer_text_formats_big_numbers() {
|
|
3100
|
+
assert_eq!(integer_text(false, &[0]), "0");
|
|
3101
|
+
assert_eq!(integer_text(true, &[5]), "-5");
|
|
3102
|
+
assert_eq!(integer_text(false, &[0, 1]), "4294967296");
|
|
3103
|
+
assert_eq!(integer_text(false, &[0x89e80000, 0x8ac72304]), "10000000000000000000");
|
|
3104
|
+
}
|
|
3105
|
+
|
|
3106
|
+
#[test]
|
|
3107
|
+
fn escaped_newlines() {
|
|
3108
|
+
assert_eq!(escaped_newline_count(b"a\\nb\n"), 1);
|
|
3109
|
+
assert_eq!(escaped_newline_count(b"a\\\\nb\n"), 0);
|
|
3110
|
+
assert_eq!(escaped_newline_count(b"a\\n"), 0);
|
|
3111
|
+
}
|
|
3112
|
+
}
|