Fix textmacro without arguments #20

This commit is contained in:
dalance 2020-11-06 12:43:22 +09:00
parent 727cd3f067
commit 6145300513
3 changed files with 56 additions and 0 deletions

View File

@ -2,6 +2,8 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.8.2...Unreleased) - ReleaseDate
* [Fixed] textmacro without arguments issue [#20](https://github.com/dalance/sv-parser/issues/20)
## [v0.8.2](https://github.com/dalance/sv-parser/compare/v0.8.1...v0.8.2) - 2020-08-28
* [Fixed] empty ifdef issue [#10](https://github.com/dalance/sv-parser/issues/10)

View File

@ -564,6 +564,19 @@ fn identifier(node: RefNode, s: &str) -> Option<String> {
None
}
fn get_str(node: RefNode, s: &str) -> String {
let mut ret = String::from("");
for x in node {
match x {
RefNode::Locate(x) => {
ret.push_str(x.str(s));
}
_ => (),
}
}
ret
}
fn split_text(s: &str) -> Vec<String> {
let mut is_string = false;
let mut is_ident = false;
@ -625,9 +638,14 @@ fn resolve_text_macro_usage<T: AsRef<Path>, U: AsRef<Path>>(
return Err(Error::ExceedRecursiveLimit);
}
let mut args_str = String::from("");
let mut actual_args = Vec::new();
let no_args = args.is_none();
if let Some(args) = args {
args_str.push_str(&get_str((&args.nodes.0).into(), s));
args_str.push_str(&get_str((&args.nodes.1).into(), s));
args_str.push_str(&get_str((&args.nodes.2).into(), s));
let (_, ref args, _) = args.nodes;
let (ref args,) = args.nodes;
for arg in args.contents() {
@ -670,6 +688,13 @@ fn resolve_text_macro_usage<T: AsRef<Path>, U: AsRef<Path>>(
arg_map.insert(String::from(arg), value);
}
// restore () for textmacro without arguments
let paren = if define.arguments.is_empty() {
Some(args_str)
} else {
None
};
if let Some(ref text) = define.text {
let mut replaced = String::from("");
for text in split_text(&text.text) {
@ -687,6 +712,11 @@ fn resolve_text_macro_usage<T: AsRef<Path>, U: AsRef<Path>>(
);
}
}
if let Some(paren) = paren {
replaced.push_str(&paren);
}
// separator is required
replaced.push_str(" ");
// remove leading whitespace
@ -1068,6 +1098,26 @@ endinterface
wire a = 1'b0;
endmodule
"##
);
}
#[test]
fn test15() {
let (ret, _) = preprocess(
get_testcase("test15.sv"),
&HashMap::new(),
&[] as &[String],
false,
false,
)
.unwrap();
assert_eq!(
ret.text(),
r##"
module mymod;
mysubmod u_mysubmod() ;
endmodule
"##
);

View File

@ -0,0 +1,4 @@
`define MOD_INST u_mysubmod
module mymod;
mysubmod `MOD_INST ();
endmodule