diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9215a..5114602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.3.2...Unreleased) - ReleaseDate +* [Fixed] define arument + ## [v0.3.2](https://github.com/dalance/sv-parser/compare/v0.2.1...v0.3.2) - 2019-10-29 * [Changed] SyntaxTree::get_str can take &RefNode diff --git a/sv-parser-parser/src/general/compiler_directives.rs b/sv-parser-parser/src/general/compiler_directives.rs index a55b48f..6eb45ef 100644 --- a/sv-parser-parser/src/general/compiler_directives.rs +++ b/sv-parser-parser/src/general/compiler_directives.rs @@ -226,49 +226,7 @@ pub(crate) fn macro_text(s: Span) -> IResult { #[tracable_parser] #[packrat_parser] pub(crate) fn default_text(s: Span) -> IResult { - let (s, a) = many1(alt(( - is_not(",)([{\""), - map(triple(tag("("), opt(is_not(")")), tag(")")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map(triple(tag("["), opt(is_not("]")), tag("]")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map(triple(tag("{"), opt(is_not("}")), tag("}")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map( - triple(tag("\""), opt(is_not("\"")), tag("\"")), - |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }, - ), - )))(s)?; - let mut ret = None; - for x in a { - ret = if let Some(ret) = ret { - Some(concat(ret, x).unwrap()) - } else { - Some(x) - } - } - let a = ret.unwrap(); + let (s, a) = define_argument(s)?; Ok(( s, DefaultText { @@ -296,39 +254,23 @@ pub(crate) fn list_of_actual_arguments(s: Span) -> IResult IResult { + let (s, a) = define_argument(s)?; + Ok(( + s, + ActualArgument { + nodes: (into_locate(a),), + }, + )) +} + +#[tracable_parser] +pub(crate) fn define_argument(s: Span) -> IResult { let (s, a) = many1(alt(( - is_not(",)([{\""), - map(triple(tag("("), opt(is_not(")")), tag(")")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map(triple(tag("["), opt(is_not("]")), tag("]")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map(triple(tag("{"), opt(is_not("}")), tag("}")), |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }), - map( - triple(tag("\""), opt(is_not("\"")), tag("\"")), - |(x, y, z)| { - if let Some(y) = y { - concat(concat(x, y).unwrap(), z).unwrap() - } else { - concat(x, z).unwrap() - } - }, - ), + is_not(",([{}])\""), + define_argument_str, + define_argument_paren, + define_argument_bracket, + define_argument_brace, )))(s)?; let mut ret = None; for x in a { @@ -339,12 +281,75 @@ pub(crate) fn actual_argument(s: Span) -> IResult { } } let a = ret.unwrap(); - Ok(( - s, - ActualArgument { - nodes: (into_locate(a),), - }, - )) + Ok((s, a)) +} + +#[tracable_parser] +pub(crate) fn define_argument_inner(s: Span) -> IResult { + let (s, a) = many1(alt(( + is_not("([{}])\""), + define_argument_str, + define_argument_paren, + define_argument_bracket, + define_argument_brace, + )))(s)?; + let mut ret = None; + for x in a { + ret = if let Some(ret) = ret { + Some(concat(ret, x).unwrap()) + } else { + Some(x) + } + } + let a = ret.unwrap(); + Ok((s, a)) +} + +#[tracable_parser] +pub(crate) fn define_argument_str(s: Span) -> IResult { + let (s, (a, b, c)) = triple(tag("\""), opt(is_not("\"")), tag("\""))(s)?; + let a = if let Some(b) = b { + concat(concat(a, b).unwrap(), c).unwrap() + } else { + concat(a, c).unwrap() + }; + Ok((s, a)) +} + +#[recursive_parser] +#[tracable_parser] +pub(crate) fn define_argument_paren(s: Span) -> IResult { + let (s, (a, b, c)) = triple(tag("("), opt(define_argument_inner), tag(")"))(s)?; + let a = if let Some(b) = b { + concat(concat(a, b).unwrap(), c).unwrap() + } else { + concat(a, c).unwrap() + }; + Ok((s, a)) +} + +#[recursive_parser] +#[tracable_parser] +pub(crate) fn define_argument_bracket(s: Span) -> IResult { + let (s, (a, b, c)) = triple(tag("["), opt(define_argument_inner), tag("]"))(s)?; + let a = if let Some(b) = b { + concat(concat(a, b).unwrap(), c).unwrap() + } else { + concat(a, c).unwrap() + }; + Ok((s, a)) +} + +#[recursive_parser] +#[tracable_parser] +pub(crate) fn define_argument_brace(s: Span) -> IResult { + let (s, (a, b, c)) = triple(tag("{"), opt(define_argument_inner), tag("}"))(s)?; + let a = if let Some(b) = b { + concat(concat(a, b).unwrap(), c).unwrap() + } else { + concat(a, c).unwrap() + }; + Ok((s, a)) } #[tracable_parser] diff --git a/sv-parser-pp/src/preprocess.rs b/sv-parser-pp/src/preprocess.rs index 72ace97..f509c1b 100644 --- a/sv-parser-pp/src/preprocess.rs +++ b/sv-parser-pp/src/preprocess.rs @@ -522,6 +522,23 @@ endmodule module a (); assign a_0__x = a[0].x; assign a_0__y = a[0].y; assign a_1__x = a[1].x; assign a_1__y = a[1].y; endmodule +"## + ); + } + + #[test] + fn test4() { + let (ret, _) = + preprocess(get_testcase("test4.sv"), &HashMap::new(), &[] as &[String]).unwrap(); + assert_eq!( + ret.text(), + r##" +module a (); + + always @(posedge clk) begin if (!(!(a[i].b && c[i]))) begin $display ("xxx(()[]]{}}}", a[i].b, c[i]) +; end end ; + +endmodule "## ); } diff --git a/sv-parser-pp/testcases/test4.sv b/sv-parser-pp/testcases/test4.sv new file mode 100644 index 0000000..7466db6 --- /dev/null +++ b/sv-parser-pp/testcases/test4.sv @@ -0,0 +1,16 @@ +`define disp(clk, exp, msg) \ + always @(posedge clk) begin \ + if (!(exp)) begin \ + $display msg; \ + end \ + end \ + +module a (); + +`disp( + clk, + !(a[i].b && c[i]), + ("xxx(()[]]{}}}", a[i].b, c[i]) +); + +endmodule