Fix define argument

This commit is contained in:
dalance 2019-11-01 10:24:32 +09:00
parent 645932130f
commit 8527a03b12
4 changed files with 121 additions and 81 deletions

View File

@ -2,6 +2,8 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.3.2...Unreleased) - ReleaseDate ## [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 ## [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 * [Changed] SyntaxTree::get_str can take &RefNode

View File

@ -226,49 +226,7 @@ pub(crate) fn macro_text(s: Span) -> IResult<Span, MacroText> {
#[tracable_parser] #[tracable_parser]
#[packrat_parser] #[packrat_parser]
pub(crate) fn default_text(s: Span) -> IResult<Span, DefaultText> { pub(crate) fn default_text(s: Span) -> IResult<Span, DefaultText> {
let (s, a) = many1(alt(( let (s, a) = define_argument(s)?;
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();
Ok(( Ok((
s, s,
DefaultText { DefaultText {
@ -296,39 +254,23 @@ pub(crate) fn list_of_actual_arguments(s: Span) -> IResult<Span, ListOfActualArg
#[tracable_parser] #[tracable_parser]
#[packrat_parser] #[packrat_parser]
pub(crate) fn actual_argument(s: Span) -> IResult<Span, ActualArgument> { pub(crate) fn actual_argument(s: Span) -> IResult<Span, ActualArgument> {
let (s, a) = many1(alt(( let (s, a) = define_argument(s)?;
is_not(",)([{\""), Ok((
map(triple(tag("("), opt(is_not(")")), tag(")")), |(x, y, z)| { s,
if let Some(y) = y { ActualArgument {
concat(concat(x, y).unwrap(), z).unwrap() nodes: (into_locate(a),),
} 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()
}
}, },
), ))
}
#[tracable_parser]
pub(crate) fn define_argument(s: Span) -> IResult<Span, Span> {
let (s, a) = many1(alt((
is_not(",([{}])\""),
define_argument_str,
define_argument_paren,
define_argument_bracket,
define_argument_brace,
)))(s)?; )))(s)?;
let mut ret = None; let mut ret = None;
for x in a { for x in a {
@ -339,12 +281,75 @@ pub(crate) fn actual_argument(s: Span) -> IResult<Span, ActualArgument> {
} }
} }
let a = ret.unwrap(); let a = ret.unwrap();
Ok(( Ok((s, a))
s, }
ActualArgument {
nodes: (into_locate(a),), #[tracable_parser]
}, pub(crate) fn define_argument_inner(s: Span) -> IResult<Span, Span> {
)) 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<Span, Span> {
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<Span, Span> {
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<Span, Span> {
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<Span, Span> {
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] #[tracable_parser]

View File

@ -522,6 +522,23 @@ endmodule
module a (); 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 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
"## "##
); );
} }

View File

@ -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