diff --git a/CHANGELOG.md b/CHANGELOG.md index ecfbedd..d86b2af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.1.4...Unreleased) - ReleaseDate +* [Fixed] spacing rule aroung text_macro_identifier * [Fixed] cond_predicate in cond_predicate * [Fixed] fixed_number priority in delay_value * [Fixed] cast/const_cast priority diff --git a/sv-parser-parser/src/general/compiler_directives.rs b/sv-parser-parser/src/general/compiler_directives.rs index 232d22e..674690a 100644 --- a/sv-parser-parser/src/general/compiler_directives.rs +++ b/sv-parser-parser/src/general/compiler_directives.rs @@ -154,9 +154,7 @@ pub(crate) fn angle_bracket_literal_impl(s: Span) -> IResult { pub(crate) fn text_macro_definition(s: Span) -> IResult { let (s, a) = symbol("`")(s)?; let (s, b) = keyword("define")(s)?; - begin_lb_not_space(); let (s, c) = text_macro_name(s)?; - end_lb_not_space(); let (s, d) = opt(macro_text)(s)?; Ok(( s, @@ -169,8 +167,8 @@ pub(crate) fn text_macro_definition(s: Span) -> IResult IResult { - let (s, a) = text_macro_identifier(s)?; - let (s, b) = opt(paren(list_of_formal_arguments))(s)?; + let (s, a) = text_macro_identifier_exact(s)?; + let (s, b) = opt(paren_exact(list_of_formal_arguments))(s)?; Ok((s, TextMacroName { nodes: (a, b) })) } @@ -196,6 +194,13 @@ pub(crate) fn text_macro_identifier(s: Span) -> IResult IResult { + let (s, a) = identifier_exact(s)?; + Ok((s, TextMacroIdentifier { nodes: (a,) })) +} + #[tracable_parser] #[packrat_parser] pub(crate) fn macro_text(s: Span) -> IResult { @@ -276,7 +281,7 @@ pub(crate) fn default_text(s: Span) -> IResult { #[packrat_parser] pub(crate) fn text_macro_usage(s: Span) -> IResult { let (s, a) = symbol("`")(s)?; - let (s, b) = text_macro_identifier(s)?; + let (s, b) = text_macro_identifier_exact(s)?; let (s, c) = opt(paren(list_of_actual_arguments))(s)?; Ok((s, TextMacroUsage { nodes: (a, b, c) })) } diff --git a/sv-parser-parser/src/general/identifiers.rs b/sv-parser-parser/src/general/identifiers.rs index 4f4b9bf..ef6d4c6 100644 --- a/sv-parser-parser/src/general/identifiers.rs +++ b/sv-parser-parser/src/general/identifiers.rs @@ -144,6 +144,12 @@ pub(crate) fn escaped_identifier(s: Span) -> IResult { Ok((s, EscapedIdentifier { nodes: a })) } +#[tracable_parser] +pub(crate) fn escaped_identifier_exact(s: Span) -> IResult { + let (s, a) = no_ws(escaped_identifier_impl)(s)?; + Ok((s, EscapedIdentifier { nodes: a })) +} + #[tracable_parser] pub(crate) fn escaped_identifier_impl(s: Span) -> IResult { let (s, a) = tag("\\")(s)?; @@ -284,6 +290,19 @@ pub(crate) fn identifier(s: Span) -> IResult { ))(s) } +#[tracable_parser] +#[packrat_parser] +pub(crate) fn identifier_exact(s: Span) -> IResult { + alt(( + map(escaped_identifier_exact, |x| { + Identifier::EscapedIdentifier(Box::new(x)) + }), + map(simple_identifier_exact, |x| { + Identifier::SimpleIdentifier(Box::new(x)) + }), + ))(s) +} + #[tracable_parser] pub(crate) fn index_variable_identifier(s: Span) -> IResult { let (s, a) = identifier(s)?; @@ -651,6 +670,13 @@ pub(crate) fn simple_identifier(s: Span) -> IResult { Ok((s, SimpleIdentifier { nodes: a })) } +#[tracable_parser] +#[packrat_parser] +pub(crate) fn simple_identifier_exact(s: Span) -> IResult { + let (s, a) = no_ws(simple_identifier_impl)(s)?; + Ok((s, SimpleIdentifier { nodes: a })) +} + #[tracable_parser] pub(crate) fn simple_identifier_impl(s: Span) -> IResult { let (s, a) = is_a(AZ_)(s)?; diff --git a/sv-parser-parser/src/tests.rs b/sv-parser-parser/src/tests.rs index 6513391..2bc9e18 100644 --- a/sv-parser-parser/src/tests.rs +++ b/sv-parser-parser/src/tests.rs @@ -346,6 +346,23 @@ mod unit { endmodule"##, Ok((_, _)) ); + test!( + source_text, + r##"module a; always_comb if ( a ? b : c ) begin end endmodule"##, + Ok((_, _)) + ); + test!( + source_text, + r##"module SimDTM; assign #0.1 debug_req_valid = __debug_req_valid; endmodule"##, + Ok((_, _)) + ); + test!( + source_text, + r##"`define LONG_MACRO( + a, + b, c) text goes here"##, + Ok((_, _)) + ); } } @@ -15818,10 +15835,6 @@ mod spec { #[test] fn debug() { - test!( - source_text, - r##"module ibex_cs_registers; localparam logic [31:0] MISA_VALUE = (32'(RV32E)); endmodule"##, - Ok((_, _)) - ); + test!(source_text, r##"module top (); endmodule"##, Ok((_, _))); nom_tracable::cumulative_histogram(); } diff --git a/sv-parser-parser/src/utils.rs b/sv-parser-parser/src/utils.rs index 028bd6a..5b99949 100644 --- a/sv-parser-parser/src/utils.rs +++ b/sv-parser-parser/src/utils.rs @@ -13,6 +13,16 @@ where } } +pub(crate) fn no_ws<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, (O, Vec)> +where + F: Fn(Span<'a>) -> IResult, O>, +{ + move |s: Span<'a>| { + let (s, x) = f(s)?; + Ok((s, (x, vec![]))) + } +} + #[cfg(not(feature = "trace"))] pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, Symbol> { move |s: Span<'a>| { @@ -38,6 +48,31 @@ pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, S } } +#[cfg(not(feature = "trace"))] +pub(crate) fn symbol_exact<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, Symbol> { + move |s: Span<'a>| { + let (s, x) = map(no_ws(map(tag(t.clone()), |x: Span| into_locate(x))), |x| { + Symbol { nodes: x } + })(s)?; + Ok((s, x)) + } +} + +#[cfg(feature = "trace")] +pub(crate) fn symbol_exact<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, Symbol> { + move |s: Span<'a>| { + let (depth, s) = nom_tracable::forward_trace(s, &format!("symbol(\"{}\")", t)); + let body = || { + let (s, x) = map(no_ws(map(tag(t.clone()), |x: Span| into_locate(x))), |x| { + Symbol { nodes: x } + })(s)?; + Ok((s, x)) + }; + let ret = body(); + nom_tracable::backward_trace(ret, &format!("symbol(\"{}\")", t), depth) + } +} + #[cfg(not(feature = "trace"))] pub(crate) fn keyword<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, Keyword> { move |s: Span<'a>| { @@ -108,6 +143,37 @@ where } } +#[cfg(not(feature = "trace"))] +pub(crate) fn paren_exact<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Paren> +where + F: Fn(Span<'a>) -> IResult, O>, +{ + move |s: Span<'a>| { + let (s, a) = symbol("(")(s)?; + let (s, b) = f(s)?; + let (s, c) = symbol_exact(")")(s)?; + Ok((s, Paren { nodes: (a, b, c) })) + } +} + +#[cfg(feature = "trace")] +pub(crate) fn paren_exact<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Paren> +where + F: Fn(Span<'a>) -> IResult, O>, +{ + move |s: Span<'a>| { + let (depth, s) = nom_tracable::forward_trace(s, "paren"); + let body = || { + let (s, a) = symbol("(")(s)?; + let (s, b) = f(s)?; + let (s, c) = symbol_exact(")")(s)?; + Ok((s, Paren { nodes: (a, b, c) })) + }; + let ret = body(); + nom_tracable::backward_trace(ret, "paren", depth) + } +} + #[cfg(not(feature = "trace"))] pub(crate) fn bracket<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult, Bracket> where @@ -258,14 +324,18 @@ where pub(crate) fn white_space(s: Span) -> IResult { if in_directive() { alt(( - map(space, |x: Span| WhiteSpace::Space(Box::new(into_locate(x)))), + map(multispace1, |x: Span| { + WhiteSpace::Space(Box::new(into_locate(x))) + }), map(preceded(peek(char('/')), comment), |x| { WhiteSpace::Comment(Box::new(x)) }), ))(s) } else { alt(( - map(space, |x: Span| WhiteSpace::Space(Box::new(into_locate(x)))), + map(multispace1, |x: Span| { + WhiteSpace::Space(Box::new(into_locate(x))) + }), map(preceded(peek(char('/')), comment), |x| { WhiteSpace::Comment(Box::new(x)) }), @@ -276,14 +346,6 @@ pub(crate) fn white_space(s: Span) -> IResult { } } -pub(crate) fn space(s: Span) -> IResult { - if lb_not_space() { - space1(s) - } else { - multispace1(s) - } -} - thread_local!( static IN_DIRECTIVE: core::cell::RefCell> = { core::cell::RefCell::new(Vec::new()) @@ -305,27 +367,6 @@ pub(crate) fn end_directive() { IN_DIRECTIVE.with(|x| x.borrow_mut().pop()); } -thread_local!( - static LB_NOT_SPACE: core::cell::RefCell> = { - core::cell::RefCell::new(Vec::new()) - } -); - -pub(crate) fn lb_not_space() -> bool { - LB_NOT_SPACE.with(|x| match x.borrow().last() { - Some(_) => true, - None => false, - }) -} - -pub(crate) fn begin_lb_not_space() { - LB_NOT_SPACE.with(|x| x.borrow_mut().push(())); -} - -pub(crate) fn end_lb_not_space() { - LB_NOT_SPACE.with(|x| x.borrow_mut().pop()); -} - // ----------------------------------------------------------------------------- #[derive(Clone, Copy)]