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,101 @@
|
|
|
1
|
+
//! The Rust side of the Ruby2Ruby parity check (`parity/ruby2ruby_run.sh`):
|
|
2
|
+
//! writes, for every sexp of every file's translated tree, the record
|
|
3
|
+
//! `parity/ruby2ruby_oracle.rb` writes for `Flay#sexp_to_rb`.
|
|
4
|
+
//!
|
|
5
|
+
//! FLAY_RS_RUBY2RUBY_FILES=LIST FLAY_RS_RUBY2RUBY_OUT=OUT \
|
|
6
|
+
//! [FLAY_RS_RUBY2RUBY_FULL=1] [FLAY_RS_RUBY_VERSION=3.3.14] \
|
|
7
|
+
//! cargo test -p flay-core --release --test ruby2ruby_parity -- --ignored
|
|
8
|
+
//!
|
|
9
|
+
//! LIST holds one path per line. See `parity/ruby2ruby_oracle.rb` for the
|
|
10
|
+
//! record format.
|
|
11
|
+
|
|
12
|
+
use std::fmt::Write as _;
|
|
13
|
+
|
|
14
|
+
use flay_core::parse_source;
|
|
15
|
+
use flay_core::ruby2ruby::{sexp_to_ruby, Comments};
|
|
16
|
+
use flay_core::sexp::{SexpId, Tree, Value};
|
|
17
|
+
|
|
18
|
+
#[test]
|
|
19
|
+
#[ignore = "run by parity/ruby2ruby_run.sh"]
|
|
20
|
+
fn ruby2ruby_records() {
|
|
21
|
+
let list = std::env::var("FLAY_RS_RUBY2RUBY_FILES").expect("FLAY_RS_RUBY2RUBY_FILES");
|
|
22
|
+
let out = std::env::var("FLAY_RS_RUBY2RUBY_OUT").expect("FLAY_RS_RUBY2RUBY_OUT");
|
|
23
|
+
let full = std::env::var("FLAY_RS_RUBY2RUBY_FULL").is_ok_and(|value| value == "1");
|
|
24
|
+
let ruby_version = std::env::var("FLAY_RS_RUBY_VERSION").unwrap_or_else(|_| "latest".to_string());
|
|
25
|
+
let text = std::fs::read_to_string(&list).expect("file list");
|
|
26
|
+
let paths: Vec<&str> = text.lines().filter(|line| !line.is_empty()).collect();
|
|
27
|
+
|
|
28
|
+
// Files in parallel; deep trees recurse deeply.
|
|
29
|
+
let threads = std::thread::available_parallelism().map_or(4, |count| count.get());
|
|
30
|
+
let chunk_size = paths.len().div_ceil(threads).max(1);
|
|
31
|
+
let chunks: Vec<String> = std::thread::scope(|scope| {
|
|
32
|
+
let handles: Vec<_> = paths
|
|
33
|
+
.chunks(chunk_size)
|
|
34
|
+
.map(|chunk| {
|
|
35
|
+
let ruby_version = &ruby_version;
|
|
36
|
+
std::thread::Builder::new()
|
|
37
|
+
.stack_size(256 << 20)
|
|
38
|
+
.spawn_scoped(scope, move || {
|
|
39
|
+
chunk.iter().map(|path| file_records(path, ruby_version, full)).collect::<String>()
|
|
40
|
+
})
|
|
41
|
+
.expect("thread")
|
|
42
|
+
})
|
|
43
|
+
.collect();
|
|
44
|
+
handles.into_iter().map(|handle| handle.join().expect("records")).collect()
|
|
45
|
+
});
|
|
46
|
+
std::fs::write(out, chunks.concat()).expect("write records");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// The records of one file: `# PATH`, then `skipped` or one line per sexp
|
|
50
|
+
/// in pre-order.
|
|
51
|
+
fn file_records(path: &str, ruby_version: &str, full: bool) -> String {
|
|
52
|
+
let mut out = format!("# {path}\n");
|
|
53
|
+
let source = std::fs::read(path).unwrap_or_default();
|
|
54
|
+
let parsed = parse_source(&source, path, ruby_version);
|
|
55
|
+
let Ok((tree, Value::Sexp(root))) = &parsed.outcome else {
|
|
56
|
+
out.push_str("skipped\n");
|
|
57
|
+
return out;
|
|
58
|
+
};
|
|
59
|
+
let comments = Comments::new(&source, tree);
|
|
60
|
+
for (index, sexp) in preorder(tree, *root).into_iter().enumerate() {
|
|
61
|
+
let elements = &tree.get(sexp).elements;
|
|
62
|
+
let record = match sexp_to_ruby(tree, &comments, sexp, elements) {
|
|
63
|
+
Ok(text) => {
|
|
64
|
+
let encoding = if text.bytes.is_ascii() { "-" } else { text.encoding };
|
|
65
|
+
let mut line = format!("ok {:08x} {} {}", crc32fast::hash(&text.bytes), text.bytes.len(), encoding);
|
|
66
|
+
if full {
|
|
67
|
+
line.push(' ');
|
|
68
|
+
line.push_str(&escaped(&text.bytes));
|
|
69
|
+
}
|
|
70
|
+
line
|
|
71
|
+
}
|
|
72
|
+
Err(exception) => format!("raise {}: {}", exception.class, escaped(exception.message.as_bytes())),
|
|
73
|
+
};
|
|
74
|
+
writeln!(out, "{index} {} {record}", escaped(tree.type_name(sexp))).expect("write to String");
|
|
75
|
+
}
|
|
76
|
+
out
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// Every sexp from `root`, in pre-order.
|
|
80
|
+
fn preorder(tree: &Tree, root: SexpId) -> Vec<SexpId> {
|
|
81
|
+
let mut order = Vec::new();
|
|
82
|
+
let mut stack = vec![root];
|
|
83
|
+
while let Some(sexp) = stack.pop() {
|
|
84
|
+
order.push(sexp);
|
|
85
|
+
stack.extend(tree.each_sexp(sexp).collect::<Vec<_>>().into_iter().rev());
|
|
86
|
+
}
|
|
87
|
+
order
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Bytes 0x20..0x7E except `\` as they are, every other byte as `\xHH`.
|
|
91
|
+
fn escaped(bytes: &[u8]) -> String {
|
|
92
|
+
let mut text = String::with_capacity(bytes.len());
|
|
93
|
+
for byte in bytes {
|
|
94
|
+
if (0x20..0x7f).contains(byte) && *byte != b'\\' {
|
|
95
|
+
text.push(*byte as char);
|
|
96
|
+
} else {
|
|
97
|
+
write!(text, "\\x{byte:02X}").expect("write to String");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
text
|
|
101
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "flay_rs"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
authors.workspace = true
|
|
8
|
+
homepage.workspace = true
|
|
9
|
+
repository.workspace = true
|
|
10
|
+
publish = false
|
|
11
|
+
|
|
12
|
+
[lib]
|
|
13
|
+
crate-type = ["cdylib"]
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
flay-core = { path = "../../crates/flay-core" }
|
|
17
|
+
magnus = "0.9"
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
//! Converts the entries of Flay's `option[:filters]` (`Sexp::Matcher`s built
|
|
2
|
+
//! with `Sexp::Matcher.parse` or the `s{}` DSL, or whatever `parse`
|
|
3
|
+
//! returned) into flay-core [`Filter`]s, by writing each as the text
|
|
4
|
+
//! `Filter::from_serialized` reads (see
|
|
5
|
+
//! `crates/flay-core/src/matcher/serialized.rs`).
|
|
6
|
+
//!
|
|
7
|
+
//! Raises ArgumentError for objects flay-core cannot hold: plain Sexps,
|
|
8
|
+
//! Arrays, Hashes, subclasses of the core classes and of the
|
|
9
|
+
//! `Sexp::Matcher` classes, and Ranges with bounds other than Integers and
|
|
10
|
+
//! nil.
|
|
11
|
+
|
|
12
|
+
use flay_core::matcher::Filter;
|
|
13
|
+
use magnus::{prelude::*, value::ReprValue, Error, Float, Integer, RArray, RClass, RString, Ruby, Symbol, Value};
|
|
14
|
+
|
|
15
|
+
/// The classes of the values in a translated tree, in the order of
|
|
16
|
+
/// `TREE_CLASSES` in `crates/flay-core/src/matcher.rs`.
|
|
17
|
+
const TREE_CLASSES: [&str; 13] = [
|
|
18
|
+
"NilClass", "TrueClass", "FalseClass", "Symbol", "String", "Integer", "Float", "Rational", "Complex", "Range",
|
|
19
|
+
"Regexp", "Array", "Sexp",
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/// The value classes a filter may hold (by exact class).
|
|
23
|
+
const VALUE_CLASSES: [&str; 11] = [
|
|
24
|
+
"NilClass", "TrueClass", "FalseClass", "Integer", "Float", "Rational", "Complex", "Range", "Symbol", "String",
|
|
25
|
+
"Regexp",
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/// Each `Sexp::Matcher` class (by exact class) and the attributes its
|
|
29
|
+
/// `satisfy?` reads; `None` for its elements.
|
|
30
|
+
const MATCHER_ATTRIBUTES: [(&str, Option<&[&str]>); 13] = [
|
|
31
|
+
("Sexp::Matcher", None),
|
|
32
|
+
("Sexp::Wild", Some(&[])),
|
|
33
|
+
("Sexp::Remaining", Some(&[])),
|
|
34
|
+
("Sexp::Atom", Some(&[])),
|
|
35
|
+
("Sexp::Any", Some(&["options"])),
|
|
36
|
+
("Sexp::All", Some(&["options"])),
|
|
37
|
+
("Sexp::Not", Some(&["value"])),
|
|
38
|
+
("Sexp::Child", Some(&["child"])),
|
|
39
|
+
("Sexp::Include", Some(&["value"])),
|
|
40
|
+
("Sexp::Pattern", Some(&["pattern"])),
|
|
41
|
+
("Sexp::Klass", Some(&["pattern"])),
|
|
42
|
+
("Sexp::Type", Some(&["sexp_type"])),
|
|
43
|
+
("Sexp::Sibling", Some(&["subject", "sibling", "distance"])),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
/// `option[:filters]` as flay-core filters.
|
|
47
|
+
pub fn convert(ruby: &Ruby, filters: Value) -> Result<Vec<Filter>, Error> {
|
|
48
|
+
let filters: RArray = filters.funcall("to_a", ())?;
|
|
49
|
+
let mut converted = Vec::with_capacity(filters.len());
|
|
50
|
+
for filter in filters {
|
|
51
|
+
let text = dump(ruby, filter)?;
|
|
52
|
+
let filter = Filter::from_serialized(&text)
|
|
53
|
+
.map_err(|exception| crate::ruby_exception(ruby, &exception))?;
|
|
54
|
+
converted.push(filter);
|
|
55
|
+
}
|
|
56
|
+
Ok(converted)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// One object as text.
|
|
60
|
+
fn dump(ruby: &Ruby, object: Value) -> Result<String, Error> {
|
|
61
|
+
if let Some(matcher_class) = sexp_matcher_class(ruby) {
|
|
62
|
+
if object.is_kind_of(matcher_class) {
|
|
63
|
+
return dump_matcher(ruby, object);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if object.is_kind_of(ruby.class_module()) {
|
|
67
|
+
return dump_module(ruby, object);
|
|
68
|
+
}
|
|
69
|
+
let class_name = class_name(object)?;
|
|
70
|
+
if !VALUE_CLASSES.contains(&class_name.as_str()) {
|
|
71
|
+
return Err(cannot_convert(&format!("a {class_name}"), object));
|
|
72
|
+
}
|
|
73
|
+
Ok(match class_name.as_str() {
|
|
74
|
+
"NilClass" => "nil".into(),
|
|
75
|
+
"TrueClass" => "true".into(),
|
|
76
|
+
"FalseClass" => "false".into(),
|
|
77
|
+
"Integer" => format!("i:{}", object.funcall::<_, _, String>("to_s", ())?),
|
|
78
|
+
"Float" => format!("f:{:016x}", Float::try_convert(object)?.to_f64().to_bits()),
|
|
79
|
+
"Rational" => {
|
|
80
|
+
let numerator: Integer = object.funcall("numerator", ())?;
|
|
81
|
+
let denominator: Integer = object.funcall("denominator", ())?;
|
|
82
|
+
format!("rational:{numerator}:{denominator}")
|
|
83
|
+
}
|
|
84
|
+
"Complex" => {
|
|
85
|
+
let real = dump(ruby, object.funcall("real", ())?)?;
|
|
86
|
+
let imaginary = dump(ruby, object.funcall("imaginary", ())?)?;
|
|
87
|
+
format!("(complex {real} {imaginary})")
|
|
88
|
+
}
|
|
89
|
+
"Range" => {
|
|
90
|
+
let first = dump_bound(ruby, object.funcall("begin", ())?)?;
|
|
91
|
+
let last = dump_bound(ruby, object.funcall("end", ())?)?;
|
|
92
|
+
let exclusive: bool = object.funcall("exclude_end?", ())?;
|
|
93
|
+
format!("(range {first} {last} {exclusive})")
|
|
94
|
+
}
|
|
95
|
+
"Symbol" => {
|
|
96
|
+
let name: RString = Symbol::try_convert(object)?.funcall("to_s", ())?;
|
|
97
|
+
format!("sym:{}:{}", encoding_name(object)?, hex_bytes(name))
|
|
98
|
+
}
|
|
99
|
+
"String" => format!("str:{}:{}", encoding_name(object)?, hex_bytes(RString::try_convert(object)?)),
|
|
100
|
+
"Regexp" => {
|
|
101
|
+
let options: i64 = object.funcall("options", ())?;
|
|
102
|
+
let source: RString = object.funcall("source", ())?;
|
|
103
|
+
format!("regexp:{}:{options}:{}", encoding_name(object)?, hex_bytes(source))
|
|
104
|
+
}
|
|
105
|
+
_ => unreachable!("VALUE_CLASSES lists the classes handled"),
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// `Sexp::Matcher`, when sexp_processor is loaded.
|
|
110
|
+
fn sexp_matcher_class(ruby: &Ruby) -> Option<RClass> {
|
|
111
|
+
let sexp: RClass = ruby.class_object().const_get("Sexp").ok()?;
|
|
112
|
+
sexp.const_get("Matcher").ok()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// A matcher: its class name without `Sexp::`, then the objects its
|
|
116
|
+
/// `satisfy?` reads.
|
|
117
|
+
fn dump_matcher(ruby: &Ruby, matcher: Value) -> Result<String, Error> {
|
|
118
|
+
let class_name = class_name(matcher)?;
|
|
119
|
+
let Some((_, attributes)) = MATCHER_ATTRIBUTES.iter().find(|(name, _)| *name == class_name) else {
|
|
120
|
+
return Err(cannot_convert(&format!("a {class_name}"), matcher));
|
|
121
|
+
};
|
|
122
|
+
let mut objects: Vec<Value> = Vec::new();
|
|
123
|
+
match attributes {
|
|
124
|
+
// `each.to_a`: `Sexp#to_a` also converts the elements.
|
|
125
|
+
None => objects.extend(matcher.funcall::<_, _, Value>("each", ())?.funcall::<_, _, RArray>("to_a", ())?),
|
|
126
|
+
Some(attributes) => {
|
|
127
|
+
for attribute in *attributes {
|
|
128
|
+
let value: Value = matcher.funcall(*attribute, ())?;
|
|
129
|
+
if *attribute == "options" {
|
|
130
|
+
objects.extend(RArray::try_convert(value)?);
|
|
131
|
+
} else {
|
|
132
|
+
objects.push(value);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
let mut parts = vec![class_name.trim_start_matches("Sexp::").to_string()];
|
|
138
|
+
for object in objects {
|
|
139
|
+
parts.push(dump(ruby, object)?);
|
|
140
|
+
}
|
|
141
|
+
Ok(format!("({})", parts.join(" ")))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// A class or module: whether it is a class, which tree classes are it or
|
|
145
|
+
/// inherit from it (`klass <= mod`), and its name.
|
|
146
|
+
fn dump_module(ruby: &Ruby, module: Value) -> Result<String, Error> {
|
|
147
|
+
let kind = if module.is_kind_of(ruby.class_class()) { "class" } else { "module" };
|
|
148
|
+
let mut tree_classes = 0u32;
|
|
149
|
+
for (index, name) in TREE_CLASSES.iter().enumerate() {
|
|
150
|
+
// `Sexp` is the only one that may be missing; a matcher needs it.
|
|
151
|
+
let class: Value = ruby.class_object().const_get(*name)?;
|
|
152
|
+
if class.funcall::<_, _, Value>("<=", (module,))?.to_bool() {
|
|
153
|
+
tree_classes |= 1 << index;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
let name: Value = module.funcall("name", ())?;
|
|
157
|
+
let name: RString = if name.is_nil() { module.funcall("inspect", ())? } else { RString::try_convert(name)? };
|
|
158
|
+
Ok(format!("{kind}:{tree_classes}:{}", hex_bytes(name)))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// A Range bound: nil or an Integer.
|
|
162
|
+
fn dump_bound(ruby: &Ruby, bound: Value) -> Result<String, Error> {
|
|
163
|
+
if bound.is_nil() || class_name(bound)? == "Integer" {
|
|
164
|
+
return dump(ruby, bound);
|
|
165
|
+
}
|
|
166
|
+
let inspect: String = bound.funcall("inspect", ())?;
|
|
167
|
+
Err(Error::new(Ruby::get_with(bound).exception_arg_error(), format!("flay_rs cannot convert a Range bound {inspect} in a filter")))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
fn class_name(object: Value) -> Result<String, Error> {
|
|
171
|
+
object.class().funcall("to_s", ())
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn encoding_name(object: Value) -> Result<String, Error> {
|
|
175
|
+
object.funcall::<_, _, Value>("encoding", ())?.funcall("name", ())
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn hex_bytes(string: RString) -> String {
|
|
179
|
+
// SAFETY: the bytes are copied before any Ruby code runs.
|
|
180
|
+
unsafe { string.as_slice() }.iter().map(|byte| format!("{byte:02x}")).collect()
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
fn cannot_convert(what: &str, object: Value) -> Error {
|
|
184
|
+
let inspect: String = object.funcall("inspect", ()).unwrap_or_default();
|
|
185
|
+
Error::new(Ruby::get_with(object).exception_arg_error(), format!("flay_rs cannot convert {what} in a filter: {inspect}"))
|
|
186
|
+
}
|