From 8ee39041ee12705ad66f5ddb2037874da1f2acbc Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Thu, 12 May 2022 16:03:27 +0000 Subject: [PATCH] Add support for env vars without braces or parentheses --- src/file_parser.rs | 7 +++++++ testcase/files.f | 1 + tests/integration_tests.rs | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/file_parser.rs b/src/file_parser.rs index f3e6c77..be50849 100644 --- a/src/file_parser.rs +++ b/src/file_parser.rs @@ -97,6 +97,7 @@ pub fn parse_file(path: impl AsRef) -> Result> { fn replace_env_vars(line: &str) -> String { let re_env_brace = Regex::new(r"\$\{(?P[^}]+)\}").unwrap(); let re_env_paren = Regex::new(r"\$\((?P[^)]+)\)").unwrap(); + let re_env_bare = Regex::new(r"\$(?P[a-zA-Z_][a-zA-Z0-9_]*)").unwrap(); let mut expanded_line = String::from(line); for caps in re_env_brace.captures_iter(&line) { @@ -111,5 +112,11 @@ fn replace_env_vars(line: &str) -> String { expanded_line = expanded_line.replace(&format!("$({})", env), &env_var); } } + for caps in re_env_bare.captures_iter(&line) { + let env = &caps["env"]; + if let Ok(env_var) = std::env::var(env) { + expanded_line = expanded_line.replace(&format!("${}", env), &env_var); + } + } expanded_line } diff --git a/testcase/files.f b/testcase/files.f index 83a5185..d250448 100644 --- a/testcase/files.f +++ b/testcase/files.f @@ -7,5 +7,6 @@ testcase/file3.sv +define+a=b+c=d+e=f +define+$(VAR1)=var1 +define+${VAR2}=var2 ++define+$VAR3=var3 +define+RTL -f testcase/files2.f \ No newline at end of file diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 15b4c63..3d1a700 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -10,6 +10,7 @@ fn simple_test() { defines.insert("c".to_string(), Some("d".to_string())); defines.insert("ENV_VAR1".to_string(), Some("var1".to_string())); defines.insert("ENV_VAR2".to_string(), Some("var2".to_string())); + defines.insert("ENV_VAR3".to_string(), Some("var3".to_string())); defines.insert("RTL".to_string(), None); let filelist_exp = verilog_filelist_parser::Filelist { @@ -28,6 +29,7 @@ fn simple_test() { // Add env vars std::env::set_var("VAR1", "ENV_VAR1"); std::env::set_var("VAR2", "ENV_VAR2"); + std::env::set_var("VAR3", "ENV_VAR3"); let filelist = verilog_filelist_parser::parse_file("testcase/files.f").expect("Error parsing"); assert_eq!(filelist_exp, filelist);