Skip to content

Commit 6ae1148

Browse files
committed
better parser build experience
1 parent 683e5af commit 6ae1148

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

compiler/parser/build.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ fn main() -> anyhow::Result<()> {
1616
Ok(())
1717
}
1818

19-
fn requires_lalrpop(source: &str, target: &str) -> bool {
19+
fn requires_lalrpop(source: &str, target: &str) -> Option<String> {
2020
let target = if let Ok(target) = File::open(target) {
2121
target
2222
} else {
23-
println!("cargo:warning=python.rs doesn't exist. regenerate.");
24-
return true;
23+
return Some("python.rs doesn't exist. regenerate.".to_owned());
2524
};
2625

2726
let sha_prefix = "// sha3: ";
28-
let sha3_line = BufReader::with_capacity(128, target)
29-
.lines()
30-
.find_map(|line| {
31-
let line = line.unwrap();
32-
line.starts_with(sha_prefix).then_some(line)
33-
})
34-
.expect("no sha3 line?");
27+
let sha3_line = if let Some(sha3_line) =
28+
BufReader::with_capacity(128, target)
29+
.lines()
30+
.find_map(|line| {
31+
let line = line.unwrap();
32+
line.starts_with(sha_prefix).then_some(line)
33+
}) {
34+
sha3_line
35+
} else {
36+
// no sha3 line - maybe old version of lalrpop installed
37+
return Some("python.rs doesn't include sha3 hash. regenerate.".to_owned());
38+
};
3539
let expected_sha3_str = sha3_line.strip_prefix(sha_prefix).unwrap();
3640

3741
let actual_sha3 = {
@@ -55,29 +59,35 @@ fn requires_lalrpop(source: &str, target: &str) -> bool {
5559
};
5660
let eq = sha_equal(expected_sha3_str, &actual_sha3);
5761
if !eq {
58-
println!("cargo:warning=python.rs hash expected: {expected_sha3_str}");
5962
let mut actual_sha3_str = String::new();
6063
for byte in actual_sha3 {
6164
write!(actual_sha3_str, "{byte:02x}").unwrap();
6265
}
63-
println!("cargo:warning=python.rs hash actual: {actual_sha3_str}");
66+
return Some(format!(
67+
"python.rs hash expected: {expected_sha3_str} but actual: {actual_sha3_str}"
68+
));
6469
}
65-
!eq
70+
None
6671
}
6772

6873
fn try_lalrpop(source: &str, target: &str) -> anyhow::Result<()> {
69-
if !requires_lalrpop(source, target) {
74+
let _message = if let Some(msg) = requires_lalrpop(source, target) {
75+
msg
76+
} else {
7077
return Ok(());
71-
}
78+
};
7279

7380
#[cfg(feature = "lalrpop")]
74-
{
75-
lalrpop::process_root().expect("running lalrpop failed");
76-
Ok(())
77-
}
81+
lalrpop::process_root().unwrap_or_else(|e| {
82+
println!("cargo:warning={_message}");
83+
panic!("running lalrpop failed. {e:?}");
84+
});
7885

7986
#[cfg(not(feature = "lalrpop"))]
80-
panic!("try: cargo build --manifest-path=compiler/parser/Cargo.toml --features=lalrpop");
87+
{
88+
println!("cargo:warning=try: cargo build --manifest-path=compiler/parser/Cargo.toml --features=lalrpop");
89+
}
90+
Ok(())
8191
}
8292

8393
fn sha_equal(expected_sha3_str: &str, actual_sha3: &[u8; 32]) -> bool {

0 commit comments

Comments
 (0)