Move Span
This commit is contained in:
parent
bf92c5550f
commit
94d29828a5
@ -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" }
|
||||
|
@ -118,7 +118,7 @@ pub(crate) fn non_zero_unsigned_number_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, RealNumber> {
|
||||
pub(crate) fn fixed_point_number(s: Span) -> IResult<Span, FixedPointNumber> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, DecimalBase> {
|
||||
#[parser]
|
||||
pub(crate) fn decimal_base_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, BinaryBase> {
|
||||
#[parser]
|
||||
pub(crate) fn binary_base_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, OctalBase> {
|
||||
#[parser]
|
||||
pub(crate) fn octal_base_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, HexBase> {
|
||||
#[parser]
|
||||
pub(crate) fn hex_base_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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]
|
||||
|
@ -39,5 +39,5 @@ pub(crate) fn string_literal_impl(s: Span) -> IResult<Span, Locate> {
|
||||
let a = concat(a, b).unwrap();
|
||||
let a = concat(a, c).unwrap();
|
||||
|
||||
Ok((s, a.into()))
|
||||
Ok((s, into_locate(a)))
|
||||
}
|
||||
|
@ -12,7 +12,12 @@ pub(crate) fn one_line_comment(s: Span) -> IResult<Span, Comment> {
|
||||
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<Span, Comment> {
|
||||
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),),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ pub(crate) fn c_identifier_impl(s: Span) -> IResult<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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<Span, Locate> {
|
||||
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]
|
||||
|
@ -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<SourceText, ()> {
|
||||
|
@ -270,8 +270,8 @@ pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, 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<Span<'a>,
|
||||
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<Span, WhiteSpace> {
|
||||
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!(
|
||||
|
@ -5,14 +5,8 @@ authors = ["dalance <dalance@gmail.com>"]
|
||||
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"
|
||||
|
@ -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<Span<'a>> 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,
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
@ -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" }
|
||||
|
Loading…
x
Reference in New Issue
Block a user