From c962719206dc7f88839c836e27344f6829da2412 Mon Sep 17 00:00:00 2001 From: Raamakrishnan Date: Thu, 30 Jan 2020 21:44:45 +0530 Subject: [PATCH] Add support for defines w/o args, i.e. without "=" --- src/file_parser.rs | 9 ++++++--- src/line_parser.rs | 24 +++++++++++++++--------- testcase/files.f | 1 + tests/integration_tests.rs | 12 ++++++------ 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/file_parser.rs b/src/file_parser.rs index 2a497a2..8dd8afc 100644 --- a/src/file_parser.rs +++ b/src/file_parser.rs @@ -10,7 +10,7 @@ use crate::line_parser::LineType; pub struct Filelist { pub files: Vec, pub incdirs: Vec, - pub defines: HashMap, + pub defines: HashMap>, pub comments_present: bool, } @@ -42,8 +42,11 @@ pub fn parse_file(path: &str) -> Result> { match line_parser::parse_line(&line) { LineType::File(file) => filelist.files.push(file.to_string()), LineType::Define(define_map) => { - for define in define_map.into_iter() { - filelist.defines.insert(define.0.to_string(), define.1.to_string()); + for (d, t) in define_map.into_iter() { + match t { + Some(text) => filelist.defines.insert(d.to_string(), Some(text.to_string())), + None => filelist.defines.insert(d.to_string(), None) + }; } }, LineType::IncDir(incdirs) => { diff --git a/src/line_parser.rs b/src/line_parser.rs index f128639..a7f63fa 100644 --- a/src/line_parser.rs +++ b/src/line_parser.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; pub enum LineType<'a> { File(&'a str), IncDir(Vec<&'a str>), - Define(HashMap<&'a str, &'a str>), + Define(HashMap<&'a str, Option<&'a str>>), Filelist(&'a str), Comment, // Unknown, @@ -20,8 +20,12 @@ pub fn parse_line(line: &str) -> LineType { let defines = line.trim_start_matches("+define+").trim_end_matches('+'); let mut define_map = HashMap::new(); for define in defines.split('+') { - let split: Vec<&str> = define.splitn(2, '=').collect(); - define_map.insert(split[0], split[1]); + if let Some(pos) = define.find("=") { + let (d, t) = define.split_at(pos); + define_map.insert(d, Some(&t[1..])); + } else { + define_map.insert(define, None); + } } LineType::Define(define_map) } else if line.starts_with("+incdir+") { @@ -49,19 +53,21 @@ mod test { #[test] fn parse_line_define_single() { - let line = "+define+CONST1=const1=23\n"; + let line = "+define+CONST1=const1=23+\n"; let mut define_map = HashMap::new(); - define_map.insert("CONST1", "const1=23"); + define_map.insert("CONST1", Some("const1=23")); assert_eq!(parse_line(line), LineType::Define(define_map)); } #[test] fn parse_line_define_multiple() { - let line = "+define+CONST1=const1+CONST2=const2+CONST3=const3=1+\n"; + let line = "+define+CONST1=const1+CONST2=const2+CONST3=const3=1+CONST4+CONST5+\n"; let mut define_map = HashMap::new(); - define_map.insert("CONST1", "const1"); - define_map.insert("CONST2", "const2"); - define_map.insert("CONST3", "const3=1"); + define_map.insert("CONST1", Some("const1")); + define_map.insert("CONST2", Some("const2")); + define_map.insert("CONST3", Some("const3=1")); + define_map.insert("CONST4", None); + define_map.insert("CONST5", None); assert_eq!(parse_line(line), LineType::Define(define_map)); } diff --git a/testcase/files.f b/testcase/files.f index 4eecee6..52cce6e 100644 --- a/testcase/files.f +++ b/testcase/files.f @@ -6,4 +6,5 @@ testcase/file3.sv +define+a=b+c=d+e=f +define+$(VAR1)=var1 +define+${VAR2}=var2 ++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 93a0723..6e95fcd 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -4,12 +4,12 @@ use std::collections::HashMap; #[test] fn simple_test() { let mut defines = HashMap::new(); - defines.insert("a".to_string(), "bad".to_string()); - defines.insert("e".to_string(), "f".to_string()); - defines.insert("c".to_string(), "d".to_string()); - defines.insert("ENV_VAR1".to_string(), "var1".to_string()); - defines.insert("ENV_VAR2".to_string(), "var2".to_string()); - + defines.insert("a".to_string(), Some("bad".to_string())); + defines.insert("e".to_string(), Some("f".to_string())); + 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("RTL".to_string(), None); let filelist_exp = sv_filelist_parser::Filelist { files : vec!["testcase/file1.sv".to_string(),