From 719b0edf46dc7fea11c652cfbf8fc89d00a2f15c Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Thu, 18 Aug 2022 19:09:22 +0900 Subject: [PATCH] lalrpop binary / feature dual support --- Cargo.lock | 125 ++++++++++++++++++++++++++++++++++++++++++++++ parser/Cargo.toml | 2 + parser/build.rs | 48 +++++++++++------- 3 files changed, 158 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 622d6f989..ab5d65c7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,6 +76,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbf56136a5198c7b01a49e3afcbef6cf84597273d298f54432926024107b0109" +[[package]] +name = "ascii-canvas" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6" +dependencies = [ + "term", +] + [[package]] name = "atomic" version = "0.5.1" @@ -117,6 +126,21 @@ dependencies = [ "serde", ] +[[package]] +name = "bit-set" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" + [[package]] name = "bitflags" version = "1.3.2" @@ -549,6 +573,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.3" @@ -605,6 +635,15 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "ena" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3" +dependencies = [ + "log", +] + [[package]] name = "encode_unicode" version = "0.3.6" @@ -655,6 +694,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "flame" version = "0.2.2" @@ -876,11 +921,37 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" +[[package]] +name = "lalrpop" +version = "0.19.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b30455341b0e18f276fa64540aff54deafb54c589de6aca68659c63dd2d5d823" +dependencies = [ + "ascii-canvas", + "atty", + "bit-set", + "diff", + "ena", + "itertools", + "lalrpop-util", + "petgraph", + "pico-args", + "regex", + "regex-syntax", + "string_cache", + "term", + "tiny-keccak", + "unicode-xid", +] + [[package]] name = "lalrpop-util" version = "0.19.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcf796c978e9b4d983414f4caedc9273aa33ee214c5b887bd55fde84c85d2dc4" +dependencies = [ + "regex", +] [[package]] name = "lazy_static" @@ -1078,6 +1149,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" + [[package]] name = "nibble_vec" version = "0.1.0" @@ -1284,6 +1361,16 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" +[[package]] +name = "petgraph" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143" +dependencies = [ + "fixedbitset", + "indexmap", +] + [[package]] name = "phf" version = "0.10.1" @@ -1322,6 +1409,12 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pico-args" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" + [[package]] name = "pkg-config" version = "0.3.22" @@ -1373,6 +1466,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "proc-macro-crate" version = "1.1.0" @@ -1737,7 +1836,9 @@ name = "rustpython-parser" version = "0.1.2" dependencies = [ "ahash", + "anyhow", "insta", + "lalrpop", "lalrpop-util", "log", "num-bigint", @@ -2141,6 +2242,19 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a" +[[package]] +name = "string_cache" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213494b7a2b503146286049378ce02b482200519accc31872ee8be91fa820a08" +dependencies = [ + "new_debug_unreachable", + "once_cell", + "parking_lot", + "phf_shared", + "precomputed-hash", +] + [[package]] name = "strsim" version = "0.8.0" @@ -2219,6 +2333,17 @@ version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9bffcddbc2458fa3e6058414599e3c838a022abae82e5c67b4f7f80298d5bff" +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + [[package]] name = "termcolor" version = "1.1.2" diff --git a/parser/Cargo.toml b/parser/Cargo.toml index bbc3b094d..5252422ae 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -11,6 +11,8 @@ edition = "2021" [build-dependencies] tiny-keccak = { version = "2", features = ["sha3"] } phf_codegen = "0.10" +lalrpop = { version = "0.19.8", optional = true } +anyhow = "1.0.45" [dependencies] rustpython-ast = { path = "../ast" } diff --git a/parser/build.rs b/parser/build.rs index 6eac57807..da3cbb9a0 100644 --- a/parser/build.rs +++ b/parser/build.rs @@ -1,21 +1,19 @@ use std::fmt::Write as _; use std::fs::File; -use std::io::{self, BufRead, BufReader, BufWriter, Write}; +use std::io::{BufRead, BufReader, BufWriter, Write}; use std::path::PathBuf; -use std::process::{Command, ExitCode}; use tiny_keccak::{Hasher, Sha3}; -fn main() -> ExitCode { +fn main() -> anyhow::Result<()> { const SOURCE: &str = "python.lalrpop"; const TARGET: &str = "python.rs"; println!("cargo:rerun-if-changed={SOURCE}"); - if let Err(exit_code) = try_lalrpop(SOURCE, TARGET) { - return exit_code; - } + try_lalrpop(SOURCE, TARGET)?; gen_phf(); - ExitCode::SUCCESS + + Ok(()) } fn requires_lalrpop(source: &str, target: &str) -> bool { @@ -67,28 +65,44 @@ fn requires_lalrpop(source: &str, target: &str) -> bool { !eq } -fn try_lalrpop(source: &str, target: &str) -> Result<(), ExitCode> { +fn try_lalrpop(source: &str, target: &str) -> anyhow::Result<()> { if !requires_lalrpop(source, target) { return Ok(()); } - match Command::new("lalrpop").arg(source).status() { + + #[cfg(feature = "lalrpop")] + lalrpop_dependency(); + #[cfg(not(feature = "lalrpop"))] + lalrpop_command(source)?; + + Ok(()) +} + +#[cfg(not(feature = "lalrpop"))] +fn lalrpop_command(source: &str) -> anyhow::Result<()> { + match std::process::Command::new("lalrpop").arg(source).status() { Ok(stat) if stat.success() => Ok(()), Ok(stat) => { eprintln!("failed to execute lalrpop; exited with {stat}"); let exit_code = stat.code().map(|v| (v % 256) as u8).unwrap_or(1); - Err(ExitCode::from(exit_code)) + Err(anyhow::anyhow!("lalrpop error status: {}", exit_code)) } - Err(e) if e.kind() == io::ErrorKind::NotFound => { - eprintln!( - "the lalrpop executable is not installed and parser/{source} has been changed" - ); - eprintln!("please install lalrpop with `cargo install lalrpop`"); - Err(ExitCode::FAILURE) + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + eprintln!("please install lalrpop with `cargo install lalrpop` or\n`cargo build --manifest-path=parser/Cargo.toml --features=lalrpop`"); + Err(anyhow::anyhow!( + "the lalrpop executable is not installed and parser/{} has been changed", + source + )) } - Err(e) => panic!("io error {e:#}"), + Err(e) => Err(anyhow::Error::new(e)), } } +#[cfg(feature = "lalrpop")] +fn lalrpop_dependency() { + lalrpop::process_root().unwrap() +} + fn sha_equal(expected_sha3_str: &str, actual_sha3: &[u8; 32]) -> bool { if expected_sha3_str.len() != 64 { panic!("lalrpop version? hash bug is fixed in 0.19.8");