Add support for env vars without braces or parentheses

This commit is contained in:
Aadi Desai 2022-05-12 16:03:27 +00:00 committed by GitHub
parent 6906c4fa60
commit 8ee39041ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 0 deletions

View file

@ -97,6 +97,7 @@ pub fn parse_file(path: impl AsRef<Path>) -> Result<Filelist, Box<dyn Error>> {
fn replace_env_vars(line: &str) -> String { fn replace_env_vars(line: &str) -> String {
let re_env_brace = Regex::new(r"\$\{(?P<env>[^}]+)\}").unwrap(); let re_env_brace = Regex::new(r"\$\{(?P<env>[^}]+)\}").unwrap();
let re_env_paren = Regex::new(r"\$\((?P<env>[^)]+)\)").unwrap(); let re_env_paren = Regex::new(r"\$\((?P<env>[^)]+)\)").unwrap();
let re_env_bare = Regex::new(r"\$(?P<env>[a-zA-Z_][a-zA-Z0-9_]*)").unwrap();
let mut expanded_line = String::from(line); let mut expanded_line = String::from(line);
for caps in re_env_brace.captures_iter(&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); 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 expanded_line
} }

View file

@ -7,5 +7,6 @@ testcase/file3.sv
+define+a=b+c=d+e=f +define+a=b+c=d+e=f
+define+$(VAR1)=var1 +define+$(VAR1)=var1
+define+${VAR2}=var2 +define+${VAR2}=var2
+define+$VAR3=var3
+define+RTL +define+RTL
-f testcase/files2.f -f testcase/files2.f

View file

@ -10,6 +10,7 @@ fn simple_test() {
defines.insert("c".to_string(), Some("d".to_string())); defines.insert("c".to_string(), Some("d".to_string()));
defines.insert("ENV_VAR1".to_string(), Some("var1".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_VAR2".to_string(), Some("var2".to_string()));
defines.insert("ENV_VAR3".to_string(), Some("var3".to_string()));
defines.insert("RTL".to_string(), None); defines.insert("RTL".to_string(), None);
let filelist_exp = verilog_filelist_parser::Filelist { let filelist_exp = verilog_filelist_parser::Filelist {
@ -28,6 +29,7 @@ fn simple_test() {
// Add env vars // Add env vars
std::env::set_var("VAR1", "ENV_VAR1"); std::env::set_var("VAR1", "ENV_VAR1");
std::env::set_var("VAR2", "ENV_VAR2"); 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"); let filelist = verilog_filelist_parser::parse_file("testcase/files.f").expect("Error parsing");
assert_eq!(filelist_exp, filelist); assert_eq!(filelist_exp, filelist);