Add ignore_include option

This commit is contained in:
dalance 2020-01-24 11:03:34 +09:00
parent 855875b27f
commit 5d55878f1b
5 changed files with 120 additions and 32 deletions

View File

@ -2,6 +2,8 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.5.0...Unreleased) - ReleaseDate ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.5.0...Unreleased) - ReleaseDate
* [Added] ignore_include option
## [v0.5.0](https://github.com/dalance/sv-parser/compare/v0.4.20...v0.5.0) - 2020-01-23 ## [v0.5.0](https://github.com/dalance/sv-parser/compare/v0.4.20...v0.5.0) - 2020-01-23
* [Changed] from `sv-parser-error::ErrorKind` to `sv-parser-error::Error` * [Changed] from `sv-parser-error::ErrorKind` to `sv-parser-error::Error`

View File

@ -118,6 +118,7 @@ pub fn preprocess<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &Defines, pre_defines: &Defines,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
) -> Result<(PreprocessedText, Defines), Error> { ) -> Result<(PreprocessedText, Defines), Error> {
let f = File::open(path.as_ref()).map_err(|x| Error::File { let f = File::open(path.as_ref()).map_err(|x| Error::File {
source: x, source: x,
@ -127,7 +128,7 @@ pub fn preprocess<T: AsRef<Path>, U: AsRef<Path>>(
let mut s = String::new(); let mut s = String::new();
reader.read_to_string(&mut s)?; reader.read_to_string(&mut s)?;
preprocess_str(&s, path, pre_defines, include_paths, 0) preprocess_str(&s, path, pre_defines, include_paths, ignore_include, 0)
} }
pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>( pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
@ -135,6 +136,7 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &Defines, pre_defines: &Defines,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
resolve_depth: usize, resolve_depth: usize,
) -> Result<(PreprocessedText, Defines), Error> { ) -> Result<(PreprocessedText, Defines), Error> {
let mut skip = false; let mut skip = false;
@ -356,7 +358,7 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
defines.insert(id, Some(define)); defines.insert(id, Some(define));
} }
NodeEvent::Enter(RefNode::IncludeCompilerDirective(x)) if !skip => { NodeEvent::Enter(RefNode::IncludeCompilerDirective(x)) if !skip && !ignore_include => {
let locate: Locate = x.try_into().unwrap(); let locate: Locate = x.try_into().unwrap();
last_include_line = Some(locate.line); last_include_line = Some(locate.line);
@ -408,8 +410,8 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
} }
} }
} }
let (include, new_defines) = let (include, new_defines) = preprocess(path, &defines, include_paths, false)
preprocess(path, &defines, include_paths).map_err(|x| Error::Include { .map_err(|x| Error::Include {
source: Box::new(x), source: Box::new(x),
})?; })?;
defines = new_defines; defines = new_defines;
@ -596,6 +598,7 @@ fn resolve_text_macro_usage<T: AsRef<Path>, U: AsRef<Path>>(
path.as_ref(), path.as_ref(),
&defines, &defines,
include_paths, include_paths,
false,
resolve_depth, resolve_depth,
)?; )?;
Ok(Some(( Ok(Some((
@ -628,8 +631,13 @@ mod tests {
#[test] #[test]
fn test1() { fn test1() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test1.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test1.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module and_op (a, b, c); r##"module and_op (a, b, c);
@ -653,7 +661,8 @@ endmodule
fn test1_predefine() { fn test1_predefine() {
let mut defines = HashMap::new(); let mut defines = HashMap::new();
defines.insert(String::from("behavioral"), None); defines.insert(String::from("behavioral"), None);
let (ret, _) = preprocess(get_testcase("test1.sv"), &defines, &[] as &[String]).unwrap(); let (ret, _) =
preprocess(get_testcase("test1.sv"), &defines, &[] as &[String], false).unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module and_op (a, b, c); r##"module and_op (a, b, c);
@ -669,8 +678,13 @@ endmodule
#[test] #[test]
fn test2() { fn test2() {
let include_paths = [get_testcase("")]; let include_paths = [get_testcase("")];
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test2.sv"), &HashMap::new(), &include_paths).unwrap(); get_testcase("test2.sv"),
&HashMap::new(),
&include_paths,
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module and_op (a, b, c); r##"module and_op (a, b, c);
@ -698,10 +712,33 @@ endmodule
assert_eq!(ret.origin(70).unwrap().1, 52); assert_eq!(ret.origin(70).unwrap().1, 52);
} }
#[test]
fn test2_ignore_include() {
let include_paths = [get_testcase("")];
let (ret, _) = preprocess(
get_testcase("test2.sv"),
&HashMap::new(),
&include_paths,
true,
)
.unwrap();
assert_eq!(
ret.text(),
r##"module and_op (a, b, c);
endmodule
"##
);
}
#[test] #[test]
fn test3() { fn test3() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test3.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test3.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##" r##"
@ -717,8 +754,13 @@ module a ();
#[test] #[test]
fn test4() { fn test4() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test4.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test4.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##" r##"
@ -738,8 +780,13 @@ endmodule
#[test] #[test]
fn test5() { fn test5() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test5.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test5.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module a; r##"module a;
@ -758,8 +805,13 @@ endmodule
#[test] #[test]
fn test6() { fn test6() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test6.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test6.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##" r##"
@ -775,34 +827,59 @@ endmodule
#[test] #[test]
fn test7() { fn test7() {
let ret = preprocess(get_testcase("test7.sv"), &HashMap::new(), &[] as &[String]); let ret = preprocess(
get_testcase("test7.sv"),
&HashMap::new(),
&[] as &[String],
false,
);
assert_eq!(format!("{:?}", ret), "Err(ExceedRecursiveLimit)"); assert_eq!(format!("{:?}", ret), "Err(ExceedRecursiveLimit)");
} }
#[test] #[test]
fn test8() { fn test8() {
let ret = preprocess(get_testcase("test8.sv"), &HashMap::new(), &[] as &[String]); let ret = preprocess(
get_testcase("test8.sv"),
&HashMap::new(),
&[] as &[String],
false,
);
assert_eq!(format!("{:?}", ret), "Err(ExceedRecursiveLimit)"); assert_eq!(format!("{:?}", ret), "Err(ExceedRecursiveLimit)");
} }
#[test] #[test]
fn test9() { fn test9() {
let include_paths = [get_testcase("")]; let include_paths = [get_testcase("")];
let ret = preprocess(get_testcase("test9.sv"), &HashMap::new(), &include_paths); let ret = preprocess(
get_testcase("test9.sv"),
&HashMap::new(),
&include_paths,
false,
);
assert_eq!(format!("{:?}", ret), "Err(IncludeLine)"); assert_eq!(format!("{:?}", ret), "Err(IncludeLine)");
} }
#[test] #[test]
fn test10() { fn test10() {
let include_paths = [get_testcase("")]; let include_paths = [get_testcase("")];
let ret = preprocess(get_testcase("test10.sv"), &HashMap::new(), &include_paths); let ret = preprocess(
get_testcase("test10.sv"),
&HashMap::new(),
&include_paths,
false,
);
assert_eq!(format!("{:?}", ret), "Err(IncludeLine)"); assert_eq!(format!("{:?}", ret), "Err(IncludeLine)");
} }
#[test] #[test]
fn test11() { fn test11() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test11.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test11.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module a; r##"module a;
@ -817,8 +894,13 @@ endmodule
#[test] #[test]
fn test12() { fn test12() {
let (ret, _) = let (ret, _) = preprocess(
preprocess(get_testcase("test12.sv"), &HashMap::new(), &[] as &[String]).unwrap(); get_testcase("test12.sv"),
&HashMap::new(),
&[] as &[String],
false,
)
.unwrap();
assert_eq!( assert_eq!(
ret.text(), ret.text(),
r##"module a; r##"module a;

View File

@ -14,7 +14,7 @@ fn main() {
let includes: Vec<PathBuf> = Vec::new(); let includes: Vec<PathBuf> = Vec::new();
// Parse // Parse
let result = parse_sv(&path, &defines, &includes); let result = parse_sv(&path, &defines, &includes, false);
if let Ok((syntax_tree, _)) = result { if let Ok((syntax_tree, _)) = result {
// &SyntexTree is iterable // &SyntexTree is iterable

View File

@ -36,7 +36,7 @@ fn main() {
let mut exit = 0; let mut exit = 0;
for path in &opt.files { for path in &opt.files {
if opt.pp { if opt.pp {
match preprocess(&path, &defines, &opt.includes) { match preprocess(&path, &defines, &opt.includes, false) {
Ok((preprocessed_text, new_defines)) => { Ok((preprocessed_text, new_defines)) => {
println!("{}", preprocessed_text.text()); println!("{}", preprocessed_text.text());
defines = new_defines; defines = new_defines;
@ -44,7 +44,7 @@ fn main() {
_ => (), _ => (),
} }
} else { } else {
match parse_sv(&path, &defines, &opt.includes) { match parse_sv(&path, &defines, &opt.includes, false) {
Ok((syntax_tree, new_defines)) => { Ok((syntax_tree, new_defines)) => {
if opt.tree { if opt.tree {
println!("{}", syntax_tree); println!("{}", syntax_tree);

View File

@ -98,8 +98,9 @@ pub fn parse_sv<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &HashMap<String, Option<Define>>, pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
) -> Result<(SyntaxTree, Defines), Error> { ) -> Result<(SyntaxTree, Defines), Error> {
let (text, defines) = preprocess(path, pre_defines, include_paths)?; let (text, defines) = preprocess(path, pre_defines, include_paths, ignore_include)?;
let span = Span::new_extra(text.text(), SpanInfo::default()); let span = Span::new_extra(text.text(), SpanInfo::default());
let result = all_consuming(sv_parser)(span); let result = all_consuming(sv_parser)(span);
match result { match result {
@ -135,8 +136,9 @@ pub fn parse_sv_str<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &HashMap<String, Option<Define>>, pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
) -> Result<(SyntaxTree, Defines), Error> { ) -> Result<(SyntaxTree, Defines), Error> {
let (text, defines) = preprocess_str(s, path, pre_defines, include_paths, 0)?; let (text, defines) = preprocess_str(s, path, pre_defines, include_paths, ignore_include, 0)?;
let span = Span::new_extra(text.text(), SpanInfo::default()); let span = Span::new_extra(text.text(), SpanInfo::default());
let result = all_consuming(sv_parser)(span); let result = all_consuming(sv_parser)(span);
match result { match result {
@ -171,8 +173,9 @@ pub fn parse_lib<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &HashMap<String, Option<Define>>, pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
) -> Result<(SyntaxTree, Defines), Error> { ) -> Result<(SyntaxTree, Defines), Error> {
let (text, defines) = preprocess(path, pre_defines, include_paths)?; let (text, defines) = preprocess(path, pre_defines, include_paths, ignore_include)?;
let span = Span::new_extra(text.text(), SpanInfo::default()); let span = Span::new_extra(text.text(), SpanInfo::default());
let result = all_consuming(lib_parser)(span); let result = all_consuming(lib_parser)(span);
match result { match result {
@ -208,8 +211,9 @@ pub fn parse_lib_str<T: AsRef<Path>, U: AsRef<Path>>(
path: T, path: T,
pre_defines: &HashMap<String, Option<Define>>, pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U], include_paths: &[U],
ignore_include: bool,
) -> Result<(SyntaxTree, Defines), Error> { ) -> Result<(SyntaxTree, Defines), Error> {
let (text, defines) = preprocess_str(s, path, pre_defines, include_paths, 0)?; let (text, defines) = preprocess_str(s, path, pre_defines, include_paths, ignore_include, 0)?;
let span = Span::new_extra(text.text(), SpanInfo::default()); let span = Span::new_extra(text.text(), SpanInfo::default());
let result = all_consuming(lib_parser)(span); let result = all_consuming(lib_parser)(span);
match result { match result {