mirror of
https://github.com/supleed2/sv-filelist-parser.git
synced 2024-12-22 21:35:49 +00:00
Add support for defines w/o args, i.e. without "="
This commit is contained in:
parent
f8086d39aa
commit
c962719206
|
@ -10,7 +10,7 @@ use crate::line_parser::LineType;
|
||||||
pub struct Filelist {
|
pub struct Filelist {
|
||||||
pub files: Vec<String>,
|
pub files: Vec<String>,
|
||||||
pub incdirs: Vec<String>,
|
pub incdirs: Vec<String>,
|
||||||
pub defines: HashMap<String, String>,
|
pub defines: HashMap<String, Option<String>>,
|
||||||
pub comments_present: bool,
|
pub comments_present: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,8 +42,11 @@ pub fn parse_file(path: &str) -> Result<Filelist, Box<dyn Error>> {
|
||||||
match line_parser::parse_line(&line) {
|
match line_parser::parse_line(&line) {
|
||||||
LineType::File(file) => filelist.files.push(file.to_string()),
|
LineType::File(file) => filelist.files.push(file.to_string()),
|
||||||
LineType::Define(define_map) => {
|
LineType::Define(define_map) => {
|
||||||
for define in define_map.into_iter() {
|
for (d, t) in define_map.into_iter() {
|
||||||
filelist.defines.insert(define.0.to_string(), define.1.to_string());
|
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) => {
|
LineType::IncDir(incdirs) => {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
||||||
pub enum LineType<'a> {
|
pub enum LineType<'a> {
|
||||||
File(&'a str),
|
File(&'a str),
|
||||||
IncDir(Vec<&'a str>),
|
IncDir(Vec<&'a str>),
|
||||||
Define(HashMap<&'a str, &'a str>),
|
Define(HashMap<&'a str, Option<&'a str>>),
|
||||||
Filelist(&'a str),
|
Filelist(&'a str),
|
||||||
Comment,
|
Comment,
|
||||||
// Unknown,
|
// Unknown,
|
||||||
|
@ -20,8 +20,12 @@ pub fn parse_line(line: &str) -> LineType {
|
||||||
let defines = line.trim_start_matches("+define+").trim_end_matches('+');
|
let defines = line.trim_start_matches("+define+").trim_end_matches('+');
|
||||||
let mut define_map = HashMap::new();
|
let mut define_map = HashMap::new();
|
||||||
for define in defines.split('+') {
|
for define in defines.split('+') {
|
||||||
let split: Vec<&str> = define.splitn(2, '=').collect();
|
if let Some(pos) = define.find("=") {
|
||||||
define_map.insert(split[0], split[1]);
|
let (d, t) = define.split_at(pos);
|
||||||
|
define_map.insert(d, Some(&t[1..]));
|
||||||
|
} else {
|
||||||
|
define_map.insert(define, None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LineType::Define(define_map)
|
LineType::Define(define_map)
|
||||||
} else if line.starts_with("+incdir+") {
|
} else if line.starts_with("+incdir+") {
|
||||||
|
@ -49,19 +53,21 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_line_define_single() {
|
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();
|
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));
|
assert_eq!(parse_line(line), LineType::Define(define_map));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_line_define_multiple() {
|
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();
|
let mut define_map = HashMap::new();
|
||||||
define_map.insert("CONST1", "const1");
|
define_map.insert("CONST1", Some("const1"));
|
||||||
define_map.insert("CONST2", "const2");
|
define_map.insert("CONST2", Some("const2"));
|
||||||
define_map.insert("CONST3", "const3=1");
|
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));
|
assert_eq!(parse_line(line), LineType::Define(define_map));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@ 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+RTL
|
||||||
-f testcase/files2.f
|
-f testcase/files2.f
|
|
@ -4,12 +4,12 @@ use std::collections::HashMap;
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_test() {
|
fn simple_test() {
|
||||||
let mut defines = HashMap::new();
|
let mut defines = HashMap::new();
|
||||||
defines.insert("a".to_string(), "bad".to_string());
|
defines.insert("a".to_string(), Some("bad".to_string()));
|
||||||
defines.insert("e".to_string(), "f".to_string());
|
defines.insert("e".to_string(), Some("f".to_string()));
|
||||||
defines.insert("c".to_string(), "d".to_string());
|
defines.insert("c".to_string(), Some("d".to_string()));
|
||||||
defines.insert("ENV_VAR1".to_string(), "var1".to_string());
|
defines.insert("ENV_VAR1".to_string(), Some("var1".to_string()));
|
||||||
defines.insert("ENV_VAR2".to_string(), "var2".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 {
|
let filelist_exp = sv_filelist_parser::Filelist {
|
||||||
files : vec!["testcase/file1.sv".to_string(),
|
files : vec!["testcase/file1.sv".to_string(),
|
||||||
|
|
Loading…
Reference in a new issue