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
* [Added] ignore_include option
## [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`

View File

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

View File

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

View File

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

View File

@ -98,8 +98,9 @@ pub fn parse_sv<T: AsRef<Path>, U: AsRef<Path>>(
path: T,
pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U],
ignore_include: bool,
) -> 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 result = all_consuming(sv_parser)(span);
match result {
@ -135,8 +136,9 @@ pub fn parse_sv_str<T: AsRef<Path>, U: AsRef<Path>>(
path: T,
pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U],
ignore_include: bool,
) -> 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 result = all_consuming(sv_parser)(span);
match result {
@ -171,8 +173,9 @@ pub fn parse_lib<T: AsRef<Path>, U: AsRef<Path>>(
path: T,
pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U],
ignore_include: bool,
) -> 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 result = all_consuming(lib_parser)(span);
match result {
@ -208,8 +211,9 @@ pub fn parse_lib_str<T: AsRef<Path>, U: AsRef<Path>>(
path: T,
pre_defines: &HashMap<String, Option<Define>>,
include_paths: &[U],
ignore_include: bool,
) -> 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 result = all_consuming(lib_parser)(span);
match result {