mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-15 00:55:22 +00:00
better parser build experience
This commit is contained in:
parent
307bc46d74
commit
2bef1f02d2
1 changed files with 30 additions and 20 deletions
|
@ -16,22 +16,26 @@ fn main() -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn requires_lalrpop(source: &str, target: &str) -> bool {
|
fn requires_lalrpop(source: &str, target: &str) -> Option<String> {
|
||||||
let target = if let Ok(target) = File::open(target) {
|
let target = if let Ok(target) = File::open(target) {
|
||||||
target
|
target
|
||||||
} else {
|
} else {
|
||||||
println!("cargo:warning=python.rs doesn't exist. regenerate.");
|
return Some("python.rs doesn't exist. regenerate.".to_owned());
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let sha_prefix = "// sha3: ";
|
let sha_prefix = "// sha3: ";
|
||||||
let sha3_line = BufReader::with_capacity(128, target)
|
let sha3_line = if let Some(sha3_line) =
|
||||||
|
BufReader::with_capacity(128, target)
|
||||||
.lines()
|
.lines()
|
||||||
.find_map(|line| {
|
.find_map(|line| {
|
||||||
let line = line.unwrap();
|
let line = line.unwrap();
|
||||||
line.starts_with(sha_prefix).then_some(line)
|
line.starts_with(sha_prefix).then_some(line)
|
||||||
})
|
}) {
|
||||||
.expect("no sha3 line?");
|
sha3_line
|
||||||
|
} else {
|
||||||
|
// no sha3 line - maybe old version of lalrpop installed
|
||||||
|
return Some("python.rs doesn't include sha3 hash. regenerate.".to_owned());
|
||||||
|
};
|
||||||
let expected_sha3_str = sha3_line.strip_prefix(sha_prefix).unwrap();
|
let expected_sha3_str = sha3_line.strip_prefix(sha_prefix).unwrap();
|
||||||
|
|
||||||
let actual_sha3 = {
|
let actual_sha3 = {
|
||||||
|
@ -55,29 +59,35 @@ fn requires_lalrpop(source: &str, target: &str) -> bool {
|
||||||
};
|
};
|
||||||
let eq = sha_equal(expected_sha3_str, &actual_sha3);
|
let eq = sha_equal(expected_sha3_str, &actual_sha3);
|
||||||
if !eq {
|
if !eq {
|
||||||
println!("cargo:warning=python.rs hash expected: {expected_sha3_str}");
|
|
||||||
let mut actual_sha3_str = String::new();
|
let mut actual_sha3_str = String::new();
|
||||||
for byte in actual_sha3 {
|
for byte in actual_sha3 {
|
||||||
write!(actual_sha3_str, "{byte:02x}").unwrap();
|
write!(actual_sha3_str, "{byte:02x}").unwrap();
|
||||||
}
|
}
|
||||||
println!("cargo:warning=python.rs hash actual: {actual_sha3_str}");
|
return Some(format!(
|
||||||
|
"python.rs hash expected: {expected_sha3_str} but actual: {actual_sha3_str}"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
!eq
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_lalrpop(source: &str, target: &str) -> anyhow::Result<()> {
|
fn try_lalrpop(source: &str, target: &str) -> anyhow::Result<()> {
|
||||||
if !requires_lalrpop(source, target) {
|
let _message = if let Some(msg) = requires_lalrpop(source, target) {
|
||||||
|
msg
|
||||||
|
} else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
};
|
||||||
|
|
||||||
#[cfg(feature = "lalrpop")]
|
#[cfg(feature = "lalrpop")]
|
||||||
{
|
lalrpop::process_root().unwrap_or_else(|e| {
|
||||||
lalrpop::process_root().expect("running lalrpop failed");
|
println!("cargo:warning={_message}");
|
||||||
Ok(())
|
panic!("running lalrpop failed. {e:?}");
|
||||||
}
|
});
|
||||||
|
|
||||||
#[cfg(not(feature = "lalrpop"))]
|
#[cfg(not(feature = "lalrpop"))]
|
||||||
panic!("try: cargo build --manifest-path=compiler/parser/Cargo.toml --features=lalrpop");
|
{
|
||||||
|
println!("cargo:warning=try: cargo build --manifest-path=compiler/parser/Cargo.toml --features=lalrpop");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sha_equal(expected_sha3_str: &str, actual_sha3: &[u8; 32]) -> bool {
|
fn sha_equal(expected_sha3_str: &str, actual_sha3: &[u8; 32]) -> bool {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue