mirror of
https://github.com/supleed2/sv-filelist-parser.git
synced 2024-12-22 13:25:49 +00:00
Add support for env vars without braces or parentheses
This commit is contained in:
parent
6906c4fa60
commit
8ee39041ee
|
@ -97,6 +97,7 @@ pub fn parse_file(path: impl AsRef<Path>) -> Result<Filelist, Box<dyn Error>> {
|
|||
fn replace_env_vars(line: &str) -> String {
|
||||
let re_env_brace = 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);
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue