Add comment and file parsing. Removed Unknown

This commit is contained in:
Raamakrishnan 2020-01-23 22:01:46 +05:30
parent 9177bd6a1c
commit b91f785509
5 changed files with 29 additions and 6 deletions

0
sample/file1.sv Normal file
View file

0
sample/file2.sv Normal file
View file

0
sample/file3.sv Normal file
View file

4
sample/files.f Normal file
View file

@ -0,0 +1,4 @@
sample/file1.sv
sample/file2.sv
sample/file3.sv
sample/file4.sv

View file

@ -1,12 +1,13 @@
use std::collections::HashMap; use std::collections::HashMap;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
enum LineType <'a> { 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, &'a str>),
Filelist(&'a str), Filelist(&'a str),
Unknown, Comment,
// Unknown,
} }
fn parse_line<'a>(line: &'a str) -> LineType<'a> { fn parse_line<'a>(line: &'a str) -> LineType<'a> {
@ -28,8 +29,11 @@ fn parse_line<'a>(line: &'a str) -> LineType<'a> {
let incdirs = line.trim_start_matches("+incdir+").trim_end_matches("+"); let incdirs = line.trim_start_matches("+incdir+").trim_end_matches("+");
let incdir_vec: Vec<&str> = incdirs.split("+").collect(); let incdir_vec: Vec<&str> = incdirs.split("+").collect();
return LineType::IncDir(incdir_vec); return LineType::IncDir(incdir_vec);
} else if line.starts_with("//") {
return LineType::Comment;
} else { } else {
return LineType::Unknown; // Mark everything else as a File
return LineType::File(line);
} }
} }
@ -71,8 +75,23 @@ mod test {
#[test] #[test]
fn parse_line_incdir_multiple() { fn parse_line_incdir_multiple() {
let line = "+incdir+../sample_dir1/sample_dir2+../sample_dir2/sample_dir3+sample_dir4/sample_dir5+\n"; let line = "+incdir+../sample_dir1/sample_dir2+../sample_dir2/sample_dir3+sample_dir4/sample_dir5+\n";
let incdir_vec = vec!["../sample_dir1/sample_dir2", "../sample_dir2/sample_dir3", let incdir_vec = vec![
"sample_dir4/sample_dir5"]; "../sample_dir1/sample_dir2",
"../sample_dir2/sample_dir3",
"sample_dir4/sample_dir5",
];
assert_eq!(parse_line(line), LineType::IncDir(incdir_vec)); assert_eq!(parse_line(line), LineType::IncDir(incdir_vec));
} }
}
#[test]
fn parse_line_comment() {
let line = "//random_comment";
assert_eq!(parse_line(line), LineType::Comment);
}
#[test]
fn parse_line_file() {
let line = "any_random_line_is_a_file";
assert_eq!(parse_line(line), LineType::File("any_random_line_is_a_file"));
}
}