Fix spacing rule aroung text_macro_identifier

This commit is contained in:
dalance 2019-10-10 17:40:33 +09:00
parent d9b6f921c2
commit a02d1b6c1a
5 changed files with 127 additions and 41 deletions

View File

@ -2,6 +2,7 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.1.4...Unreleased) - ReleaseDate ## [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] cond_predicate in cond_predicate
* [Fixed] fixed_number priority in delay_value * [Fixed] fixed_number priority in delay_value
* [Fixed] cast/const_cast priority * [Fixed] cast/const_cast priority

View File

@ -154,9 +154,7 @@ pub(crate) fn angle_bracket_literal_impl(s: Span) -> IResult<Span, Locate> {
pub(crate) fn text_macro_definition(s: Span) -> IResult<Span, TextMacroDefinition> { pub(crate) fn text_macro_definition(s: Span) -> IResult<Span, TextMacroDefinition> {
let (s, a) = symbol("`")(s)?; let (s, a) = symbol("`")(s)?;
let (s, b) = keyword("define")(s)?; let (s, b) = keyword("define")(s)?;
begin_lb_not_space();
let (s, c) = text_macro_name(s)?; let (s, c) = text_macro_name(s)?;
end_lb_not_space();
let (s, d) = opt(macro_text)(s)?; let (s, d) = opt(macro_text)(s)?;
Ok(( Ok((
s, s,
@ -169,8 +167,8 @@ pub(crate) fn text_macro_definition(s: Span) -> IResult<Span, TextMacroDefinitio
#[tracable_parser] #[tracable_parser]
#[packrat_parser] #[packrat_parser]
pub(crate) fn text_macro_name(s: Span) -> IResult<Span, TextMacroName> { pub(crate) fn text_macro_name(s: Span) -> IResult<Span, TextMacroName> {
let (s, a) = text_macro_identifier(s)?; let (s, a) = text_macro_identifier_exact(s)?;
let (s, b) = opt(paren(list_of_formal_arguments))(s)?; let (s, b) = opt(paren_exact(list_of_formal_arguments))(s)?;
Ok((s, TextMacroName { nodes: (a, b) })) Ok((s, TextMacroName { nodes: (a, b) }))
} }
@ -196,6 +194,13 @@ pub(crate) fn text_macro_identifier(s: Span) -> IResult<Span, TextMacroIdentifie
Ok((s, TextMacroIdentifier { nodes: (a,) })) Ok((s, TextMacroIdentifier { nodes: (a,) }))
} }
#[tracable_parser]
#[packrat_parser]
pub(crate) fn text_macro_identifier_exact(s: Span) -> IResult<Span, TextMacroIdentifier> {
let (s, a) = identifier_exact(s)?;
Ok((s, TextMacroIdentifier { nodes: (a,) }))
}
#[tracable_parser] #[tracable_parser]
#[packrat_parser] #[packrat_parser]
pub(crate) fn macro_text(s: Span) -> IResult<Span, MacroText> { pub(crate) fn macro_text(s: Span) -> IResult<Span, MacroText> {
@ -276,7 +281,7 @@ pub(crate) fn default_text(s: Span) -> IResult<Span, DefaultText> {
#[packrat_parser] #[packrat_parser]
pub(crate) fn text_macro_usage(s: Span) -> IResult<Span, TextMacroUsage> { pub(crate) fn text_macro_usage(s: Span) -> IResult<Span, TextMacroUsage> {
let (s, a) = symbol("`")(s)?; 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)?; let (s, c) = opt(paren(list_of_actual_arguments))(s)?;
Ok((s, TextMacroUsage { nodes: (a, b, c) })) Ok((s, TextMacroUsage { nodes: (a, b, c) }))
} }

View File

@ -144,6 +144,12 @@ pub(crate) fn escaped_identifier(s: Span) -> IResult<Span, EscapedIdentifier> {
Ok((s, EscapedIdentifier { nodes: a })) Ok((s, EscapedIdentifier { nodes: a }))
} }
#[tracable_parser]
pub(crate) fn escaped_identifier_exact(s: Span) -> IResult<Span, EscapedIdentifier> {
let (s, a) = no_ws(escaped_identifier_impl)(s)?;
Ok((s, EscapedIdentifier { nodes: a }))
}
#[tracable_parser] #[tracable_parser]
pub(crate) fn escaped_identifier_impl(s: Span) -> IResult<Span, Locate> { pub(crate) fn escaped_identifier_impl(s: Span) -> IResult<Span, Locate> {
let (s, a) = tag("\\")(s)?; let (s, a) = tag("\\")(s)?;
@ -284,6 +290,19 @@ pub(crate) fn identifier(s: Span) -> IResult<Span, Identifier> {
))(s) ))(s)
} }
#[tracable_parser]
#[packrat_parser]
pub(crate) fn identifier_exact(s: Span) -> IResult<Span, Identifier> {
alt((
map(escaped_identifier_exact, |x| {
Identifier::EscapedIdentifier(Box::new(x))
}),
map(simple_identifier_exact, |x| {
Identifier::SimpleIdentifier(Box::new(x))
}),
))(s)
}
#[tracable_parser] #[tracable_parser]
pub(crate) fn index_variable_identifier(s: Span) -> IResult<Span, IndexVariableIdentifier> { pub(crate) fn index_variable_identifier(s: Span) -> IResult<Span, IndexVariableIdentifier> {
let (s, a) = identifier(s)?; let (s, a) = identifier(s)?;
@ -651,6 +670,13 @@ pub(crate) fn simple_identifier(s: Span) -> IResult<Span, SimpleIdentifier> {
Ok((s, SimpleIdentifier { nodes: a })) Ok((s, SimpleIdentifier { nodes: a }))
} }
#[tracable_parser]
#[packrat_parser]
pub(crate) fn simple_identifier_exact(s: Span) -> IResult<Span, SimpleIdentifier> {
let (s, a) = no_ws(simple_identifier_impl)(s)?;
Ok((s, SimpleIdentifier { nodes: a }))
}
#[tracable_parser] #[tracable_parser]
pub(crate) fn simple_identifier_impl(s: Span) -> IResult<Span, Locate> { pub(crate) fn simple_identifier_impl(s: Span) -> IResult<Span, Locate> {
let (s, a) = is_a(AZ_)(s)?; let (s, a) = is_a(AZ_)(s)?;

View File

@ -346,6 +346,23 @@ mod unit {
endmodule"##, endmodule"##,
Ok((_, _)) 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] #[test]
fn debug() { fn debug() {
test!( test!(source_text, r##"module top (); endmodule"##, Ok((_, _)));
source_text,
r##"module ibex_cs_registers; localparam logic [31:0] MISA_VALUE = (32'(RV32E)); endmodule"##,
Ok((_, _))
);
nom_tracable::cumulative_histogram(); nom_tracable::cumulative_histogram();
} }

View File

@ -13,6 +13,16 @@ where
} }
} }
pub(crate) fn no_ws<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult<Span<'a>, (O, Vec<WhiteSpace>)>
where
F: Fn(Span<'a>) -> IResult<Span<'a>, O>,
{
move |s: Span<'a>| {
let (s, x) = f(s)?;
Ok((s, (x, vec![])))
}
}
#[cfg(not(feature = "trace"))] #[cfg(not(feature = "trace"))]
pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Symbol> { pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Symbol> {
move |s: Span<'a>| { move |s: Span<'a>| {
@ -38,6 +48,31 @@ pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, S
} }
} }
#[cfg(not(feature = "trace"))]
pub(crate) fn symbol_exact<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, 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<Span<'a>, 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"))] #[cfg(not(feature = "trace"))]
pub(crate) fn keyword<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Keyword> { pub(crate) fn keyword<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Keyword> {
move |s: Span<'a>| { 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<Span<'a>, Paren<O>>
where
F: Fn(Span<'a>) -> IResult<Span<'a>, 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<Span<'a>, Paren<O>>
where
F: Fn(Span<'a>) -> IResult<Span<'a>, 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"))] #[cfg(not(feature = "trace"))]
pub(crate) fn bracket<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Bracket<O>> pub(crate) fn bracket<'a, O, F>(f: F) -> impl Fn(Span<'a>) -> IResult<Span<'a>, Bracket<O>>
where where
@ -258,14 +324,18 @@ where
pub(crate) fn white_space(s: Span) -> IResult<Span, WhiteSpace> { pub(crate) fn white_space(s: Span) -> IResult<Span, WhiteSpace> {
if in_directive() { if in_directive() {
alt(( 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| { map(preceded(peek(char('/')), comment), |x| {
WhiteSpace::Comment(Box::new(x)) WhiteSpace::Comment(Box::new(x))
}), }),
))(s) ))(s)
} else { } else {
alt(( 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| { map(preceded(peek(char('/')), comment), |x| {
WhiteSpace::Comment(Box::new(x)) WhiteSpace::Comment(Box::new(x))
}), }),
@ -276,14 +346,6 @@ pub(crate) fn white_space(s: Span) -> IResult<Span, WhiteSpace> {
} }
} }
pub(crate) fn space(s: Span) -> IResult<Span, Span> {
if lb_not_space() {
space1(s)
} else {
multispace1(s)
}
}
thread_local!( thread_local!(
static IN_DIRECTIVE: core::cell::RefCell<Vec<()>> = { static IN_DIRECTIVE: core::cell::RefCell<Vec<()>> = {
core::cell::RefCell::new(Vec::new()) core::cell::RefCell::new(Vec::new())
@ -305,27 +367,6 @@ pub(crate) fn end_directive() {
IN_DIRECTIVE.with(|x| x.borrow_mut().pop()); IN_DIRECTIVE.with(|x| x.borrow_mut().pop());
} }
thread_local!(
static LB_NOT_SPACE: core::cell::RefCell<Vec<()>> = {
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)] #[derive(Clone, Copy)]