diff --git a/sv-parser-parser/Cargo.toml b/sv-parser-parser/Cargo.toml index 199e5c5..ce3453e 100644 --- a/sv-parser-parser/Cargo.toml +++ b/sv-parser-parser/Cargo.toml @@ -6,12 +6,13 @@ edition = "2018" [features] default = [] -trace = ["sv-parser-syntaxtree/trace"] +trace = [] [dependencies] nom = "5.0.0" +nom_locate = { path = "../../nom_locate" } nom-packrat = "0.1.17" nom-recursive = { path = "../../nom-recursive/nom-recursive" } str-concat = "*" -sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" } sv-parser-macros = { path = "../sv-parser-macros" } +sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" } diff --git a/sv-parser-parser/src/expressions/numbers.rs b/sv-parser-parser/src/expressions/numbers.rs index 5dcb95b..a3c24c2 100644 --- a/sv-parser-parser/src/expressions/numbers.rs +++ b/sv-parser-parser/src/expressions/numbers.rs @@ -118,7 +118,7 @@ pub(crate) fn non_zero_unsigned_number_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), digit1)), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -150,7 +150,7 @@ pub(crate) fn real_number_floating(s: Span) -> IResult { pub(crate) fn fixed_point_number(s: Span) -> IResult { let (s, a) = unsigned_number(s)?; let (s, b) = map(tag("."), |x: Span| Symbol { - nodes: (x.into(), vec![]), + nodes: (into_locate(x), vec![]), })(s)?; let (s, c) = unsigned_number(s)?; Ok((s, FixedPointNumber { nodes: (a, b, c) })) @@ -174,7 +174,7 @@ pub(crate) fn unsigned_number_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), digit1)), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -189,7 +189,7 @@ pub(crate) fn binary_value_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), is_a("01xXzZ?"))), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -204,7 +204,7 @@ pub(crate) fn octal_value_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), is_a("01234567xXzZ?"))), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -221,7 +221,7 @@ pub(crate) fn hex_value_impl(s: Span) -> IResult { a, |acc, item| concat(acc, item).unwrap(), )(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -233,7 +233,7 @@ pub(crate) fn decimal_base(s: Span) -> IResult { #[parser] pub(crate) fn decimal_base_impl(s: Span) -> IResult { let (s, a) = alt((tag_no_case("'d"), tag_no_case("'sd")))(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -245,7 +245,7 @@ pub(crate) fn binary_base(s: Span) -> IResult { #[parser] pub(crate) fn binary_base_impl(s: Span) -> IResult { let (s, a) = alt((tag_no_case("'b"), tag_no_case("'sb")))(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -257,7 +257,7 @@ pub(crate) fn octal_base(s: Span) -> IResult { #[parser] pub(crate) fn octal_base_impl(s: Span) -> IResult { let (s, a) = alt((tag_no_case("'o"), tag_no_case("'so")))(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -269,7 +269,7 @@ pub(crate) fn hex_base(s: Span) -> IResult { #[parser] pub(crate) fn hex_base_impl(s: Span) -> IResult { let (s, a) = alt((tag_no_case("'h"), tag_no_case("'sh")))(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -284,7 +284,7 @@ pub(crate) fn x_number_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), is_a("_"))), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] @@ -299,7 +299,7 @@ pub(crate) fn z_number_impl(s: Span) -> IResult { let (s, a) = fold_many0(alt((tag("_"), is_a("_"))), a, |acc, item| { concat(acc, item).unwrap() })(s)?; - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] diff --git a/sv-parser-parser/src/expressions/strings.rs b/sv-parser-parser/src/expressions/strings.rs index 9c3437e..6b2403a 100644 --- a/sv-parser-parser/src/expressions/strings.rs +++ b/sv-parser-parser/src/expressions/strings.rs @@ -39,5 +39,5 @@ pub(crate) fn string_literal_impl(s: Span) -> IResult { let a = concat(a, b).unwrap(); let a = concat(a, c).unwrap(); - Ok((s, a.into())) + Ok((s, into_locate(a))) } diff --git a/sv-parser-parser/src/general/comments.rs b/sv-parser-parser/src/general/comments.rs index cf93c3b..d30e4ac 100644 --- a/sv-parser-parser/src/general/comments.rs +++ b/sv-parser-parser/src/general/comments.rs @@ -12,7 +12,12 @@ pub(crate) fn one_line_comment(s: Span) -> IResult { let (s, a) = tag("//")(s)?; let (s, b) = is_not("\n")(s)?; let a = concat(a, b).unwrap(); - Ok((s, Comment { nodes: (a.into(),) })) + Ok(( + s, + Comment { + nodes: (into_locate(a),), + }, + )) } #[parser] @@ -22,5 +27,10 @@ pub(crate) fn block_comment(s: Span) -> IResult { let (s, c) = tag("*/")(s)?; let a = concat(a, b).unwrap(); let a = concat(a, c).unwrap(); - Ok((s, Comment { nodes: (a.into(),) })) + Ok(( + s, + Comment { + nodes: (into_locate(a),), + }, + )) } diff --git a/sv-parser-parser/src/general/identifiers.rs b/sv-parser-parser/src/general/identifiers.rs index 5950224..579d798 100644 --- a/sv-parser-parser/src/general/identifiers.rs +++ b/sv-parser-parser/src/general/identifiers.rs @@ -44,7 +44,7 @@ pub(crate) fn c_identifier_impl(s: Span) -> IResult { if is_keyword(&a) { Err(Err::Error(make_error(s, ErrorKind::Fix))) } else { - Ok((s, a.into())) + Ok((s, into_locate(a))) } } @@ -148,7 +148,7 @@ pub(crate) fn escaped_identifier_impl(s: Span) -> IResult { let (s, a) = tag("\\")(s)?; let (s, b) = is_not(" \t\r\n")(s)?; let a = concat(a, b).unwrap(); - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[allow(dead_code)] @@ -637,7 +637,7 @@ pub(crate) fn simple_identifier_impl(s: Span) -> IResult { if is_keyword(&a) { Err(Err::Error(make_error(s, ErrorKind::Fix))) } else { - Ok((s, a.into())) + Ok((s, into_locate(a))) } } @@ -658,7 +658,7 @@ pub(crate) fn system_tf_identifier_impl(s: Span) -> IResult { let (s, a) = tag("$")(s)?; let (s, b) = is_a(AZ09_DOLLAR)(s)?; let a = concat(a, b).unwrap(); - Ok((s, a.into())) + Ok((s, into_locate(a))) } #[parser] diff --git a/sv-parser-parser/src/lib.rs b/sv-parser-parser/src/lib.rs index 852a8a1..8e2e3a2 100644 --- a/sv-parser-parser/src/lib.rs +++ b/sv-parser-parser/src/lib.rs @@ -32,12 +32,34 @@ pub(crate) use nom::multi::*; pub(crate) use nom::sequence::*; pub(crate) use nom::{Err, IResult}; pub(crate) use nom_packrat::{self, packrat_parser}; -pub(crate) use nom_recursive::{recursive_parser, RecursiveTracer}; +pub(crate) use nom_recursive::{recursive_parser, RecursiveInfo, RecursiveTracer}; pub(crate) use sv_parser_macros::*; pub(crate) use sv_parser_syntaxtree::*; // ----------------------------------------------------------------------------- +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct SpanInfo { + #[cfg(feature = "trace")] + pub depth: usize, + pub recursive_info: RecursiveInfo, +} + +pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, SpanInfo>; + +impl RecursiveTracer for SpanInfo { + fn get_info(&self) -> RecursiveInfo { + self.recursive_info + } + + fn set_info(mut self, info: RecursiveInfo) -> Self { + self.recursive_info = info; + self + } +} + +// ----------------------------------------------------------------------------- + nom_packrat::storage!(AnyNode); pub fn parse_sv(s: &str) -> Result { diff --git a/sv-parser-parser/src/utils.rs b/sv-parser-parser/src/utils.rs index 71dc71f..a6a32b1 100644 --- a/sv-parser-parser/src/utils.rs +++ b/sv-parser-parser/src/utils.rs @@ -270,8 +270,8 @@ pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, S move |s: Span<'a>| { #[cfg(feature = "trace")] let s = trace(s, &format!("symbol(\"{}\")", t)); - let (s, x) = map(ws(map(tag(t.clone()), |x: Span| x.into())), |x| Symbol { - nodes: x, + let (s, x) = map(ws(map(tag(t.clone()), |x: Span| into_locate(x))), |x| { + Symbol { nodes: x } })(s)?; Ok((s, x)) } @@ -283,7 +283,7 @@ pub(crate) fn keyword<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult, let s = trace(s, &format!("keyword(\"{}\")", t)); let (s, x) = map( ws(terminated( - map(tag(t.clone()), |x: Span| x.into()), + map(tag(t.clone()), |x: Span| into_locate(x)), peek(none_of(AZ09_)), )), |x| Keyword { nodes: x }, @@ -427,7 +427,9 @@ where #[parser] pub(crate) fn white_space(s: Span) -> IResult { alt(( - map(multispace1, |x: Span| WhiteSpace::Space(Box::new(x.into()))), + map(multispace1, |x: Span| { + WhiteSpace::Space(Box::new(into_locate(x))) + }), map(comment, |x| WhiteSpace::Comment(Box::new(x))), ))(s) } @@ -457,6 +459,14 @@ pub(crate) fn is_keyword(s: &Span) -> bool { false } +pub(crate) fn into_locate(s: Span) -> Locate { + Locate { + offset: s.offset, + line: s.line, + len: s.fragment.len(), + } +} + #[cfg(feature = "trace")] pub(crate) fn trace<'a>(mut s: Span<'a>, name: &str) -> Span<'a> { println!( diff --git a/sv-parser-syntaxtree/Cargo.toml b/sv-parser-syntaxtree/Cargo.toml index 5c4aa05..e92e3b1 100644 --- a/sv-parser-syntaxtree/Cargo.toml +++ b/sv-parser-syntaxtree/Cargo.toml @@ -5,14 +5,8 @@ authors = ["dalance "] edition = "2018" build = "build.rs" -[features] -default = [] -trace = [] - [dependencies] sv-parser-macros = { path = "../sv-parser-macros" } -nom_locate = { path = "../../nom_locate" } -nom-recursive = { path = "../../nom-recursive/nom-recursive" } [build-dependencies] walkdir = "2" diff --git a/sv-parser-syntaxtree/src/lib.rs b/sv-parser-syntaxtree/src/lib.rs index 7a8b86d..b5deb25 100644 --- a/sv-parser-syntaxtree/src/lib.rs +++ b/sv-parser-syntaxtree/src/lib.rs @@ -23,48 +23,15 @@ pub use special_node::*; pub use specify_section::*; pub use udp_declaration_and_instantiation::*; -pub(crate) use nom_recursive::{RecursiveInfo, RecursiveTracer}; pub(crate) use sv_parser_macros::*; // ----------------------------------------------------------------------------- -#[derive(Clone, Copy, Debug, Default, PartialEq)] -pub struct SpanInfo { - #[cfg(feature = "trace")] - pub depth: usize, - pub recursive_info: RecursiveInfo, -} - -pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, SpanInfo>; - -impl RecursiveTracer for SpanInfo { - fn get_info(&self) -> RecursiveInfo { - self.recursive_info - } - - fn set_info(mut self, info: RecursiveInfo) -> Self { - self.recursive_info = info; - self - } -} - -// ----------------------------------------------------------------------------- - #[derive(Copy, Clone, Default, Debug, PartialEq)] pub struct Locate { - offset: usize, - line: u32, - len: usize, -} - -impl<'a> From> for Locate { - fn from(x: Span<'a>) -> Self { - Locate { - offset: x.offset, - line: x.line, - len: x.fragment.len(), - } - } + pub offset: usize, + pub line: u32, + pub len: usize, } // ----------------------------------------------------------------------------- diff --git a/sv-parser/Cargo.toml b/sv-parser/Cargo.toml index 820b73a..5bdf61a 100644 --- a/sv-parser/Cargo.toml +++ b/sv-parser/Cargo.toml @@ -15,5 +15,5 @@ default = [] trace = ["sv-parser-parser/trace"] [dependencies] -sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" } sv-parser-parser = { path = "../sv-parser-parser" } +sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" }