flay-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 +495 -0
- data/Cargo.toml +23 -0
- data/DIVERGENCES.md +23 -0
- data/LICENSE +22 -0
- data/README.md +111 -0
- data/THIRD_PARTY_NOTICES.md +70 -0
- data/crates/flay-cli/Cargo.toml +22 -0
- data/crates/flay-cli/src/bin/flay-matcher-parity.rs +195 -0
- data/crates/flay-cli/src/main.rs +202 -0
- data/crates/flay-core/Cargo.toml +20 -0
- data/crates/flay-core/src/attach_comments.rs +353 -0
- data/crates/flay-core/src/erb.rs +418 -0
- data/crates/flay-core/src/lib.rs +488 -0
- data/crates/flay-core/src/matcher/parser.rs +345 -0
- data/crates/flay-core/src/matcher/regexp.rs +1243 -0
- data/crates/flay-core/src/matcher/ruby_text.rs +413 -0
- data/crates/flay-core/src/matcher/serialized.rs +340 -0
- data/crates/flay-core/src/matcher.rs +487 -0
- data/crates/flay-core/src/process.rs +312 -0
- data/crates/flay-core/src/report.rs +567 -0
- data/crates/flay-core/src/ruby2ruby.rs +3291 -0
- data/crates/flay-core/src/ruby_format.rs +163 -0
- data/crates/flay-core/src/ruby_sort.rs +246 -0
- data/crates/flay-core/src/sexp.rs +639 -0
- data/crates/flay-core/src/translate.rs +2290 -0
- data/crates/flay-core/tests/ruby2ruby_parity.rs +101 -0
- data/ext/flay_rs/Cargo.toml +17 -0
- data/ext/flay_rs/extconf.rb +6 -0
- data/ext/flay_rs/src/filters.rs +186 -0
- data/ext/flay_rs/src/lib.rs +544 -0
- data/lib/flay_rs/version.rb +8 -0
- data/lib/flay_rs.rb +18 -0
- metadata +94 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
//! Ruby's `to_s` and `inspect` for the values of a translated tree and the
|
|
2
|
+
//! objects of a filter, as `Sexp::Pattern#satisfy?` (`o.to_s`),
|
|
3
|
+
//! `Sexp.m` (`value.to_s`) and the parser's error messages (`%p`) use them.
|
|
4
|
+
//!
|
|
5
|
+
//! Strings are inspected as Ruby does with the default external encoding
|
|
6
|
+
//! UTF-8. Which non-ASCII characters are printable is approximated: control
|
|
7
|
+
//! characters, U+2028, U+2029 and noncharacters are not; unassigned code
|
|
8
|
+
//! points count as printable here, though Ruby escapes them.
|
|
9
|
+
|
|
10
|
+
use super::regexp::{self, escaped_character};
|
|
11
|
+
use super::{Matcher, Object, Result, TREE_CLASSES};
|
|
12
|
+
use crate::ruby_format::float_to_s;
|
|
13
|
+
use crate::sexp::{Encoding, Integer, RubyString, Tree, Value, UTF_8};
|
|
14
|
+
use crate::RubyException;
|
|
15
|
+
|
|
16
|
+
/// `value.to_s` for a value of `tree`.
|
|
17
|
+
pub fn to_s(tree: &Tree, value: &Value) -> Result<RubyString> {
|
|
18
|
+
let ascii = |text: String| RubyString::new(text, regexp::US_ASCII);
|
|
19
|
+
Ok(match value {
|
|
20
|
+
Value::Nil => ascii(String::new()),
|
|
21
|
+
Value::True => ascii("true".into()),
|
|
22
|
+
Value::False => ascii("false".into()),
|
|
23
|
+
Value::Symbol(id) => {
|
|
24
|
+
let (bytes, encoding) = tree.symbol(*id);
|
|
25
|
+
RubyString::new(bytes, encoding)
|
|
26
|
+
}
|
|
27
|
+
Value::String(string) => (**string).clone(),
|
|
28
|
+
Value::Integer(_) | Value::Float(_) | Value::Rational(_) | Value::Complex(_) => ascii(number_to_s(value)),
|
|
29
|
+
Value::Range(range) => {
|
|
30
|
+
let (first, last, exclusive) = &**range;
|
|
31
|
+
let bound = |bound: &Option<Integer>| bound.as_ref().map_or(String::new(), Integer::to_s);
|
|
32
|
+
ascii(format!("{}{}{}", bound(first), if *exclusive { "..." } else { ".." }, bound(last)))
|
|
33
|
+
}
|
|
34
|
+
Value::Regexp(regexp) => RubyString::new(regexp::to_s(regexp, Some(b'/'))?, regexp.encoding),
|
|
35
|
+
Value::Array(_) | Value::Sexp(_) => RubyString::new(inspect(tree, value)?, UTF_8),
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// `to_s` of an Integer, Float, Rational or Complex.
|
|
40
|
+
fn number_to_s(value: &Value) -> String {
|
|
41
|
+
match value {
|
|
42
|
+
Value::Integer(integer) => integer.to_s(),
|
|
43
|
+
Value::Float(float) => float_to_s(*float),
|
|
44
|
+
Value::Rational(rational) => format!("{}/{}", rational.0.to_s(), rational.1.to_s()),
|
|
45
|
+
Value::Complex(complex) => complex_to_s(&complex.0, &complex.1),
|
|
46
|
+
_ => unreachable!("number_to_s is only called with numbers"),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// `Complex#to_s` (`f_format`): the real part, the imaginary part's sign
|
|
51
|
+
/// and absolute value, `*` when that does not end in a digit, and `i`.
|
|
52
|
+
fn complex_to_s(real: &Value, imaginary: &Value) -> String {
|
|
53
|
+
let (positive, absolute) = match imaginary {
|
|
54
|
+
Value::Float(float) => (float.is_nan() || !float.is_sign_negative(), Value::Float(float.abs())),
|
|
55
|
+
Value::Integer(integer) => (!integer.negative, Value::integer(Integer::new(false, &integer.limbs))),
|
|
56
|
+
Value::Rational(rational) => {
|
|
57
|
+
let numerator = Integer::new(false, &rational.0.limbs);
|
|
58
|
+
(!rational.0.negative, Value::Rational(Box::new((numerator, rational.1.clone()))))
|
|
59
|
+
}
|
|
60
|
+
other => (true, other.clone()),
|
|
61
|
+
};
|
|
62
|
+
let mut text = number_to_s(real);
|
|
63
|
+
text.push(if positive { '+' } else { '-' });
|
|
64
|
+
text.push_str(&number_to_s(&absolute));
|
|
65
|
+
if !text.ends_with(|character: char| character.is_ascii_digit()) {
|
|
66
|
+
text.push('*');
|
|
67
|
+
}
|
|
68
|
+
text.push('i');
|
|
69
|
+
text
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// `value.inspect` for a value of `tree` (`Sexp#inspect` without the
|
|
73
|
+
/// `VERBOSE` line suffix).
|
|
74
|
+
pub fn inspect(tree: &Tree, value: &Value) -> Result<Vec<u8>> {
|
|
75
|
+
let joined = |values: &[Value], open: &str, close: &str| -> Result<Vec<u8>> {
|
|
76
|
+
let mut text = open.as_bytes().to_vec();
|
|
77
|
+
for (index, value) in values.iter().enumerate() {
|
|
78
|
+
if index > 0 {
|
|
79
|
+
text.extend(b", ");
|
|
80
|
+
}
|
|
81
|
+
text.extend(inspect(tree, value)?);
|
|
82
|
+
}
|
|
83
|
+
text.extend(close.bytes());
|
|
84
|
+
Ok(text)
|
|
85
|
+
};
|
|
86
|
+
Ok(match value {
|
|
87
|
+
Value::Nil => b"nil".to_vec(),
|
|
88
|
+
Value::Symbol(id) => {
|
|
89
|
+
let (bytes, encoding) = tree.symbol(*id);
|
|
90
|
+
symbol_inspect(bytes, encoding)?
|
|
91
|
+
}
|
|
92
|
+
Value::String(string) => string_inspect(&string.bytes, string.encoding)?,
|
|
93
|
+
Value::Rational(_) | Value::Complex(_) => format!("({})", number_to_s(value)).into_bytes(),
|
|
94
|
+
Value::Range(range) => {
|
|
95
|
+
// `Range#inspect` leaves out a nil end, or a nil start unless the
|
|
96
|
+
// end is nil too.
|
|
97
|
+
let (first, last, exclusive) = &**range;
|
|
98
|
+
let bound = |bound: &Option<Integer>| bound.as_ref().map_or("nil".to_string(), Integer::to_s);
|
|
99
|
+
let first = if first.is_some() || last.is_none() { bound(first) } else { String::new() };
|
|
100
|
+
let last = if last.is_some() || range.0.is_none() { bound(last) } else { String::new() };
|
|
101
|
+
format!("{first}{}{last}", if *exclusive { "..." } else { ".." }).into_bytes()
|
|
102
|
+
}
|
|
103
|
+
Value::Regexp(regexp) => regexp::inspect(regexp)?,
|
|
104
|
+
Value::Array(values) => joined(values, "[", "]")?,
|
|
105
|
+
Value::Sexp(id) => joined(&tree.get(*id).elements, "s(", ")")?,
|
|
106
|
+
other => to_s(tree, other)?.bytes,
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// `String#inspect` (`rb_str_inspect`).
|
|
111
|
+
pub fn string_inspect(bytes: &[u8], encoding: Encoding) -> Result<Vec<u8>> {
|
|
112
|
+
let unicode = encoding == UTF_8;
|
|
113
|
+
let mut text = b"\"".to_vec();
|
|
114
|
+
let mut index = 0;
|
|
115
|
+
while index < bytes.len() {
|
|
116
|
+
let start = index;
|
|
117
|
+
let character = if bytes[index] < 0x80 {
|
|
118
|
+
index += 1;
|
|
119
|
+
u32::from(bytes[start])
|
|
120
|
+
} else {
|
|
121
|
+
match regexp::character_length(bytes, index, encoding)? {
|
|
122
|
+
None => {
|
|
123
|
+
text.extend(format!("\\x{:02X}", bytes[index]).bytes());
|
|
124
|
+
index += 1;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
Some(length) => {
|
|
128
|
+
index += length;
|
|
129
|
+
regexp::code_point(bytes, start, length, encoding)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
let escape = match character {
|
|
134
|
+
0x22 | 0x5c => None,
|
|
135
|
+
0x23 if matches!(bytes.get(index), Some(b'$' | b'@' | b'{')) => None,
|
|
136
|
+
0x0a => Some("\\n"),
|
|
137
|
+
0x0d => Some("\\r"),
|
|
138
|
+
0x09 => Some("\\t"),
|
|
139
|
+
0x0c => Some("\\f"),
|
|
140
|
+
0x0b => Some("\\v"),
|
|
141
|
+
0x08 => Some("\\b"),
|
|
142
|
+
0x07 => Some("\\a"),
|
|
143
|
+
0x1b => Some("\\e"),
|
|
144
|
+
_ => {
|
|
145
|
+
let printable = if character < 0x80 {
|
|
146
|
+
(0x20..0x7f).contains(&character)
|
|
147
|
+
} else {
|
|
148
|
+
unicode && is_printable_non_ascii(character)
|
|
149
|
+
};
|
|
150
|
+
if printable {
|
|
151
|
+
text.extend_from_slice(&bytes[start..index]);
|
|
152
|
+
} else {
|
|
153
|
+
escaped_character(&mut text, character, unicode);
|
|
154
|
+
}
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
match escape {
|
|
159
|
+
Some(escape) => text.extend(escape.bytes()),
|
|
160
|
+
None => text.extend([b'\\', character as u8]),
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
text.push(b'"');
|
|
164
|
+
Ok(text)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Whether Onigmo counts a non-ASCII code point as printable (see the
|
|
168
|
+
/// module documentation).
|
|
169
|
+
fn is_printable_non_ascii(character: u32) -> bool {
|
|
170
|
+
!((0x80..=0x9f).contains(&character)
|
|
171
|
+
|| character == 0x2028
|
|
172
|
+
|| character == 0x2029
|
|
173
|
+
|| (0xfdd0..=0xfdef).contains(&character)
|
|
174
|
+
|| character & 0xfffe == 0xfffe)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// `Symbol#inspect`: `:name` when the name is one Ruby writes bare,
|
|
178
|
+
/// `:"..."` otherwise.
|
|
179
|
+
pub fn symbol_inspect(bytes: &[u8], encoding: Encoding) -> Result<Vec<u8>> {
|
|
180
|
+
let printable = std::str::from_utf8(bytes)
|
|
181
|
+
.is_ok_and(|text| text.chars().all(|character| character >= ' ' && is_printable_char(character)));
|
|
182
|
+
let bare = (encoding == UTF_8 || bytes.is_ascii()) && printable && is_symbol_name(bytes);
|
|
183
|
+
let mut text = b":".to_vec();
|
|
184
|
+
if bare {
|
|
185
|
+
text.extend_from_slice(bytes);
|
|
186
|
+
} else {
|
|
187
|
+
text.extend(string_inspect(bytes, encoding)?);
|
|
188
|
+
}
|
|
189
|
+
Ok(text)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
fn is_printable_char(character: char) -> bool {
|
|
193
|
+
let code = u32::from(character);
|
|
194
|
+
if code < 0x80 {
|
|
195
|
+
(0x20..0x7f).contains(&code)
|
|
196
|
+
} else {
|
|
197
|
+
is_printable_non_ascii(code)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
#[derive(Clone, Copy, PartialEq)]
|
|
202
|
+
enum IdType {
|
|
203
|
+
Local,
|
|
204
|
+
Const,
|
|
205
|
+
Global,
|
|
206
|
+
Instance,
|
|
207
|
+
Class,
|
|
208
|
+
Junk,
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/// `rb_enc_symname_type(name, len, enc, IDSET_ATTRSET_FOR_SYNTAX) != -1`:
|
|
212
|
+
/// whether `Symbol#inspect` writes the name without quotes.
|
|
213
|
+
fn is_symbol_name(name: &[u8]) -> bool {
|
|
214
|
+
symbol_name_type(name).is_some()
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
fn symbol_name_type(name: &[u8]) -> Option<IdType> {
|
|
218
|
+
let at = |index: usize| name.get(index).copied().unwrap_or(0);
|
|
219
|
+
let end = name.len();
|
|
220
|
+
let (mut index, id_type) = match *name.first()? {
|
|
221
|
+
b'$' => {
|
|
222
|
+
if is_special_global_name(&name[1..]) {
|
|
223
|
+
return Some(IdType::Global);
|
|
224
|
+
}
|
|
225
|
+
(1, IdType::Global)
|
|
226
|
+
}
|
|
227
|
+
b'@' if at(1) == b'@' => (2, IdType::Class),
|
|
228
|
+
b'@' => (1, IdType::Instance),
|
|
229
|
+
b'<' => {
|
|
230
|
+
let length = match (at(1), at(2)) {
|
|
231
|
+
(b'<', _) => 2,
|
|
232
|
+
(b'=', b'>') => 3,
|
|
233
|
+
(b'=', _) => 2,
|
|
234
|
+
_ => 1,
|
|
235
|
+
};
|
|
236
|
+
return (length == end).then_some(IdType::Junk);
|
|
237
|
+
}
|
|
238
|
+
b'>' => return (end == if matches!(at(1), b'>' | b'=') { 2 } else { 1 }).then_some(IdType::Junk),
|
|
239
|
+
b'=' => {
|
|
240
|
+
let length = match (at(1), at(2)) {
|
|
241
|
+
(b'~', _) => 2,
|
|
242
|
+
(b'=', b'=') => 3,
|
|
243
|
+
(b'=', _) => 2,
|
|
244
|
+
_ => return None,
|
|
245
|
+
};
|
|
246
|
+
return (length == end).then_some(IdType::Junk);
|
|
247
|
+
}
|
|
248
|
+
b'*' => return (end == if at(1) == b'*' { 2 } else { 1 }).then_some(IdType::Junk),
|
|
249
|
+
b'+' | b'-' => return (end == if at(1) == b'@' { 2 } else { 1 }).then_some(IdType::Junk),
|
|
250
|
+
b'|' | b'^' | b'&' | b'/' | b'%' | b'~' | b'`' => return (end == 1).then_some(IdType::Junk),
|
|
251
|
+
b'[' if at(1) == b']' => return (end == if at(2) == b'=' { 3 } else { 2 }).then_some(IdType::Junk),
|
|
252
|
+
b'[' => (0, IdType::Junk),
|
|
253
|
+
b'!' => {
|
|
254
|
+
if end == 1 {
|
|
255
|
+
return Some(IdType::Junk);
|
|
256
|
+
}
|
|
257
|
+
return (matches!(at(1), b'=' | b'~') && end == 2).then_some(IdType::Junk);
|
|
258
|
+
}
|
|
259
|
+
first if first.is_ascii_uppercase() => (0, IdType::Const),
|
|
260
|
+
_ => (0, IdType::Local),
|
|
261
|
+
};
|
|
262
|
+
let allowed_attrset = |id_type: IdType| matches!(id_type, IdType::Local | IdType::Const);
|
|
263
|
+
let first = at(index);
|
|
264
|
+
if index >= end || (first != b'_' && !first.is_ascii_alphabetic() && first.is_ascii()) {
|
|
265
|
+
if end > 1 && name[end - 1] == b'=' {
|
|
266
|
+
return symbol_name_type(&name[..end - 1]).filter(|id_type| allowed_attrset(*id_type)).map(|_| IdType::Junk);
|
|
267
|
+
}
|
|
268
|
+
return None;
|
|
269
|
+
}
|
|
270
|
+
while index < end && (name[index].is_ascii_alphanumeric() || name[index] == b'_' || !name[index].is_ascii()) {
|
|
271
|
+
index += 1;
|
|
272
|
+
}
|
|
273
|
+
if index >= end {
|
|
274
|
+
return Some(id_type);
|
|
275
|
+
}
|
|
276
|
+
let mut id_type = id_type;
|
|
277
|
+
match name[index] {
|
|
278
|
+
b'!' | b'?' => {
|
|
279
|
+
if matches!(id_type, IdType::Global | IdType::Class | IdType::Instance) {
|
|
280
|
+
return None;
|
|
281
|
+
}
|
|
282
|
+
index += 1;
|
|
283
|
+
if index + 1 < end || at(index) != b'=' {
|
|
284
|
+
return (index == end).then_some(IdType::Junk);
|
|
285
|
+
}
|
|
286
|
+
// `a?=`: an attribute writer of a junk name is not allowed.
|
|
287
|
+
return None;
|
|
288
|
+
}
|
|
289
|
+
b'=' => {
|
|
290
|
+
if !allowed_attrset(id_type) {
|
|
291
|
+
return None;
|
|
292
|
+
}
|
|
293
|
+
id_type = IdType::Junk;
|
|
294
|
+
index += 1;
|
|
295
|
+
}
|
|
296
|
+
_ => {}
|
|
297
|
+
}
|
|
298
|
+
(index == end).then_some(id_type)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/// `is_special_global_name`: `$~`, `$1`, `$-w` and the like.
|
|
302
|
+
fn is_special_global_name(name: &[u8]) -> bool {
|
|
303
|
+
const PUNCTUATION: &[u8] = b"~*$?!@/\\;,.=:<>\"&`'+0";
|
|
304
|
+
match name.first() {
|
|
305
|
+
None => false,
|
|
306
|
+
Some(first) if PUNCTUATION.contains(first) => name.len() == 1,
|
|
307
|
+
Some(b'-') => match name.get(1) {
|
|
308
|
+
None => false,
|
|
309
|
+
Some(second) if second.is_ascii_alphanumeric() || *second == b'_' => name.len() == 2,
|
|
310
|
+
Some(second) if !second.is_ascii() => std::str::from_utf8(&name[1..]).is_ok_and(|rest| rest.chars().count() == 1),
|
|
311
|
+
Some(_) => name.len() == 1,
|
|
312
|
+
},
|
|
313
|
+
Some(_) => name.iter().all(u8::is_ascii_digit),
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/// How `NoMethodError` names the receiver in Ruby 3.3: `nil`, `true`,
|
|
318
|
+
/// `false`, `class String`, `module Kernel`, `an instance of Symbol`.
|
|
319
|
+
pub fn receiver_description(object: &Object) -> String {
|
|
320
|
+
match object {
|
|
321
|
+
Object::Value(Value::Nil) => "nil".to_string(),
|
|
322
|
+
Object::Value(Value::True) => "true".to_string(),
|
|
323
|
+
Object::Value(Value::False) => "false".to_string(),
|
|
324
|
+
Object::Value(value) => format!("an instance of {}", TREE_CLASSES[super::tree_class(value)]),
|
|
325
|
+
Object::Module(module) => format!("{} {}", if module.is_class { "class" } else { "module" }, module.name),
|
|
326
|
+
Object::Matcher(_) => "an instance of Sexp::Matcher".to_string(),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// `object.inspect` for an object of a filter.
|
|
331
|
+
pub fn object_inspect(symbols: &Tree, object: &Object) -> Result<Vec<u8>> {
|
|
332
|
+
match object {
|
|
333
|
+
Object::Value(value) => inspect(symbols, value),
|
|
334
|
+
Object::Module(module) => Ok(module.name.clone().into_bytes()),
|
|
335
|
+
Object::Matcher(matcher) => matcher_inspect(symbols, matcher),
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/// `inspect` of each `Sexp::Matcher` class.
|
|
340
|
+
fn matcher_inspect(symbols: &Tree, matcher: &Matcher) -> Result<Vec<u8>> {
|
|
341
|
+
let joined = |objects: &[Object], separator: &str| -> Result<Vec<u8>> {
|
|
342
|
+
let mut text = Vec::new();
|
|
343
|
+
for (index, object) in objects.iter().enumerate() {
|
|
344
|
+
if index > 0 {
|
|
345
|
+
text.extend(separator.bytes());
|
|
346
|
+
}
|
|
347
|
+
text.extend(object_inspect(symbols, object)?);
|
|
348
|
+
}
|
|
349
|
+
Ok(text)
|
|
350
|
+
};
|
|
351
|
+
let wrapped = |name: &str, inner: Vec<u8>| [name.as_bytes(), b"(", &inner, b")"].concat();
|
|
352
|
+
Ok(match matcher {
|
|
353
|
+
Matcher::Matcher(elements) => wrapped("q", joined(elements, ", ")?),
|
|
354
|
+
Matcher::Wild => b"_".to_vec(),
|
|
355
|
+
Matcher::Remaining => b"___".to_vec(),
|
|
356
|
+
Matcher::Atom => b"atom".to_vec(),
|
|
357
|
+
Matcher::Any(options) => joined(options, " | ")?,
|
|
358
|
+
Matcher::All(options) => joined(options, " & ")?,
|
|
359
|
+
Matcher::Not(value) => wrapped("not?", object_inspect(symbols, value)?),
|
|
360
|
+
Matcher::Child(child) => wrapped("child", object_inspect(symbols, child)?),
|
|
361
|
+
Matcher::Include(value) => wrapped("include", object_inspect(symbols, value)?),
|
|
362
|
+
Matcher::Pattern(pattern) => wrapped("m", regexp::inspect(&pattern.regexp)?),
|
|
363
|
+
Matcher::Klass(pattern) => wrapped("k", format_argument(symbols, pattern)?),
|
|
364
|
+
Matcher::Type(sexp_type) => wrapped("t", format_argument(symbols, sexp_type)?),
|
|
365
|
+
Matcher::Sibling { subject, sibling, .. } => {
|
|
366
|
+
[object_inspect(symbols, subject)?, b" >> ".to_vec(), object_inspect(symbols, sibling)?].concat()
|
|
367
|
+
}
|
|
368
|
+
})
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/// `"%p" % object`: an Array (every matcher is one) gives its elements as
|
|
372
|
+
/// the arguments, so its first element is inspected.
|
|
373
|
+
fn format_argument(symbols: &Tree, object: &Object) -> Result<Vec<u8>> {
|
|
374
|
+
let too_few = || RubyException { class: "ArgumentError", message: "too few arguments".to_string() };
|
|
375
|
+
match object {
|
|
376
|
+
Object::Matcher(matcher) => match &**matcher {
|
|
377
|
+
Matcher::Matcher(elements) => object_inspect(symbols, elements.first().ok_or_else(too_few)?),
|
|
378
|
+
_ => Err(too_few()),
|
|
379
|
+
},
|
|
380
|
+
other => object_inspect(symbols, other),
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/// `object.to_s` for an object of a filter, as `Sexp.m` calls it.
|
|
385
|
+
/// `Sexp#to_s` is `Sexp#inspect` (`s(...)`), which builds an empty copy
|
|
386
|
+
/// with `self.class.new`: that raises for the classes whose `initialize`
|
|
387
|
+
/// takes arguments.
|
|
388
|
+
pub fn object_to_s(symbols: &Tree, object: &Object) -> Result<Vec<u8>> {
|
|
389
|
+
let wrong_arguments = |expected: &str| RubyException {
|
|
390
|
+
class: "ArgumentError",
|
|
391
|
+
message: format!("wrong number of arguments (given 0, expected {expected})"),
|
|
392
|
+
};
|
|
393
|
+
match object {
|
|
394
|
+
Object::Value(value) => Ok(to_s(symbols, value)?.bytes),
|
|
395
|
+
Object::Module(module) => Ok(module.name.clone().into_bytes()),
|
|
396
|
+
Object::Matcher(matcher) => match &**matcher {
|
|
397
|
+
Matcher::Matcher(elements) => {
|
|
398
|
+
let mut text = b"s(".to_vec();
|
|
399
|
+
for (index, element) in elements.iter().enumerate() {
|
|
400
|
+
if index > 0 {
|
|
401
|
+
text.extend(b", ");
|
|
402
|
+
}
|
|
403
|
+
text.extend(object_inspect(symbols, element)?);
|
|
404
|
+
}
|
|
405
|
+
text.push(b')');
|
|
406
|
+
Ok(text)
|
|
407
|
+
}
|
|
408
|
+
Matcher::Wild | Matcher::Remaining | Matcher::Atom | Matcher::Any(_) | Matcher::All(_) => Ok(b"s()".to_vec()),
|
|
409
|
+
Matcher::Sibling { .. } => Err(wrong_arguments("2..3")),
|
|
410
|
+
_ => Err(wrong_arguments("1")),
|
|
411
|
+
},
|
|
412
|
+
}
|
|
413
|
+
}
|